changeset 991:c675b4a87201

Store outs of artifacts into datacage db at initial scan. flys-artifacts/trunk@2423 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 29 Jul 2011 11:24:41 +0000
parents e573c4715d81
children 5b467bc27eda
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, 96 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jul 29 08:40:11 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 29 11:24:41 2011 +0000
@@ -1,3 +1,10 @@
+2011-07-29  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:
+	  Store outs of artifacts into datacage db at initial scan.
+	  TODO: store facets.
+
 2011-07-29  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* doc/conf/datacage.sql: Added 'type' column in artifacts data.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Fri Jul 29 08:40:11 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Fri Jul 29 11:24:41 2011 +0000
@@ -1,8 +1,10 @@
 package de.intevation.flys.artifacts.datacage;
 
 import java.util.Collection;
+import java.util.List;
 
 import java.sql.SQLException;
+import java.sql.PreparedStatement;
 import java.sql.Types;
 
 import de.intevation.artifacts.GlobalContext;
@@ -14,6 +16,8 @@
 
 import de.intevation.artifactdatabase.data.StateData;
 
+import de.intevation.artifactdatabase.state.Output;
+
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactDatabase;
 import de.intevation.artifacts.ArtifactDatabaseException;
@@ -47,8 +51,10 @@
     private String SQL_INSERT_COLLECTION_ITEM = "insert.collection.item";
     private String SQL_ARTIFACT_ID_NEXTVAL    = "artifact.id.nextval";
     private String SQL_INSERT_ARTIFACT        = "insert.artifact";
-    private String SQL_ARTIFACT_DATA_ID_NEXT_VAL = "artifact.data.id.nextval";
+    private String SQL_ARTIFACT_DATA_ID_NEXTVAL = "artifact.data.id.nextval";
     private String SQL_INSERT_ARTIFACT_DATA   = "insert.artifact.data";
+    private String SQL_OUT_ID_NEXTVALUE       = "out.id.nextval";
+    private String SQL_INSERT_OUT             = "insert.out";
 
 
     protected SQLExecutor sqlExecutor;
@@ -60,12 +66,19 @@
         protected LRUCache<String, Integer> collections;
         protected LRUCache<String, Integer> artifacts;
 
+        protected GlobalContext context;
+
         public InitialScan() {
             users       = new LRUCache<String, Integer>();
             collections = new LRUCache<String, Integer>();
             artifacts   = new LRUCache<String, Integer>();
         }
 
