changeset 253:a2df2b48d2aa

Enabled the artifact database to handle requests specific to collection attributes and collection item attributes. artifacts/trunk@1746 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 26 Apr 2011 07:43:40 +0000
parents 6de74b0b878e
children 6cf9560bd249
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java artifact-database/src/main/resources/sql/org-h2-driver.properties artifact-database/src/main/resources/sql/org-postgresql-driver.properties artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java
diffstat 7 files changed, 172 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Apr 26 06:56:01 2011 +0000
+++ b/ChangeLog	Tue Apr 26 07:43:40 2011 +0000
@@ -1,3 +1,18 @@
+2011-04-26  Ingo Weinzierl <ingo@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java,
+	  artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  New methods for querying and updating attributes of collections.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java:
+	  Enabled the request dispatcher to handle requests specific to collection
+	  attributes and collection item attributes.
+
+	* artifact-database/src/main/resources/sql/org-h2-driver.properties,
+	  artifact-database/src/main/resources/sql/org-postgresql-driver.properties:
+	  New statements for querying and inserting attributes of collections.
+
 2011-04-26  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java,
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Tue Apr 26 06:56:01 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Tue Apr 26 07:43:40 2011 +0000
@@ -1265,7 +1265,54 @@
         finally {
             cc.postCall();
         }
+    }
 
