comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 311:1d517e051e95

Made backend listeners loadable at boot time. artifacts/trunk@2436 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 01 Aug 2011 14:17:09 +0000
parents 63122b9dee1d
children fa056f9c8a0c
comparison
equal deleted inserted replaced
310:63122b9dee1d 311:1d517e051e95
356 356
357 public void addListener(BackendListener listener) { 357 public void addListener(BackendListener listener) {
358 listeners.add(listener); 358 listeners.add(listener);
359 } 359 }
360 360
361 public void addAllListeners(List<BackendListener> others) {
362 listeners.addAll(others);
363 }
364
361 /** 365 /**
362 * Sets the factory lookup mechanism to decouple ArtifactDatabase 366 * Sets the factory lookup mechanism to decouple ArtifactDatabase
363 * and Backend. 367 * and Backend.
364 * @param factoryLookup 368 * @param factoryLookup
365 */ 369 */
726 730
727 if (!exec.runWrite()) { 731 if (!exec.runWrite()) {
728 throw new RuntimeException("failed insert artifact into database"); 732 throw new RuntimeException("failed insert artifact into database");
729 } 733 }
730 734
731 fireArtifactStored(artifact); 735 fireCreatedArtifact(artifact);
732 736
733 return id[0]; 737 return id[0];
734 } 738 }
735 739
736 protected void fireArtifactStored(Artifact artifact) { 740 protected void fireCreatedArtifact(Artifact artifact) {
737 for (BackendListener listener: listeners) { 741 for (BackendListener listener: listeners) {
738 listener.artifactStored(artifact, this); 742 listener.createdArtifact(artifact, this);
739 } 743 }
740 } 744 }
741 745
742 /** 746 /**
743 * Touches the access timestamp of a given artifact to prevent 747 * Touches the access timestamp of a given artifact to prevent
777 return true; 781 return true;
778 } 782 }
779 }.runWrite(); 783 }.runWrite();
780 784
781 if (success) { 785 if (success) {
782 fireArtifactStored(artifact.getArtifact()); 786 fireStoredArtifact(artifact.getArtifact());
783 } 787 }
784 } 788 }
789
790 protected void fireStoredArtifact(Artifact artifact) {
791 for (BackendListener listener: listeners) {
792 listener.storedArtifact(artifact, this);
793 }
794 }
795
785 796
786 public User createUser( 797 public User createUser(
787 final String name, 798 final String name,
788 final Document role, 799 final Document role,
789 final UserFactory factory, 800 final UserFactory factory,
829 identifier, name, role, context); 840 identifier, name, role, context);
830 return true; 841 return true;
831 } 842 }
832 }; 843 };
833 844
834 return exec.runWrite() ? user[0] : null; 845 boolean success = exec.runWrite();
846
847 if (success) {
848 fireCreatedUser(user[0]);
849 return user[0];
850 }
851
852 return null;
853 }
854
855 protected void fireCreatedUser(User user) {
856 for (BackendListener listener: listeners) {
857 listener.createdUser(user, this);
858 }
835 } 859 }
836 860
837 public boolean deleteUser(final String identifier) { 861 public boolean deleteUser(final String identifier) {
838 862
839 if (!isValidIdentifier(identifier)) { 863 if (!isValidIdentifier(identifier)) {
889 conn.commit(); 913 conn.commit();
890 return true; 914 return true;
891 } 915 }
892 }; 916 };
893 917
894 return exec.runWrite(); 918 boolean success = exec.runWrite();
919
920 if (success) {
921 fireDeletedUser(identifier);
922 }
923
924 return success;
925 }
926
927 protected void fireDeletedUser(String identifier) {
928 for (BackendListener listener: listeners) {
929 listener.deletedUser(identifier, this);
930 }
895 } 931 }
896 932
897 public User getUser( 933 public User getUser(
898 final String identifier, 934 final String identifier,
899 final UserFactory factory, 935 final UserFactory factory,
1058 1094
1059 return true; 1095 return true;
1060 } 1096 }
1061 }; 1097 };
1062 1098
1063 return exec.runWrite() ? collection[0]: null; 1099 boolean success = exec.runWrite();
1100
1101 if (success) {
1102 fireCreatedCollection(collection[0]);
1103 return collection[0];
1104 }
1105 return null;
1106 }
1107
1108 protected void fireCreatedCollection(ArtifactCollection collection) {
1109 for (BackendListener listener: listeners) {
1110 listener.createdCollection(collection, this);
1111 }
1064 } 1112 }
1065 1113
1066 public ArtifactCollection getCollection( 1114 public ArtifactCollection getCollection(
1067 final String collectionId, 1115 final String collectionId,
1068 final ArtifactCollectionFactory collectionFactory, 1116 final ArtifactCollectionFactory collectionFactory,
1231 stmnt.execute(); 1279 stmnt.execute();
1232 conn.commit(); 1280 conn.commit();
1233 return true; 1281 return true;
1234 } 1282 }
1235 }; 1283 };
1236 return exec.runWrite(); 1284 boolean success = exec.runWrite();
1285
1286 if (success) {
1287 fireDeletedCollection(collectionId);
1288 }
1289
1290 return success;
1291 }
1292
1293 protected void fireDeletedCollection(String identifier) {
1294 for (BackendListener listener: listeners) {
1295 listener.deletedCollection(identifier, this);
1296 }
1237 } 1297 }
1238 1298
1239 public Document getCollectionAttribute(final String collectionId) { 1299 public Document getCollectionAttribute(final String collectionId) {
1240 if (!isValidIdentifier(collectionId)) { 1300 if (!isValidIdentifier(collectionId)) {
1241 logger.debug("collection id is not valid: " + collectionId); 1301 logger.debug("collection id is not valid: " + collectionId);
1272 return false; 1332 return false;
1273 } 1333 }
1274 1334
1275 final byte [] data = XMLUtils.toByteArray(attribute, true); 1335 final byte [] data = XMLUtils.toByteArray(attribute, true);
1276 1336
1277 return sqlExecutor.new Instance() { 1337 boolean success = sqlExecutor.new Instance() {
1278 public boolean doIt() throws SQLException { 1338 public boolean doIt() throws SQLException {
1279 1339
1280 // set the column in collection items 1340 // set the column in collection items
1281 prepareStatement(SQL_COLLECTION_SET_ATTRIBUTE); 1341 prepareStatement(SQL_COLLECTION_SET_ATTRIBUTE);
1282 if (data == null) { 1342 if (data == null) {
1296 1356
1297 conn.commit(); 1357 conn.commit();
1298 return true; 1358 return true;
1299 } 1359 }
1300 }.runWrite(); 1360 }.runWrite();
1361
1362 if (success) {
1363 fireChangedCollectionAttribute(collectionId, attribute);
1364 }
1365
1366 return success;
1367 }
1368
1369 protected void fireChangedCollectionAttribute(
1370 String collectionId,
1371 Document document
1372 ) {
1373 for (BackendListener listener: listeners) {
1374 listener.changedCollectionAttribute(collectionId, document, this);
1375 }
1301 } 1376 }
1302 1377
1303 public Document getCollectionItemAttribute( 1378 public Document getCollectionItemAttribute(
1304 final String collectionId, 1379 final String collectionId,
1305 final String artifactId 1380 final String artifactId
1349 return false; 1424 return false;
1350 } 1425 }
1351 1426
1352 final byte [] data = XMLUtils.toByteArray(attribute, true); 1427 final byte [] data = XMLUtils.toByteArray(attribute, true);
1353 1428
1354 return sqlExecutor.new Instance() { 1429 boolean success = sqlExecutor.new Instance() {
1355 public boolean doIt() throws SQLException { 1430 public boolean doIt() throws SQLException {
1356 1431
1357 // set the column in collection items 1432 // set the column in collection items
1358 prepareStatement(SQL_COLLECTION_ITEM_SET_ATTRIBUTE); 1433 prepareStatement(SQL_COLLECTION_ITEM_SET_ATTRIBUTE);
1359 if (data == null) { 1434 if (data == null) {
1374 1449
1375 conn.commit(); 1450 conn.commit();
1376 return true; 1451 return true;
1377 } 1452 }
1378 }.runWrite(); 1453 }.runWrite();
1454
1455 if (success) {
1456 fireChangedCollectionItemAttribute(
1457 collectionId, artifactId, attribute);
1458 }
1459
1460 return success;
1461 }
1462
1463 protected void fireChangedCollectionItemAttribute(
1464 String collectionId,
1465 String artifactId,
1466 Document document
1467 ) {
1468 for (BackendListener listener: listeners) {
1469 listener.changedCollectionItemAttribute(
1470 collectionId, artifactId, document, this);
1471 }
1379 } 1472 }
1380 1473
1381 public boolean addCollectionArtifact( 1474 public boolean addCollectionArtifact(
1382 final String collectionId, 1475 final String collectionId,
1383 final String artifactId, 1476 final String artifactId,
1455 conn.commit(); 1548 conn.commit();
1456 1549
1457 return true; 1550 return true;
1458 } 1551 }
1459 }; 1552 };
1460 return exec.runWrite(); 1553 boolean success = exec.runWrite();
1554
1555 if (success) {
1556 fireAddedArtifactToCollection(artifactId, collectionId);
1557 }
1558
1559 return success;
1560 }
1561
1562 protected void fireAddedArtifactToCollection(
1563 String artifactId,
1564 String collectionId
1565 ) {
1566 for (BackendListener listener: listeners) {
1567 listener.addedArtifactToCollection(
1568 artifactId, collectionId, this);
1569 }
1461 } 1570 }
1462 1571
1463 public boolean removeCollectionArtifact( 1572 public boolean removeCollectionArtifact(
1464 final String collectionId, 1573 final String collectionId,
1465 final String artifactId 1574 final String artifactId
1466 ) { 1575 ) {
1467 if (!isValidIdentifier(collectionId)) { 1576 if (!isValidIdentifier(collectionId)) {
1468 logger.debug("Invalid collection id: '" + collectionId + "'"); 1577 logger.debug("Invalid collection id: '" + collectionId + "'");
1469 return false; 1578 return false;
1470 } 1579 }
1471 return sqlExecutor.new Instance() { 1580
1581 boolean success = sqlExecutor.new Instance() {
1472 public boolean doIt() throws SQLException { 1582 public boolean doIt() throws SQLException {
1473 1583
1474 // fetch id, collection id and artitfact id 1584 // fetch id, collection id and artitfact id
1475 prepareStatement(SQL_COLLECTION_ITEM_ID_CID_AID); 1585 prepareStatement(SQL_COLLECTION_ITEM_ID_CID_AID);
1476 stmnt.setString(1, collectionId); 1586 stmnt.setString(1, collectionId);
1506 1616
1507 conn.commit(); 1617 conn.commit();
1508 return true; 1618 return true;
1509 } 1619 }
1510 }.runWrite(); 1620 }.runWrite();
1621
1622 if (success) {
1623 fireRemovedArtifactFromCollection(artifactId, collectionId);
1624 }
1625
1626 return success;
1627 }
1628
1629 protected void fireRemovedArtifactFromCollection(
1630 String artifactId,
1631 String collectionId
1632 ) {
1633 for (BackendListener listener: listeners) {
1634 listener.removedArtifactFromCollection(
1635 artifactId, collectionId, this);
1636 }
1511 } 1637 }
1512 1638
1513 public CollectionItem [] listCollectionArtifacts( 1639 public CollectionItem [] listCollectionArtifacts(
1514 final String collectionId 1640 final String collectionId
1515 ) { 1641 ) {
1572 if (!isValidIdentifier(uuid)) { 1698 if (!isValidIdentifier(uuid)) {
1573 logger.debug("Invalid collection id: '" + uuid + "'"); 1699 logger.debug("Invalid collection id: '" + uuid + "'");
1574 return false; 1700 return false;
1575 } 1701 }
1576 1702
1577 return sqlExecutor.new Instance() { 1703 boolean success = sqlExecutor.new Instance() {
1578 public boolean doIt() throws SQLException { 1704 public boolean doIt() throws SQLException {
1579 prepareStatement(SQL_UPDATE_COLLECTION_NAME); 1705 prepareStatement(SQL_UPDATE_COLLECTION_NAME);
1580 stmnt.setString(1, name); 1706 stmnt.setString(1, name);
1581 stmnt.setString(2, uuid); 1707 stmnt.setString(2, uuid);
1582 stmnt.execute(); 1708 stmnt.execute();
1583 conn.commit(); 1709 conn.commit();
1584 1710
1585 return true; 1711 return true;
1586 } 1712 }
1587 }.runWrite(); 1713 }.runWrite();
1714
1715 if (success) {
1716 fireSetCollectionName(uuid, name);
1717 }
1718
1719 return success;
1720 }
1721
1722 protected void fireSetCollectionName(String identifier, String name) {
1723 for (BackendListener listener: listeners) {
1724 listener.setCollectionName(identifier, name);
1725 }
1588 } 1726 }
1589 1727
1590 public boolean loadAllArtifacts(final ArtifactLoadedCallback alc) { 1728 public boolean loadAllArtifacts(final ArtifactLoadedCallback alc) {
1591 1729
1592 logger.debug("loadAllArtifacts"); 1730 logger.debug("loadAllArtifacts");

http://dive4elements.wald.intevation.org