Warning: Can't synchronize with repository "(default)" (/home/git/ome.git does not appear to be a Git repository.). Look in the Trac log for more information.

Ticket #1951: TableStringExample.java

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

1.3.13-PRO © 2008-2011 Agilo Software all rights reserved (this page was served in: 0.31411 sec.)

We're Hiring!