Add a Field class and implement some loading of it in Document.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / database / DetailsDBAccess.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.Connection;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.ArrayList;
27
28 import org.glom.libglom.LayoutFieldVector;
29 import org.glom.libglom.Value;
30 import org.glom.web.server.Log;
31 import org.glom.web.server.SqlUtils;
32 import org.glom.web.server.Utils;
33 import org.glom.web.shared.DataItem;
34 import org.glom.web.shared.TypedDataItem;
35 import org.glom.web.shared.libglom.Document;
36 import org.glom.web.shared.libglom.Field;
37
38 import com.mchange.v2.c3p0.ComboPooledDataSource;
39
40 /**
41  *
42  */
43 public class DetailsDBAccess extends DBAccess {
44
45         public DetailsDBAccess(final Document document, final String documentID, final ComboPooledDataSource cpds,
46                         final String tableName) {
47                 super(document, documentID, cpds, tableName);
48                 this.tableName = tableName;
49         }
50
51         public DataItem[] getData(final TypedDataItem primaryKeyValue) {
52
53                 final LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document.get_data_layout_groups("details",
54                                 tableName));
55
56                 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
57                         Log.warn(documentID, tableName, "Didn't find any fields to show. Returning null.");
58                         return null;
59                 }
60
61                 final Field primaryKey = getPrimaryKeyField(tableName);
62
63                 if (primaryKey == null) {
64                         Log.error(documentID, tableName, "Couldn't find primary key in table. Returning null.");
65                         return null;
66                 }
67
68                 ArrayList<DataItem[]> rowsList = new ArrayList<DataItem[]>();
69                 Connection conn = null;
70                 Statement st = null;
71                 ResultSet rs = null;
72                 Value gdaPrimaryKeyValue = null;
73                 try {
74                         // Setup the JDBC driver and get the query.
75                         conn = cpds.getConnection();
76                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
77
78                         gdaPrimaryKeyValue = Utils.getGlomTypeGdaValueForTypedDataItem(documentID, tableName,
79                                         primaryKey.get_glom_type(), primaryKeyValue);
80
81                         // Only create the query if we've created a Gda Value from the TypedDataItem.
82                         if (gdaPrimaryKeyValue != null) {
83
84                                 final String query = SqlUtils.build_sql_select_with_key(conn, tableName, fieldsToGet, primaryKey,
85                                                 gdaPrimaryKeyValue);
86
87                                 rs = st.executeQuery(query);
88
89                                 // get the results from the ResultSet
90                                 // using 2 as a length parameter so we can log a warning if appropriate
91                                 rowsList = convertResultSetToDTO(2, fieldsToGet, rs);
92                         }
93
94                 } catch (final SQLException e) {
95                         Log.error(documentID, tableName, "Error executing database query.", e);
96                         // TODO: somehow notify user of problem
97                         return null;
98                 } finally {
99                         // cleanup everything that has been used
100                         try {
101                                 if (rs != null)
102                                         rs.close();
103                                 if (st != null)
104                                         st.close();
105                                 if (conn != null)
106                                         conn.close();
107                         } catch (final Exception e) {
108                                 Log.error(documentID, tableName,
109                                                 "Error closing database resources. Subsequent database queries may not work.", e);
110                         }
111                 }
112
113                 if (rowsList.size() == 0) {
114                         Log.error(documentID, tableName, "The query returned an empty ResultSet. Returning null.");
115                         return null;
116                 } else if (rowsList.size() > 1 && !gdaPrimaryKeyValue.is_null()) {
117                         // Only log a warning if the result size is greater than 1 and the gdaPrimaryKeyValue is not null. When
118                         // gdaPrimaryKeyValue.is_null() is true, the default query for the details view is being executed so we
119                         // expect a result set that is larger than one.
120                         Log.warn(documentID, tableName,
121                                         "The query did not return the expected unique result. Returning the first result in the set.");
122                 }
123
124                 return rowsList.get(0);
125         }
126 }