1 package org.glom.web.server;
4 import java.util.ArrayList;
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;
21 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
23 @SuppressWarnings("serial")
24 public class LibGlomServiceImpl extends RemoteServiceServlet implements LibGlomService {
25 private Document document;
27 public LibGlomServiceImpl() {
29 // FIXME Need to call Glom.libglom_deinit()
30 document = new Document();
31 document.set_file_uri("file://" + Glom.GLOM_EXAMPLE_FILE_DIR + File.separator + "example_music_collection.glom");
33 @SuppressWarnings("unused")
34 boolean retval = document.load(error);
35 // FIXME handle error condition
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
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.");
50 public GlomDocument getGlomDocument() {
51 GlomDocument glomDocument = new GlomDocument();
54 glomDocument.setTitle(document.get_database_title());
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);
69 glomDocument.setTables(tables);
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();
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.
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);
95 * deal with the case when the requested number of rows is larger than
96 * the number of rows in the data
98 int displayLength = Math.min(safeLongToInt(rows.size()), length);
100 List<String[]> rowsList = new ArrayList<String[]>(displayLength);
101 for (int i = start; i < displayLength; i++) {
102 RowDataVector row = rows.get(i);
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";
114 * need to find the field that the layoutItem is referring
115 * to so we can put the example data in the layout list
118 FieldVector fields = document.get_table_fields(table);
121 for (; k < fields.size(); k++) {
122 field = fields.get(k);
123 if (fieldName.equals(field.get_name()))
126 rowArray[j] = Glom.get_text_for_gda_value(field.get_glom_type(), row.get(k));
129 rowsList.add(rowArray);