Update to new java-libglom API.
[online-glom:gwt-glom.git] / src / org / glom / web / server / TableNamesServiceImpl.java
1 package org.glom.web.server;
2
3 import java.io.File;
4
5 import org.glom.libglom.Document;
6 import org.glom.libglom.Glom;
7 import org.glom.libglom.StringVector;
8 import org.glom.web.client.GlomTable;
9 import org.glom.web.client.TableNameService;
10
11 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
12
13 @SuppressWarnings("serial")
14 public class TableNamesServiceImpl extends RemoteServiceServlet implements
15                 TableNameService {
16         private Document document;
17         
18         public TableNamesServiceImpl() {
19                 Glom.libglom_init();
20                 // FIXME Need to call Glom.libglom_deinit()
21                 document = new Document();
22                 document.set_file_uri("file://" + Glom.GLOM_EXAMPLE_FILE_DIR +  File.separator + "example_music_collection.glom");
23                 int error = 0;
24                 @SuppressWarnings("unused")
25                 boolean retval = document.load(error);
26                 // FIXME handle error condition
27         }
28
29         @Override
30         public GlomTable[] getNames() {
31                 StringVector names = document.get_table_names();
32                 int tableSize = safeLongToInt(names.size());
33                 GlomTable[] tableNames = new GlomTable[tableSize];
34                 for (int i = 0; i < tableSize; i++) {
35                         tableNames[i] = new GlomTable(names.get(i));
36                 }
37                 return tableNames;
38         }
39         
40         // FIXME I think Swig is generating long on 64-bit machines and int on 32-bit machines - need to keep this constant
41         // From http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
42         public static int safeLongToInt(long l) {
43             if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
44                 throw new IllegalArgumentException
45                     (l + " cannot be cast to int without changing its value.");
46             }
47             return (int) l;
48         }
49
50
51 }