changeset 990:e573c4715d81

Add artifacts data into datacage db at initial scan. flys-artifacts/trunk@2422 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 29 Jul 2011 08:40:11 +0000
parents f3be883325ce
children c675b4a87201
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, 84 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jul 29 07:56:28 2011 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 29 08:40:11 2011 +0000
@@ -1,3 +1,13 @@
+2011-07-29  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* doc/conf/datacage.sql: Added 'type' column in artifacts data.
+	  Maybe useful for filtering.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java,
+	  src/main/resources/datacage-sql/org-h2-driver.properties:
+	  Store artifact data into db at initial scan.
+	  TODO: store outs and facets.
+
 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 07:56:28 2011 +0000
+++ b/flys-artifacts/doc/conf/datacage.sql	Fri Jul 29 08:40:11 2011 +0000
@@ -37,6 +37,7 @@
 CREATE TABLE artifact_data (
     id          INT PRIMARY KEY NOT NULL,
     artifact_id INT             NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
+    kind        VARCHAR(256)    NOT NULL,
     k           VARCHAR(256)    NOT NULL,
     v           VARCHAR(256),   -- Maybe too short
     UNIQUE (artifact_id, k)
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Fri Jul 29 07:56:28 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Fri Jul 29 08:40:11 2011 +0000
@@ -1,6 +1,9 @@
 package de.intevation.flys.artifacts.datacage;
 
+import java.util.Collection;
+
 import java.sql.SQLException;
+import java.sql.Types;
 
 import de.intevation.artifacts.GlobalContext;
 
@@ -9,6 +12,8 @@
 
 import de.intevation.artifactdatabase.LifetimeListener;
 
+import de.intevation.artifactdatabase.data.StateData;
+
 import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactDatabase;
 import de.intevation.artifacts.ArtifactDatabaseException;
@@ -42,6 +47,8 @@
     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_INSERT_ARTIFACT_DATA   = "insert.artifact.data";
 
 
     protected SQLExecutor sqlExecutor;
@@ -145,6 +152,7 @@
                     stmnt.setString(2, artifactId);
                     stmnt.setString(3, artifact.getCurrentStateId());
                     stmnt.execute();
+                    conn.commit();
                     return true;
                 }
             };
@@ -158,10 +166,67 @@
 
             storeCollectionItem(collectionId, aId);
 
+            storeData(aId, artifact);
+
             // store data, outs and facets
             // TODO: Implement me!
         }
 
+        protected void storeData(
+            final int     artifactId,
+            FLYSArtifact  artifact
+        ) {
+            final Collection<StateData> data = artifact.getAllData();
+
+            if (data.isEmpty()) {
+                return;
+            }
+
+            SQLExecutor.Instance exec = sqlExecutor.new Instance() {
+                @Override
+                public boolean doIt() throws SQLException {
+                    int [] ids = new int[data.size()];
+                    prepareStatement(SQL_ARTIFACT_DATA_ID_NEXT_VAL);
+
+                    for (int i = 0; i < ids.length; ++i) {
+                        result = stmnt.executeQuery();
+                        if (!result.next()) {
+                            log.error("generating id for artifact data failed");
+                            return false;
+                        }
+                        ids[i] = result.getInt(1);
+                        result.close(); result = null;
+                    }
+                    reset();
+                    prepareStatement(SQL_INSERT_ARTIFACT_DATA);
+
+                    int i = 0;
+                    for (StateData sd: data) {
+                        int id = ids[i++];
+                        stmnt.setInt(1, id);
+                        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());
+                        }
+                        stmnt.execute();
+                    }
+
+                    conn.commit();
+                    return true;
+                }
+            };
+
+            if (!exec.runWrite()) {
+                log.error("storing artifact data failed");
+            }
+        }
+
         protected void storeCollectionItem(
             final Integer collectionId,
             final Integer artifactId
@@ -181,6 +246,7 @@
                     stmnt.setInt(2, collectionId);
                     stmnt.setInt(3, artifactId);
                     stmnt.execute();
+                    conn.commit();
                     return true;
                 }
             };
@@ -219,6 +285,7 @@
                     stmnt.setInt   (3, ownerId);
                     stmnt.setString(4, collectionName);
                     stmnt.execute();
+                    conn.commit();
                     return true;
                 }
             };
@@ -255,6 +322,7 @@
                     stmnt.setInt   (1, res[0]);
                     stmnt.setString(2, userId);
                     stmnt.execute();
+                    conn.commit();
                     return true;
                 }
             };
@@ -306,6 +374,8 @@
             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_INSERT_ARTIFACT_DATA = sql.get(SQL_INSERT_ARTIFACT_DATA);
     }
 
     @Override
@@ -347,6 +417,7 @@
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_DELETE_ALL_USERS);
                 stmnt.execute();
+                conn.commit();
                 return true;
             }
         }.runWrite();
--- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Fri Jul 29 07:56:28 2011 +0000
+++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Fri Jul 29 08:40:11 2011 +0000
@@ -10,3 +10,5 @@
 insert.collection.item = INSERT INTO collection_items (id, collection_id, artifact_id) VALUES (?, ?, ?)
 artifact.id.nextval = SELECT NEXTVAL('ARTIFACTS_ID_SEQ')
 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 (?, ?, ?, ?, ?)

http://dive4elements.wald.intevation.org