changeset 179:644fd11ddd9f

Added code to set attribute of a collection item. artifacts/trunk@1405 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 06 Mar 2011 10:55:37 +0000
parents 535e4ea2ef9b
children 38fbbeffe8fe
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/resources/sql/org-h2-driver.properties artifact-database/src/main/resources/sql/org-postgresql-driver.properties
diffstat 4 files changed, 79 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Mar 06 10:27:16 2011 +0000
+++ b/ChangeLog	Sun Mar 06 10:55:37 2011 +0000
@@ -1,3 +1,13 @@
+2011-03-06	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Added code to set the attribute of a collection item.
+
+	* artifact-database/src/main/resources/sql/org-h2-driver.properties,
+	  artifact-database/src/main/resources/sql/org-postgresql-driver.properties:
+	  Added SQL statements to set the attribute column of a collection item
+	  given a collection id and an artifact id.
+
 2011-03-06	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
@@ -19,7 +29,7 @@
 2011-03-04	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
-	  TODO: Parse incoming document for an initial attribute of an
+	  TODO: Parse incoming document for an initial attribute of a
 	  collection item.
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Mar 06 10:27:16 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Mar 06 10:55:37 2011 +0000
@@ -167,6 +167,12 @@
     public static final String SQL_COLLECTION_ITEM_GET_ATTRIBUTE =
         SQL.get("collection.item.get.attribute");
 
+    public static final String SQL_COLLECTION_ITEM_SET_ATTRIBUTE =
+        SQL.get("collection.item.set.attribute");
+
+    public static final String SQL_COLLECTIONS_TOUCH_BY_GID =
+        SQL.get("collections.touch.by,gid");
+
     /** The singleton.*/
     protected static Backend instance;
 
@@ -1121,6 +1127,7 @@
         }
         if (!isValidIdentifier(artifactId)) {
             logger.debug("artifact id is not valid: " + artifactId);
+            return null;
         }
 
         final Document [] document = new Document[1];
@@ -1144,12 +1151,46 @@
     }
 
     public boolean setCollectionAttribute(
-        String collectionId, 
-        String artifactId,
-        Document attribute
+        final String   collectionId, 
+        final String   artifactId,
+        Document       attribute
     ) {
-        // TODO: Implement me!
-        return false;
+        if (!isValidIdentifier(collectionId)) {
+            logger.debug("collection id is not valid: " + collectionId);
+            return false;
+        }
+        if (!isValidIdentifier(artifactId)) {
+            logger.debug("artifact id is not valid: " + artifactId);
+            return false;
+        }
+
+        final byte [] data = XMLUtils.toByteArray(attribute);
+
+        return new SQLExecutor() {
+            public boolean doIt() throws SQLException {
+
+                // set the column in collection items
+                prepareStatement(SQL_COLLECTION_ITEM_SET_ATTRIBUTE);
+                if (data == null) {
+                    stmnt.setNull(1, Types.BINARY);
+                }
+                else {
+                    stmnt.setBytes(1, data);
+                }
+                stmnt.setString(2, collectionId);
+                stmnt.setString(3, artifactId);
+                stmnt.execute();
+                reset();
+
+                // touch the collection
+                prepareStatement(SQL_COLLECTIONS_TOUCH_BY_GID);
+                stmnt.setString(1, collectionId);
+                stmnt.execute();
+
+                conn.commit();
+                return true;
+            }
+        }.runWrite();
     }
 
     public boolean addCollectionArtifact(
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Sun Mar 06 10:27:16 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Sun Mar 06 10:55:37 2011 +0000
@@ -54,7 +54,18 @@
         INNER JOIN attributes a ON ci.artifact_id = a.id \
         WHERE c.gid = ? AND a.gid = ?
 
+collection.item.set.attribute= \
+    UPDATE collections_items SET attribute = ? WHERE id IN ( \
+        SELECT ci.id FROM collections_items ci \
+            INNER JOIN collection c ON ci.collection_id = c.id \
+            INNER JOIN attributes a ON ci.artifact_id = a.id \
+            WHERE c.gid = ? AND a.gid = ?)
+
 # COLLECTIONS
+collections.touch.by.gid =\
+    UPDATE collection SET last_access = CURRENT_TIMESTAMP \
+        WHERE gid = ?
+
 collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ')
 
 collections.id.by.gid=SELECT id FROM collections WHERE gid = ?
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Sun Mar 06 10:27:16 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Sun Mar 06 10:55:37 2011 +0000
@@ -54,7 +54,18 @@
         INNER JOIN attributes a ON ci.artifact_id = a.id \
         WHERE c.gid = ?::uuid AND a.gid = ?::uuid
 
+collection.item.set.attribute= \
+    UPDATE collections_items SET attribute = ? WHERE id IN ( \
+        SELECT ci.id FROM collections_items ci \
+            INNER JOIN collection c ON ci.collection_id = c.id \
+            INNER JOIN attributes a ON ci.artifact_id = a.id \
+            WHERE c.gid = ?::uuid AND a.gid = ?::uuid)
+
 # COLLECTIONS
+collections.touch.by.gid =\
+    UPDATE collection SET last_access = CURRENT_TIMESTAMP \
+        WHERE gid = ?::uuid
+
 collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ')
 
 collections.id.by.gid=SELECT id FROM collections WHERE gid = ?::uuid

http://dive4elements.wald.intevation.org