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