+        public InitialScan(GlobalContext context) {
+            this();
+            this.context = context;
+        }
+
         @Override
         public void artifactLoaded(
             String   userId,
@@ -168,8 +181,58 @@
 
             storeData(aId, artifact);
 
-            // store data, outs and facets
-            // TODO: Implement me!
+            storeOuts(aId, artifact);
+        }
+
+        protected void storeOuts(
+            final int          artifactId,
+            final FLYSArtifact artifact
+        ) {
+            final List<Output> outs = artifact.getCurrentOutputs(context);
+
+            if (outs.isEmpty()) {
+                return;
+            }
+
+            final int [] ids = new int[outs.size()];
+
+            //final Map<Integer, List<Facet>> facets =
+                //new TreeMap<Integer, List<Facet>>();
+
+            SQLExecutor.Instance exec = sqlExecutor.new Instance() {
+                @Override
+                public boolean doIt() throws SQLException {
+                    prepareStatement(SQL_OUT_ID_NEXTVALUE);
+                    for (int i = 0; i < ids.length; ++i) {
+                        result = stmnt.executeQuery();
+                        if (!result.next()) {
+                            log.error("generation of out ids failed");
+                            return false;
+                        }
+                        ids[i] = result.getInt(1);
+                        result.close(); result = null;
+                    }
+                    reset();
+                    prepareStatement(SQL_INSERT_OUT);
+                    for (int i = 0; i < ids.length; ++i) {
+                        Output out = outs.get(i);
+                        stmnt.setInt(1, ids[i]);
+                        stmnt.setInt(2, artifactId);
+                        stmnt.setString(3, out.getName());
+                        setString(stmnt, 4, out.getDescription());
+                        setString(stmnt, 5, out.getType());
+                        stmnt.execute();
+                    }
+                    conn.commit();
+                    return true;
+                }
+            };
+
+            if (!exec.runWrite()) {
+                log.error("storing artifact outs/facets failed");
+            }
+
+            // TODO: Store facets
         }
 
         protected void storeData(
@@ -186,7 +249,7 @@
                 @Override
                 public boolean doIt() throws SQLException {
                     int [] ids = new int[data.size()];
-                    prepareStatement(SQL_ARTIFACT_DATA_ID_NEXT_VAL);
+                    prepareStatement(SQL_ARTIFACT_DATA_ID_NEXTVAL);
 
                     for (int i = 0; i < ids.length; ++i) {
                         result = stmnt.executeQuery();
@@ -207,13 +270,7 @@
                         stmnt.setInt(2, artifactId);
                         stmnt.setString(3, sd.getType());
                         stmnt.setString(4, sd.getName());
-                        Object value = sd.getValue();
-                        if (value == null) {
-                            stmnt.setNull(5, Types.VARCHAR);
-                        }
-                        else {
-                            stmnt.setString(5, value.toString());
-                        }
+                        setString(stmnt, 5, sd);
                         stmnt.execute();
                     }
 
@@ -374,8 +431,25 @@
             sql.get(SQL_INSERT_COLLECTION_ITEM);
         SQL_ARTIFACT_ID_NEXTVAL = sql.get(SQL_ARTIFACT_ID_NEXTVAL);
         SQL_INSERT_ARTIFACT     = sql.get(SQL_INSERT_ARTIFACT);
-        SQL_ARTIFACT_DATA_ID_NEXT_VAL = sql.get(SQL_ARTIFACT_DATA_ID_NEXT_VAL);
+        SQL_ARTIFACT_DATA_ID_NEXTVAL = sql.get(SQL_ARTIFACT_DATA_ID_NEXTVAL);
         SQL_INSERT_ARTIFACT_DATA = sql.get(SQL_INSERT_ARTIFACT_DATA);
+        SQL_OUT_ID_NEXTVALUE     = sql.get(SQL_OUT_ID_NEXTVALUE);
+        SQL_INSERT_OUT           = sql.get(SQL_INSERT_OUT);
+    }
+
+    private static void setString(
+        PreparedStatement stmnt, 
+        int               index,
+        Object            value
+    ) 
+    throws SQLException
+    {
+        if (value == null) {
+            stmnt.setNull(index, Types.VARCHAR);
+        }
+        else {
+            stmnt.setString(index, value.toString());
+        }
     }
 
     @Override
@@ -401,7 +475,7 @@
             return;
         }
 
-        InitialScan is = new InitialScan();
+        InitialScan is = new InitialScan(context);
 
         if (!is.scan(adb)) {
             log.error("initial scan failed");
--- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Fri Jul 29 08:40:11 2011 +0000
+++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Fri Jul 29 11:24:41 2011 +0000
@@ -12,3 +12,5 @@
 insert.artifact = INSERT INTO artifacts (id, gid, state) VALUES (?, ?, ?)
 artifact.data.id.nextval = SELECT NEXTVAL('ARTIFACT_DATA_ID_SEQ')
 insert.artifact.data = INSERT INTO artifact_data (id, artifact_id, kind, k, v) VALUES (?, ?, ?, ?, ?)
+out.id.nextval = SELECT NEXTVAL('OUTS_ID_SEQ')
+insert.out = INSERT INTO outs (id, artifact_id, name, description, out_type) VALUES (?, ?, ?, ?, ?)

http://dive4elements.wald.intevation.org