diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 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 b0a949d3fe09
children f33401ea2a6c
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org