Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 1190:f514894ec2fd
merged flys-artifacts/2.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:17 +0200 |
parents | eccf966fb677 |
children | 238145ef67da |
comparison
equal
deleted
inserted
replaced
917:b48c36076e17 | 1190:f514894ec2fd |
---|---|
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 instanceof FLYSArtifact)) { | |
492 log.warn("need FLYSArtifact here"); | |
493 return; | |
494 } | |
495 | |
496 final FLYSArtifact flys = (FLYSArtifact)artifact; | |
497 | |
498 final int [] res = new int[1]; | |
499 | |
500 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
501 @Override | |
502 public boolean doIt() throws SQLException { | |
503 prepareStatement(SQL_ARTIFACT_ID_NEXTVAL); | |
504 result = stmnt.executeQuery(); | |
505 if (!result.next()) { | |
506 log.error("id generation for artifact failed"); | |
507 return false; | |
508 } | |
509 res[0] = result.getInt(1); | |
510 reset(); | |
511 prepareStatement(SQL_INSERT_ARTIFACT); | |
512 stmnt.setInt (1, res[0]); | |
513 stmnt.setString (2, flys.identifier()); | |
514 stmnt.setString (3, flys.getCurrentStateId()); | |
515 stmnt.setTimestamp(4, | |
516 new Timestamp(System.currentTimeMillis())); | |
517 stmnt.execute(); | |
518 conn.commit(); | |
519 return true; | |
520 } | |
521 }; | |
522 | |
523 if (!exec.runWrite()) { | |
524 log.error("storing of artifact failed."); | |
525 return; | |
526 } | |
527 | |
528 storeData(res[0], flys); | |
529 storeOuts(res[0], flys, context); | |
530 } | |
531 | |
532 public void storedArtifact( | |
533 Artifact artifact, | |
534 Backend backend, | |
535 GlobalContext context | |
536 ) { | |
537 log.debug("storedArtifact"); | |
538 if (!(artifact instanceof FLYSArtifact)) { | |
539 log.warn("need FLYSArtifact here"); | |
540 return; | |
541 } | |
542 | |
543 final FLYSArtifact flys = (FLYSArtifact)artifact; | |
544 | |
545 final Integer [] res = new Integer[1]; | |
546 | |
547 // check first if artifact already exists | |
548 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
549 @Override | |
550 public boolean doIt() throws SQLException { | |
551 prepareStatement(SQL_ARTIFACT_BY_GID); | |
552 stmnt.setString(1, flys.identifier()); | |
553 result = stmnt.executeQuery(); | |
554 if (!result.next()) { | |
555 // new artifact | |
556 return true; | |
557 } | |
558 res[0] = result.getInt(1); | |
559 return true; | |
560 } | |
561 }; | |
562 | |
563 if (!exec.runRead()) { | |
564 log.error("querying artifact failed"); | |
565 return; | |
566 } | |
567 | |
568 if (res[0] == null) { // new artifact | |
569 createdArtifact(artifact, backend, context); | |
570 return; | |
571 } | |
572 | |
573 // artifact already exists -> delete old data | |
574 exec = sqlExecutor.new Instance() { | |
575 @Override | |
576 public boolean doIt() throws SQLException { | |
577 prepareStatement(SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID); | |
578 stmnt.setInt(1, res[0]); | |
579 stmnt.execute(); | |
580 prepareStatement(SQL_DELETE_FACETS_BY_ARTIFACT_ID); | |
581 stmnt.setInt(1, res[0]); | |
582 stmnt.execute(); | |
583 prepareStatement(SQL_DELETE_OUTS_BY_ARTIFACT_ID); | |
584 stmnt.setInt(1, res[0]); | |
585 stmnt.execute(); | |
586 conn.commit(); | |
587 return true; | |
588 } | |
589 }; | |
590 | |
591 if (!exec.runWrite()) { | |
592 log.error("deleting old artifact data failed"); | |
593 return; | |
594 } | |
595 | |
596 // write new data | |
597 storeData(res[0], flys); | |
598 storeOuts(res[0], flys, context); | |
599 } | |
600 | |
601 public void createdUser( | |
602 final User user, | |
603 Backend backend, | |
604 GlobalContext context | |
605 ) { | |
606 log.debug("createdUser"); | |
607 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
608 @Override | |
609 public boolean doIt() throws SQLException { | |
610 prepareStatement(SQL_USER_ID_NEXTVAL); | |
611 result = stmnt.executeQuery(); | |
612 if (!result.next()) { | |
613 log.error("id generation for user failed"); | |
614 return false; | |
615 } | |
616 int uId = result.getInt(1); | |
617 reset(); | |
618 prepareStatement(SQL_INSERT_USER); | |
619 stmnt.setInt(1, uId); | |
620 stmnt.setString(2, user.identifier()); | |
621 stmnt.execute(); | |
622 conn.commit(); | |
623 return true; | |
624 } | |
625 }; | |
626 | |
627 if (!exec.runWrite()) { | |
628 log.error("create user failed"); | |
629 } | |
630 } | |
631 | |
632 public void deletedUser( | |
633 final String identifier, | |
634 Backend backend, | |
635 GlobalContext context | |
636 ) { | |
637 log.debug("deletedUser"); | |
638 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
639 @Override | |
640 public boolean doIt() throws SQLException { | |
641 prepareStatement(SQL_DELETE_USER_BY_GID); | |
642 stmnt.setString(1, identifier); | |
643 stmnt.execute(); | |
644 conn.commit(); | |
645 return true; | |
646 } | |
647 }; | |
648 | |
649 if (!exec.runWrite()) { | |
650 log.error("delete user failed"); | |
651 } | |
652 } | |
653 | |
654 public void createdCollection( | |
655 final ArtifactCollection collection, | |
656 Backend backend, | |
657 GlobalContext context | |
658 ) { | |
659 log.debug("createdCollection"); | |
660 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
661 @Override | |
662 public boolean doIt() throws SQLException { | |
663 String userId = collection.getUser().identifier(); | |
664 prepareStatement(SQL_USER_BY_GID); | |
665 stmnt.setString(1, userId); | |
666 result = stmnt.executeQuery(); | |
667 int uId; | |
668 if (result.next()) { | |
669 uId = result.getInt(1); | |
670 reset(); | |
671 } | |
672 else { | |
673 // need to create user first | |
674 reset(); | |
675 prepareStatement(SQL_USER_ID_NEXTVAL); | |
676 result = stmnt.executeQuery(); | |
677 if (!result.next()) { | |
678 log.error("id generation for user failed"); | |
679 return false; | |
680 } | |
681 uId = result.getInt(1); | |
682 reset(); | |
683 prepareStatement(SQL_INSERT_USER); | |
684 stmnt.setInt(1, uId); | |
685 stmnt.setString(2, userId); | |
686 stmnt.execute(); | |
687 conn.commit(); | |
688 reset(); | |
689 } | |
690 | |
691 prepareStatement(SQL_COLLECTION_ID_NEXTVAL); | |
692 result = stmnt.executeQuery(); | |
693 if (!result.next()) { | |
694 log.error("id generation for collection failed"); | |
695 return false; | |
696 } | |
697 int cId = result.getInt(1); | |
698 reset(); | |
699 | |
700 String identifier = collection.identifier(); | |
701 String name = collection.getName(); | |
702 | |
703 prepareStatement(SQL_INSERT_COLLECTION); | |
704 stmnt.setInt(1, cId); | |
705 stmnt.setString(2, identifier); | |
706 stmnt.setInt(3, uId); | |
707 setString(stmnt, 4, name); | |
708 stmnt.setTimestamp(5, | |
709 new Timestamp(System.currentTimeMillis())); | |
710 stmnt.execute(); | |
711 | |
712 conn.commit(); | |
713 return true; | |
714 } | |
715 }; | |
716 | |
717 if (!exec.runWrite()) { | |
718 log.error("create collection failed"); | |
719 } | |
720 } | |
721 | |
722 public void deletedCollection( | |
723 final String identifier, | |
724 Backend backend, | |
725 GlobalContext context | |
726 ) { | |
727 log.debug("deletedCollection"); | |
728 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
729 @Override | |
730 public boolean doIt() throws SQLException { | |
731 prepareStatement(SQL_DELETE_COLLECTION_BY_GID); | |
732 stmnt.setString(1, identifier); | |
733 stmnt.execute(); | |
734 conn.commit(); | |
735 return true; | |
736 } | |
737 }; | |
738 | |
739 if (!exec.runWrite()) { | |
740 log.error("delete collection failed"); | |
741 } | |
742 } | |
743 | |
744 public void changedCollectionAttribute( | |
745 String identifier, | |
746 Document document, | |
747 Backend backend, | |
748 GlobalContext context | |
749 ) { | |
750 log.debug("changedCollectionAttribute"); | |
751 } | |
752 | |
753 public void changedCollectionItemAttribute( | |
754 String collectionId, | |
755 String artifactId, | |
756 Document document, | |
757 Backend backend, | |
758 GlobalContext context | |
759 ) { | |
760 log.debug("changedCollectionItemAttribute"); | |
761 } | |
762 | |
763 public void addedArtifactToCollection( | |
764 final String artifactId, | |
765 final String collectionId, | |
766 Backend backend, | |
767 GlobalContext context | |
768 ) { | |
769 log.debug("addedArtifactToCollection"); | |
770 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
771 @Override | |
772 public boolean doIt() throws SQLException { | |
773 prepareStatement(SQL_ARTIFACT_BY_GID); | |
774 stmnt.setString(1, artifactId); | |
775 result = stmnt.executeQuery(); | |
776 if (!result.next()) { | |
777 return false; | |
778 } | |
779 int aId = result.getInt(1); | |
780 reset(); | |
781 | |
782 prepareStatement(SQL_COLLECTION_BY_GID); | |
783 stmnt.setString(1, collectionId); | |
784 result = stmnt.executeQuery(); | |
785 if (!result.next()) { | |
786 return false; | |
787 } | |
788 int cId = result.getInt(1); | |
789 reset(); | |
790 | |
791 prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL); | |
792 result = stmnt.executeQuery(); | |
793 if (!result.next()) { | |
794 return false; | |
795 } | |
796 int ciId = result.getInt(1); | |
797 reset(); | |
798 | |
799 prepareStatement(SQL_INSERT_COLLECTION_ITEM); | |
800 stmnt.setInt(1, ciId); | |
801 stmnt.setInt(2, cId); | |
802 stmnt.setInt(3, aId); | |
803 stmnt.execute(); | |
804 | |
805 conn.commit(); | |
806 return true; | |
807 } | |
808 }; | |
809 if (!exec.runWrite()) { | |
810 log.error("added artifact to collection failed"); | |
811 } | |
812 } | |
813 | |
814 public void removedArtifactFromCollection( | |
815 final String artifactId, | |
816 final String collectionId, | |
817 Backend backend, | |
818 GlobalContext context | |
819 ) { | |
820 log.debug("removedArtifactFromCollection"); | |
821 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
822 @Override | |
823 public boolean doIt() throws SQLException { | |
824 prepareStatement(SQL_ARTIFACT_BY_GID); | |
825 stmnt.setString(1, artifactId); | |
826 result = stmnt.executeQuery(); | |
827 if (!result.next()) { | |
828 return false; | |
829 } | |
830 int aId = result.getInt(1); | |
831 reset(); | |
832 prepareStatement(SQL_COLLECTION_BY_GID); | |
833 stmnt.setString(1, collectionId); | |
834 result = stmnt.executeQuery(); | |
835 if (!result.next()) { | |
836 return false; | |
837 } | |
838 int cId = result.getInt(1); | |
839 reset(); | |
840 prepareStatement(SQL_DELETE_ARTIFACT_FROM_COLLECTION); | |
841 stmnt.setInt(1, cId); | |
842 stmnt.setInt(2, aId); | |
843 stmnt.execute(); | |
844 conn.commit(); | |
845 return true; | |
846 } | |
847 }; | |
848 if (!exec.runWrite()) { | |
849 log.error("removing artifact from collection failed"); | |
850 } | |
851 } | |
852 | |
853 public void setCollectionName( | |
854 final String collectionId, | |
855 final String name, | |
856 GlobalContext context | |
857 ) { | |
858 log.debug("setCollectionName"); | |
859 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
860 @Override | |
861 public boolean doIt() throws SQLException { | |
862 prepareStatement(SQL_UPDATE_COLLECTION_NAME); | |
863 stmnt.setString(1, name); | |
864 stmnt.setString(2, collectionId); | |
865 stmnt.execute(); | |
866 conn.commit(); | |
867 return true; | |
868 } | |
869 }; | |
870 if (!exec.runWrite()) { | |
871 log.error("changing name failed"); | |
872 } | |
873 } | |
874 | |
875 protected void storeData( | |
876 final int artifactId, | |
877 FLYSArtifact artifact | |
878 ) { | |
879 final Collection<StateData> data = artifact.getAllData(); | |
880 | |
881 if (data.isEmpty()) { | |
882 return; | |
883 } | |
884 | |
885 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
886 @Override | |
887 public boolean doIt() throws SQLException { | |
888 int [] ids = new int[data.size()]; | |
889 prepareStatement(SQL_ARTIFACT_DATA_ID_NEXTVAL); | |
890 | |
891 for (int i = 0; i < ids.length; ++i) { | |
892 result = stmnt.executeQuery(); | |
893 if (!result.next()) { | |
894 log.error("generating id for artifact data failed"); | |
895 return false; | |
896 } | |
897 ids[i] = result.getInt(1); | |
898 result.close(); result = null; | |
899 } | |
900 reset(); | |
901 prepareStatement(SQL_INSERT_ARTIFACT_DATA); | |
902 | |
903 int i = 0; | |
904 for (StateData sd: data) { | |
905 int id = ids[i++]; | |
906 stmnt.setInt(1, id); | |
907 stmnt.setInt(2, artifactId); | |
908 // XXX: Where come the nulls from? | |
909 String type = sd.getType(); | |
910 if (type == null) type = "String"; | |
911 stmnt.setString(3, type); | |
912 stmnt.setString(4, sd.getName()); | |
913 setString(stmnt, 5, sd.getValue()); | |
914 stmnt.execute(); | |
915 } | |
916 | |
917 conn.commit(); | |
918 return true; | |
919 } | |
920 }; | |
921 | |
922 if (!exec.runWrite()) { | |
923 log.error("storing artifact data failed"); | |
924 } | |
925 } | |
926 | |
927 protected void storeOuts( | |
928 final int artifactId, | |
929 final FLYSArtifact artifact, | |
930 GlobalContext context | |
931 ) { | |
932 final List<Output> outs = artifact.getOutputs(context); | |
933 | |
934 if (outs.isEmpty()) { | |
935 return; | |
936 } | |
937 | |
938 final int [] outIds = new int[outs.size()]; | |
939 | |
940 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
941 @Override | |
942 public boolean doIt() throws SQLException { | |
943 prepareStatement(SQL_OUT_ID_NEXTVALUE); | |
944 for (int i = 0; i < outIds.length; ++i) { | |
945 result = stmnt.executeQuery(); | |
946 if (!result.next()) { | |
947 log.error("generation of out ids failed"); | |
948 return false; | |
949 } | |
950 outIds[i] = result.getInt(1); | |
951 result.close(); result = null; | |
952 } | |
953 reset(); | |
954 prepareStatement(SQL_INSERT_OUT); | |
955 for (int i = 0; i < outIds.length; ++i) { | |
956 Output out = outs.get(i); | |
957 stmnt.setInt(1, outIds[i]); | |
958 stmnt.setInt(2, artifactId); | |
959 stmnt.setString(3, out.getName()); | |
960 setString(stmnt, 4, out.getDescription()); | |
961 setString(stmnt, 5, out.getType()); | |
962 stmnt.execute(); | |
963 } | |
964 conn.commit(); | |
965 return true; | |
966 } | |
967 }; | |
968 | |
969 if (!exec.runWrite()) { | |
970 log.error("storing artifact outs failed"); | |
971 return; | |
972 } | |
973 | |
974 final int FACETS = numFacets(outs); | |
975 | |
976 if (FACETS == 0) { | |
977 return; | |
978 } | |
979 | |
980 exec = sqlExecutor.new Instance() { | |
981 @Override | |
982 public boolean doIt() throws SQLException { | |
983 int [] facetIds = new int[FACETS]; | |
984 prepareStatement(SQL_FACET_ID_NEXTVAL); | |
985 for (int i = 0; i < facetIds.length; ++i) { | |
986 result = stmnt.executeQuery(); | |
987 if (!result.next()) { | |
988 log.error("generation of facet ids failed"); | |
989 return false; | |
990 } | |
991 facetIds[i] = result.getInt(1); | |
992 result.close(); result = null; | |
993 } | |
994 reset(); | |
995 prepareStatement(SQL_INSERT_FACET); | |
996 int index = 0; | |
997 for (int i = 0, N = outs.size(); i < N; ++i) { | |
998 Output out = outs.get(i); | |
999 int outId = outIds[i]; | |
1000 for (Facet facet: out.getFacets()) { | |
1001 stmnt.setInt(1, facetIds[index]); | |
1002 stmnt.setInt(2, outId); | |
1003 stmnt.setString(3, facet.getName()); | |
1004 stmnt.setInt(4, facet.getIndex()); | |
1005 stmnt.setString(5, "XXX"); // TODO: handle states | |
1006 setString(stmnt, 6, facet.getDescription()); | |
1007 stmnt.execute(); | |
1008 ++index; | |
1009 } | |
1010 } | |
1011 conn.commit(); | |
1012 return true; | |
1013 } | |
1014 }; | |
1015 | |
1016 if (!exec.runWrite()) { | |
1017 log.error("storing facets failed"); | |
1018 } | |
1019 } | |
1020 | |
1021 public void killedCollections( | |
1022 final List<String> identifiers, | |
1023 GlobalContext context | |
1024 ) { | |
1025 log.debug("killedCollections"); | |
1026 | |
1027 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
1028 @Override | |
1029 public boolean doIt() throws SQLException { | |
1030 prepareStatement(SQL_DELETE_COLLECTION_BY_GID); | |
1031 for (String identifier: identifiers) { | |
1032 stmnt.setString(1, identifier); | |
1033 stmnt.execute(); | |
1034 } | |
1035 conn.commit(); | |
1036 return true; | |
1037 } | |
1038 }; | |
1039 | |
1040 if (!exec.runWrite()) { | |
1041 log.error("killing collections failed"); | |
1042 } | |
1043 } | |
1044 | |
1045 public void killedArtifacts( | |
1046 final List<String> identifiers, | |
1047 GlobalContext context | |
1048 ) { | |
1049 log.debug("killedArtifacts"); | |
1050 | |
1051 SQLExecutor.Instance exec = sqlExecutor.new Instance() { | |
1052 @Override | |
1053 public boolean doIt() throws SQLException { | |
1054 prepareStatement(SQL_DELETE_ARTIFACT_BY_GID); | |
1055 for (String identifier: identifiers) { | |
1056 stmnt.setString(1, identifier); | |
1057 stmnt.execute(); | |
1058 } | |
1059 conn.commit(); | |
1060 return true; | |
1061 } | |
1062 }; | |
1063 | |
1064 if (!exec.runWrite()) { | |
1065 log.error("killing artifacts failed"); | |
1066 } | |
1067 } | |
1068 } | |
1069 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |