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