+
+    public Document getCollectionAttribute(String collectionId, CallMeta meta)
+    throws ArtifactDatabaseException
+    {
+        logger.debug("Fetch collection attribute for: " + collectionId);
+
+        return backend.getCollectionAttribute(collectionId);
+    }
+
+
+    public Document setCollectionAttribute(
+        String   collectionId,
+        CallMeta meta,
+        Document attribute)
+    throws ArtifactDatabaseException
+    {
+        logger.debug("Set the attribute for the collection: " + collectionId);
+
+        Document attributes = XMLUtils.newDocument();
+
+        Node attr = (Node) XMLUtils.xpath(
+            attribute,
+            XPATH_COLLECTION_ATTRIBUTE,
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (attr != null) {
+            attributes.appendChild(attributes.importNode(attr, true));
+        }
+
+        Document result = XMLUtils.newDocument();
+
+        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
+            result,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Element root = ec.create("result");
+        result.appendChild(root);
+
+        boolean success = backend.setCollectionAttribute(
+            collectionId, attributes);
+
+        root.setTextContent(success ? OPERATION_SUCCESSFUL: OPERATION_FAILURE);
+
+        return result;
     }
 
     public Document getCollectionItemAttribute(String collectionId, String artifactId,
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Tue Apr 26 06:56:01 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Tue Apr 26 07:43:40 2011 +0000
@@ -168,6 +168,12 @@
     public static final String SQL_COLLECTION_ITEMS_INSERT =
         SQL.get("collection.items.insert");
 
+    public static final String SQL_COLLECTION_GET_ATTRIBUTE =
+        SQL.get("collection.get.attribute");
+
+    public static final String SQL_COLLECTION_SET_ATTRIBUTE =
+        SQL.get("collection.set.attribute");
+
     public static final String SQL_COLLECTION_ITEM_GET_ATTRIBUTE =
         SQL.get("collection.item.get.attribute");
 
@@ -1202,6 +1208,70 @@
         return exec.runWrite();
     }
 
+    public Document getCollectionAttribute(final String collectionId) {
+        if (!isValidIdentifier(collectionId)) {
+            logger.debug("collection id is not valid: " + collectionId);
+        }
+
+        final byte[][] data = new byte[1][1];
+
+        SQLExecutor exec = new SQLExecutor() {
+            public boolean doIt() throws SQLException {
+                prepareStatement(SQL_COLLECTION_GET_ATTRIBUTE);
+                stmnt.setString(1, collectionId);
+                result = stmnt.executeQuery();
+                if (!result.next()) {
+                    logger.debug("No such collection.");
+                    return false;
+                }
+
+                data[0] = result.getBytes(1);
+                return true;
+            }
+        };
+
+        return exec.runRead()
+            ? XMLUtils.fromByteArray(data[0], true)
+            : null;
+    }
+
+    public boolean setCollectionAttribute(
+        final String   collectionId,
+        Document       attribute
+    ) {
+        if (!isValidIdentifier(collectionId)) {
+            logger.debug("collection id is not valid: " + collectionId);
+            return false;
+        }
+
+        final byte [] data = XMLUtils.toByteArray(attribute, true);
+
+        return new SQLExecutor() {
+            public boolean doIt() throws SQLException {
+
+                // set the column in collection items
+                prepareStatement(SQL_COLLECTION_SET_ATTRIBUTE);
+                if (data == null) {
+                    stmnt.setNull(1, Types.BINARY);
+                }
+                else {
+                    stmnt.setBytes(1, data);
+                }
+                stmnt.setString(2, collectionId);
+                stmnt.execute();
+                reset();
+
+                // touch the collection
+                prepareStatement(SQL_COLLECTIONS_TOUCH_BY_GID);
+                stmnt.setString(1, collectionId);
+                stmnt.execute();
+
+                conn.commit();
+                return true;
+            }
+        }.runWrite();
+    }
+
     public Document getCollectionItemAttribute(
         final String collectionId,
         final String artifactId
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java	Tue Apr 26 06:56:01 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/rest/CollectionResource.java	Tue Apr 26 07:43:40 2011 +0000
@@ -66,13 +66,19 @@
     /** Action name for describing the collection.*/
     public static final String ACTION_DESCRIBE = "describe";
 
+    /** Action name for retrieving the attribute of a collection.*/
+    public static final String ACTION_GET_ATTRIBUTE = "getattribute";
+
     /** Action name for retrieving the attributes of an artifact stored in the
      * collection.*/
-    public static final String ACTION_GET_ATTRIBUTE = "getattribute";
+    public static final String ACTION_GET_ITEM_ATTRIBUTE = "getitemattribute";
+
+    /** Action name for setting the attribute of a collection.*/
+    public static final String ACTION_SET_ATTRIBUTE = "setattribute";
 
     /** Action name for setting the attribute for an artifact stored in the
      * collection.*/
-    public static final String ACTION_SET_ATTRIBUTE = "setattribute";
+    public static final String ACTION_SET_ITEM_ATTRIBUTE = "setitemattribute";
 
     /** Action name for adding a new artifact to the collection.*/
     public static final String ACTION_ADD_ARTIFACT = "addartifact";
@@ -135,12 +141,24 @@
             else if (action.equals(ACTION_SET_ATTRIBUTE)) {
                 String art = getArtifactIdentifier(source);
 
+                logger.info("Set attribute for collection '" + identifier + "'");
+                out = db.setCollectionAttribute(identifier, meta, source);
+            }
+            else if (action.equals(ACTION_SET_ITEM_ATTRIBUTE)) {
+                String art = getArtifactIdentifier(source);
+
                 logger.info("Set attribute for artifact '" + art + "'");
                 out = db.setCollectionItemAttribute(identifier, art, source, meta);
             }
             else if (action.equals(ACTION_GET_ATTRIBUTE)) {
                 String art = getArtifactIdentifier(source);
 
+                logger.info("Retrieve attribute of collection '" + identifier + "'");
+                out = db.getCollectionAttribute(identifier, meta);
+            }
+            else if (action.equals(ACTION_GET_ITEM_ATTRIBUTE)) {
+                String art = getArtifactIdentifier(source);
+
                 logger.info("Retrieve attribute of artifact '" + art + "'");
                 out = db.getCollectionItemAttribute(identifier, art, meta);
             }
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Tue Apr 26 06:56:01 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Tue Apr 26 07:43:40 2011 +0000
@@ -170,3 +170,9 @@
         AND artifact_id NOT IN \
             (SELECT artifact_id FROM collection_items WHERE collection_id IN \
                 (SELECT id FROM collections WHERE owner_id <> ?)))
+
+collection.get.attribute= \
+    SELECT c.attribute FROM collections c WHERE c.gid = ?
+
+collection.set.attribute= \
+    UPDATE collections SET attribute = ? WHERE gid = ?
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Tue Apr 26 06:56:01 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Tue Apr 26 07:43:40 2011 +0000
@@ -160,3 +160,8 @@
             (SELECT artifact_id FROM collection_items WHERE collection_id IN \
                 (SELECT id FROM collections WHERE owner_id <> ?)))
 
+collection.get.attribute= \
+    SELECT c.attribute FROM collections c WHERE c.gid = ?::uuid
+
+collection.set.attribute= \
+    UPDATE collections SET attribute = ? WHERE gid = ?::uuid
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Tue Apr 26 06:56:01 2011 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java	Tue Apr 26 07:43:40 2011 +0000
@@ -206,6 +206,15 @@
     Document describeCollection(String collectionId, CallMeta callMeta)
         throws ArtifactDatabaseException;
 
+    Document getCollectionAttribute(String collectionId, CallMeta callMeta)
+    throws ArtifactDatabaseException;
+
+    Document setCollectionAttribute(
+        String   collectionId,
+        CallMeta callMeta,
+        Document attribute)
+    throws ArtifactDatabaseException;
+
     Document getCollectionItemAttribute(String collectionId, String artifactId,
         CallMeta callMeta) throws ArtifactDatabaseException;
 

http://dive4elements.wald.intevation.org