diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 180:38fbbeffe8fe

Added code to remove an artifact from a collection artifacts/trunk@1406 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 06 Mar 2011 11:49:00 +0000
parents 644fd11ddd9f
children 7eebe707a034
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Mar 06 10:55:37 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Mar 06 11:49:00 2011 +0000
@@ -171,7 +171,19 @@
         SQL.get("collection.item.set.attribute");
 
     public static final String SQL_COLLECTIONS_TOUCH_BY_GID =
-        SQL.get("collections.touch.by,gid");
+        SQL.get("collections.touch.by.gid");
+
+    public static final String SQL_COLLECTION_ITEM_ID_CID_AID =
+        SQL.get("collection.item.id.cid.aid");
+
+    public static final String SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT =
+        SQL.get("collection.item.outdate.artifact");
+
+    public static final String SQL_COLLECTION_ITEM_DELETE =
+        SQL.get("collection.item.delete");
+
+    public static final String SQL_COLLECTIONS_TOUCH_BY_ID =
+        SQL.get("collections.touch.by.id");
 
     /** The singleton.*/
     protected static Backend instance;
@@ -1278,11 +1290,52 @@
     }
 
     public boolean removeCollectionArtifact(
-        String collectionId,
-        String artifactId
+        final String collectionId,
+        final String artifactId
     ) {
-        // TODO: Implement me!
-        return false;
+        if (!isValidIdentifier(collectionId)) {
+            logger.debug("Invalid collection id: '" + collectionId + "'");
+            return false;
+        }
+        return new SQLExecutor() {
+            public boolean doIt() throws SQLException {
+
+                // fetch id, collection id and artitfact id
+                prepareStatement(SQL_COLLECTION_ITEM_ID_CID_AID);
+                stmnt.setString(1, collectionId);
+                stmnt.setString(2, artifactId);
+                result = stmnt.executeQuery();
+                if (!result.next()) {
+                    logger.debug("No such collection item");
+                    return false;
+                }
+                int  id = result.getInt(1);
+                int cid = result.getInt(2);
+                int aid = result.getInt(3);
+                reset();
+
+                // outdate artifact iff it is only in this collection
+                prepareStatement(SQL_COLLECTION_ITEM_OUTDATE_ARTIFACT);
+                stmnt.setInt(1, aid);
+                stmnt.setInt(2, cid);
+                stmnt.execute();
+                reset();
+
+                // delete collection item
+                prepareStatement(SQL_COLLECTION_ITEM_DELETE);
+                stmnt.setInt(1, id);
+                stmnt.execute();
+                reset();
+
+                // touch collection
+                prepareStatement(SQL_COLLECTIONS_TOUCH_BY_ID);
+                stmnt.setInt(1, cid);
+                stmnt.execute();
+
+                conn.commit();
+                return true;
+            }
+        }.runWrite();
     }
 
     public ArtifactCollection [] listCollectionArtifacts(String collectionId) {

http://dive4elements.wald.intevation.org