2 * Copyright (C) 2010, 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;
22 import java.beans.PropertyVetoException;
23 import java.sql.Connection;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27 import java.util.ArrayList;
29 import org.glom.libglom.Document;
30 import org.glom.libglom.Field;
31 import org.glom.libglom.Glom;
32 import org.glom.libglom.LayoutFieldVector;
33 import org.glom.libglom.LayoutGroupVector;
34 import org.glom.libglom.LayoutItem;
35 import org.glom.libglom.LayoutItemVector;
36 import org.glom.libglom.LayoutItem_Field;
37 import org.glom.libglom.SortFieldPair;
38 import org.glom.libglom.StringVector;
39 import org.glom.libglom.SortClause;
40 import org.glom.web.client.OnlineGlomService;
41 import org.glom.web.shared.GlomDocument;
42 import org.glom.web.shared.GlomTable;
44 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
45 import com.mchange.v2.c3p0.ComboPooledDataSource;
46 import com.mchange.v2.c3p0.DataSources;
48 @SuppressWarnings("serial")
49 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
50 private Document document;
51 ComboPooledDataSource cpds;
53 // Called only when the servlet is stopped (the servlet container is stopped or restarted)
54 public OnlineGlomServiceImpl() {
56 document = new Document();
57 // TODO hardcoded for now, need to figure out something for this
58 document.set_file_uri("file:///home/ben/music-collection.glom");
60 @SuppressWarnings("unused")
61 boolean retval = document.load(error);
62 // TODO handle error condition (also below)
64 cpds = new ComboPooledDataSource();
65 // load the jdbc driver
67 cpds.setDriverClass("org.postgresql.Driver");
68 } catch (PropertyVetoException e) {
69 // TODO log error, fatal error can't continue, user can be nofified when db access doesn't work
73 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
74 + document.get_connection_database());
75 // TODO figure out something for db user name and password
77 cpds.setPassword("ChangeMe"); // of course it's not the password I'm using on my server
80 /* FIXME I think Swig is generating long on 64-bit machines and int on 32-bit machines - need to keep this constant
81 * http://stackoverflow.com/questions /1590831/safely-casting-long-to-int-in-java */
82 public static int safeLongToInt(long l) {
83 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
84 throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
89 public GlomDocument getGlomDocument() {
90 GlomDocument glomDocument = new GlomDocument();
93 glomDocument.setTitle(document.get_database_title());
95 // set array of GlomTables and the default table index
96 StringVector tableNames = document.get_table_names();
97 GlomTable[] tables = new GlomTable[safeLongToInt(tableNames.size())];
98 for (int i = 0; i < tableNames.size(); i++) {
99 String tableName = tableNames.get(i);
100 GlomTable glomTable = new GlomTable();
101 glomTable.setName(tableName);
102 glomTable.setTitle(document.get_table_title(tableName));
103 tables[i] = glomTable;
104 if (tableName.equals(document.get_default_table())) {
105 glomDocument.setDefaultTableIndex(i);
108 glomDocument.setTables(tables);
113 public String[] getLayoutListHeaders(String table) {
114 LayoutGroupVector layoutList = document.get_data_layout_groups("list", table);
115 LayoutItemVector layoutItems = layoutList.get(0).get_items();
116 String[] headers = new String[safeLongToInt(layoutItems.size())];
117 for (int i = 0; i < layoutItems.size(); i++) {
118 headers[i] = layoutItems.get(i).get_title_or_name();
123 public ArrayList<String[]> getTableData(int start, int length, String table) {
124 LayoutGroupVector layoutList = document.get_data_layout_groups("list", table);
125 LayoutItemVector layoutItems = layoutList.get(0).get_items();
127 LayoutFieldVector layoutFields = new LayoutFieldVector();
128 SortClause sortClause = new SortClause();
129 for (int i = 0; i < layoutItems.size(); i++) {
130 LayoutItem item = layoutItems.get(i);
131 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
133 layoutFields.add(field);
134 Field details = field.get_full_field_details();
135 if (details != null && details.get_primary_key()) {
136 sortClause.addLast(new SortFieldPair(field, true)); // ascending
141 ArrayList<String[]> rowsList = new ArrayList<String[]>();
143 Connection conn = cpds.getConnection();
144 Statement st = conn.createStatement();
146 String query = Glom.build_sql_select_simple(table, layoutFields, sortClause);
147 ResultSet rs = st.executeQuery(query);
150 String[] rowArray = new String[safeLongToInt(layoutItems.size())];
151 for (int i = 0; i < layoutItems.size(); i++) {
152 rowArray[i] = rs.getString(i + 1);
154 rowsList.add(rowArray);
159 } catch (SQLException e) {
160 // TODO: log error, notify user of problem
167 // Called only when the servlet is stopped (the servlet container is stopped or restarted)
168 public void destroy() {
169 Glom.libglom_deinit();
171 DataSources.destroy(cpds);
172 } catch (SQLException e) {
173 // TODO log error, don't need to notify user because this is a clean up method