changeset 992:5b467bc27eda

Datacage: Store facets of outs into datacage db at initial scan. flys-artifacts/trunk@2424 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 29 Jul 2011 13:29:29 +0000
parents c675b4a87201
children aabcca7aeb6c
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/datacage.sql flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties
diffstat 4 files changed, 81 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jul 29 11:24:41 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 29 13:29:29 2011 +0000
@@ -1,3 +1,13 @@
+2011-07-29  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* doc/conf/datacage.sql: Removed artifact_id from facet because there
+	  is a link via out_id -> outs.id: artifact_id -> artifacts to
+	  find the corresponding artifact.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java,
+	  src/main/resources/datacage-sql/org-h2-driver.properties: Store
+	  facets of outs into datacage db at initial scan.
+
 2011-07-29  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java,
--- a/flys-artifacts/doc/conf/datacage.sql	Fri Jul 29 11:24:41 2011 +0000
+++ b/flys-artifacts/doc/conf/datacage.sql	Fri Jul 29 13:29:29 2011 +0000
@@ -57,11 +57,10 @@
 
 CREATE TABLE facets (
     id          INT PRIMARY KEY NOT NULL,
-    artifact_id INT             NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
-    out_id      INT             NOT NULL REFERENCES outs(id)      ON DELETE CASCADE,
+    out_id      INT             NOT NULL REFERENCES outs(id) ON DELETE CASCADE,
     name        VARCHAR(256)    NOT NULL,
-    num         INT                  NOT NULL,
-    state       VARCHAR(256)         NOT NULL,
+    num         INT             NOT NULL,
+    state       VARCHAR(256)    NOT NULL,
     description VARCHAR(256),
     UNIQUE (artifact_id, out_id, num, name)
 );
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Fri Jul 29 11:24:41 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Fri Jul 29 13:29:29 2011 +0000
@@ -17,6 +17,7 @@
 import de.intevation.artifactdatabase.data.StateData;
 
 import de.intevation.artifactdatabase.state.Output;
+import de.intevation.artifactdatabase.state.Facet;
 
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactDatabase;
@@ -55,7 +56,8 @@
     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";
-
+    private String SQL_FACET_ID_NEXTVAL       = "facet.id.nextval";
+    private String SQL_INSERT_FACET           = "insert.facet";
 
     protected SQLExecutor sqlExecutor;
 
@@ -194,29 +196,26 @@
                 return;
             }
 
-            final int [] ids = new int[outs.size()];
-
-            //final Map<Integer, List<Facet>> facets =
-                //new TreeMap<Integer, List<Facet>>();
+            final int [] outIds = new int[outs.size()];
 
             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) {
+                    for (int i = 0; i < outIds.length; ++i) {
                         result = stmnt.executeQuery();
                         if (!result.next()) {
                             log.error("generation of out ids failed");
                             return false;
                         }
-                        ids[i] = result.getInt(1);
+                        outIds[i] = result.getInt(1);
                         result.close(); result = null;
                     }
                     reset();
                     prepareStatement(SQL_INSERT_OUT);
-                    for (int i = 0; i < ids.length; ++i) {
+                    for (int i = 0; i < outIds.length; ++i) {
                         Output out = outs.get(i);
-                        stmnt.setInt(1, ids[i]);
+                        stmnt.setInt(1, outIds[i]);
                         stmnt.setInt(2, artifactId);
                         stmnt.setString(3, out.getName());
                         setString(stmnt, 4, out.getDescription());
@@ -229,10 +228,55 @@
             };
 
             if (!exec.runWrite()) {
-                log.error("storing artifact outs/facets failed");
+                log.error("storing artifact outs failed");
+                return;
             }
 
-            // TODO: Store facets
+            final int FACETS = numFacets(outs);
+
+            if (FACETS == 0) {
+                return;
+            }
+
+            exec = sqlExecutor.new Instance() {
+                @Override
+                public boolean doIt() throws SQLException {
+                    int [] facetIds = new int[FACETS];
+                    prepareStatement(SQL_FACET_ID_NEXTVAL);
+                    for (int i = 0; i < facetIds.length; ++i) {
+                        result = stmnt.executeQuery();
+                        if (!result.next()) {
+                            log.error("generation of facet ids failed");
+                            return false;
+                        }
+                        facetIds[i] = result.getInt(1);
+                        result.close(); result = null;
+                    }
+                    reset();
+                    prepareStatement(SQL_INSERT_FACET);
+                    int index = 0;
+                    for (int i = 0, N = outs.size(); i < N; ++i) {
+                        Output out = outs.get(i);
+                        int outId = outIds[i];
+                        for (Facet facet: out.getFacets()) {
+                            stmnt.setInt(1, facetIds[index]);
+                            stmnt.setInt(2, outId);
+                            stmnt.setString(3, facet.getName());
+                            stmnt.setInt(4, facet.getIndex());
+                            stmnt.setString(5, "XXX"); // TODO: handle states
+                            setString(stmnt, 6, facet.getDescription());
+                            stmnt.execute();
+                            ++index;
+                        }
+                    }
+                    conn.commit();
+                    return true;
+                }
+            };
+
+            if (!exec.runWrite()) {
+                log.error("storing facets failed");
+            }
         }
 
         protected void storeData(
@@ -435,9 +479,19 @@
         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);
+        SQL_FACET_ID_NEXTVAL     = sql.get(SQL_FACET_ID_NEXTVAL);
+        SQL_INSERT_FACET         = sql.get(SQL_INSERT_FACET);
     }
 
-    private static void setString(
+    protected static final int numFacets(List<Output> outs) {
+        int sum = 0;
+        for (Output out: outs) {
+            sum += out.getFacets().size();
+        }
+        return sum;
+    }
+
+    protected static final void setString(
         PreparedStatement stmnt, 
         int               index,
         Object            value
--- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Fri Jul 29 11:24:41 2011 +0000
+++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Fri Jul 29 13:29:29 2011 +0000
@@ -14,3 +14,5 @@
 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 (?, ?, ?, ?, ?)
+facet.id.nextval = SELECT NEXTVAL('FACETS_ID_SEQ')
+insert.facet = INSERT INTO facets (id, out_id, name, num, state, description) VALUES (?, ?, ?, ?, ?, ?)

http://dive4elements.wald.intevation.org