Breakup the OnlineGlomServiceImpl class to make it more manageable.
[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.web.server.Log;
33 import org.glom.web.shared.GlomField;
34
35 import com.mchange.v2.c3p0.ComboPooledDataSource;
36
37 /**
38  * @author Ben Konrath <ben@bagu.org>
39  * 
40  */
41 public class DetailsDBAccess extends DBAccess {
42
43         public DetailsDBAccess(Document document, String documentID, ComboPooledDataSource cpds, String tableName) {
44                 super(document, documentID, cpds, tableName);
45                 this.tableName = tableName;
46         }
47
48         public GlomField[] getData(String primaryKeyValue) {
49
50                 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document
51                                 .get_data_layout_groups("details", tableName));
52
53                 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
54                         Log.warn(documentID, tableName, "Didn't find any fields to show. Returning null.");
55                         return null;
56                 }
57
58                 Field primaryKey = getPrimaryKeyField();
59
60                 if (primaryKey == null) {
61                         Log.error(documentID, tableName, "Couldn't find primary key in table. Returning null.");
62                         return null;
63                 }
64
65                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
66                 Connection conn = null;
67                 Statement st = null;
68                 ResultSet rs = null;
69                 try {
70                         // Setup the JDBC driver and get the query.
71                         conn = cpds.getConnection();
72                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
73                         String query = Glom.build_sql_select_with_key(tableName, fieldsToGet, primaryKey, primaryKeyValue);
74                         System.out.println("DETAILS QUERY: " + query);
75                         rs = st.executeQuery(query);
76
77                         // get the results from the ResultSet
78                         // using 2 as a length parameter so we can log a warning if appropriate
79                         rowsList = convertResultSetToDTO(2, fieldsToGet, rs);
80                 } catch (SQLException e) {
81                         Log.error(documentID, tableName, "Error executing database query.", e);
82                         // TODO: somehow notify user of problem
83                         return null;
84                 } finally {
85                         // cleanup everything that has been used
86                         try {
87                                 if (rs != null)
88                                         rs.close();
89                                 if (st != null)
90                                         st.close();
91                                 if (conn != null)
92                                         conn.close();
93                         } catch (Exception e) {
94                                 Log.error(documentID, tableName,
95                                                 "Error closing database resources. Subsequent database queries may not work.", e);
96                         }
97                 }
98
99                 if (rowsList.size() == 0) {
100                         Log.error(documentID, tableName, "The query returned an empty ResultSet. Returning null.");
101                         return null;
102                 } else if (rowsList.size() > 1 && primaryKeyValue != null && !primaryKeyValue.isEmpty()) {
103                         // only log a warning if the result size is greater than 1 and the primaryKeyValue was set
104                         Log.warn(documentID, tableName,
105                                         "The query did not return the expected unique result. Returning the first result in the set.");
106                 }
107
108                 return rowsList.get(0);
109         }
110
111 }