Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 3318:dbe2f85bf160
merged flys-artifacts/2.8
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:35 +0200 |
parents | 5642a83420f2 |
children | cbe2febe30cc |
comparison
equal
deleted
inserted
replaced
2987:98c7a46ec5ae | 3318:dbe2f85bf160 |
---|---|
1 package de.intevation.flys.artifacts.datacage; | |
2 | |
3 import java.util.Collection; | |
4 import java.util.List; | |
5 import java.util.Date; | |
6 | |
7 import java.sql.SQLException; | |
8 import java.sql.PreparedStatement; | |
9 import java.sql.Types; | |
10 import java.sql.Timestamp; | |
11 | |
12 import de.intevation.artifacts.GlobalContext; | |
13 import de.intevation.artifacts.ArtifactCollection; | |
14 import de.intevation.artifacts.User; | |
15 | |
16 import de.intevation.artifactdatabase.db.SQL; | |
17 import de.intevation.artifactdatabase.db.SQLExecutor; | |
18 | |
19 import de.intevation.artifactdatabase.LifetimeListener; | |
20 import de.intevation.artifactdatabase.Backend; | |
21 | |
22 import de.intevation.artifactdatabase.data.StateData; | |
23 | |
24 import de.intevation.artifactdatabase.state.Output; | |
25 import de.intevation.artifactdatabase.state.Facet; | |
26 | |
27 import de.intevation.artifacts.Artifact; | |
28 import de.intevation.artifacts.ArtifactDatabase; | |
29 import de.intevation.artifacts.ArtifactDatabaseException; | |
30 | |
31 import de.intevation.flys.artifacts.FLYSArtifact; | |
32 | |
33 import de.intevation.artifacts.common.utils.LRUCache; | |
34 | |
35 import org.apache.log4j.Logger; | |
36 | |
37 import org.w3c.dom.Document; | |
38 | |
39 public class Datacage | |
40 implements LifetimeListener | |
41 { | |
42 private static Logger log = Logger.getLogger(Datacage.class); | |
43 | |
44 public static final String DATACAGE_KEY = | |
45 "global.datacage.instance"; | |
46 | |
47 public static final String ARTEFACT_DATABASE_KEY = | |
48 "global.artifact.database"; | |
49 | |
50 private String SQL_DELETE_ALL_USERS = "delete.all.users"; | |
51 private String SQL_DELETE_ALL_ARTIFACTS = "delete.all.artifacts"; | |
52 private String SQL_USER_ID_NEXTVAL = "user.id.nextval"; | |
53 private String SQL_USER_BY_GID = "user.by.gid"; | |
54 private String SQL_INSERT_USER = "insert.user"; | |
55 private String SQL_COLLECTION_BY_GID = "collection.by.gid"; | |
56 private String SQL_COLLECTION_ID_NEXTVAL = "collection.id.nextval"; | |
57 private String SQL_INSERT_COLLECTION = "insert.collection"; | |
58 private String SQL_ARTIFACT_BY_GID = "artifact.by.gid"; | |
59 private String SQL_COLLECTION_ITEM_ID_NEXTVAL = | |
60 "collection.item.id.nextval"; | |
61 private String SQL_INSERT_COLLECTION_ITEM = "insert.collection.item"; | |
62 private String SQL_ARTIFACT_ID_NEXTVAL = "artifact.id.nextval"; | |
63 private String SQL_INSERT_ARTIFACT = "insert.artifact"; | |
64 private String SQL_ARTIFACT_DATA_ID_NEXTVAL = "artifact.data.id.nextval"; | |
65 private String SQL_INSERT_ARTIFACT_DATA = "insert.artifact.data"; | |
66 private String SQL_OUT_ID_NEXTVALUE = "out.id.nextval"; | |
67 private String SQL_INSERT_OUT = "insert.out"; | |
68 private String SQL_FACET_ID_NEXTVAL = "facet.id.nextval"; | |
69 private String SQL_INSERT_FACET = "insert.facet"; | |
70 private String SQL_UPDATE_COLLECTION_NAME = "update.collection.name"; | |
71 private String SQL_DELETE_ARTIFACT_FROM_COLLECTION = | |
72 "delete.artifact.from.collection"; | |
73 private String SQL_DELETE_COLLECTION_BY_GID = | |
74 "delete.collection.by.gid"; | |
75 private String SQL_DELETE_USER_BY_GID = "delete.user.by.gid"; | |
76 private String SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID = | |
77 "delete.artifact.data.by.artifact.id"; | |
78 private String SQL_DELETE_OUTS_BY_ARTIFACT_ID = | |
79 "delete.outs.by.artifact.id"; | |
80 private String SQL_DELETE_FACETS_BY_ARTIFACT_ID = | |
81 "delete.facets.by.artifact.id"; | |
82 private String SQL_DELETE_ARTIFACT_BY_GID = | |
83 "delete.artifact.by.gid"; | |
84 | |
85 protected SQLExecutor sqlExecutor; | |
86 | |
87 public class InitialScan | |
88 implements ArtifactDatabase.ArtifactLoadedCallback | |
89 { | |
90 protected LRUCache<String, Integer> users; | |
91 protected LRUCache<String, Integer> collections; | |
92 protected LRUCache<String, Integer> artifacts; | |
93 | |
94 protected GlobalContext context; | |
95 | |
96 public InitialScan() { | |
97 users = new LRUCache<String, Integer>(); | |
98 collections = new LRUCache<String, Integer>(); | |
99 artifacts = new LRUCache<String, Integer>(); | |
100 } | |
101 | |
102 public InitialScan(GlobalContext context) { | |
103 this(); | |
104 this.context = context; | |
105 } | |
106 | |
107 @Override | |
108 public void artifactLoaded( | |
109 String userId, | |
110 String collectionId, | |
111 String collectionName, | |
112 Date collectionCreated, | |
113 String artifactId, | |
114 Date artifactCreated, | |
115 Artifact artifact | |
116 ) { | |
117 if (!(artifact instanceof FLYSArtifact)) { | |
118 log.warn("ignoring none FLYS artifacts"); | |
119 return; | |
120 } | |
121 | |
122 FLYSArtifact flysArtifact = (FLYSArtifact)artifact; | |
123 | |
124 Integer uId = getUserId(userId); | |
125 Integer cId = getCollectionId( | |
126 collectionId, uId, collectionName, collectionCreated); | |
127 | |
128 storeArtifact(artifactId, cId, flysArtifact, artifactCreated); | |
129 } | |
130 | |
131 protected Integer getId( | |
132 LRUCache<String, Integer> cache, | |
133 final String idString, | |
134 final String selectById | |
135 ) { | |
136 Integer id = cache.get(idString); | |
137 if (id != null) { | |
138 return id; | |
139 } | |
140 | |
141 final Integer [] res = new Integer[1]; | |
142 | |
143 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
144 @Override | |
145 public boolean doIt() throws SQLException { | |
146 prepareStatement(selectById); | |
147 stmnt.setString(1, idString); | |
148 result = stmnt.executeQuery(); | |
149 if (!result.next()) { | |
150 return false; | |
151 } | |
152 res[0] = result.getInt(1); | |
153 return true; | |
154 } | |
155 }; | |
156 | |
157 if (exec.runRead()) { | |
158 cache.put(idString, res[0]); | |
159 return res[0]; | |
160 } | |
161 | |
162 return null; | |
163 } | |
164 | |
165 protected void storeArtifact( | |
166 final String artifactId, | |
167 Integer collectionId, | |
168 final FLYSArtifact artifact, | |
169 final Date artifactCreated | |
170 ) { | |
171 Integer aId = getId(artifacts, artifactId, SQL_ARTIFACT_BY_GID); | |
172 | |
173 if (aId != null) { | |
174 // We've already stored it. Just create the collection item. | |
175 storeCollectionItem(collectionId, aId); | |
176 return; | |
177 } | |
178 // We need to write it to database | |
179 | |
180 final Integer [] res = new Integer[1]; | |
181 | |
182 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
183 @Override | |
184 public boolean doIt() throws SQLException { | |
185 prepareStatement(SQL_ARTIFACT_ID_NEXTVAL); | |
186 result = stmnt.executeQuery(); | |
187 if (!result.next()) { | |
188 return false; | |
189 } | |
190 res[0] = result.getInt(1); | |
191 reset(); | |
192 prepareStatement(SQL_INSERT_ARTIFACT); | |
193 stmnt.setInt (1, res[0]); | |
194 stmnt.setString(2, artifactId); | |
195 stmnt.setString(3, artifact.getCurrentStateId()); | |
196 Timestamp timestamp = new Timestamp(artifactCreated != null | |
197 ? artifactCreated.getTime() | |
198 : System.currentTimeMillis()); | |
199 stmnt.setTimestamp(4, timestamp); | |
200 stmnt.execute(); | |
201 conn.commit(); | |
202 return true; | |
203 } | |
204 }; | |
205 | |
206 if (!exec.runWrite()) { | |
207 log.error("storing of artifact failed."); | |
208 return; | |
209 } | |
210 | |
211 artifacts.put(artifactId, aId = res[0]); | |
212 | |
213 storeCollectionItem(collectionId, aId); | |
214 | |
215 storeData(aId, artifact); | |
216 | |
217 storeOuts(aId, artifact, context); | |
218 } | |
219 | |
220 | |
221 protected void storeCollectionItem( | |
222 final Integer collectionId, | |
223 final Integer artifactId | |
224 ) { | |
225 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
226 @Override | |
227 public boolean doIt() throws SQLException { | |
228 prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL); | |
229 result = stmnt.executeQuery(); | |
230 if (!result.next()) { | |
231 return false; | |
232 } | |
233 int ciId = result.getInt(1); | |
234 reset(); | |
235 prepareStatement(SQL_INSERT_COLLECTION_ITEM); | |
236 stmnt.setInt(1, ciId); | |
237 stmnt.setInt(2, collectionId); | |
238 stmnt.setInt(3, artifactId); | |
239 stmnt.execute(); | |
240 conn.commit(); | |
241 return true; | |
242 } | |
243 }; | |
244 | |
245 if (!exec.runWrite()) { | |
246 log.error("storing of collection item failed."); | |
247 } | |
248 } | |
249 | |
250 protected Integer getCollectionId( | |
251 final String collectionId, | |
252 final Integer ownerId, | |
253 final String collectionName, | |
254 final Date collectionCreated | |
255 ) { | |
256 Integer c = getId(collections, collectionId, SQL_COLLECTION_BY_GID); | |
257 | |
258 if (c != null) { | |
259 return c; | |
260 } | |
261 | |
262 final Integer [] res = new Integer[1]; | |
263 | |
264 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
265 @Override | |
266 public boolean doIt() throws SQLException { | |
267 prepareStatement(SQL_COLLECTION_ID_NEXTVAL); | |
268 result = stmnt.executeQuery(); | |
269 if (!result.next()) { | |
270 return false; | |
271 } | |
272 res[0] = result.getInt(1); | |
273 reset(); | |
274 prepareStatement(SQL_INSERT_COLLECTION); | |
275 stmnt.setInt (1, res[0]); | |
276 stmnt.setString(2, collectionId); | |
277 stmnt.setInt (3, ownerId); | |
278 setString(stmnt, 4, collectionName); | |
279 Timestamp timestamp = new Timestamp(collectionCreated != null | |
280 ? collectionCreated.getTime() | |
281 : System.currentTimeMillis()); | |
282 stmnt.setTimestamp(5, timestamp); | |
283 stmnt.execute(); | |
284 conn.commit(); | |
285 return true; | |
286 } | |
287 }; | |
288 | |
289 if (exec.runWrite()) { | |
290 collections.put(collectionId, res[0]); | |
291 return res[0]; | |
292 } | |
293 | |
294 return null; | |
295 } | |
296 | |
297 protected Integer getUserId(final String userId) { | |
298 | |
299 Integer u = getId(users, userId, SQL_USER_BY_GID); | |
300 | |
301 if (u != null) { | |
302 return u; | |
303 } | |
304 | |
305 final Integer [] res = new Integer[1]; | |
306 | |
307 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
308 @Override | |
309 public boolean doIt() throws SQLException { | |
310 prepareStatement(SQL_USER_ID_NEXTVAL); | |
311 result = stmnt.executeQuery(); | |
312 if (!result.next()) { | |
313 return false; | |
314 } | |
315 res[0] = result.getInt(1); | |
316 reset(); | |
317 prepareStatement(SQL_INSERT_USER); | |
318 stmnt.setInt (1, res[0]); | |
319 stmnt.setString(2, userId); | |
320 stmnt.execute(); | |
321 conn.commit(); | |
322 return true; | |
323 } | |
324 }; | |
325 | |
326 if (exec.runWrite()) { | |
327 users.put(userId, res[0]); | |
328 return res[0]; | |
329 } | |
330 | |
331 return null; | |
332 } | |
333 | |
334 public boolean scan(ArtifactDatabase adb) { | |
335 log.debug("scan"); | |
336 try { | |
337 adb.loadAllArtifacts(this); | |
338 } | |
339 catch (ArtifactDatabaseException ade) { | |
340 log.error(ade); | |
341 return false; | |
342 } | |
343 return true; | |
344 } | |
345 } // class InitialScan | |
346 | |
347 | |
348 public Datacage() { | |
349 } | |
350 | |
351 @Override | |
352 public void setup(Document document) { | |
353 log.debug("setup"); | |
354 DBConfig config = DBConfig.getInstance(); | |
355 setupSQL(config.getSQL()); | |
356 sqlExecutor = new SQLExecutor(config.getDBConnection()); | |
357 } | |
358 | |
359 protected void setupSQL(SQL sql) { | |
360 SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS); | |
361 SQL_DELETE_ALL_ARTIFACTS = sql.get(SQL_DELETE_ALL_ARTIFACTS); | |
362 SQL_USER_ID_NEXTVAL = sql.get(SQL_USER_ID_NEXTVAL); | |
363 SQL_USER_BY_GID = sql.get(SQL_USER_BY_GID); | |
364 SQL_INSERT_USER = sql.get(SQL_INSERT_USER); | |
365 SQL_COLLECTION_BY_GID = sql.get(SQL_COLLECTION_BY_GID); | |
366 SQL_COLLECTION_ID_NEXTVAL = sql.get(SQL_COLLECTION_ID_NEXTVAL); | |
367 SQL_INSERT_COLLECTION = sql.get(SQL_INSERT_COLLECTION); | |
368 SQL_ARTIFACT_BY_GID = sql.get(SQL_ARTIFACT_BY_GID); | |
369 SQL_COLLECTION_ITEM_ID_NEXTVAL = | |
370 sql.get(SQL_COLLECTION_ITEM_ID_NEXTVAL); | |
371 SQL_INSERT_COLLECTION_ITEM = | |
372 sql.get(SQL_INSERT_COLLECTION_ITEM); | |
373 SQL_ARTIFACT_ID_NEXTVAL = sql.get(SQL_ARTIFACT_ID_NEXTVAL); | |
374 SQL_INSERT_ARTIFACT = sql.get(SQL_INSERT_ARTIFACT); | |
375 SQL_ARTIFACT_DATA_ID_NEXTVAL = sql.get(SQL_ARTIFACT_DATA_ID_NEXTVAL); | |
376 SQL_INSERT_ARTIFACT_DATA = sql.get(SQL_INSERT_ARTIFACT_DATA); | |
377 SQL_OUT_ID_NEXTVALUE = sql.get(SQL_OUT_ID_NEXTVALUE); | |
378 SQL_INSERT_OUT = sql.get(SQL_INSERT_OUT); | |
379 SQL_FACET_ID_NEXTVAL = sql.get(SQL_FACET_ID_NEXTVAL); | |
380 SQL_INSERT_FACET = sql.get(SQL_INSERT_FACET); | |
381 SQL_UPDATE_COLLECTION_NAME = sql.get(SQL_UPDATE_COLLECTION_NAME); | |
382 SQL_DELETE_ARTIFACT_FROM_COLLECTION = | |
383 sql.get(SQL_DELETE_ARTIFACT_FROM_COLLECTION); | |
384 SQL_DELETE_COLLECTION_BY_GID = sql.get(SQL_DELETE_COLLECTION_BY_GID); | |
385 SQL_DELETE_USER_BY_GID = sql.get(SQL_DELETE_USER_BY_GID); | |
386 SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID = | |
387 sql.get(SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID); | |
388 SQL_DELETE_OUTS_BY_ARTIFACT_ID = | |
389 sql.get(SQL_DELETE_OUTS_BY_ARTIFACT_ID); | |
390 SQL_DELETE_FACETS_BY_ARTIFACT_ID = | |
391 sql.get(SQL_DELETE_FACETS_BY_ARTIFACT_ID); | |
392 SQL_DELETE_ARTIFACT_BY_GID = | |
393 sql.get(SQL_DELETE_ARTIFACT_BY_GID); | |
394 } | |
395 | |
396 protected static final int numFacets(List<Output> outs) { | |
397 int sum = 0; | |
398 for (Output out: outs) { | |
399 sum += out.getFacets().size(); | |
400 } | |
401 return sum; | |
402 } | |
403 | |
404 protected static final void setString( | |
405 PreparedStatement stmnt, | |
406 int index, | |
407 Object value | |
408 ) | |
409 throws SQLException | |
410 { | |
411 if (value == null) { | |
412 stmnt.setNull(index, Types.VARCHAR); | |
413 } | |
414 else { | |
415 stmnt.setString(index, value.toString()); | |
416 } | |
417 } | |
418 | |
419 @Override | |
420 public void systemUp(GlobalContext context) { | |
421 log.debug("systemUp entered"); | |
422 initialScan(context); | |
423 context.put(DATACAGE_KEY, this); | |
424 log.debug("systemUp leaved"); | |
425 } | |
426 | |
427 protected void initialScan(GlobalContext context) { | |
428 log.debug("initialScan"); | |
429 | |
430 Object adbObject = context.get(ARTEFACT_DATABASE_KEY); | |
431 | |
432 if (!(adbObject instanceof ArtifactDatabase)) { | |
433 log.error("missing artefact database. Cannot scan"); | |
434 return; | |
435 } | |
436 | |
437 ArtifactDatabase adb = (ArtifactDatabase)adbObject; | |
438 | |
439 if (!cleanDatabase()) { | |
440 log.error("cleaning database failed"); | |
441 return; | |
442 } | |
443 | |
444 InitialScan is = new InitialScan(context); | |
445 | |
446 if (!is.scan(adb)) { | |
447 log.error("initial scan failed"); | |
448 return; | |
449 } | |
450 | |
451 } | |
452 | |
453 protected boolean cleanDatabase() { | |
454 | |
455 log.debug("cleanDatabase"); | |
456 | |
457 boolean success = sqlExecutor.new Instance() { | |
458 @Override | |
459 public boolean doIt() throws SQLException { | |
460 prepareStatement(SQL_DELETE_ALL_USERS); | |
461 stmnt.execute(); | |
462 prepareStatement(SQL_DELETE_ALL_ARTIFACTS); | |
463 stmnt.execute(); | |
464 conn.commit(); | |
465 return true; | |
466 } | |
467 }.runWrite(); | |
468 | |
469 log.debug("after runWrite(): " + success); | |
470 | |
471 return success; | |
472 } | |
473 | |
474 | |
475 @Override | |
476 public void systemDown(GlobalContext context) { | |
477 log.debug("systemDown"); | |
478 } | |
479 | |
480 public void setup(GlobalContext globalContext) { | |
481 log.debug("setup"); | |
482 } | |
483 | |
484 public void createdArtifact( | |
485 Artifact artifact, | |
486 Backend backend, | |
487 GlobalContext context | |
488 ) { | |
489 log.debug("createdArtifact"); | |
490 | |
491 if (artifact == null) { | |
492 log.warn("artifact to create is null"); | |
493 return; | |
494 } | |
495 | |
496 if (!(artifact instanceof FLYSArtifact)) { | |
497 log.warn("need FLYSArtifact here (have " + artifact.getClass() + ")"); | |
498 return; | |
499 } | |
500 | |
501 final FLYSArtifact flys = (FLYSArtifact)artifact; | |
502 | |
503 final int [] res = new int[1]; | |
504 | |
505 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
506 @Override | |
507 public boolean doIt() throws SQLException { | |
508 prepareStatement(SQL_ARTIFACT_ID_NEXTVAL); | |
509 result = stmnt.executeQuery(); | |
510 if (!result.next()) { | |
511 log.error("id generation for artifact failed"); | |
512 return false; | |
513 } | |
514 res[0] = result.getInt(1); | |
515 reset(); | |
516 prepareStatement(SQL_INSERT_ARTIFACT); | |
517 stmnt.setInt (1, res[0]); | |
518 stmnt.setString (2, flys.identifier()); | |
519 stmnt.setString (3, flys.getCurrentStateId()); | |
520 stmnt.setTimestamp(4, | |
521 new Timestamp(System.currentTimeMillis())); | |
522 stmnt.execute(); | |
523 conn.commit(); | |
524 return true; | |
525 } | |
526 }; | |
527 | |
528 if (!exec.runWrite()) { | |
529 log.error("storing of artifact failed."); | |
530 return; | |
531 } | |
532 | |
533 storeData(res[0], flys); | |
534 storeOuts(res[0], flys, context); | |
535 } | |
536 | |
537 public void storedArtifact( | |
538 Artifact artifact, | |
539 Backend backend, | |
540 GlobalContext context | |
541 ) { | |
542 log.debug("storedArtifact"); | |
543 if (!(artifact instanceof FLYSArtifact)) { | |
544 log.warn("need FLYSArtifact here but have a " + artifact.getClass()); | |
545 return; | |
546 } | |
547 | |
548 final FLYSArtifact flys = (FLYSArtifact)artifact; | |
549 | |
550 final Integer [] res = new Integer[1]; | |
551 | |
552 // check first if artifact already exists | |
553 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
554 @Override | |
555 public boolean doIt() throws SQLException { | |
556 prepareStatement(SQL_ARTIFACT_BY_GID); | |
557 stmnt.setString(1, flys.identifier()); | |
558 result = stmnt.executeQuery(); | |
559 if (!result.next()) { | |
560 // new artifact | |
561 return true; | |
562 } | |
563 res[0] = result.getInt(1); | |
564 return true; | |
565 } | |
566 }; | |
567 | |
568 if (!exec.runRead()) { | |
569 log.error("querying artifact failed"); | |
570 return; | |
571 } | |
572 | |
573 if (res[0] == null) { // new artifact | |
574 createdArtifact(artifact, backend, context); | |
575 return; | |
576 } | |
577 | |
578 // artifact already exists -> delete old data | |
579 exec = sqlExecutor.new Instance() { | |
580 @Override | |
581 public boolean doIt() throws SQLException { | |
582 prepareStatement(SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID); | |
583 stmnt.setInt(1, res[0]); | |
584 stmnt.execute(); | |
585 prepareStatement(SQL_DELETE_FACETS_BY_ARTIFACT_ID); | |
586 stmnt.setInt(1, res[0]); | |
587 stmnt.execute(); | |
588 prepareStatement(SQL_DELETE_OUTS_BY_ARTIFACT_ID); | |
589 stmnt.setInt(1, res[0]); | |
590 stmnt.execute(); | |
591 conn.commit(); | |
592 return true; | |
593 } | |
594 }; | |
595 | |
596 if (!exec.runWrite()) { | |
597 log.error("deleting old artifact data failed"); | |
598 return; | |
599 } | |
600 | |
601 // write new data | |
602 storeData(res[0], flys); | |
603 storeOuts(res[0], flys, context); | |
604 } | |
605 | |
606 public void createdUser( | |
607 final User user, | |
608 Backend backend, | |
609 GlobalContext context | |
610 ) { | |
611 log.debug("createdUser"); | |
612 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
613 @Override | |
614 public boolean doIt() throws SQLException { | |
615 prepareStatement(SQL_USER_ID_NEXTVAL); | |
616 result = stmnt.executeQuery(); | |
617 if (!result.next()) { | |
618 log.error("id generation for user failed"); | |
619 return false; | |
620 } | |
621 int uId = result.getInt(1); | |
622 reset(); | |
623 prepareStatement(SQL_INSERT_USER); | |
624 stmnt.setInt(1, uId); | |
625 stmnt.setString(2, user.identifier()); | |
626 stmnt.execute(); | |
627 conn.commit(); | |
628 return true; | |
629 } | |
630 }; | |
631 | |
632 if (!exec.runWrite()) { | |
633 log.error("create user failed"); | |
634 } | |
635 } | |
636 | |
637 public void deletedUser( | |
638 final String identifier, | |
639 Backend backend, | |
640 GlobalContext context | |
641 ) { | |
642 log.debug("deletedUser"); | |
643 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
644 @Override | |
645 public boolean doIt() throws SQLException { | |
646 prepareStatement(SQL_DELETE_USER_BY_GID); | |
647 stmnt.setString(1, identifier); | |
648 stmnt.execute(); | |
649 conn.commit(); | |
650 return true; | |
651 } | |
652 }; | |
653 | |
654 if (!exec.runWrite()) { | |
655 log.error("delete user failed"); | |
656 } | |
657 } | |
658 | |
659 public void createdCollection( | |
660 final ArtifactCollection collection, | |
661 Backend backend, | |
662 GlobalContext context | |
663 ) { | |
664 log.debug("createdCollection"); | |
665 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
666 @Override | |
667 public boolean doIt() throws SQLException { | |
668 String userId = collection.getUser().identifier(); | |
669 prepareStatement(SQL_USER_BY_GID); | |
670 stmnt.setString(1, userId); | |
671 result = stmnt.executeQuery(); | |
672 int uId; | |
673 if (result.next()) { | |
674 uId = result.getInt(1); | |
675 reset(); | |
676 } | |
677 else { | |
678 // need to create user first | |
679 reset(); | |
680 prepareStatement(SQL_USER_ID_NEXTVAL); | |
681 result = stmnt.executeQuery(); | |
682 if (!result.next()) { | |
683 log.error("id generation for user failed"); | |
684 return false; | |
685 } | |
686 uId = result.getInt(1); | |
687 reset(); | |
688 prepareStatement(SQL_INSERT_USER); | |
689 stmnt.setInt(1, uId); | |
690 stmnt.setString(2, userId); | |
691 stmnt.execute(); | |
692 conn.commit(); | |
693 reset(); | |
694 } | |
695 | |
696 prepareStatement(SQL_COLLECTION_ID_NEXTVAL); | |
697 result = stmnt.executeQuery(); | |
698 if (!result.next()) { | |
699 log.error("id generation for collection failed"); | |
700 return false; | |
701 } | |
702 int cId = result.getInt(1); | |
703 reset(); | |
704 | |
705 String identifier = collection.identifier(); | |
706 String name = collection.getName(); | |
707 | |
708 prepareStatement(SQL_INSERT_COLLECTION); | |
709 stmnt.setInt(1, cId); | |
710 stmnt.setString(2, identifier); | |
711 stmnt.setInt(3, uId); | |
712 setString(stmnt, 4, name); | |
713 stmnt.setTimestamp(5, | |
714 new Timestamp(System.currentTimeMillis())); | |
715 stmnt.execute(); | |
716 | |
717 conn.commit(); | |
718 return true; | |
719 } | |
720 }; | |
721 | |
722 if (!exec.runWrite()) { | |
723 log.error("create collection failed"); | |
724 } | |
725 } | |
726 | |
727 public void deletedCollection( | |
728 final String identifier, | |
729 Backend backend, | |
730 GlobalContext context | |
731 ) { | |
732 log.debug("deletedCollection"); | |
733 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
734 @Override | |
735 public boolean doIt() throws SQLException { | |
736 prepareStatement(SQL_DELETE_COLLECTION_BY_GID); | |
737 stmnt.setString(1, identifier); | |
738 stmnt.execute(); | |
739 conn.commit(); | |
740 return true; | |
741 } | |
742 }; | |
743 | |
744 if (!exec.runWrite()) { | |
745 log.error("delete collection failed"); | |
746 } | |
747 } | |
748 | |
749 public void changedCollectionAttribute( | |
750 String identifier, | |
751 Document document, | |
752 Backend backend, | |
753 GlobalContext context | |
754 ) { | |
755 log.debug("changedCollectionAttribute"); | |
756 } | |
757 | |
758 public void changedCollectionItemAttribute( | |
759 String collectionId, | |
760 String artifactId, | |
761 Document document, | |
762 Backend backend, | |
763 GlobalContext context | |
764 ) { | |
765 log.debug("changedCollectionItemAttribute"); | |
766 } | |
767 | |
768 public void addedArtifactToCollection( | |
769 final String artifactId, | |
770 final String collectionId, | |
771 Backend backend, | |
772 GlobalContext context | |
773 ) { | |
774 log.debug("addedArtifactToCollection"); | |
775 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
776 @Override | |
777 public boolean doIt() throws SQLException { | |
778 prepareStatement(SQL_ARTIFACT_BY_GID); | |
779 stmnt.setString(1, artifactId); | |
780 result = stmnt.executeQuery(); | |
781 if (!result.next()) { | |
782 return false; | |
783 } | |
784 int aId = result.getInt(1); | |
785 reset(); | |
786 | |
787 prepareStatement(SQL_COLLECTION_BY_GID); | |
788 stmnt.setString(1, collectionId); | |
789 result = stmnt.executeQuery(); | |
790 if (!result.next()) { | |
791 return false; | |
792 } | |
793 int cId = result.getInt(1); | |
794 reset(); | |
795 | |
796 prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL); | |
797 result = stmnt.executeQuery(); | |
798 if (!result.next()) { | |
799 return false; | |
800 } | |
801 int ciId = result.getInt(1); | |
802 reset(); | |
803 | |
804 prepareStatement(SQL_INSERT_COLLECTION_ITEM); | |
805 stmnt.setInt(1, ciId); | |
806 stmnt.setInt(2, cId); | |
807 stmnt.setInt(3, aId); | |
808 stmnt.execute(); | |
809 | |
810 conn.commit(); | |
811 return true; | |
812 } | |
813 }; | |
814 if (!exec.runWrite()) { | |
815 log.error("added artifact to collection failed"); | |
816 } | |
817 } | |
818 | |
819 public void removedArtifactFromCollection( | |
820 final String artifactId, | |
821 final String collectionId, | |
822 Backend backend, | |
823 GlobalContext context | |
824 ) { | |
825 log.debug("removedArtifactFromCollection"); | |
826 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
827 @Override | |
828 public boolean doIt() throws SQLException { | |
829 prepareStatement(SQL_ARTIFACT_BY_GID); | |
830 stmnt.setString(1, artifactId); | |
831 result = stmnt.executeQuery(); | |
832 if (!result.next()) { | |
833 return false; | |
834 } | |
835 int aId = result.getInt(1); | |
836 reset(); | |
837 prepareStatement(SQL_COLLECTION_BY_GID); | |
838 stmnt.setString(1, collectionId); | |
839 result = stmnt.executeQuery(); | |
840 if (!result.next()) { | |
841 return false; | |
842 } | |
843 int cId = result.getInt(1); | |
844 reset(); | |
845 prepareStatement(SQL_DELETE_ARTIFACT_FROM_COLLECTION); | |
846 stmnt.setInt(1, cId); | |
847 stmnt.setInt(2, aId); | |
848 stmnt.execute(); | |
849 conn.commit(); | |
850 return true; | |
851 } | |
852 }; | |
853 if (!exec.runWrite()) { | |
854 log.error("removing artifact from collection failed"); | |
855 } | |
856 } | |
857 | |
858 public void setCollectionName( | |
859 final String collectionId, | |
860 final String name, | |
861 GlobalContext context | |
862 ) { | |
863 log.debug("setCollectionName"); | |
864 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
865 @Override | |
866 public boolean doIt() throws SQLException { | |
867 prepareStatement(SQL_UPDATE_COLLECTION_NAME); | |
868 stmnt.setString(1, name); | |
869 stmnt.setString(2, collectionId); | |
870 stmnt.execute(); | |
871 conn.commit(); | |
872 return true; | |
873 } | |
874 }; | |
875 if (!exec.runWrite()) { | |
876 log.error("changing name failed"); | |
877 } | |
878 } | |
879 | |
880 protected void storeData( | |
881 final int artifactId, | |
882 FLYSArtifact artifact | |
883 ) { | |
884 final Collection<StateData> data = artifact.getAllData(); | |
885 | |
886 if (data.isEmpty()) { | |
887 return; | |
888 } | |
889 | |
890 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
891 @Override | |
892 public boolean doIt() throws SQLException { | |
893 int [] ids = new int[data.size()]; | |
894 prepareStatement(SQL_ARTIFACT_DATA_ID_NEXTVAL); | |
895 | |
896 for (int i = 0; i < ids.length; ++i) { | |
897 result = stmnt.executeQuery(); | |
898 if (!result.next()) { | |
899 log.error("generating id for artifact data failed"); | |
900 return false; | |
901 } | |
902 ids[i] = result.getInt(1); | |
903 result.close(); result = null; | |
904 } | |
905 reset(); | |
906 prepareStatement(SQL_INSERT_ARTIFACT_DATA); | |
907 | |
908 int i = 0; | |
909 for (StateData sd: data) { | |
910 int id = ids[i++]; | |
911 stmnt.setInt(1, id); | |
912 stmnt.setInt(2, artifactId); | |
913 // XXX: Where come the nulls from? | |
914 String type = sd.getType(); | |
915 if (type == null) type = "String"; | |
916 stmnt.setString(3, type); | |
917 stmnt.setString(4, sd.getName()); | |
918 setString(stmnt, 5, sd.getValue()); | |
919 stmnt.execute(); | |
920 } | |
921 | |
922 conn.commit(); | |
923 return true; | |
924 } | |
925 }; | |
926 | |
927 if (!exec.runWrite()) { | |
928 log.error("storing artifact data failed"); | |
929 } | |
930 } | |
931 | |
932 protected void storeOuts( | |
933 final int artifactId, | |
934 final FLYSArtifact artifact, | |
935 GlobalContext context | |
936 ) { | |
937 final List<Output> outs = artifact.getOutputs(context); | |
938 | |
939 if (outs.isEmpty()) { | |
940 return; | |
941 } | |
942 | |
943 final int [] outIds = new int[outs.size()]; | |
944 | |
945 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
946 @Override | |
947 public boolean doIt() throws SQLException { | |
948 prepareStatement(SQL_OUT_ID_NEXTVALUE); | |
949 for (int i = 0; i < outIds.length; ++i) { | |
950 result = stmnt.executeQuery(); | |
951 if (!result.next()) { | |
952 log.error("generation of out ids failed"); | |
953 return false; | |
954 } | |
955 outIds[i] = result.getInt(1); | |
956 result.close(); result = null; | |
957 } | |
958 reset(); | |
959 prepareStatement(SQL_INSERT_OUT); | |
960 for (int i = 0; i < outIds.length; ++i) { | |
961 Output out = outs.get(i); | |
962 stmnt.setInt(1, outIds[i]); | |
963 stmnt.setInt(2, artifactId); | |
964 stmnt.setString(3, out.getName()); | |
965 setString(stmnt, 4, out.getDescription()); | |
966 setString(stmnt, 5, out.getType()); | |
967 stmnt.execute(); | |
968 } | |
969 conn.commit(); | |
970 return true; | |
971 } | |
972 }; | |
973 | |
974 if (!exec.runWrite()) { | |
975 log.error("storing artifact outs failed"); | |
976 return; | |
977 } | |
978 | |
979 final int FACETS = numFacets(outs); | |
980 | |
981 if (FACETS == 0) { | |
982 return; | |
983 } | |
984 | |
985 exec = sqlExecutor.new Instance() { | |
986 @Override | |
987 public boolean doIt() throws SQLException { | |
988 int [] facetIds = new int[FACETS]; | |
989 prepareStatement(SQL_FACET_ID_NEXTVAL); | |
990 for (int i = 0; i < facetIds.length; ++i) { | |
991 result = stmnt.executeQuery(); | |
992 if (!result.next()) { | |
993 log.error("generation of facet ids failed"); | |
994 return false; | |
995 } | |
996 facetIds[i] = result.getInt(1); | |
997 result.close(); result = null; | |
998 } | |
999 reset(); | |
1000 prepareStatement(SQL_INSERT_FACET); | |
1001 int index = 0; | |
1002 for (int i = 0, N = outs.size(); i < N; ++i) { | |
1003 Output out = outs.get(i); | |
1004 int outId = outIds[i]; | |
1005 for (Facet facet: out.getFacets()) { | |
1006 stmnt.setInt(1, facetIds[index]); | |
1007 stmnt.setInt(2, outId); | |
1008 stmnt.setString(3, facet.getName()); | |
1009 stmnt.setInt(4, facet.getIndex()); | |
1010 stmnt.setString(5, "XXX"); // TODO: handle states | |
1011 setString(stmnt, 6, facet.getDescription()); | |
1012 stmnt.execute(); | |
1013 ++index; | |
1014 } | |
1015 } | |
1016 conn.commit(); | |
1017 return true; | |
1018 } | |
1019 }; | |
1020 | |
1021 if (!exec.runWrite()) { | |
1022 log.error("storing facets failed"); | |
1023 } | |
1024 } | |
1025 | |
1026 public void killedCollections( | |
1027 final List<String> identifiers, | |
1028 GlobalContext context | |
1029 ) { | |
1030 log.debug("killedCollections"); | |
1031 | |
1032 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
1033 @Override | |
1034 public boolean doIt() throws SQLException { | |
1035 prepareStatement(SQL_DELETE_COLLECTION_BY_GID); | |
1036 for (String identifier: identifiers) { | |
1037 stmnt.setString(1, identifier); | |
1038 stmnt.execute(); | |
1039 } | |
1040 conn.commit(); | |
1041 return true; | |
1042 } | |
1043 }; | |
1044 | |
1045 if (!exec.runWrite()) { | |
1046 log.error("killing collections failed"); | |
1047 } | |
1048 } | |
1049 | |
1050 public void killedArtifacts( | |
1051 final List<String> identifiers, | |
1052 GlobalContext context | |
1053 ) { | |
1054 log.debug("killedArtifacts"); | |
1055 | |
1056 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
1057 @Override | |
1058 public boolean doIt() throws SQLException { | |
1059 prepareStatement(SQL_DELETE_ARTIFACT_BY_GID); | |
1060 for (String identifier: identifiers) { | |
1061 stmnt.setString(1, identifier); | |
1062 stmnt.execute(); | |
1063 } | |
1064 conn.commit(); | |
1065 return true; | |
1066 } | |
1067 }; | |
1068 | |
1069 if (!exec.runWrite()) { | |
1070 log.error("killing artifacts failed"); | |
1071 } | |
1072 } | |
1073 } | |
1074 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |