changeset 175:16e6e661e6bf

Added code to delete collections. artifacts/trunk@1401 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 04 Mar 2011 17:11:40 +0000
parents 25d472a67a9f
children a0eff2227588
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, 70 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Mar 04 16:43:03 2011 +0000
+++ b/ChangeLog	Fri Mar 04 17:11:40 2011 +0000
@@ -1,3 +1,12 @@
+2011-03-04	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Added code to delete a collection.
+
+	* artifact-database/src/main/resources/sql/org-h2-driver.properties,
+	  artifact-database/src/main/resources/sql/org-postgresql-driver.properties:
+	  Added SQL statements to delete collections.
+
 2011-03-04	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/SQLExecutor.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Fri Mar 04 16:43:03 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Fri Mar 04 17:11:40 2011 +0000
@@ -146,6 +146,15 @@
     public static final String SQL_COLLECTIONS_CREATION_TIME =
         SQL.get("collection.creation.time");
 
+    public static final String SQL_COLLECTIONS_ID_BY_GID =
+        SQL.get("collection.creation.time");
+
+    public static final String SQL_DELETE_COLLECTION_ITEMS =
+        SQL.get("delete.collection.items");
+
+    public static final String SQL_DELETE_COLLECTION =
+        SQL.get("delete.collection");
+
     /** The singleton.*/
     protected static Backend instance;
 
@@ -1044,9 +1053,46 @@
     }
 
 
-    public boolean deleteCollection(String collectionId) {
-        // TODO: Implement me!
-        return false;
+    public boolean deleteCollection(final String collectionId) {
+        if (!StringUtils.checkUUID(collectionId)) {
+            logger.debug("Invalid collection id: '" + collectionId + "'");
+            return false;
+        }
+        SQLExecutor exec = new SQLExecutor() {
+            public boolean doIt() throws SQLException {
+                // fetch collection id
+                prepareStatement(SQL_COLLECTIONS_ID_BY_GID);
+                stmnt.setString(1, collectionId);
+                result = stmnt.executeQuery();
+                if (!result.next()) {
+                    logger.debug("No such collection: " + collectionId);
+                    return false;
+                }
+                int id = result.getInt(1);
+                reset();
+
+                // outdate artifacts that are only in this collection
+                prepareStatement(SQL_OUTDATE_ARTIFACTS_COLLECTION);
+                stmnt.setInt(1, id);
+                stmnt.setInt(2, id);
+                stmnt.execute();
+                reset();
+
+                // delete the collection items
+                prepareStatement(SQL_DELETE_COLLECTION_ITEMS);
+                stmnt.setInt(1, id);
+                stmnt.execute();
+                reset();
+
+                // delete the collection
+                prepareStatement(SQL_DELETE_COLLECTION);
+                stmnt.setInt(1, id);
+                stmnt.execute();
+                conn.commit();
+                return true;
+            }
+        };
+        return exec.runWrite();
     }
 
     public Document getCollectionAttribute(
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Fri Mar 04 16:43:03 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Fri Mar 04 17:11:40 2011 +0000
@@ -41,6 +41,12 @@
 # COLLECTIONS
 collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ')
 
+collections.id.by.gid=SELECT id FROM collections WHERE gid = ?
+
+delete.collection.items=DELETE FROM collection_items WHERE collection_id = ?
+
+delete.collection=DELETE FROM collections WHERE id = ?
+
 delete.user.collection.items= \
     DELETE FROM collection_items WHERE collection_id IN \
     (SELECT id FROM collections WHERE owner_id = ?)
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Fri Mar 04 16:43:03 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Fri Mar 04 17:11:40 2011 +0000
@@ -41,6 +41,12 @@
 # COLLECTIONS
 collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ')
 
+collections.id.by.gid=SELECT id FROM collections WHERE gid = ?::uuid
+
+delete.collection.items=DELETE FROM collection_items WHERE collection_id = ?
+
+delete.collection=DELETE FROM collections WHERE id = ?
+
 delete.user.collection.items= \
     DELETE FROM collection_items WHERE collection_id IN \
     (SELECT id FROM collections WHERE owner_id = ?)

http://dive4elements.wald.intevation.org