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;
24 import java.io.FilenameFilter;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.sql.SQLException;
28 import java.util.ArrayList;
29 import java.util.Hashtable;
30 import java.util.Properties;
32 import javax.servlet.ServletException;
34 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
35 import org.glom.libglom.Document;
36 import org.glom.libglom.Glom;
37 import org.glom.web.client.OnlineGlomService;
38 import org.glom.web.shared.DataItem;
39 import org.glom.web.shared.DetailsLayoutAndData;
40 import org.glom.web.shared.DocumentInfo;
41 import org.glom.web.shared.Documents;
42 import org.glom.web.shared.NavigationRecord;
43 import org.glom.web.shared.layout.LayoutGroup;
45 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
46 import com.mchange.v2.c3p0.DataSources;
49 * The servlet class for setting up the server side of Online Glom. The public methods in this class are the methods
50 * that can be called by the client side code.
52 * @author Ben Konrath <ben@bagu.org>
54 @SuppressWarnings("serial")
55 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
57 private static final String GLOM_FILE_EXTENSION = ".glom";
59 // convenience class to for dealing with the Online Glom configuration file
60 private class OnlineGlomProperties extends Properties {
61 public String getKey(String value) {
62 for (String key : stringPropertyNames()) {
63 if (getProperty(key).trim().equals(value))
70 private final Hashtable<String, ConfiguredDocument> documentMapping = new Hashtable<String, ConfiguredDocument>();
73 * This is called when the servlet is started or restarted.
77 * @see javax.servlet.GenericServlet#init()
80 public void init() throws ServletException {
81 // Find the configuration file. See this thread for background info:
82 // http://stackoverflow.com/questions/2161054/where-to-place-properties-files-in-a-jsp-servlet-web-application
83 // FIXME move onlineglom.properties to the WEB-INF folder (option number 2 from the stackoverflow question)
84 OnlineGlomProperties config = new OnlineGlomProperties();
85 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("onlineglom.properties");
87 Log.fatal("onlineglom.properties not found.");
88 throw new ServletException("onlineglom.properties not found.");
92 } catch (IOException e) {
93 throw new ServletException(e.getMessage(), e);
96 // check if we can read the configured glom file directory
97 String documentDirName = config.getProperty("glom.document.directory");
98 File documentDir = new File(documentDirName);
99 if (!documentDir.isDirectory()) {
100 Log.fatal(documentDirName + " is not a directory.");
101 throw new ServletException(documentDirName + " is not a directory.");
103 if (!documentDir.canRead()) {
104 Log.fatal("Can't read the files in : " + documentDirName);
105 throw new ServletException("Can't read the files in : " + documentDirName);
108 // get and check the glom files in the specified directory
109 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
111 public boolean accept(File dir, String name) {
112 return name.endsWith(GLOM_FILE_EXTENSION);
116 // don't continue if there aren't any Glom files to configure
117 if (glomFiles.length <= 0) {
118 Log.error("Unable to find any Glom documents in the configured directory: " + documentDirName);
119 Log.error("Check the onlineglom.properties file to ensure that 'glom.document.directory' is set to the correct directory.");
124 for (File glomFile : glomFiles) {
125 Document document = new Document();
126 document.set_file_uri("file://" + glomFile.getAbsolutePath());
128 boolean retval = document.load(error);
129 if (retval == false) {
131 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
132 message = "Could not find file: " + glomFile.getAbsolutePath();
134 message = "An unknown error occurred when trying to load file: " + glomFile.getAbsolutePath();
137 // continue with for loop because there may be other documents in the directory
141 ConfiguredDocument configuredDocument;
143 configuredDocument = new ConfiguredDocument(document);
144 } catch (PropertyVetoException e) {
145 throw new ServletException(e.getMessage(), e);
147 // check if a username and password have been set and work for the current document
148 String filename = glomFile.getName();
149 String key = config.getKey(filename);
151 String[] keyArray = key.split("\\.");
152 if (keyArray.length == 3 && "filename".equals(keyArray[2])) {
153 // username/password could be set, let's check to see if it works
154 String usernameKey = key.replaceAll(keyArray[2], "username");
155 String passwordKey = key.replaceAll(keyArray[2], "password");
157 configuredDocument.setUsernameAndPassword(config.getProperty(usernameKey).trim(),
158 config.getProperty(passwordKey));
159 } catch (SQLException e) {
160 throw new ServletException(e.getMessage(), e);
165 // check the if the global username and password have been set and work with this document
166 if (!configuredDocument.isAuthenticated()) {
168 configuredDocument.setUsernameAndPassword(config.getProperty("glom.document.username").trim(),
169 config.getProperty("glom.document.password"));
170 } catch (SQLException e) {
171 throw new ServletException(e.getMessage(), e);
175 // The key for the hash table is the file name without the .glom extension and with spaces ( ) replaced with
176 // pluses (+). The space/plus replacement makes the key more friendly for URLs.
177 String documentID = filename.substring(0, glomFile.getName().length() - GLOM_FILE_EXTENSION.length())
179 configuredDocument.setDocumentID(documentID);
180 documentMapping.put(documentID, configuredDocument);
183 // Allow a fake connection, so sqlbuilder_get_full_query() can work:
184 Glom.set_fake_connection();
188 * This is called when the servlet is stopped or restarted.
190 * @see javax.servlet.GenericServlet#destroy()
193 public void destroy() {
194 Glom.libglom_deinit();
196 for (String documenTitle : documentMapping.keySet()) {
197 ConfiguredDocument configuredDoc = documentMapping.get(documenTitle);
199 DataSources.destroy(configuredDoc.getCpds());
200 } catch (SQLException e) {
201 Log.error(documenTitle, "Error cleaning up the ComboPooledDataSource.", e);
209 * @see org.glom.web.client.OnlineGlomService#getDocumentInfo(java.lang.String)
212 public DocumentInfo getDocumentInfo(String documentID) {
214 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
216 // FIXME check for authentication
218 return configuredDoc.getDocumentInfo();
225 * @see org.glom.web.client.OnlineGlomService#getListViewLayout(java.lang.String, java.lang.String)
228 public LayoutGroup getListViewLayout(String documentID, String tableName) {
229 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
231 // FIXME check for authentication
233 return configuredDoc.getListViewLayoutGroup(tableName);
239 * @see org.glom.web.client.OnlineGlomService#getListViewData(java.lang.String, java.lang.String, int, int)
242 public ArrayList<DataItem[]> getListViewData(String documentID, String tableName, int start, int length) {
243 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
244 if (!configuredDoc.isAuthenticated()) {
245 return new ArrayList<DataItem[]>();
247 return configuredDoc.getListViewData(tableName, start, length, false, 0, false);
253 * @see org.glom.web.client.OnlineGlomService#getSortedListViewData(java.lang.String, java.lang.String, int, int,
257 public ArrayList<DataItem[]> getSortedListViewData(String documentID, String tableName, int start, int length,
258 int sortColumnIndex, boolean isAscending) {
259 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
260 if (!configuredDoc.isAuthenticated()) {
261 return new ArrayList<DataItem[]>();
263 return configuredDoc.getListViewData(tableName, start, length, true, sortColumnIndex, isAscending);
269 * @see org.glom.web.client.OnlineGlomService#getDocuments()
272 public Documents getDocuments() {
273 Documents documents = new Documents();
274 for (String documentID : documentMapping.keySet()) {
275 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
276 documents.addDocument(documentID, configuredDoc.getDocument().get_database_title());
284 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
286 public boolean isAuthenticated(String documentID) {
287 return documentMapping.get(documentID).isAuthenticated();
293 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
297 public boolean checkAuthentication(String documentID, String username, String password) {
298 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
300 return configuredDoc.setUsernameAndPassword(username, password);
301 } catch (SQLException e) {
302 Log.error(documentID, "Unknown SQL Error checking the database authentication.", e);
310 * @see org.glom.web.client.OnlineGlomService#getDetailsData(java.lang.String, java.lang.String, java.lang.String)
313 public DataItem[] getDetailsData(String documentID, String tableName, String primaryKeyValue) {
314 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
316 // FIXME check for authentication
318 return configuredDoc.getDetailsData(tableName, primaryKeyValue);
324 * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutAndData(java.lang.String, java.lang.String,
328 public DetailsLayoutAndData getDetailsLayoutAndData(String documentID, String tableName, String primaryKeyValue) {
329 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
330 DetailsLayoutAndData initalDetailsView = new DetailsLayoutAndData();
332 // FIXME check for authentication
334 initalDetailsView.setLayout(configuredDoc.getDetailsLayoutGroup(tableName));
335 initalDetailsView.setData(configuredDoc.getDetailsData(tableName, primaryKeyValue));
337 return initalDetailsView;
343 * @see org.glom.web.client.OnlineGlomService#getRelatedListData(java.lang.String, java.lang.String, int, int)
346 public ArrayList<DataItem[]> getRelatedListData(String documentID, String tableName, String relationshipName,
347 String foreignKeyValue, int start, int length) {
348 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
350 // FIXME check for authentication
352 return configuredDoc.getRelatedListData(tableName, relationshipName, foreignKeyValue, start, length, false, 0,
359 * @see org.glom.web.client.OnlineGlomService#getSortedRelatedListData(java.lang.String, java.lang.String, int, int,
363 public ArrayList<DataItem[]> getSortedRelatedListData(String documentID, String tableName, String relationshipName,
364 String foreignKeyValue, int start, int length, int sortColumnIndex, boolean ascending) {
365 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
367 // FIXME check for authentication
369 return configuredDoc.getRelatedListData(tableName, relationshipName, foreignKeyValue, start, length, true,
370 sortColumnIndex, ascending);
373 public int getRelatedListRowCount(String documentID, String tableName, String relationshipName,
374 String foreignKeyValue) {
375 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
377 // FIXME check for authentication
379 return configuredDoc.getRelatedListRowCount(tableName, relationshipName, foreignKeyValue);
385 * @see org.glom.web.client.OnlineGlomService#getSuitableRecordToViewDetails(java.lang.String, java.lang.String,
386 * java.lang.String, java.lang.String)
389 public NavigationRecord getSuitableRecordToViewDetails(String documentID, String tableName,
390 String relationshipName, String primaryKeyValue) {
391 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
393 // FIXME check for authentication
395 return configuredDoc.getSuitableRecordToViewDetails(tableName, relationshipName, primaryKeyValue);