changeset 984:2b05c4a0c6fd

Datacage: clear database before initial scan flys-artifacts/trunk@2416 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 14:26:01 +0000
parents 4ea5f5a2284e
children 8094ba4ab873
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties
diffstat 3 files changed, 103 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jul 28 13:32:27 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jul 28 14:26:01 2011 +0000
@@ -1,3 +1,9 @@
+2011-07-27  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java,
+	  src/main/resources/datacage-sql/org-h2-driver.properties: Clear database
+	  before initial scan.
+
 2011-07-27  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/datacage/DBConnection.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Thu Jul 28 13:32:27 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Thu Jul 28 14:26:01 2011 +0000
@@ -1,5 +1,7 @@
 package de.intevation.flys.artifacts.datacage;
 
+import java.sql.SQLException;
+
 import de.intevation.artifacts.GlobalContext;
 
 import de.intevation.artifactdatabase.db.SQL;
@@ -7,6 +9,14 @@
 
 import de.intevation.artifactdatabase.LifetimeListener;
 
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactDatabase;
+import de.intevation.artifacts.ArtifactDatabaseException;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+import de.intevation.artifacts.common.utils.LRUCache;
+
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
@@ -16,8 +26,51 @@
 {
     private static Logger log = Logger.getLogger(Datacage.class);
 
+    public static final String ARTEFACT_DATABASE_KEY =
+        "global.artifact.database";
+
+    private String SQL_DELETE_ALL_USERS = "delete.all.users";
+    private String SQL_INSERT_USER      = "insert.user";
+
     protected SQLExecutor sqlExecutor;
 
+    public class InitialScan 
+    implements   ArtifactDatabase.ArtifactLoadedCallback
+    {
+        protected LRUCache<String, Integer> users;
+
+        public InitialScan() {
+        }
+
+        @Override
+        public void artifactLoaded(
+            String   userId,
+            String   collectionId,
+            String   artifactId,
+            Artifact artifact
+        ) {
+            if (!(artifact instanceof FLYSArtifact)) {
+                log.warn("ignoring none FLYS artifacts");
+                return;
+            }
+
+            FLYSArtifact flysArtifact = (FLYSArtifact)artifact;
+            // TODO: implement me!
+        }
+
+        public boolean scan(ArtifactDatabase adb) {
+            try {
+                adb.loadAllArtifacts(this);
+            }
+            catch (ArtifactDatabaseException ade) {
+                log.error(ade);
+                return false;
+            }
+            return true;
+        }
+    } // class InitialScan
+
+
     public Datacage() {
     }
 
@@ -30,14 +83,55 @@
     }
 
     protected void setupSQL(SQL sql) {
-        log.debug("implement me!");
+        SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS);
+        SQL_INSERT_USER      = sql.get(SQL_INSERT_USER);
     }
 
     @Override
     public void systemUp(GlobalContext context) {
         log.debug("systemUp");
+        initialScan(context);
     }
 
+    protected void initialScan(GlobalContext context) {
+        log.debug("initialScan");
+
+        Object adbObject = context.get(ARTEFACT_DATABASE_KEY);
+
+        if (!(adbObject instanceof ArtifactDatabase)) {
+            log.error("missing artefact database. Cannot scan");
+            return;
+        }
+
+        ArtifactDatabase adb = (ArtifactDatabase)adbObject;
+
+        if (!cleanDatabase()) {
+            log.error("cleaning database failed");
+            return;
+        }
+
+        InitialScan is = new InitialScan();
+
+        if (!is.scan(adb)) {
+            log.error("initial scan failed");
+            return;
+        }
+
+    }
+
+    protected boolean cleanDatabase() {
+
+        return sqlExecutor.new Instance() {
+            @Override
+            public boolean doIt() throws SQLException {
+                prepareStatement(SQL_DELETE_ALL_USERS);
+                stmnt.execute();
+                return true;
+            }
+        }.runWrite();
+    }
+
+
     @Override
     public void systemDown(GlobalContext context) {
         log.debug("systemDown");
--- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Thu Jul 28 13:32:27 2011 +0000
+++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Thu Jul 28 14:26:01 2011 +0000
@@ -1,1 +1,2 @@
-# WRITE ME!
+delete.all.users = DELETE FROM users
+insert.user = INSERT INTO users (gid) VALUES (?)

http://dive4elements.wald.intevation.org