diff 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
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Aug 01 09:41:40 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Aug 01 14:17:09 2011 +0000
@@ -358,6 +358,10 @@
         listeners.add(listener);
     }
 
+    public void addAllListeners(List<BackendListener> others) {
+        listeners.addAll(others);
+    }
+
     /**
      * Sets the factory lookup mechanism to decouple ArtifactDatabase
      * and Backend.
@@ -728,14 +732,14 @@
             throw new RuntimeException("failed insert artifact into database");
         }
 
-        fireArtifactStored(artifact);
+        fireCreatedArtifact(artifact);
 
         return id[0];
     }
 
-    protected void fireArtifactStored(Artifact artifact) {
+    protected void fireCreatedArtifact(Artifact artifact) {
         for (BackendListener listener: listeners) {
-            listener.artifactStored(artifact, this);
+            listener.createdArtifact(artifact, this);
         }
     }
 
@@ -779,10 +783,17 @@
         }.runWrite();
 
         if (success) {
-            fireArtifactStored(artifact.getArtifact());
+            fireStoredArtifact(artifact.getArtifact());
         }
     }
 
+    protected void fireStoredArtifact(Artifact artifact) {
+        for (BackendListener listener: listeners) {
+            listener.storedArtifact(artifact, this);
+        }
+    }
+
+
     public User createUser(
         final String      name,
         final Document    role,
@@ -831,7 +842,20 @@
             }
         };
 
-        return exec.runWrite() ? user[0] : null;
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireCreatedUser(user[0]);
+            return user[0];
+        }
+
+        return null;
+    }
+
+    protected void fireCreatedUser(User user) {
+        for (BackendListener listener: listeners) {
+            listener.createdUser(user, this);
+        }
     }
 
     public boolean deleteUser(final String identifier) {
@@ -891,7 +915,19 @@
             }
         };
 
-        return exec.runWrite();
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireDeletedUser(identifier);
+        }
+
+        return success;
+    }
+
+    protected void fireDeletedUser(String identifier) {
+        for (BackendListener listener: listeners) {
+            listener.deletedUser(identifier, this);
+        }
     }
 
     public User getUser(
@@ -1060,7 +1096,19 @@
             }
         };
 
-        return exec.runWrite() ? collection[0]: null;
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireCreatedCollection(collection[0]);
+            return collection[0];
+        }
+        return null;
+    }
+
+    protected void fireCreatedCollection(ArtifactCollection collection) {
+        for (BackendListener listener: listeners) {
+            listener.createdCollection(collection, this);
+        }
     }
 
     public ArtifactCollection getCollection(
@@ -1233,7 +1281,19 @@
                 return true;
             }
         };
-        return exec.runWrite();
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireDeletedCollection(collectionId);
+        }
+
+        return success;
+    }
+
+    protected void fireDeletedCollection(String identifier) {
+        for (BackendListener listener: listeners) {
+            listener.deletedCollection(identifier, this);
+        }
     }
 
     public Document getCollectionAttribute(final String collectionId) {
@@ -1274,7 +1334,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        return sqlExecutor.new Instance() {
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // set the column in collection items
@@ -1298,6 +1358,21 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireChangedCollectionAttribute(collectionId, attribute);
+        }
+
+        return success;
+    }
+
+    protected void fireChangedCollectionAttribute(
+        String   collectionId,
+        Document document
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.changedCollectionAttribute(collectionId, document, this);
+        }
     }
 
     public Document getCollectionItemAttribute(
@@ -1351,7 +1426,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        return sqlExecutor.new Instance() {
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // set the column in collection items
@@ -1376,6 +1451,24 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireChangedCollectionItemAttribute(
+                collectionId, artifactId, attribute);
+        }
+
+        return success;
+    }
+
+    protected void fireChangedCollectionItemAttribute(
+        String collectionId,
+        String artifactId,
+        Document document
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.changedCollectionItemAttribute(
+                collectionId, artifactId, document, this);
+        }
     }
 
     public boolean addCollectionArtifact(
@@ -1457,7 +1550,23 @@
                 return true;
             }
         };
-        return exec.runWrite();
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireAddedArtifactToCollection(artifactId, collectionId);
+        }
+
+        return success;
+    }
+
+    protected void fireAddedArtifactToCollection(
+        String artifactId,
+        String collectionId
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.addedArtifactToCollection(
+                artifactId, collectionId, this);
+        }
     }
 
     public boolean removeCollectionArtifact(
@@ -1468,7 +1577,8 @@
             logger.debug("Invalid collection id: '" + collectionId + "'");
             return false;
         }
-        return sqlExecutor.new Instance() {
+
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // fetch id, collection id and artitfact id
@@ -1508,6 +1618,22 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireRemovedArtifactFromCollection(artifactId, collectionId);
+        }
+
+        return success;
+    }
+
+    protected void fireRemovedArtifactFromCollection(
+        String artifactId,
+        String collectionId
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.removedArtifactFromCollection(
+                artifactId, collectionId, this);
+        }
     }
 
     public CollectionItem [] listCollectionArtifacts(
@@ -1574,7 +1700,7 @@
             return false;
         }
 
-        return sqlExecutor.new Instance() {
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_UPDATE_COLLECTION_NAME);
                 stmnt.setString(1, name);
@@ -1585,6 +1711,18 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireSetCollectionName(uuid, name);
+        }
+
+        return success;
+    }
+
+    protected void fireSetCollectionName(String identifier, String name) {
+        for (BackendListener listener: listeners) {
+            listener.setCollectionName(identifier, name);
+        }
     }
 
     public boolean loadAllArtifacts(final ArtifactLoadedCallback alc) {

http://dive4elements.wald.intevation.org