changeset 167:c9c27aca2f70

Added code to list collections. artifacts/trunk@1392 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 03 Mar 2011 17:01:04 +0000
parents 89db80380f7f
children f70977cf2faf
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/resources/sql/org-h2-driver.properties artifact-database/src/main/resources/sql/org-postgresql-driver.properties
diffstat 5 files changed, 116 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Mar 03 14:49:09 2011 +0000
+++ b/ChangeLog	Thu Mar 03 17:01:04 2011 +0000
@@ -1,3 +1,15 @@
+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 list collections.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Added code to make collections accessible.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	  Bind backend functionality to REST.
+
 2011-03-03  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Mar 03 14:49:09 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Thu Mar 03 17:01:04 2011 +0000
@@ -1118,14 +1118,19 @@
         throws ArtifactDatabaseException
     {
         ArtifactCollectionFactory acf = getArtifactCollectionFactory();
+        UserFactory               uf  = getUserFactory();
 
-        if (acf == null) {
+        if (acf == null || uf == null) {
             throw new ArtifactDatabaseException(NO_SUCH_FACTORY);
         }
 
         logger.debug("Fetch the list of collection for user: " + userId);
 
-        ArtifactCollection [] ac = backend.listCollections(userId, acf);
+        ArtifactCollection [] ac = backend.listCollections(
+            userId,
+            null, // XXX: fetch from REST
+            acf, uf,
+            context);
 
         Document result = XMLUtils.newDocument();
 
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Mar 03 14:49:09 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Thu Mar 03 17:01:04 2011 +0000
@@ -141,6 +141,12 @@
     public static final String SQL_COLLECTIONS_INSERT =
         SQL.get("collections.insert");
 
+    public static final String SQL_COLLECTIONS_SELECT_USER =
+        SQL.get("collections.select.user");
+
+    public static final String SQL_COLLECTIONS_SELECT_ALL =
+        SQL.get("collections.select.all");
+
     /** The singleton.*/
     protected static Backend instance;
 
@@ -1198,10 +1204,79 @@
     }
 
     public ArtifactCollection [] listCollections(
-        String                    userId,
-        ArtifactCollectionFactory factory)
-    {
-        // TODO: Implement me!
+        String                    ownerIdentifier,
+        Document                  data,
+        ArtifactCollectionFactory collectionFactory,
+        UserFactory               userFactory,
+        Object                    context
+    ) {
+        if (ownerIdentifier != null 
+        && !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 {
+            ArrayList<ArtifactCollection> collections =
+                new ArrayList<ArtifactCollection>();
+            conn = dataSource.getConnection();
+
+            if (ownerIdentifier != null) {
+                stmnt = conn.prepareStatement(SQL_COLLECTIONS_SELECT_USER);
+                stmnt.setString(1, ownerIdentifier);
+            }
+            else {
+                stmnt = conn.prepareStatement(SQL_COLLECTIONS_SELECT_ALL);
+            }
+
+            result = stmnt.executeQuery();
+
+            while (result.next()) {
+                String collectionIdentifier = result.getString(1);
+                String collectionName       = result.getString(2);
+                long   creationTime         = result.getLong(3);
+                String userIdentifier       = result.getString(4);
+
+                ArtifactCollection collection =
+                    collectionFactory.createCollection(
+                        collectionIdentifier,
+                        collectionName,
+                        data,
+                        context);
+
+                if (userIdentifier != null) {
+                    collection.setUser(new LazyBackendUser(
+                        userIdentifier, userFactory, this, context));
+                }
+
+                collections.add(collection);
+            }
+
+            return collections.toArray(
+                new ArtifactCollection[collections.size()]);
+        }
+        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;
     }
 
--- a/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Mar 03 14:49:09 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties	Thu Mar 03 17:01:04 2011 +0000
@@ -49,6 +49,15 @@
     INSERT INTO collections (id, gid, name, owner_id, creation, last_access, ttl) \
     VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)
 
+collections.select.user= \
+    SELECT c.gid, c.name, c.creation, u.gid FROM \
+    collections c OUTER LEFT JOIN user u ON c.owner_id = u.id \
+    WHERE u.gid = ?
+
+collections.select.all= \
+    SELECT c.gid, c.name, c.creation, u.gid FROM \
+    collections c OUTER LEFT JOIN user u ON c.owner_id = u.id
+
 users.collections=SELECT collection_id, gid, name FROM collections WHERE owner_id = ?
 users.collection.ids=SELECT collection_id FROM collections WHERE owner_id = ?
 
--- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Thu Mar 03 14:49:09 2011 +0000
+++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties	Thu Mar 03 17:01:04 2011 +0000
@@ -49,6 +49,15 @@
     INSERT INTO collections (id, gid, name, owner_id, creation, last_access, ttl) \
     VALUES (?, ?::uuid, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?)
 
+collections.select.user= \
+    SELECT c.gid, c.name, c.creation, u.gid FROM \
+    collections c OUTER LEFT JOIN user u ON c.owner_id = u.id \
+    WHERE u.gid = ?::uuid
+
+collections.select.all= \
+    SELECT c.gid, c.name, c.creation, u.gid FROM \
+    collections c OUTER LEFT JOIN user u ON c.owner_id = u.id
+
 users.collections=SELECT collection_id, gid, name FROM collections WHERE owner_id = ?
 users.collection.ids=SELECT collection_id FROM collections WHERE owner_id = ?
 

http://dive4elements.wald.intevation.org