comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 1002:db86c665ab6e

Datacage: Pass global context to datacage if calls come from backend listener. flys-artifacts/trunk@2442 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 01 Aug 2011 16:34:48 +0000
parents b0218f21c664
children c58da6dd15ed
comparison
equal deleted inserted replaced
1001:a640944b5dd7 1002:db86c665ab6e
13 13
14 import de.intevation.artifactdatabase.db.SQL; 14 import de.intevation.artifactdatabase.db.SQL;
15 import de.intevation.artifactdatabase.db.SQLExecutor; 15 import de.intevation.artifactdatabase.db.SQLExecutor;
16 16
17 import de.intevation.artifactdatabase.LifetimeListener; 17 import de.intevation.artifactdatabase.LifetimeListener;
18 import de.intevation.artifactdatabase.BackendListener;
19 import de.intevation.artifactdatabase.Backend; 18 import de.intevation.artifactdatabase.Backend;
20 19
21 import de.intevation.artifactdatabase.data.StateData; 20 import de.intevation.artifactdatabase.data.StateData;
22 21
23 import de.intevation.artifactdatabase.state.Output; 22 import de.intevation.artifactdatabase.state.Output;
34 import org.apache.log4j.Logger; 33 import org.apache.log4j.Logger;
35 34
36 import org.w3c.dom.Document; 35 import org.w3c.dom.Document;
37 36
38 public class Datacage 37 public class Datacage
39 implements LifetimeListener, BackendListener 38 implements LifetimeListener
40 { 39 {
41 private static Logger log = Logger.getLogger(Datacage.class); 40 private static Logger log = Logger.getLogger(Datacage.class);
42 41
43 public static final String DATACAGE_KEY = 42 public static final String DATACAGE_KEY =
44 "global.datacage.instance"; 43 "global.datacage.instance";
189 188
190 storeCollectionItem(collectionId, aId); 189 storeCollectionItem(collectionId, aId);
191 190
192 storeData(aId, artifact); 191 storeData(aId, artifact);
193 192
194 storeOuts(aId, artifact); 193 storeOuts(aId, artifact, context);
195 } 194 }
196 195
197 protected void storeOuts(
198 final int artifactId,
199 final FLYSArtifact artifact
200 ) {
201 final List<Output> outs = artifact.getCurrentOutputs(context);
202
203 if (outs.isEmpty()) {
204 return;
205 }
206
207 final int [] outIds = new int[outs.size()];
208
209 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
210 @Override
211 public boolean doIt() throws SQLException {
212 prepareStatement(SQL_OUT_ID_NEXTVALUE);
213 for (int i = 0; i < outIds.length; ++i) {
214 result = stmnt.executeQuery();
215 if (!result.next()) {
216 log.error("generation of out ids failed");
217 return false;
218 }
219 outIds[i] = result.getInt(1);
220 result.close(); result = null;
221 }
222 reset();
223 prepareStatement(SQL_INSERT_OUT);
224 for (int i = 0; i < outIds.length; ++i) {
225 Output out = outs.get(i);
226 stmnt.setInt(1, outIds[i]);
227 stmnt.setInt(2, artifactId);
228 stmnt.setString(3, out.getName());
229 setString(stmnt, 4, out.getDescription());
230 setString(stmnt, 5, out.getType());
231 stmnt.execute();
232 }
233 conn.commit();
234 return true;
235 }
236 };
237
238 if (!exec.runWrite()) {
239 log.error("storing artifact outs failed");
240 return;
241 }
242
243 final int FACETS = numFacets(outs);
244
245 if (FACETS == 0) {
246 return;
247 }
248
249 exec = sqlExecutor.new Instance() {
250 @Override
251 public boolean doIt() throws SQLException {
252 int [] facetIds = new int[FACETS];
253 prepareStatement(SQL_FACET_ID_NEXTVAL);
254 for (int i = 0; i < facetIds.length; ++i) {
255 result = stmnt.executeQuery();
256 if (!result.next()) {
257 log.error("generation of facet ids failed");
258 return false;
259 }
260 facetIds[i] = result.getInt(1);
261 result.close(); result = null;
262 }
263 reset();
264 prepareStatement(SQL_INSERT_FACET);
265 int index = 0;
266 for (int i = 0, N = outs.size(); i < N; ++i) {
267 Output out = outs.get(i);
268 int outId = outIds[i];
269 for (Facet facet: out.getFacets()) {
270 stmnt.setInt(1, facetIds[index]);
271 stmnt.setInt(2, outId);
272 stmnt.setString(3, facet.getName());
273 stmnt.setInt(4, facet.getIndex());
274 stmnt.setString(5, "XXX"); // TODO: handle states
275 setString(stmnt, 6, facet.getDescription());
276 stmnt.execute();
277 ++index;
278 }
279 }
280 conn.commit();
281 return true;
282 }
283 };
284
285 if (!exec.runWrite()) {
286 log.error("storing facets failed");
287 }
288 }
289
290 protected void storeData(
291 final int artifactId,
292 FLYSArtifact artifact
293 ) {
294 final Collection<StateData> data = artifact.getAllData();
295
296 if (data.isEmpty()) {
297 return;
298 }
299
300 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
301 @Override
302 public boolean doIt() throws SQLException {
303 int [] ids = new int[data.size()];
304 prepareStatement(SQL_ARTIFACT_DATA_ID_NEXTVAL);
305
306 for (int i = 0; i < ids.length; ++i) {
307 result = stmnt.executeQuery();
308 if (!result.next()) {
309 log.error("generating id for artifact data failed");
310 return false;
311 }
312 ids[i] = result.getInt(1);
313 result.close(); result = null;
314 }
315 reset();
316 prepareStatement(SQL_INSERT_ARTIFACT_DATA);
317
318 int i = 0;
319 for (StateData sd: data) {
320 int id = ids[i++];
321 stmnt.setInt(1, id);
322 stmnt.setInt(2, artifactId);
323 // XXX: Where come the nulls from?
324 String type = sd.getType();
325 if (type == null) type = "String";
326 stmnt.setString(3, type);
327 stmnt.setString(4, sd.getName());
328 setString(stmnt, 5, sd.getValue());
329 stmnt.execute();
330 }
331
332 conn.commit();
333 return true;
334 }
335 };
336
337 if (!exec.runWrite()) {
338 log.error("storing artifact data failed");
339 }
340 }
341 196
342 protected void storeCollectionItem( 197 protected void storeCollectionItem(
343 final Integer collectionId, 198 final Integer collectionId,
344 final Integer artifactId 199 final Integer artifactId
345 ) { 200 ) {
578 @Override 433 @Override
579 public void systemDown(GlobalContext context) { 434 public void systemDown(GlobalContext context) {
580 log.debug("systemDown"); 435 log.debug("systemDown");
581 } 436 }
582 437
583 @Override
584 public void setup(GlobalContext globalContext) { 438 public void setup(GlobalContext globalContext) {
585 log.debug("setup"); 439 log.debug("setup");
586 } 440 }
587 441
588 @Override 442 public void createdArtifact(
589 public void createdArtifact(Artifact artifact, Backend backend) { 443 Artifact artifact,
444 Backend backend,
445 GlobalContext context
446 ) {
590 log.debug("createdArtifact"); 447 log.debug("createdArtifact");
591 } 448
592 449 if (!(artifact instanceof FLYSArtifact)) {
593 @Override 450 return;
594 public void storedArtifact(Artifact artifact, Backend backend) { 451 }
452
453 final FLYSArtifact flys = (FLYSArtifact)artifact;
454
455 final int [] res = new int[1];
456
457 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
458 @Override
459 public boolean doIt() throws SQLException {
460 prepareStatement(SQL_ARTIFACT_ID_NEXTVAL);
461 result = stmnt.executeQuery();
462 if (!result.next()) {
463 return false;
464 }
465 res[0] = result.getInt(1);
466 reset();
467 prepareStatement(SQL_INSERT_ARTIFACT);
468 stmnt.setInt (1, res[0]);
469 stmnt.setString(2, flys.identifier());
470 stmnt.setString(3, flys.getCurrentStateId());
471 stmnt.execute();
472 conn.commit();
473 return true;
474 }
475 };
476
477 if (!exec.runWrite()) {
478 log.error("storing of artifact failed.");
479 return;
480 }
481
482 storeData(res[0], flys);
483 storeOuts(res[0], flys, context);
484 }
485
486 public void storedArtifact(
487 Artifact artifact,
488 Backend backend,
489 GlobalContext context
490 ) {
595 log.debug("storedArtifact"); 491 log.debug("storedArtifact");
596 } 492 }
597 493
598 @Override 494 public void createdUser(
599 public void createdUser(User user, Backend backend) { 495 User user,
496 Backend backend,
497 GlobalContext context
498 ) {
600 log.debug("createdUser"); 499 log.debug("createdUser");
601 } 500 }
602 501
603 @Override 502 public void deletedUser(
604 public void deletedUser(String identifier, Backend backend) { 503 String identifier,
504 Backend backend,
505 GlobalContext context
506 ) {
605 log.debug("deletedUser"); 507 log.debug("deletedUser");
606 } 508 }
607 509
608 @Override
609 public void createdCollection( 510 public void createdCollection(
610 ArtifactCollection collection, 511 ArtifactCollection collection,
611 Backend backend 512 Backend backend,
513 GlobalContext context
612 ) { 514 ) {
613 log.debug("createdCollection"); 515 log.debug("createdCollection");
614 } 516 }
615 517
616 @Override 518 public void deletedCollection(
617 public void deletedCollection(String identifier, Backend backend) { 519 String identifier,
520 Backend backend,
521 GlobalContext context
522 ) {
618 log.debug("deletedCollection"); 523 log.debug("deletedCollection");
619 } 524 }
620 525
621 @Override
622 public void changedCollectionAttribute( 526 public void changedCollectionAttribute(
623 String identifier, 527 String identifier,
624 Document document, 528 Document document,
625 Backend backend 529 Backend backend,
530 GlobalContext context
626 ) { 531 ) {
627 log.debug("changedCollectionAttribute"); 532 log.debug("changedCollectionAttribute");
628 } 533 }
629 534
630 @Override
631 public void changedCollectionItemAttribute( 535 public void changedCollectionItemAttribute(
632 String collectionId, 536 String collectionId,
633 String artifactId, 537 String artifactId,
634 Document document, 538 Document document,
635 Backend backend 539 Backend backend,
540 GlobalContext context
636 ) { 541 ) {
637 log.debug("changedCollectionItemAttribute"); 542 log.debug("changedCollectionItemAttribute");
638 } 543 }
639 544
640 @Override
641 public void addedArtifactToCollection( 545 public void addedArtifactToCollection(
642 String artifactId, 546 String artifactId,
643 String collectionId, 547 String collectionId,
644 Backend backend 548 Backend backend,
549 GlobalContext context
645 ) { 550 ) {
646 log.debug("addedArtifactToCollection"); 551 log.debug("addedArtifactToCollection");
647 } 552 }
648 553
649 @Override
650 public void removedArtifactFromCollection( 554 public void removedArtifactFromCollection(
651 String artifactId, 555 String artifactId,
652 String collectionId, 556 String collectionId,
653 Backend backend 557 Backend backend,
558 GlobalContext context
654 ) { 559 ) {
655 log.debug("removedArtifactFromCollection"); 560 log.debug("removedArtifactFromCollection");
656 } 561 }
657 562
658 @Override
659 public void setCollectionName( 563 public void setCollectionName(
660 String collectionId, 564 String collectionId,
661 String name 565 String name,
566 GlobalContext context
662 ) { 567 ) {
663 log.debug("setCollectionName"); 568 log.debug("setCollectionName");
664 } 569 }
570
571 protected void storeData(
572 final int artifactId,
573 FLYSArtifact artifact
574 ) {
575 final Collection<StateData> data = artifact.getAllData();
576
577 if (data.isEmpty()) {
578 return;
579 }
580
581 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
582 @Override
583 public boolean doIt() throws SQLException {
584 int [] ids = new int[data.size()];
585 prepareStatement(SQL_ARTIFACT_DATA_ID_NEXTVAL);
586
587 for (int i = 0; i < ids.length; ++i) {
588 result = stmnt.executeQuery();
589 if (!result.next()) {
590 log.error("generating id for artifact data failed");
591 return false;
592 }
593 ids[i] = result.getInt(1);
594 result.close(); result = null;
595 }
596 reset();
597 prepareStatement(SQL_INSERT_ARTIFACT_DATA);
598
599 int i = 0;
600 for (StateData sd: data) {
601 int id = ids[i++];
602 stmnt.setInt(1, id);
603 stmnt.setInt(2, artifactId);
604 // XXX: Where come the nulls from?
605 String type = sd.getType();
606 if (type == null) type = "String";
607 stmnt.setString(3, type);
608 stmnt.setString(4, sd.getName());
609 setString(stmnt, 5, sd.getValue());
610 stmnt.execute();
611 }
612
613 conn.commit();
614 return true;
615 }
616 };
617
618 if (!exec.runWrite()) {
619 log.error("storing artifact data failed");
620 }
621 }
622
623 protected void storeOuts(
624 final int artifactId,
625 final FLYSArtifact artifact,
626 GlobalContext context
627 ) {
628 final List<Output> outs = artifact.getCurrentOutputs(context);
629
630 if (outs.isEmpty()) {
631 return;
632 }
633
634 final int [] outIds = new int[outs.size()];
635
636 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
637 @Override
638 public boolean doIt() throws SQLException {
639 prepareStatement(SQL_OUT_ID_NEXTVALUE);
640 for (int i = 0; i < outIds.length; ++i) {
641 result = stmnt.executeQuery();
642 if (!result.next()) {
643 log.error("generation of out ids failed");
644 return false;
645 }
646 outIds[i] = result.getInt(1);
647 result.close(); result = null;
648 }
649 reset();
650 prepareStatement(SQL_INSERT_OUT);
651 for (int i = 0; i < outIds.length; ++i) {
652 Output out = outs.get(i);
653 stmnt.setInt(1, outIds[i]);
654 stmnt.setInt(2, artifactId);
655 stmnt.setString(3, out.getName());
656 setString(stmnt, 4, out.getDescription());
657 setString(stmnt, 5, out.getType());
658 stmnt.execute();
659 }
660 conn.commit();
661 return true;
662 }
663 };
664
665 if (!exec.runWrite()) {
666 log.error("storing artifact outs failed");
667 return;
668 }
669
670 final int FACETS = numFacets(outs);
671
672 if (FACETS == 0) {
673 return;
674 }
675
676 exec = sqlExecutor.new Instance() {
677 @Override
678 public boolean doIt() throws SQLException {
679 int [] facetIds = new int[FACETS];
680 prepareStatement(SQL_FACET_ID_NEXTVAL);
681 for (int i = 0; i < facetIds.length; ++i) {
682 result = stmnt.executeQuery();
683 if (!result.next()) {
684 log.error("generation of facet ids failed");
685 return false;
686 }
687 facetIds[i] = result.getInt(1);
688 result.close(); result = null;
689 }
690 reset();
691 prepareStatement(SQL_INSERT_FACET);
692 int index = 0;
693 for (int i = 0, N = outs.size(); i < N; ++i) {
694 Output out = outs.get(i);
695 int outId = outIds[i];
696 for (Facet facet: out.getFacets()) {
697 stmnt.setInt(1, facetIds[index]);
698 stmnt.setInt(2, outId);
699 stmnt.setString(3, facet.getName());
700 stmnt.setInt(4, facet.getIndex());
701 stmnt.setString(5, "XXX"); // TODO: handle states
702 setString(stmnt, 6, facet.getDescription());
703 stmnt.execute();
704 ++index;
705 }
706 }
707 conn.commit();
708 return true;
709 }
710 };
711
712 if (!exec.runWrite()) {
713 log.error("storing facets failed");
714 }
715 }
716
665 } 717 }
666 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 718 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org