changeset 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 dc687827439f
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/DefaultArtifactCollection.java artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.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/ArtifactCollection.java artifacts/src/main/java/de/intevation/artifacts/ArtifactCollectionFactory.java
diffstat 9 files changed, 176 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Mar 03 10:54:33 2011 +0000
+++ b/ChangeLog	Thu Mar 03 12:13:24 2011 +0000
@@ -1,3 +1,23 @@
+2011-03-03	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* artifact-database/src/main/resources/sql/org-h2-driver.properties,
+	  artifact-database/src/main/resources/sql/org-postgresql-driver.properties:
+	  Added SQL statements to create collections.
+
+	* artifacts/src/main/java/de/intevation/artifacts/ArtifactCollectionFactory.java,
+	  artifacts/src/main/java/de/intevation/artifacts/ArtifactCollection.java:
+	  Added name to collection.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollection.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java:
+	  Adjusted factory api to cope with names.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Implemented create collection.
+
+	* database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	  Adjusted the backend call.
+
 2011-03-03  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Mar 03 12:13:24 2011 +0000
@@ -1103,8 +1103,11 @@
             throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
         }
 
+        // FIXME: fetch name from incoming document
+        String name = "Collection-" + new java.util.Date();
+
         ArtifactCollection ac = backend.createCollection(
-            ownerId, data, context, acf);
+            ownerId, name, acf, data, context);
 
         if (ac == null) {
             throw new ArtifactDatabaseException(COLLECTION_CREATION_FAILED);
--- 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;
     }
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollection.java	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollection.java	Thu Mar 03 12:13:24 2011 +0000
@@ -43,6 +43,11 @@
     protected String identifier;
 
     /**
+     * The identifier of the collection.
+     */
+    protected String name;
+
+    /**
      * The owner of this collection.
      */
     protected User user;
@@ -113,6 +118,23 @@
 
 
     /**
+     * Name of this collection.
+     * @return Returns the name of this collection
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * Name of this collection.
+     * @return Returns the name of this collection
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+
+    /**
      * Set a new owner of this collection.
      * @param user New owner for this collection.
      */
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactCollectionFactory.java	Thu Mar 03 12:13:24 2011 +0000
@@ -120,9 +120,10 @@
      */
     public ArtifactCollection createCollection(
         String   identifier,
-        Object   context,
-        Document data)
-    {
+        String   name,
+        Document data,
+        Object   context
+    ) {
         try {
             ArtifactCollection collection =
                 (ArtifactCollection) clazz.newInstance();
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Mar 03 12:13:24 2011 +0000
@@ -39,17 +39,22 @@
 users.select.all=SELECT id, gid, name, role FROM users
 
 # COLLECTIONS
+collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ')
 
 delete.user.collection.items= \
     DELETE FROM collection_items WHERE collection_id IN \
     (SELECT id FROM collections WHERE owner_id = ?)
 
+collections.insert= \
+    INSERT INTO collections (id, gid, name, owner_id, creation, last_access, ttl) \
+    VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)
+
 users.collections=SELECT collection_id, gid, name FROM collections WHERE owner_id = ?
 users.collection.ids=SELECT collection_id FROM collections WHERE owner_id = ?
 
 users.delete.all.collections=DELETE FROM collections WHERE owner_id = ?
 
-artifacts.in.one.collection.only=\
+artifacts.in.one.collection.only= \
     SELECT artifact_id FROM collection_items\
     WHERE collection_id = ? AND\
     artifact_id NOT IN (SELECT DISTINCT artifact_id FROM collection_items WHERE collection_id <> ?)
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Thu Mar 03 12:13:24 2011 +0000
@@ -39,11 +39,16 @@
 users.select.all=SELECT id, gid, name, role FROM users
 
 # COLLECTIONS
+collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ')
 
 delete.user.collection.items= \
     DELETE FROM collection_items WHERE collection_id IN \
     (SELECT id FROM collections WHERE owner_id = ?)
 
+collections.insert= \
+    INSERT INTO collections (id, gid, name, owner_id, creation, last_access, ttl) \
+    VALUES (?, ?::uuid, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)
+
 users.collections=SELECT collection_id, gid, name FROM collections WHERE owner_id = ?
 users.collection.ids=SELECT collection_id FROM collections WHERE owner_id = ?
 
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactCollection.java	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactCollection.java	Thu Mar 03 12:13:24 2011 +0000
@@ -31,6 +31,10 @@
      */
     String identifier();
 
+    String getName();
+
+    void setName(String name);
+
     Date getCreationTime();
 
     void setCreationTime(Date creationTime);
--- a/artifacts/src/main/java/de/intevation/artifacts/ArtifactCollectionFactory.java	Thu Mar 03 10:54:33 2011 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/ArtifactCollectionFactory.java	Thu Mar 03 12:13:24 2011 +0000
@@ -32,7 +32,11 @@
      * @param data  the data containing more details for the setup of an Artifact.
      * @return a new {@linkplain de.intevation.artifacts.ArtifactCollection ArtifactCollection}
      */
-    ArtifactCollection createCollection(String identifier, Object context, Document data);
+    ArtifactCollection createCollection(
+        String   identifier, 
+        String   name, 
+        Document data, 
+        Object   context);
 
     /**
      * Setup the factory with a given configuration

http://dive4elements.wald.intevation.org