1 | package ome.formats.importer.gui; |
---|
2 | |
---|
3 | import java.text.SimpleDateFormat; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import org.apache.commons.logging.Log; |
---|
8 | import org.apache.commons.logging.LogFactory; |
---|
9 | |
---|
10 | import Glacier2.CannotCreateSessionException; |
---|
11 | import Glacier2.PermissionDeniedException; |
---|
12 | |
---|
13 | import ome.formats.OMEROMetadataStoreClient; |
---|
14 | import ome.formats.importer.IObserver; |
---|
15 | import omero.ServerError; |
---|
16 | import omero.api.IQueryPrx; |
---|
17 | import omero.api.ServiceFactoryPrx; |
---|
18 | import omero.grid.Column; |
---|
19 | import omero.grid.LongColumn; |
---|
20 | import omero.grid.StringColumn; |
---|
21 | import omero.grid.TablePrx; |
---|
22 | import omero.model.OriginalFile; |
---|
23 | |
---|
24 | public class TableStringExample |
---|
25 | { |
---|
26 | |
---|
27 | private static String USER = "user"; |
---|
28 | private static String PASS = "pass"; |
---|
29 | private static String SERVER = "server"; |
---|
30 | |
---|
31 | /** Logger for this class. */ |
---|
32 | private static Log log = LogFactory.getLog(TableStringExample.class); |
---|
33 | |
---|
34 | ArrayList<IObserver> observers = new ArrayList<IObserver>(); |
---|
35 | |
---|
36 | public SimpleDateFormat day = new SimpleDateFormat("MMM d, ''yy"); |
---|
37 | public SimpleDateFormat hour = new SimpleDateFormat("HH:mm"); |
---|
38 | |
---|
39 | private static final int DEFAULT_BUFFER_SIZE = 3; |
---|
40 | |
---|
41 | private static final int BASE_UID_COLUMN = 0; |
---|
42 | private static final int BASE_DATETIME_COLUMN = 1; |
---|
43 | private static final int BASE_STATUS_COLUMN = 2; |
---|
44 | |
---|
45 | private ServiceFactoryPrx sf; |
---|
46 | private IQueryPrx iQuery; |
---|
47 | private TablePrx baseTable; |
---|
48 | private Column[] baseColumns; |
---|
49 | |
---|
50 | private String dbName = "StringTextExample"; |
---|
51 | |
---|
52 | private int lastIndex; |
---|
53 | |
---|
54 | public void initialize(ServiceFactoryPrx sf) |
---|
55 | throws ServerError { |
---|
56 | this.sf = sf; |
---|
57 | this.iQuery = sf.getQueryService(); |
---|
58 | baseColumns = createBaseColumns(DEFAULT_BUFFER_SIZE); |
---|
59 | createBaseTable(); |
---|
60 | } |
---|
61 | |
---|
62 | // Creates a number of empty rows of [rows] size for the base history table |
---|
63 | private Column[] createBaseColumns(int rows) { |
---|
64 | Column[] newColumns = new Column[3]; |
---|
65 | newColumns[BASE_UID_COLUMN] = new LongColumn("Uid", "", new long[rows]); |
---|
66 | newColumns[BASE_DATETIME_COLUMN] = new StringColumn("DateTime", "", 64, new String[rows]); |
---|
67 | newColumns[BASE_STATUS_COLUMN] = new StringColumn("Status", "", 64, new String[rows]); |
---|
68 | return newColumns; |
---|
69 | } |
---|
70 | |
---|
71 | @SuppressWarnings("deprecation") |
---|
72 | private void createBaseTable() throws ServerError |
---|
73 | { |
---|
74 | List<OriginalFile> files = getOriginalFiles(dbName); |
---|
75 | |
---|
76 | baseTable = sf.sharedResources().newTable(1, dbName); |
---|
77 | baseTable.initialize(baseColumns); |
---|
78 | |
---|
79 | if (files.isEmpty()) |
---|
80 | { |
---|
81 | log.debug("Creating new table."); |
---|
82 | baseTable = sf.sharedResources().newTable(1, dbName); |
---|
83 | if (baseTable == null) |
---|
84 | System.err.println("baseTable is null"); |
---|
85 | baseTable.initialize(baseColumns); |
---|
86 | } else { |
---|
87 | log.debug("Using existing table."); |
---|
88 | System.err.println(files.get(0).getPath().getValue()); |
---|
89 | baseTable = sf.sharedResources().openTable(files.get(0)); |
---|
90 | if (baseTable == null) |
---|
91 | System.err.println("table is null"); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | private void addBaseTableRow(String import_status) { |
---|
96 | try |
---|
97 | { |
---|
98 | LongColumn uid = (LongColumn) baseColumns[BASE_UID_COLUMN]; |
---|
99 | StringColumn date = (StringColumn) baseColumns[BASE_DATETIME_COLUMN]; |
---|
100 | StringColumn status = (StringColumn) baseColumns[BASE_STATUS_COLUMN]; |
---|
101 | |
---|
102 | int size = lastIndex + 1; |
---|
103 | Column[] newColumns = createBaseColumns(size); |
---|
104 | |
---|
105 | LongColumn uid2 = (LongColumn) newColumns[BASE_UID_COLUMN]; |
---|
106 | System.arraycopy(uid.values, 0, uid2.values, 0, size); |
---|
107 | |
---|
108 | StringColumn date2 = (StringColumn) newColumns[BASE_DATETIME_COLUMN]; |
---|
109 | System.arraycopy(date.values, 0, date2.values, 0, size); |
---|
110 | |
---|
111 | StringColumn status2 = (StringColumn) newColumns[BASE_STATUS_COLUMN]; |
---|
112 | System.arraycopy(status.values, 0, status2.values, 0, size); |
---|
113 | |
---|
114 | // Update our references |
---|
115 | baseColumns = newColumns; |
---|
116 | uid = (LongColumn) baseColumns[BASE_UID_COLUMN]; |
---|
117 | date = (StringColumn) baseColumns[BASE_DATETIME_COLUMN]; |
---|
118 | status = (StringColumn) baseColumns[BASE_STATUS_COLUMN]; |
---|
119 | |
---|
120 | //uid2.values[size] = size; |
---|
121 | //date2.values[size] = sqlDateTimeFormat.format(new Date()); |
---|
122 | //status2.values[size] = import_status; |
---|
123 | |
---|
124 | System.err.println("Rows in table:" + baseTable.getNumberOfRows()); |
---|
125 | baseTable.addData(baseColumns); |
---|
126 | baseColumns = createBaseColumns(DEFAULT_BUFFER_SIZE); |
---|
127 | } catch (Throwable t) { |
---|
128 | throw new RuntimeException(t); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | public List<OriginalFile> getOriginalFiles(String fileName) |
---|
133 | { |
---|
134 | try |
---|
135 | { |
---|
136 | List l = iQuery.findAllByString(OriginalFile.class.getName(), "name", fileName, false, null); |
---|
137 | return (List<OriginalFile>) l; |
---|
138 | } |
---|
139 | catch (ServerError e) |
---|
140 | { |
---|
141 | throw new RuntimeException(e); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | public static void main (String[] args) throws CannotCreateSessionException, PermissionDeniedException, ServerError |
---|
146 | { |
---|
147 | OMEROMetadataStoreClient store = new OMEROMetadataStoreClient(); |
---|
148 | |
---|
149 | store.initialize(USER, PASS, SERVER, 4063); |
---|
150 | |
---|
151 | TableStringExample hts = new TableStringExample(); |
---|
152 | try |
---|
153 | { |
---|
154 | hts.initialize(store.getServiceFactory()); |
---|
155 | hts.addBaseTableRow("test"); |
---|
156 | } catch (ServerError e) |
---|
157 | { |
---|
158 | throw new RuntimeException(e); |
---|
159 | } |
---|
160 | System.err.println("Done"); |
---|
161 | } |
---|
162 | } |
---|