Remove all javadoc author tags.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / database / DBAccess.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.server.database;
21
22 import java.sql.Date;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Time;
26 import java.text.DateFormat;
27 import java.util.ArrayList;
28 import java.util.Locale;
29
30 import org.glom.libglom.Document;
31 import org.glom.libglom.Field;
32 import org.glom.libglom.FieldVector;
33 import org.glom.libglom.LayoutFieldVector;
34 import org.glom.libglom.LayoutGroupVector;
35 import org.glom.libglom.LayoutItem;
36 import org.glom.libglom.LayoutItemVector;
37 import org.glom.libglom.LayoutItem_Field;
38 import org.glom.libglom.LayoutItem_Portal;
39 import org.glom.web.server.Log;
40 import org.glom.web.server.Utils;
41 import org.glom.web.shared.DataItem;
42
43 import com.mchange.v2.c3p0.ComboPooledDataSource;
44
45 /**
46  *
47  */
48 abstract class DBAccess {
49         protected Document document;
50         protected String documentID;
51         protected String tableName;
52         protected ComboPooledDataSource cpds;
53
54         protected DBAccess(Document document, String documentID, ComboPooledDataSource cpds, String tableName) {
55                 this.document = document;
56                 this.documentID = documentID;
57                 this.cpds = cpds;
58                 this.tableName = tableName;
59         }
60
61         /*
62          * Converts data from a ResultSet to an ArrayList of DataItem array suitable for sending back to the client.
63          */
64         final protected ArrayList<DataItem[]> convertResultSetToDTO(int length, LayoutFieldVector layoutFields, ResultSet rs)
65                         throws SQLException {
66
67                 // get the data we've been asked for
68                 int rowCount = 0;
69                 ArrayList<DataItem[]> rowsList = new ArrayList<DataItem[]>();
70                 while (rs.next() && rowCount <= length) {
71                         int layoutFieldsSize = Utils.safeLongToInt(layoutFields.size());
72                         DataItem[] rowArray = new DataItem[layoutFieldsSize];
73                         for (int i = 0; i < layoutFieldsSize; i++) {
74                                 // make a new DataItem to set the text and colours
75                                 rowArray[i] = new DataItem();
76
77                                 // Convert the field value to a string based on the glom type. We're doing the formatting on the
78                                 // server side for now but it might be useful to move this to the client side.
79                                 LayoutItem_Field field = layoutFields.get(i);
80                                 switch (field.get_glom_type()) {
81                                 case TYPE_TEXT:
82                                         String text = rs.getString(i + 1);
83                                         rowArray[i].setText(text != null ? text : "");
84                                         break;
85                                 case TYPE_BOOLEAN:
86                                         rowArray[i].setBoolean(rs.getBoolean(i + 1));
87                                         break;
88                                 case TYPE_NUMERIC:
89                                         rowArray[i].setNumber(rs.getDouble(i + 1));
90                                         break;
91                                 case TYPE_DATE:
92                                         Date date = rs.getDate(i + 1);
93                                         if (date != null) {
94                                                 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ROOT);
95                                                 rowArray[i].setText(dateFormat.format(date));
96                                         } else {
97                                                 rowArray[i].setText("");
98                                         }
99                                         break;
100                                 case TYPE_TIME:
101                                         Time time = rs.getTime(i + 1);
102                                         if (time != null) {
103                                                 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.ROOT);
104                                                 rowArray[i].setText(timeFormat.format(time));
105                                         } else {
106                                                 rowArray[i].setText("");
107                                         }
108                                         break;
109                                 case TYPE_IMAGE:
110                                         byte[] image = rs.getBytes(i + 1);
111                                         if (image != null) {
112                                                 // TODO implement field TYPE_IMAGE
113                                                 rowArray[i].setText("Image (FIXME)");
114                                         } else {
115                                                 rowArray[i].setText("");
116                                         }
117                                         break;
118                                 case TYPE_INVALID:
119                                 default:
120                                         Log.warn(documentID, tableName, "Invalid LayoutItem Field type. Using empty string for value.");
121                                         rowArray[i].setText("");
122                                         break;
123                                 }
124                         }
125
126                         // add the row of DataItems to the ArrayList we're going to return and update the row count
127                         rowsList.add(rowArray);
128                         rowCount++;
129                 }
130
131                 return rowsList;
132         }
133
134         /*
135          * Gets a LayoutFieldVector to use when generating an SQL query.
136          */
137         protected LayoutFieldVector getFieldsToShowForSQLQuery(LayoutGroupVector layoutGroupVec) {
138                 LayoutFieldVector layoutFieldVector = new LayoutFieldVector();
139
140                 // We will show the fields that the document says we should:
141                 for (int i = 0; i < layoutGroupVec.size(); i++) {
142                         org.glom.libglom.LayoutGroup layoutGroup = layoutGroupVec.get(i);
143
144                         // satisfy the precondition of getDetailsLayoutGroup(String tableName, org.glom.libglom.LayoutGroup
145                         // libglomLayoutGroup)
146                         if (layoutGroup == null)
147                                 continue;
148
149                         // Get the fields:
150                         ArrayList<LayoutItem_Field> layoutItemFields = getFieldsToShowForSQLQueryAddGroup(layoutGroup);
151                         for (LayoutItem_Field layoutItem_Field : layoutItemFields) {
152                                 layoutFieldVector.add(layoutItem_Field);
153                         }
154                 }
155                 return layoutFieldVector;
156         }
157
158         /*
159          * Gets an ArrayList of LayoutItem_Field objects to use when generating an SQL query.
160          * 
161          * @precondition libglomLayoutGroup must not be null
162          */
163         private ArrayList<LayoutItem_Field> getFieldsToShowForSQLQueryAddGroup(
164                         org.glom.libglom.LayoutGroup libglomLayoutGroup) {
165
166                 ArrayList<LayoutItem_Field> layoutItemFields = new ArrayList<LayoutItem_Field>();
167                 LayoutItemVector items = libglomLayoutGroup.get_items();
168                 int numItems = Utils.safeLongToInt(items.size());
169                 for (int i = 0; i < numItems; i++) {
170                         LayoutItem layoutItem = items.get(i);
171
172                         LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(layoutItem);
173                         if (layoutItemField != null) {
174                                 // the layoutItem is a LayoutItem_Field
175                                 FieldVector fields;
176                                 if (layoutItemField.get_has_relationship_name()) {
177                                         // layoutItemField is a field in a related table
178                                         fields = document.get_table_fields(layoutItemField.get_table_used(tableName));
179                                 } else {
180                                         // layoutItemField is a field in this table
181                                         fields = document.get_table_fields(tableName);
182                                 }
183
184                                 // set the layoutItemFeild with details from its Field in the document and
185                                 // add it to the list to be returned
186                                 for (int j = 0; j < fields.size(); j++) {
187                                         // check the names to see if they're the same
188                                         // this works because we're using the field list from the related table if necessary
189                                         if (layoutItemField.get_name().equals(fields.get(j).get_name())) {
190                                                 Field field = fields.get(j);
191                                                 if (field != null) {
192                                                         layoutItemField.set_full_field_details(field);
193                                                         layoutItemFields.add(layoutItemField);
194                                                 } else {
195                                                         Log.warn(document.get_database_title(), tableName,
196                                                                         "LayoutItem_Field " + layoutItemField.get_layout_display_name()
197                                                                                         + " not found in document field list.");
198                                                 }
199                                                 break;
200                                         }
201                                 }
202
203                         } else {
204                                 // the layoutItem is not a LayoutItem_Field
205                                 org.glom.libglom.LayoutGroup subLayoutGroup = org.glom.libglom.LayoutGroup.cast_dynamic(layoutItem);
206                                 if (subLayoutGroup != null) {
207                                         // the layoutItem is a LayoutGroup
208                                         LayoutItem_Portal layoutItemPortal = LayoutItem_Portal.cast_dynamic(layoutItem);
209                                         if (layoutItemPortal == null) {
210                                                 // The subGroup is not a LayoutItem_Portal.
211                                                 // We're ignoring portals because they are filled by means of a separate SQL query.
212                                                 layoutItemFields.addAll(getFieldsToShowForSQLQueryAddGroup(subLayoutGroup));
213                                         }
214                                 }
215                         }
216                 }
217                 return layoutItemFields;
218         }
219
220         /**
221          * Gets the primary key Field for the specified table name.
222          * 
223          * @param tableName
224          *            name of table to search for the primary key field
225          * @return primary key Field
226          */
227         protected Field getPrimaryKeyField(String tableName) {
228                 Field primaryKey = null;
229                 FieldVector fieldsVec = document.get_table_fields(tableName);
230                 for (int i = 0; i < Utils.safeLongToInt(fieldsVec.size()); i++) {
231                         Field field = fieldsVec.get(i);
232                         if (field.get_primary_key()) {
233                                 primaryKey = field;
234                                 break;
235                         }
236                 }
237                 return primaryKey;
238         }
239
240         /**
241          * Gets the primary key LayoutItem_Field for the specified table.
242          * 
243          * @param tableName
244          *            name of table to search for the primary key LayoutItem_Field
245          * @return primary key LayoutItem_Field
246          */
247         protected LayoutItem_Field getPrimaryKeyLayoutItemField(String tableName) {
248                 Field primaryKey = getPrimaryKeyField(tableName);
249
250                 LayoutItem_Field libglomLayoutItemField = new LayoutItem_Field();
251
252                 if (primaryKey != null) {
253                         libglomLayoutItemField.set_full_field_details(primaryKey);
254                 } else {
255                         Log.error(document.get_database_title(), this.tableName,
256                                         "A primary key was not found in the FieldVector for this table.");
257                 }
258
259                 return libglomLayoutItemField;
260         }
261
262         /*
263          * Find the LayoutItem_Portal for the related list name
264          */
265         final protected LayoutItem_Portal getPortal(String relationshipName) {
266
267                 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
268                 // LayoutItem_Portal portal = null;
269                 for (int i = 0; i < layoutGroupVec.size(); i++) {
270                         org.glom.libglom.LayoutGroup layoutGroup = layoutGroupVec.get(i);
271                         LayoutItem_Portal portal = getPortal(relationshipName, layoutGroup);
272                         if (portal != null) {
273                                 return portal;
274                         }
275                 }
276
277                 // the LayoutItem_Portal with relationshipName was not found
278                 return null;
279         }
280
281         /*
282          * Recursive helper method.
283          */
284         final private LayoutItem_Portal getPortal(String relationshipName, org.glom.libglom.LayoutGroup layoutGroup) {
285
286                 if (relationshipName == null)
287                         return null;
288
289                 LayoutItemVector items = layoutGroup.get_items();
290                 for (int i = 0; i < items.size(); i++) {
291                         LayoutItem layoutItem = items.get(i);
292
293                         LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(layoutItem);
294                         if (layoutItemField != null) {
295                                 // the layoutItem is a LayoutItem_Field
296                                 continue;
297
298                         } else {
299                                 // the layoutItem is not a LayoutItem_Field
300                                 org.glom.libglom.LayoutGroup subLayoutGroup = org.glom.libglom.LayoutGroup.cast_dynamic(layoutItem);
301                                 if (subLayoutGroup != null) {
302                                         // the layoutItem is a LayoutGroup
303                                         LayoutItem_Portal layoutItemPortal = LayoutItem_Portal.cast_dynamic(layoutItem);
304                                         if (layoutItemPortal != null) {
305                                                 // The subGroup is a LayoutItem_Protal
306                                                 if (relationshipName.equals(layoutItemPortal.get_relationship_name_used())) {
307                                                         // yey, we found it!
308                                                         return layoutItemPortal;
309                                                 }
310                                         } else {
311                                                 // The subGroup is not a LayoutItem_Portal.
312                                                 LayoutItem_Portal retval = getPortal(relationshipName, subLayoutGroup);
313                                                 if (retval != null) {
314                                                         return retval;
315                                                 }
316                                         }
317                                 }
318                         }
319                 }
320
321                 // the LayoutItem_Portal with relationshipName was not found
322                 return null;
323         }
324
325 }