changeset 987:82ef338fee91

Datacage: Add collections at initial scan. flys-artifacts/trunk@2419 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jul 2011 15:34:26 +0000
parents 70545233f8ee
children dbe39e1fb5e7
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, 93 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jul 28 15:07:20 2011 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jul 28 15:34:26 2011 +0000
@@ -1,3 +1,9 @@
+2011-07-28  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: Add
+	  collections at initial scan.
+
 2011-07-28  Sascha L. Teichmann <sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java,
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Thu Jul 28 15:07:20 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java	Thu Jul 28 15:34:26 2011 +0000
@@ -29,10 +29,13 @@
     public static final String ARTEFACT_DATABASE_KEY =
         "global.artifact.database";
 
-    private String SQL_DELETE_ALL_USERS = "delete.all.users";
-    private String SQL_USER_ID_NEXTVAL  = "user.id.nextval";
-    private String SQL_USER_BY_GID      = "user.by.gid";
-    private String SQL_INSERT_USER      = "insert.user";
+    private String SQL_DELETE_ALL_USERS      = "delete.all.users";
+    private String SQL_USER_ID_NEXTVAL       = "user.id.nextval";
+    private String SQL_USER_BY_GID           = "user.by.gid";
+    private String SQL_INSERT_USER           = "insert.user";
+    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";
 
     protected SQLExecutor sqlExecutor;
 
@@ -62,13 +65,20 @@
             FLYSArtifact flysArtifact = (FLYSArtifact)artifact;
 
             Integer uId = getUserId(userId);
+            // TODO: We need the name of the collection
+            Integer cId = getCollectionId(collectionId, uId, "XXX");
+
             // TODO: implement me!
         }
 
-        protected Integer getUserId(final String userId) {
-            Integer u = users.get(userId);
-            if (u != null) {
-                return u;
+        protected Integer getId(
+            LRUCache<String, Integer> cache,
+            final String              idString,
+            final String              selectById
+        ) {
+            Integer id = cache.get(idString);
+            if (id != null) {
+                return id;
             }
 
             final Integer [] res = new Integer[1];
@@ -76,8 +86,8 @@
             SQLExecutor.Instance exec = sqlExecutor.new Instance() {
                 @Override
                 public boolean doIt() throws SQLException {
-                    prepareStatement(SQL_USER_BY_GID);
-                    stmnt.setString(1, userId);
+                    prepareStatement(selectById);
+                    stmnt.setString(1, idString);
                     result = stmnt.executeQuery();
                     if (!result.next()) {
                         return false;
@@ -88,11 +98,66 @@
             };
 
             if (exec.runRead()) {
-                users.put(userId, res[0]);
+                cache.put(idString, res[0]);
                 return res[0];
             }
 
-            exec = sqlExecutor.new Instance() {
+            return null;
+        }
+
+
+        protected Integer getCollectionId(
+            final String  collectionId,
+            final Integer ownerId,
+            final String  collectionName
+        ) {
+            Integer c = getId(collections, collectionId, SQL_COLLECTION_BY_GID);
+
+            if (c != null) {
+                return c;
+            }
+
+            final Integer [] res = new Integer[1];
+
+            SQLExecutor.Instance exec = sqlExecutor.new Instance() {
+                @Override
+                public boolean doIt() throws SQLException {
+                    prepareStatement(SQL_COLLECTION_ID_NEXTVAL);
+                    result = stmnt.executeQuery();
+                    if (!result.next()) {
+                        return false;
+                    }
+                    res[0] = result.getInt(1);
+                    reset();
+                    prepareStatement(SQL_INSERT_COLLECTION);
+                    stmnt.setInt   (1, res[0]);
+                    stmnt.setString(2, collectionId);
+                    stmnt.setInt   (3, ownerId);
+                    stmnt.setString(4, collectionName);
+                    stmnt.execute();
+                    return true;
+                }
+            };
+
+            if (exec.runWrite()) {
+                collections.put(collectionId, res[0]);
+                return res[0];
+            }
+
+            return null;
+        }
+
+        protected Integer getUserId(final String userId) {
+
+            Integer u = getId(users, userId, SQL_USER_BY_GID);
+
+            if (u != null) {
+                return u;
+            }
+
+            final Integer [] res = new Integer[1];
+
+            SQLExecutor.Instance exec = sqlExecutor.new Instance() {
                 @Override
                 public boolean doIt() throws SQLException {
                     prepareStatement(SQL_USER_ID_NEXTVAL);
@@ -143,10 +208,13 @@
     }
 
     protected void setupSQL(SQL sql) {
-        SQL_DELETE_ALL_USERS = sql.get(SQL_DELETE_ALL_USERS);
-        SQL_USER_ID_NEXTVAL  = sql.get(SQL_USER_ID_NEXTVAL);
-        SQL_USER_BY_GID      = sql.get(SQL_USER_BY_GID);
-        SQL_INSERT_USER      = sql.get(SQL_INSERT_USER);
+        SQL_DELETE_ALL_USERS      = sql.get(SQL_DELETE_ALL_USERS);
+        SQL_USER_ID_NEXTVAL       = sql.get(SQL_USER_ID_NEXTVAL);
+        SQL_USER_BY_GID           = sql.get(SQL_USER_BY_GID);
+        SQL_INSERT_USER           = sql.get(SQL_INSERT_USER);
+        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);
     }
 
     @Override
--- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Thu Jul 28 15:07:20 2011 +0000
+++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties	Thu Jul 28 15:34:26 2011 +0000
@@ -2,3 +2,6 @@
 user.id.nextval = SELECT NEXTVAL('USERS_ID_SEQ')
 user.by.gid = SELECT id FROM users WHERE gid = ?
 insert.user = INSERT INTO users (id, gid) VALUES (?, ?)
+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 (?, ?, ?, ?)

http://dive4elements.wald.intevation.org