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