comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 1004:0e8c03b69627

Datacage: completed backend listener stuff. flys-artifacts/trunk@2444 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 01 Aug 2011 20:35:43 +0000
parents c58da6dd15ed
children 1917f32701dc
comparison
equal deleted inserted replaced
1003:c58da6dd15ed 1004:0e8c03b69627
66 private String SQL_FACET_ID_NEXTVAL = "facet.id.nextval"; 66 private String SQL_FACET_ID_NEXTVAL = "facet.id.nextval";
67 private String SQL_INSERT_FACET = "insert.facet"; 67 private String SQL_INSERT_FACET = "insert.facet";
68 private String SQL_UPDATE_COLLECTION_NAME = "update.collection.name"; 68 private String SQL_UPDATE_COLLECTION_NAME = "update.collection.name";
69 private String SQL_DELETE_ARTIFACT_FROM_COLLECTION = 69 private String SQL_DELETE_ARTIFACT_FROM_COLLECTION =
70 "delete.artifact.from.collection"; 70 "delete.artifact.from.collection";
71 private String SQL_DELETE_COLLECTION_BY_GID =
72 "delete.collection.by.gid";
73 private String SQL_DELETE_USER_BY_GID = "delete.user.by.gid";
74 private String SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID =
75 "delete.artifact.data.by.artifact.id";
76 private String SQL_DELETE_OUTS_BY_ARTIFACT_ID =
77 "delete.outs.by.artifact.id";
78 private String SQL_DELETE_FACETS_BY_ARTIFACT_ID =
79 "delete.facets.by.artifacts.id";
71 80
72 protected SQLExecutor sqlExecutor; 81 protected SQLExecutor sqlExecutor;
73 82
74 public class InitialScan 83 public class InitialScan
75 implements ArtifactDatabase.ArtifactLoadedCallback 84 implements ArtifactDatabase.ArtifactLoadedCallback
353 SQL_FACET_ID_NEXTVAL = sql.get(SQL_FACET_ID_NEXTVAL); 362 SQL_FACET_ID_NEXTVAL = sql.get(SQL_FACET_ID_NEXTVAL);
354 SQL_INSERT_FACET = sql.get(SQL_INSERT_FACET); 363 SQL_INSERT_FACET = sql.get(SQL_INSERT_FACET);
355 SQL_UPDATE_COLLECTION_NAME = sql.get(SQL_UPDATE_COLLECTION_NAME); 364 SQL_UPDATE_COLLECTION_NAME = sql.get(SQL_UPDATE_COLLECTION_NAME);
356 SQL_DELETE_ARTIFACT_FROM_COLLECTION = 365 SQL_DELETE_ARTIFACT_FROM_COLLECTION =
357 sql.get(SQL_DELETE_ARTIFACT_FROM_COLLECTION); 366 sql.get(SQL_DELETE_ARTIFACT_FROM_COLLECTION);
367 SQL_DELETE_COLLECTION_BY_GID = sql.get(SQL_DELETE_COLLECTION_BY_GID);
368 SQL_DELETE_USER_BY_GID = sql.get(SQL_DELETE_USER_BY_GID);
369 SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID =
370 sql.get(SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID);
371 SQL_DELETE_OUTS_BY_ARTIFACT_ID =
372 sql.get(SQL_DELETE_OUTS_BY_ARTIFACT_ID);
373 SQL_DELETE_FACETS_BY_ARTIFACT_ID =
374 sql.get(SQL_DELETE_FACETS_BY_ARTIFACT_ID);
358 } 375 }
359 376
360 protected static final int numFacets(List<Output> outs) { 377 protected static final int numFacets(List<Output> outs) {
361 int sum = 0; 378 int sum = 0;
362 for (Output out: outs) { 379 for (Output out: outs) {
451 GlobalContext context 468 GlobalContext context
452 ) { 469 ) {
453 log.debug("createdArtifact"); 470 log.debug("createdArtifact");
454 471
455 if (!(artifact instanceof FLYSArtifact)) { 472 if (!(artifact instanceof FLYSArtifact)) {
473 log.warn("need FLYSArtifact here");
456 return; 474 return;
457 } 475 }
458 476
459 final FLYSArtifact flys = (FLYSArtifact)artifact; 477 final FLYSArtifact flys = (FLYSArtifact)artifact;
460 478
464 @Override 482 @Override
465 public boolean doIt() throws SQLException { 483 public boolean doIt() throws SQLException {
466 prepareStatement(SQL_ARTIFACT_ID_NEXTVAL); 484 prepareStatement(SQL_ARTIFACT_ID_NEXTVAL);
467 result = stmnt.executeQuery(); 485 result = stmnt.executeQuery();
468 if (!result.next()) { 486 if (!result.next()) {
487 log.error("id generation for artifact failed");
469 return false; 488 return false;
470 } 489 }
471 res[0] = result.getInt(1); 490 res[0] = result.getInt(1);
472 reset(); 491 reset();
473 prepareStatement(SQL_INSERT_ARTIFACT); 492 prepareStatement(SQL_INSERT_ARTIFACT);
493 Artifact artifact, 512 Artifact artifact,
494 Backend backend, 513 Backend backend,
495 GlobalContext context 514 GlobalContext context
496 ) { 515 ) {
497 log.debug("storedArtifact"); 516 log.debug("storedArtifact");
517 if (!(artifact instanceof FLYSArtifact)) {
518 log.warn("need FLYSArtifact here");
519 return;
520 }
521
522 final FLYSArtifact flys = (FLYSArtifact)artifact;
523
524 final Integer [] res = new Integer[1];
525
526 // check first if artifact already exists
527 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
528 @Override
529 public boolean doIt() throws SQLException {
530 prepareStatement(SQL_ARTIFACT_BY_GID);
531 result = stmnt.executeQuery();
532 if (!result.next()) {
533 // new artifact
534 return true;
535 }
536 res[0] = result.getInt(1);
537 return true;
538 }
539 };
540
541 if (!exec.runRead()) {
542 log.error("querying artifact failed");
543 return;
544 }
545
546 if (res[0] == null) { // new artifact
547 createdArtifact(artifact, backend, context);
548 return;
549 }
550
551 // artifact already exists -> delete old data
552 exec = sqlExecutor.new Instance() {
553 @Override
554 public boolean doIt() throws SQLException {
555 prepareStatement(SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID);
556 stmnt.setInt(1, res[0]);
557 stmnt.execute();
558 prepareStatement(SQL_DELETE_FACETS_BY_ARTIFACT_ID);
559 stmnt.setInt(1, res[0]);
560 stmnt.execute();
561 prepareStatement(SQL_DELETE_OUTS_BY_ARTIFACT_ID);
562 stmnt.setInt(1, res[0]);
563 stmnt.execute();
564 conn.commit();
565 return true;
566 }
567 };
568
569 if (!exec.runWrite()) {
570 log.error("deleting old artifact data failed");
571 return;
572 }
573
574 // write new data
575 storeData(res[0], flys);
576 storeOuts(res[0], flys, context);
498 } 577 }
499 578
500 public void createdUser( 579 public void createdUser(
501 User user, 580 final User user,
502 Backend backend, 581 Backend backend,
503 GlobalContext context 582 GlobalContext context
504 ) { 583 ) {
505 log.debug("createdUser"); 584 log.debug("createdUser");
585 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
586 @Override
587 public boolean doIt() throws SQLException {
588 prepareStatement(SQL_USER_ID_NEXTVAL);
589 result = stmnt.executeQuery();
590 if (!result.next()) {
591 log.error("id generation for user failed");
592 return false;
593 }
594 int uId = result.getInt(1);
595 reset();
596 prepareStatement(SQL_INSERT_USER);
597 stmnt.setInt(1, uId);
598 stmnt.setString(2, user.identifier());
599 stmnt.execute();
600 conn.commit();
601 return true;
602 }
603 };
604
605 if (!exec.runWrite()) {
606 log.error("create user failed");
607 }
506 } 608 }
507 609
508 public void deletedUser( 610 public void deletedUser(
509 String identifier, 611 final String identifier,
510 Backend backend, 612 Backend backend,
511 GlobalContext context 613 GlobalContext context
512 ) { 614 ) {
513 log.debug("deletedUser"); 615 log.debug("deletedUser");
616 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
617 @Override
618 public boolean doIt() throws SQLException {
619 prepareStatement(SQL_DELETE_USER_BY_GID);
620 stmnt.setString(1, identifier);
621 stmnt.execute();
622 conn.commit();
623 return true;
624 }
625 };
626
627 if (!exec.runWrite()) {
628 log.error("delete user failed");
629 }
514 } 630 }
515 631
516 public void createdCollection( 632 public void createdCollection(
517 ArtifactCollection collection, 633 final ArtifactCollection collection,
518 Backend backend, 634 Backend backend,
519 GlobalContext context 635 GlobalContext context
520 ) { 636 ) {
521 log.debug("createdCollection"); 637 log.debug("createdCollection");
638 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
639 @Override
640 public boolean doIt() throws SQLException {
641 String userId = collection.getUser().identifier();
642 prepareStatement(SQL_USER_BY_GID);
643 stmnt.setString(1, userId);
644 result = stmnt.executeQuery();
645 int uId;
646 if (result.next()) {
647 uId = result.getInt(1);
648 reset();
649 }
650 else {
651 // need to create user first
652 reset();
653 prepareStatement(SQL_USER_ID_NEXTVAL);
654 result = stmnt.executeQuery();
655 if (!result.next()) {
656 log.error("id generation for user failed");
657 return false;
658 }
659 uId = result.getInt(1);
660 reset();
661 prepareStatement(SQL_INSERT_USER);
662 stmnt.setInt(1, uId);
663 stmnt.setString(2, userId);
664 stmnt.execute();
665 conn.commit();
666 reset();
667 }
668
669 prepareStatement(SQL_COLLECTION_ID_NEXTVAL);
670 result = stmnt.executeQuery();
671 if (!result.next()) {
672 log.error("id generation for collection failed");
673 return false;
674 }
675 int cId = result.getInt(1);
676 reset();
677
678 String identifier = collection.identifier();
679 String name = collection.getName();
680
681 prepareStatement(SQL_INSERT_COLLECTION);
682 stmnt.setInt(1, cId);
683 stmnt.setString(2, identifier);
684 stmnt.setInt(3, uId);
685 setString(stmnt, 4, name);
686 stmnt.execute();
687
688 conn.commit();
689 return true;
690 }
691 };
692
693 if (!exec.runWrite()) {
694 log.error("create collection failed");
695 }
522 } 696 }
523 697
524 public void deletedCollection( 698 public void deletedCollection(
525 String identifier, 699 final String identifier,
526 Backend backend, 700 Backend backend,
527 GlobalContext context 701 GlobalContext context
528 ) { 702 ) {
529 log.debug("deletedCollection"); 703 log.debug("deletedCollection");
704 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
705 @Override
706 public boolean doIt() throws SQLException {
707 prepareStatement(SQL_DELETE_COLLECTION_BY_GID);
708 stmnt.setString(1, identifier);
709 stmnt.execute();
710 conn.commit();
711 return true;
712 }
713 };
714
715 if (!exec.runWrite()) {
716 log.error("delete collection failed");
717 }
530 } 718 }
531 719
532 public void changedCollectionAttribute( 720 public void changedCollectionAttribute(
533 String identifier, 721 String identifier,
534 Document document, 722 Document document,
547 ) { 735 ) {
548 log.debug("changedCollectionItemAttribute"); 736 log.debug("changedCollectionItemAttribute");
549 } 737 }
550 738
551 public void addedArtifactToCollection( 739 public void addedArtifactToCollection(
552 String artifactId, 740 final String artifactId,
553 String collectionId, 741 final String collectionId,
554 Backend backend, 742 Backend backend,
555 GlobalContext context 743 GlobalContext context
556 ) { 744 ) {
557 log.debug("addedArtifactToCollection"); 745 log.debug("addedArtifactToCollection");
746 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
747 @Override
748 public boolean doIt() throws SQLException {
749 prepareStatement(SQL_ARTIFACT_BY_GID);
750 stmnt.setString(1, artifactId);
751 result = stmnt.executeQuery();
752 if (!result.next()) {
753 return false;
754 }
755 int aId = result.getInt(1);
756 reset();
757
758 prepareStatement(SQL_COLLECTION_BY_GID);
759 stmnt.setString(1, collectionId);
760 result = stmnt.executeQuery();
761 if (!result.next()) {
762 return false;
763 }
764 int cId = result.getInt(1);
765 reset();
766
767 prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL);
768 result = stmnt.executeQuery();
769 if (!result.next()) {
770 return false;
771 }
772 int ciId = result.getInt(1);
773 reset();
774
775 prepareStatement(SQL_INSERT_COLLECTION_ITEM);
776 stmnt.setInt(1, ciId);
777 stmnt.setInt(2, cId);
778 stmnt.setInt(3, aId);
779 stmnt.execute();
780
781 conn.commit();
782 return true;
783 }
784 };
785 if (!exec.runWrite()) {
786 log.error("added artifact to collection failed");
787 }
558 } 788 }
559 789
560 public void removedArtifactFromCollection( 790 public void removedArtifactFromCollection(
561 final String artifactId, 791 final String artifactId,
562 final String collectionId, 792 final String collectionId,

http://dive4elements.wald.intevation.org