# HG changeset patch # User Sascha L. Teichmann # Date 1299171664 0 # Node ID c9c27aca2f706307eda419e9c857ed5fc2ed9154 # Parent 89db80380f7f5e0ac9b4768b75313ee0fcc72ef3 Added code to list collections. artifacts/trunk@1392 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 89db80380f7f -r c9c27aca2f70 ChangeLog --- 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 + + * 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 * artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java: diff -r 89db80380f7f -r c9c27aca2f70 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(); diff -r 89db80380f7f -r c9c27aca2f70 artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java --- 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 collections = + new ArrayList(); + 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; } diff -r 89db80380f7f -r c9c27aca2f70 artifact-database/src/main/resources/sql/org-h2-driver.properties --- 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 = ? diff -r 89db80380f7f -r c9c27aca2f70 artifact-database/src/main/resources/sql/org-postgresql-driver.properties --- 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 = ?