diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 159:db0d20440b92

Added code to create collections. artifacts/trunk@1384 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 03 Mar 2011 12:13:24 +0000
parents d718a4d55662
children e4a1562dfc21
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Mar 03 12:13:24 2011 +0000
@@ -135,6 +135,12 @@
     public static final String SQL_DELETE_USER_COLLECTION_ITEMS =
         SQL.get("delete.user.collection.items");
 
+    public static final String SQL_COLLECTIONS_NEXT_ID =
+        SQL.get("collections.id.nextval");
+
+    public static final String SQL_COLLECTIONS_INSERT =
+        SQL.get("collections.insert");
+
     /** The singleton.*/
     protected static Backend instance;
 
@@ -1090,17 +1096,109 @@
         return null;
     }
 
-    public ArtifactCollection [] listCollections(String userId) {
-        // TODO: Implement me!
+    public ArtifactCollection createCollection(
+        String                    ownerIdentifier, 
+        String                    name,
+        ArtifactCollectionFactory factory,
+        Document                  data,
+        Object                    context
+    ) {
+        if (name == null) {
+            logger.debug("Name is null");
+            return null;
+        }
+
+        if (!StringUtils.checkUUID(ownerIdentifier)) {
+            logger.debug("Invalid owner id: '" + ownerIdentifier + "'");
+            return null;
+        }
+
+        Connection        conn   = null;
+        ResultSet         result = null;
+        PreparedStatement stmnt  = null;
+
+        DataSource dataSource = DBConnection.getDataSource();
+        try {
+            conn.setAutoCommit(false);
+            try {
+                conn = dataSource.getConnection();
+
+                // fetch owner id
+                stmnt = conn.prepareStatement(SQL_USERS_SELECT_ID_BY_GID);
+                stmnt.setString(1, ownerIdentifier);
+                result = stmnt.executeQuery();
+
+                if (!result.next()) { // no such user
+                    return null;
+                }
+
+                int ownerId = result.getInt(1);
+                result.close(); result = null;
+                stmnt.close();  stmnt  = null;
+
+                // fetch new collection seq number.
+                stmnt = conn.prepareStatement(SQL_COLLECTIONS_NEXT_ID);
+                result = stmnt.executeQuery();
+
+                if (!result.next()) { // no identifier generated
+                    return null;
+                }
+
+                int id = result.getInt(1);
+                result.close(); result = null;
+                stmnt.close();  stmnt  = null;
+
+                String identifier = newIdentifier();
+
+                stmnt = conn.prepareStatement(SQL_COLLECTIONS_INSERT);
+
+                stmnt.setInt(1, id);
+                stmnt.setString(2, identifier);
+                stmnt.setInt(3, ownerId);
+                stmnt.setString(4, name);
+
+                // XXX: A bit odd: we don't have a collection, yet.
+                Long ttl = factory.timeToLiveUntouched(null, context);
+
+                if (ttl == null) {
+                    stmnt.setNull(5, Types.BIGINT);
+                }
+                else {
+                    stmnt.setLong(5, ttl);
+                }
+
+                stmnt.execute();
+                conn.commit();
+
+                return factory.createCollection(
+                    identifier, name, data, context);
+            }
+            catch (SQLException sqle) {
+                conn.rollback();
+                throw sqle;
+            }
+        }
+        catch (SQLException sqle) {
+            logger.error(sqle.getLocalizedMessage(), sqle);
+        }
+        finally {
+            if (result != null) {
+                try { result.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (stmnt != null) {
+                try { stmnt.close(); }
+                catch (SQLException sqle) {}
+            }
+            if (conn != null) {
+                try { conn.close(); }
+                catch (SQLException sqle) {}
+            }
+        }
         return null;
     }
 
-    public ArtifactCollection createCollection(
-        String                    ownerId,
-        Document                  data,
-        Object                    context,
-        ArtifactCollectionFactory factory)
-    {
+    public ArtifactCollection [] listCollections(String userId) {
         // TODO: Implement me!
         return null;
     }

http://dive4elements.wald.intevation.org