diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 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 268c2972d4a7
children ac0f8bd97277
line wrap: on
line diff
--- 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;
     }
 

http://dive4elements.wald.intevation.org