Call Glom.libglom_deinit() when the servlet is shutdown.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / LibGlomServiceImpl.java
1 package org.glom.web.server;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.glom.libglom.Document;
8 import org.glom.libglom.ExampleRowVector;
9 import org.glom.libglom.Field;
10 import org.glom.libglom.FieldVector;
11 import org.glom.libglom.Glom;
12 import org.glom.libglom.LayoutGroupVector;
13 import org.glom.libglom.LayoutItem;
14 import org.glom.libglom.LayoutItemVector;
15 import org.glom.libglom.RowDataVector;
16 import org.glom.libglom.StringVector;
17 import org.glom.web.client.GlomDocument;
18 import org.glom.web.client.GlomTable;
19 import org.glom.web.client.LibGlomService;
20
21 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
22
23 @SuppressWarnings("serial")
24 public class LibGlomServiceImpl extends RemoteServiceServlet implements LibGlomService {
25         private Document document;
26
27         // Called only when the servlet is stopped (the servlet container is stopped or restarted)
28         public LibGlomServiceImpl() {
29                 Glom.libglom_init();
30                 document = new Document();
31                 document.set_file_uri("file://" + Glom.GLOM_EXAMPLE_FILE_DIR + File.separator + "example_music_collection.glom");
32                 int error = 0;
33                 @SuppressWarnings("unused")
34                 boolean retval = document.load(error);
35                 // FIXME handle error condition
36         }
37
38         /*
39          * FIXME I think Swig is generating long on 64-bit machines and int on
40          * 32-bit machines - need to keep this constant
41          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
42          */
43         public static int safeLongToInt(long l) {
44                 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
45                         throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
46                 }
47                 return (int) l;
48         }
49
50         public GlomDocument getGlomDocument() {
51                 GlomDocument glomDocument = new GlomDocument();
52
53                 // set visible title
54                 glomDocument.setTitle(document.get_database_title());
55
56                 // set array of GlomTables and the default table index
57                 StringVector tableNames = document.get_table_names();
58                 GlomTable[] tables = new GlomTable[safeLongToInt(tableNames.size())];
59                 for (int i = 0; i < tableNames.size(); i++) {
60                         String tableName = tableNames.get(i);
61                         GlomTable glomTable = new GlomTable();
62                         glomTable.setName(tableName);
63                         glomTable.setTitle(document.get_table_title(tableName));
64                         tables[i] = glomTable;
65                         if (tableName.equals(document.get_default_table())) {
66                                 glomDocument.setDefaultTableIndex(i);
67                         }
68                 }
69                 glomDocument.setTables(tables);
70                 return glomDocument;
71
72         }
73
74         public String[] getLayoutListHeaders(String table) {
75                 LayoutGroupVector layoutList = document.get_data_layout_groups("list", table);
76                 LayoutItemVector layoutItems = layoutList.get(0).get_items();
77                 String[] headers = new String[safeLongToInt(layoutItems.size())];
78                 for (int i = 0; i < layoutItems.size(); i++) {
79                         headers[i] = layoutItems.get(i).get_title_or_name();
80                 }
81                 return headers;
82         }
83
84         /*
85          * This is a big hack just get the Layout List widget working. Next steps
86          * will be adding the example data to a db so that it can be queried from it
87          * rather than from the example data API as I'm doing now.
88          */
89         public List<String[]> getTableData(int start, int length, String table) {
90                 LayoutGroupVector layoutList = document.get_data_layout_groups("list", table);
91                 LayoutItemVector layoutItems = layoutList.get(0).get_items();
92                 ExampleRowVector rows = document.get_table_example_data(table);
93
94                 /*
95                  * deal with the case when the requested number of rows is larger than
96                  * the number of rows in the data
97                  */
98                 int displayLength = Math.min(safeLongToInt(rows.size()), length);
99
100                 List<String[]> rowsList = new ArrayList<String[]>(displayLength);
101                 for (int i = start; i < displayLength; i++) {
102                         RowDataVector row = rows.get(i);
103
104                         String[] rowArray = new String[safeLongToInt(layoutItems.size())];
105                         for (int j = 0; j < layoutItems.size(); j++) {
106                                 LayoutItem layoutItem = layoutItems.get(j);
107                                 String fieldName = layoutItem.get_layout_display_name();
108                                 if (fieldName.contains("::")) {
109                                         // implement this when querying data from a db
110                                         rowArray[j] = "Not Implemented";
111                                 } else {
112
113                                         /*
114                                          * need to find the field that the layoutItem is referring
115                                          * to so we can put the example data in the layout list
116                                          * order
117                                          */
118                                         FieldVector fields = document.get_table_fields(table);
119                                         Field field = null;
120                                         int k = 0;
121                                         for (; k < fields.size(); k++) {
122                                                 field = fields.get(k);
123                                                 if (fieldName.equals(field.get_name()))
124                                                         break;
125                                         }
126                                         rowArray[j] = Glom.get_text_for_gda_value(field.get_glom_type(), row.get(k));
127                                 }
128                         }
129                         rowsList.add(rowArray);
130                 }
131                 return rowsList;
132
133         }
134
135         // Called only when the servlet is stopped (the servlet container is stopped or restarted)
136         public void destroy() {
137                 Glom.libglom_deinit();
138         }
139
140 }