changeset 988:dbe39e1fb5e7

Datacage: Add collection items at initial scan. flys-artifacts/trunk@2420 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 15:59:38 +0000
parents 82ef338fee91
children f3be883325ce
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, 70 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jul 28 15:34:26 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jul 28 15:59:38 2011 +0000
@@ -1,3 +1,12 @@
+2011-07-28  Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+	* doc/conf/datacage.sql: Fixed spelling in sequence name.
+
+	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java,
+	  src/main/resources/datacage-sql/org-h2-driver.properties: Simply add
+	  collection item at initial scan if artifact was stored before.
+	  TODO: Store new artifacts.
+
 2011-07-28  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java,
--- a/flys-artifacts/doc/conf/datacage.sql	Thu Jul 28 15:34:26 2011 +0000
+++ b/flys-artifacts/doc/conf/datacage.sql	Thu Jul 28 15:59:38 2011 +0000
@@ -24,7 +24,7 @@
     state VARCHAR(256)     NOT NULL
 );
 
-CREATE SEQUENCE COLLECTIONS_ITEMS_ID_SEQ;
+CREATE SEQUENCE COLLECTION_ITEMS_ID_SEQ;
 
 CREATE TABLE collection_items (
     id            INT PRIMARY KEY NOT NULL,
@@ -68,7 +68,7 @@
 -- DROP SEQUENCE USERS_ID_SEQ;
 -- DROP SEQUENCE COLLECTIONS_ID_SEQ;
 -- DROP SEQUENCE ARTIFACTS_ID_SEQ;
--- DROP SEQUENCE COLLECTIONS_ITEMS_ID_SEQ;
+-- DROP SEQUENCE COLLECTION_ITEMS_ID_SEQ;
 -- DROP SEQUENCE ARTIFACT_DATA_ID_SEQ;
 -- DROP SEQUENCE OUTS_ID_SEQ;
 -- DROP SEQUENCE FACETS_ID_SEQ;
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Thu Jul 28 15:34:26 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Thu Jul 28 15:59:38 2011 +0000
@@ -36,6 +36,11 @@
     private String SQL_COLLECTION_BY_GID     = "collection.by.gid";
     private String SQL_COLLECTION_ID_NEXTVAL = "collection.id.nextval";
     private String SQL_INSERT_COLLECTION     = "insert.collection";
+    private String SQL_ARTIFACT_BY_GID       = "artifact.by.gid";
+    private String SQL_COLLECTION_ITEM_ID_NEXTVAL =
+        "collection.item.id.nextval";
+    private String SQL_INSERT_COLLECTION_ITEM = "insert.collection.item";
+
 
     protected SQLExecutor sqlExecutor;
 
@@ -44,10 +49,12 @@
     {
         protected LRUCache<String, Integer> users;
         protected LRUCache<String, Integer> collections;
+        protected LRUCache<String, Integer> artifacts;
 
         public InitialScan() {
             users       = new LRUCache<String, Integer>();
             collections = new LRUCache<String, Integer>();
+            artifacts   = new LRUCache<String, Integer>();
         }
 
         @Override
@@ -68,7 +75,7 @@
             // TODO: We need the name of the collection
             Integer cId = getCollectionId(collectionId, uId, "XXX");
 
-            // TODO: implement me!
+            storeArtifact(artifactId, cId, flysArtifact);
         }
 
         protected Integer getId(
@@ -105,6 +112,49 @@
             return null;
         }
 
+        protected void storeArtifact(
+            String       artifactId,
+            Integer      collectionId,
+            FLYSArtifact artifact
+        ) {
+            Integer aId = getId(artifacts, artifactId, SQL_ARTIFACT_BY_GID);
+
+            if (aId != null) {
+                // We've already stored it. Just create the collection item.
+                storeCollectionItem(collectionId, aId);
+                return;
+            }
+            // We need to write it to database
+            // TODO: Implement me!
+        }
+
+        protected void storeCollectionItem(
+            final Integer collectionId,
+            final Integer artifactId
+        ) {
+            SQLExecutor.Instance exec = sqlExecutor.new Instance() {
+                @Override
+                public boolean doIt() throws SQLException {
+                    prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL);
+                    result = stmnt.executeQuery();
+                    if (!result.next()) {
+                        return false;
+                    }
+                    int ciId = result.getInt(1);
+                    reset();
+                    prepareStatement(SQL_INSERT_COLLECTION_ITEM);
+                    stmnt.setInt(1, ciId);
+                    stmnt.setInt(2, collectionId);
+                    stmnt.setInt(3, artifactId);
+                    stmnt.execute();
+                    return true;
+                }
+            };
+
+            if (!exec.runWrite()) {
+                log.error("storing of collection item failed.");
+            }
+        }
 
         protected Integer getCollectionId(
             final String  collectionId,
@@ -215,6 +265,11 @@
         SQL_COLLECTION_BY_GID     = sql.get(SQL_COLLECTION_BY_GID);
         SQL_COLLECTION_ID_NEXTVAL = sql.get(SQL_COLLECTION_ID_NEXTVAL);
         SQL_INSERT_COLLECTION     = sql.get(SQL_INSERT_COLLECTION);
+        SQL_ARTIFACT_BY_GID       = sql.get(SQL_ARTIFACT_BY_GID);
+        SQL_COLLECTION_ITEM_ID_NEXTVAL =
+            sql.get(SQL_COLLECTION_ITEM_ID_NEXTVAL);
+        SQL_INSERT_COLLECTION_ITEM =
+            sql.get(SQL_INSERT_COLLECTION_ITEM);
     }
 
     @Override
--- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Thu Jul 28 15:34:26 2011 +0000
+++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Thu Jul 28 15:59:38 2011 +0000
@@ -5,3 +5,6 @@
 collection.by.gid = SELECT id FROM collections WHERE gid = ?
 collection.id.nextval = SELECT NEXTVAL('COLLECTIONS_ID_SEQ')
 insert.collection = INSERT INTO collections (id, gid, user_id, name) VALUES (?, ?, ?, ?)
+artifact.by.gid = SELECT id FROM artifacts WHERE gid = ?
+collection.item.id.nextval = SELECT NEXTVAL('COLLECTION_ITEMS_ID_SEQ')
+insert.collection.item = INSERT INTO collection_items (id, collection_id, artifact_id) VALUES (?, ?, ?)

http://dive4elements.wald.intevation.org