1
2
3
4
5
6
7 package org.astrogrid.tableserver.test;
8 import java.io.IOException;
9 import java.net.URL;
10 import java.sql.Connection;
11 import java.sql.SQLException;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14 import org.astrogrid.cfg.ConfigReader;
15 import org.astrogrid.cfg.ConfigFactory;
16 import org.astrogrid.dataservice.metadata.VoDescriptionServer;
17 import org.astrogrid.dataservice.metadata.queryable.ConeConfigQueryableResource;
18 import org.astrogrid.dataservice.queriers.DatabaseAccessException;
19 import org.astrogrid.dataservice.queriers.QuerierPluginFactory;
20 import org.astrogrid.tableserver.jdbc.JdbcConnections;
21 import org.astrogrid.tableserver.jdbc.JdbcPlugin;
22 import org.astrogrid.tableserver.jdbc.StdSqlMaker;
23 import org.astrogrid.tableserver.metadata.TableMetaDocInterpreter;
24 import org.astrogrid.tableserver.metadata.TabularDbResources;
25 import org.astrogrid.tableserver.metadata.TabularSkyServiceResources;
26
27 /***
28 * This plugin works with a 'fixed' set of values in an HSQL database. So
29 * SQL statements can be run against it.
30 *
31 * @author M Hill
32 */
33
34 public class SampleStarsPlugin extends JdbcPlugin
35 {
36
37 private static boolean initialised = false;
38
39 private static final SimpleDateFormat sqlDateFormat = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
40
41 public SampleStarsPlugin() throws IOException
42 {
43 if (!initialised ) { initialise(); }
44
45 }
46
47 /*** Sets up the configuration etc for accessing this database */
48 public static void initConfig() {
49
50 ConfigFactory.getCommonConfig().setProperty(QuerierPluginFactory.QUERIER_PLUGIN_KEY, SampleStarsPlugin.class.getName());
51
52 ConfigFactory.getCommonConfig().setProperty(JdbcPlugin.SQL_TRANSLATOR, StdSqlMaker.class.getName());
53
54 ConfigFactory.getCommonConfig().setProperty("datacenter.name", "Sample");
55
56 ConfigFactory.getCommonConfig().setProperty(ConeConfigQueryableResource.CONE_SEARCH_RA_COL_KEY, "RA");
57 ConfigFactory.getCommonConfig().setProperty(ConeConfigQueryableResource.CONE_SEARCH_DEC_COL_KEY,"DEC");
58 ConfigFactory.getCommonConfig().setProperty(ConeConfigQueryableResource.CONE_SEARCH_TABLE_KEY, "SampleStars");
59
60
61
62 ConfigFactory.getCommonConfig().setProperty(ConeConfigQueryableResource.CONE_SEARCH_COL_UNITS_KEY, "deg");
63
64
65 ConfigFactory.getCommonConfig().setProperty(JdbcConnections.JDBC_DRIVERS_KEY, "org.hsqldb.jdbcDriver");
66
67 ConfigFactory.getCommonConfig().setProperty(JdbcConnections.JDBC_URL_KEY, "jdbc:hsqldb:dummydb");
68 ConfigFactory.getCommonConfig().setProperty(JdbcConnections.JDBC_USER_KEY, "sa");
69 ConfigFactory.getCommonConfig().setProperty(JdbcConnections.JDBC_PASSWORD_KEY, "");
70
71
72 ConfigFactory.getCommonConfig().setProperty("datacenter.url", ConfigFactory.getCommonConfig().getProperty("datacenter.url", "http://localhost:8080/pal-Sample/"));
73
74
75
76 URL url = SampleStarsPlugin.class.getResource("samplestars.metadoc.xml");
77 if (url == null) {
78
79 try {
80 url = ConfigReader.resolveFilename("samplestars.metadoc.xml");
81 }
82 catch (IOException e) {
83 throw new RuntimeException(e);
84 }
85 }
86 ConfigFactory.getCommonConfig().setProperty(TableMetaDocInterpreter.TABLE_METADOC_URL_KEY, url.toString());
87
88
89 ConfigFactory.getCommonConfig().setProperties(VoDescriptionServer.RESOURCE_PLUGIN_KEY, new Object[] {
90 TabularSkyServiceResources.class.getName(),
91 TabularDbResources.class.getName(),
92 });
93
94
95
96 ConfigFactory.getCommonConfig().setProperty("datacenter.name", "SampleStars AstroGrid Datacenter");
97 ConfigFactory.getCommonConfig().setProperty("datacenter.shortname", "PAL-Sample");
98 ConfigFactory.getCommonConfig().setProperty("datacenter.publisher", "AstroGrid");
99 ConfigFactory.getCommonConfig().setProperty("datacenter.description", "An unconfigured datacenter; it contains two tables of sample stars and galaxies for testing and demonstration purposes.");
100 ConfigFactory.getCommonConfig().setProperty("datacenter.contact.name", "Martin Hill");
101 ConfigFactory.getCommonConfig().setProperty("datacenter.contact.email", "mch@roe.ac.uk");
102
103 ConfigFactory.getCommonConfig().setProperty("datacenter.authorityId", "astrogrid.org");
104 ConfigFactory.getCommonConfig().setProperty("datacenter.resourceKey", "pal-sample");
105 }
106
107
108
109
110 public static synchronized void initialise() throws DatabaseAccessException {
111
112 if (initialised) return;
113
114 initConfig();
115
116 populateDb();
117 }
118
119 /*** Creates & populates the in-memory database.
120 * Static so we can reach it from our test harness
121 */
122 public static synchronized void populateDb() throws DatabaseAccessException {
123
124
125 Connection connection = null;
126 try {
127 connection = JdbcConnections.makeFromConfig().createConnection();
128 }
129 catch (SQLException se) {
130 throw new DatabaseAccessException("Could not connect to JDBC: "+se);
131 }
132
133
134 try {
135 connection.createStatement().execute("DROP TABLE SampleStars" );
136 connection.createStatement().execute("DROP TABLE SampleGalaxies" );
137 connection.createStatement().execute("DROP TABLE Plates" );
138 }
139 catch (SQLException se) {
140
141 }
142
143 log.info("Populating Database");
144
145
146 try {
147
148
149 connection.createStatement().execute(
150 "CREATE TABLE SampleStars (Id INTEGER IDENTITY, Name VARCHAR(30), Ra DOUBLE, Dec DOUBLE, Mag DOUBLE) "
151 );
152
153
154 connection.createStatement().execute(
155 "CREATE INDEX ssIndex ON SampleStars (Ra, Dec) "
156 );
157
158
159
160 for (int i=0;i<20;i++) {
161 connection.createStatement().execute(
162 "INSERT INTO SampleStars VALUES ("+i+", 'A star', "+(30+i*2)+", "+(30-i*2)+", "+i+")"
163 );
164 }
165
166
167 int id=21;
168 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Pleidies LE', 56.6, 23.65, 10)"); id++;
169 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Pleidies RE', 56.9, 23.65, 10)"); id++;
170 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Pleidies Nose', 56.75, 23.87, 8)"); id++;
171 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Pleidies Grin', 56.5, 23.9, 12)"); id++;
172 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Pleidies Grin', 56.7, 24.0, 12)"); id++;
173 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Pleidies Grin', 56.8, 24.0, 12)"); id++;
174 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Pleidies Grin', 57.0, 23.9, 12)"); id++;
175
176
177 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Not Pleidies', 56.6, 23.6, 10)"); id++;
178 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Not Pleidies', 56, 23, 5)"); id++;
179 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Not Pleidies', 58, 24.5, 5)"); id++;
180 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Not Pleidies', 56, 24.5, 5)"); id++;
181 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Not Pleidies', 58, 23, 5)"); id++;
182
183
184 for (double ra=0;ra<360;ra=ra+2) {
185
186 for (double dec=-90;dec<90;dec=dec+2) {
187
188 connection.createStatement().execute("INSERT INTO SampleStars VALUES ("+id+", 'Background', "+ra+", "+dec+", 20)"); id++;
189 }
190
191 System.out.print(".");
192 }
193
194
195
196 connection.createStatement().execute(
197 "CREATE TABLE SampleGalaxies (Id INTEGER IDENTITY, Ra DOUBLE, Dec DOUBLE, Shape VARCHAR(20)) "
198 );
199
200
201 String[] shapes = new String[] {"ELLIPTICAL", "SPIRAL", "IRREGULAR" };
202 for (int i=0;i<20;i++) {
203
204 connection.createStatement().execute(
205 "INSERT INTO SampleGalaxies VALUES ("+i+", "+(200+i*2)+", "+(200-i*2)+", '"+shapes[i % 2]+"')"
206 );
207 }
208
209
210 connection.createStatement().execute(
211 "CREATE TABLE Plates (Id INTEGER IDENTITY, CenterRa DOUBLE, CenterDec DOUBLE, ObsTime DATETIME) "
212 );
213 long todayTime = new Date().getTime();
214 for (int i=0;i<5;i++) {
215 connection.createStatement().execute(
216
217 "INSERT INTO Plates VALUES ("+i+", "+(100+i*20)+", "+(100-i*20)+", '"+new java.sql.Timestamp(todayTime-i*1000)+"')"
218 );
219 }
220
221
222 connection.commit();
223 connection.close();
224 }
225 catch (SQLException se) {
226 log.error("Populating demo db",se);
227 }
228
229 initialised = true;
230
231 log.info("...database populated");
232
233
234
235
236
237
238
239
240
241
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368