changeset 303:190aa68ae7a8

Added method to artifact database to load all artifacts. artifacts/trunk@2408 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 27 Jul 2011 09:32:26 +0000
parents c066343c63b0
children 40b64b4aafce
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/resources/sql/org-h2-driver.properties artifact-database/src/main/resources/sql/org-postgresql-driver.properties artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java
diffstat 6 files changed, 124 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Jul 26 10:19:19 2011 +0000
+++ b/ChangeLog	Wed Jul 27 09:32:26 2011 +0000
@@ -1,3 +1,20 @@
+2011-07-27	Sascha L. Teichmann	<teichmann@intevation.de>
+
+	* artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java:
+	  Added a method loadAllArtifacts() to load all artifacts which are in
+	  a collection and have an owner. The loaded artifacts are passed one by
+	  one to an instance of the interface ArtifactLoadedCallback.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	  Adjusted to implement the interface.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Load the artifacts from the SQL backend.
+
+	* artifact-database/src/main/resources/sql/org-h2-driver.properties,
+	  artifact-database/src/main/resources/sql/org-postgresql-driver.properties:
+	  Added statements to load all the artifacts.
+
 2011-07-26	Sascha L. Teichmann	<teichmann@intevation.de>
 
 	* artifact-database/pom.xml: Bumped H2 version up to latest stable 1.3.158
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Tue Jul 26 10:19:19 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Wed Jul 27 09:32:26 2011 +0000
@@ -1708,5 +1708,16 @@
             callContextListener.close(cc);
         }
     }
+
+    @Override
+    public void loadAllArtifacts(ArtifactLoadedCallback callback) 
+        throws ArtifactDatabaseException
+    {
+        logger.debug("loadAllArtifacts");
+        boolean success = backend.loadAllArtifacts(callback);
+        if (!success) {
+            throw new ArtifactDatabaseException(INTERNAL_ERROR);
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Tue Jul 26 10:19:19 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Wed Jul 27 09:32:26 2011 +0000
@@ -10,6 +10,7 @@
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactCollection;
 import de.intevation.artifacts.ArtifactCollectionFactory;
+import de.intevation.artifacts.ArtifactDatabase.ArtifactLoadedCallback;
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.ArtifactSerializer;
 import de.intevation.artifacts.CollectionItem;
@@ -25,6 +26,8 @@
 
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.Map;
+import java.util.LinkedHashMap;
 import java.util.HashMap;
 
 import org.apache.log4j.Logger;
@@ -205,6 +208,9 @@
     public static final String SQL_COLLECTION_ITEMS_LIST_GID =
         SQL.get("collection.items.list.gid");
 
+    public static final String SQL_ALL_ARTIFACTS =
+        SQL.get("all.artifacts");
+
     /** The singleton.*/
     protected static Backend instance;
 
@@ -498,6 +504,11 @@
             return null;
         }
 
+        if (factoryLookup == null) {
+            logger.error("factory lookup == null");
+            return false;
+        }
+
         final Object [] loaded = new Object[1];
 
         SQLExecutor exec = new SQLExecutor() {
@@ -517,11 +528,6 @@
 
                 String factoryName = result.getString(3);
 
-                if (factoryLookup == null) {
-                    logger.error("factory lookup == null");
-                    return false;
-                }
-
                 ArtifactFactory factory = factoryLookup
                     .getArtifactFactory(factoryName);
 
@@ -1569,5 +1575,65 @@
             }
         }.runWrite();
     }
+
+    public boolean loadAllArtifacts(final ArtifactLoadedCallback alc) {
+
+        if (factoryLookup == null) {
+            logger.error("factory lookup == null");
+            return false;
+        }
+
+        return new SQLExecutor() {
+            public boolean doIt() throws SQLException {
+
+                // a little cache to avoid too much deserializations.
+                Map<String, Artifact> alreadyLoaded =
+                    new LinkedHashMap<String, Artifact>() {
+                        @Override
+                        protected boolean removeEldestEntry(Map.Entry eldest) {
+                            // store only the last 200 to avoid memory flooding
+                            return size() > 200;
+                        }
+                    };
+
+                prepareStatement(SQL_ALL_ARTIFACTS);
+                result = stmnt.executeQuery();
+                while (result.next()) {
+                    String userId       = result.getString(1);
+                    String collectionId = result.getString(2);
+                    String artifactId   = result.getString(3);
+                    String factoryName  = result.getString(4);
+
+                    Artifact artifact = alreadyLoaded.get(artifactId);
+
+                    if (artifact != null) {
+                        alc.artifactLoaded(
+                            userId, collectionId, artifactId, artifact);
+                        continue;
+                    }
+
+                    ArtifactFactory factory = factoryLookup
+                        .getArtifactFactory(factoryName);
+
+                    if (factory == null) {
+                        logger.error("factory '" + factoryName + "' not found");
+                        continue;
+                    }
+
+                    byte [] bytes = result.getBytes(5);
+
+                    artifact = factory.getSerializer().fromBytes(bytes);
+
+                    if (artifact != null) {
+                        alc.artifactLoaded(
+                            userId, collectionId, artifactId, artifact);
+                    }
+
+                    alreadyLoaded.put(artifactId, artifact);
+                }
+                return true;
+            }
+        }.runRead();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Tue Jul 26 10:19:19 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Wed Jul 27 09:32:26 2011 +0000
@@ -181,3 +181,10 @@
 
 collection.set.attribute= \
     UPDATE collections SET attribute = ? WHERE gid = ?
+
+all.artifacts = \
+    SELECT u.gid AS u_gid, c.gid AS c_gid, a.gid AS a_gid, a.factory AS factory, a.data AS data FROM \
+        users u INNER JOIN collections c       ON u.id = c.owner_id \
+                INNER JOIN collection_items ci ON c.id = ci.collection_id \
+                INNER JOIN artifacts a         ON a.if = ci.artifact_id \
+        ORDER BY u_gid, c_gid
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Tue Jul 26 10:19:19 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Wed Jul 27 09:32:26 2011 +0000
@@ -170,3 +170,10 @@
 
 collection.set.attribute= \
     UPDATE collections SET attribute = ? WHERE gid = ?::uuid
+
+all.artifacts = \
+    SELECT u.gid AS u_gid, c.gid AS c_gid, a.gid AS a_gid, a.factory AS factory, a.data AS data FROM \
+        users u INNER JOIN collections c       ON u.id = c.owner_id \
+                INNER JOIN collection_items ci ON c.id = ci.collection_id \
+                INNER JOIN artifacts a         ON a.if = ci.artifact_id \
+        ORDER BY u_gid, c_gid
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Tue Jul 26 10:19:19 2011 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Wed Jul 27 09:32:26 2011 +0000
@@ -272,5 +272,16 @@
 
     Document setCollectionName(String collectionId, Document doc, CallMeta meta)
     throws ArtifactDatabaseException;
+
+    public interface ArtifactLoadedCallback {
+        void artifactLoaded(
+            String   userId,
+            String   collectionId,
+            String   artifactId,
+            Artifact artifact);
+    };
+
+    public void loadAllArtifacts(ArtifactLoadedCallback callback)
+        throws ArtifactDatabaseException;
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org