2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.server.database;
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.apache.commons.lang3.StringUtils;
26 import org.glom.web.server.SqlUtils;
27 import org.glom.web.server.libglom.Document;
28 import org.glom.web.shared.DataItem;
29 import org.glom.web.shared.TypedDataItem;
30 import org.glom.web.shared.libglom.Field;
31 import org.glom.web.shared.libglom.layout.LayoutGroup;
32 import org.glom.web.shared.libglom.layout.LayoutItemField;
33 import org.glom.web.shared.libglom.layout.SortClause;
34 import org.jooq.Condition;
36 import com.mchange.v2.c3p0.ComboPooledDataSource;
41 public class ListViewDBAccess extends ListDBAccess {
43 public ListViewDBAccess(final Document document, final String documentID, final ComboPooledDataSource cpds,
44 final String tableName, final LayoutGroup libglomLayoutGroup) {
45 super(document, documentID, cpds, tableName);
47 // Convert the LayoutGroup object into a List suitable for SQL queries.
48 final List<LayoutGroup> tempLayoutGroupVec = new ArrayList<LayoutGroup>();
49 tempLayoutGroupVec.add(libglomLayoutGroup);
50 fieldsToGet = getFieldsToShowForSQLQuery(tempLayoutGroupVec);
52 // Add a LayoutItem_Field for the primary key to the end of the LayoutFieldVector if it doesn't already contain
54 if (getPrimaryKeyIndex() < 0) {
55 fieldsToGet.add(getPrimaryKeyLayoutItemField(tableName));
59 public ArrayList<DataItem[]> getData(final String quickFind, final int start, final int length,
60 final boolean useSortClause, final int sortColumnIndex, final boolean isAscending) {
62 return getListData(quickFind, start, length, useSortClause, sortColumnIndex, isAscending);
68 * @see org.glom.web.server.ListDBAccess#getExpectedResultSize()
70 public int getExpectedResultSize() {
72 if (fieldsToGet == null || fieldsToGet.size() <= 0)
75 return getResultSizeOfSQLQuery();
81 * @see org.glom.web.server.ListDBAccess#getSQLQuery(LayoutFieldVector, SortClause)
84 protected String getSelectQuery(final String quickFind, final SortClause sortClause) {
85 // Later versions of libglom actually return an empty SqlExpr when quickFindValue is empty,
87 Condition whereClause = null;
88 if (!StringUtils.isEmpty(quickFind)) {
89 final TypedDataItem quickFindValue = new TypedDataItem();
90 quickFindValue.setText(quickFind);
91 whereClause = SqlUtils.getFindWhereClauseQuick(document, tableName, quickFindValue);
94 return SqlUtils.buildSqlSelectWithWhereClause(tableName, fieldsToGet, whereClause, sortClause);
100 * @see org.glom.web.server.ListDBAccess#getCountQuery(LayoutFieldVector)
103 protected String getCountQuery() {
104 return SqlUtils.buildSqlCountSelectWithWhereClause(tableName, fieldsToGet);
108 * Gets the primary key index of this list layout.
110 * @return index of primary key or -1 if a primary key was not found
112 private int getPrimaryKeyIndex() {
113 for (int i = 0; i < fieldsToGet.size(); i++) {
114 final LayoutItemField layoutItemField = fieldsToGet.get(i);
115 final Field field = layoutItemField.getFullFieldDetails();
116 if (tableName.equals(layoutItemField.getTableUsed(tableName)) && field != null && field.getPrimaryKey())