# HG changeset patch # User Sascha L. Teichmann # Date 1299407236 0 # Node ID 535e4ea2ef9b1842270704c8d7b56c8f6df82e25 # Parent 77cd37a93fca2f26a77db20114de7fb8109414cd Added code to get the attribute of a collection item artifacts/trunk@1404 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 77cd37a93fca -r 535e4ea2ef9b ChangeLog --- a/ChangeLog Sun Mar 06 10:03:53 2011 +0000 +++ b/ChangeLog Sun Mar 06 10:27:16 2011 +0000 @@ -1,3 +1,13 @@ +2011-03-06 Sascha L. Teichmann + + * artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java: + Added code to get the attribute of a collection item. + + * artifact-database/src/main/resources/sql/org-h2-driver.properties, + artifact-database/src/main/resources/sql/org-postgresql-driver.properties: + Added SQL statements to get the attribute column of a collection item + given the collection and the artifact uuid. + 2011-03-06 Sascha L. Teichmann * artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java: diff -r 77cd37a93fca -r 535e4ea2ef9b artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java --- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java Sun Mar 06 10:03:53 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java Sun Mar 06 10:27:16 2011 +0000 @@ -164,6 +164,9 @@ public static final String SQL_COLLECTION_ITEMS_INSERT = SQL.get("collection.items.insert"); + public static final String SQL_COLLECTION_ITEM_GET_ATTRIBUTE = + SQL.get("collection.item.get.attribute"); + /** The singleton.*/ protected static Backend instance; @@ -1109,11 +1112,35 @@ } public Document getCollectionAttribute( - String collectionId, - String artifactId + final String collectionId, + final String artifactId ) { - // TODO: Implement me! - return null; + if (!isValidIdentifier(collectionId)) { + logger.debug("collection id is not valid: " + collectionId); + return null; + } + if (!isValidIdentifier(artifactId)) { + logger.debug("artifact id is not valid: " + artifactId); + } + + final Document [] document = new Document[1]; + + SQLExecutor exec = new SQLExecutor() { + public boolean doIt() throws SQLException { + prepareStatement(SQL_COLLECTION_ITEM_GET_ATTRIBUTE); + stmnt.setString(1, collectionId); + stmnt.setString(2, artifactId); + result = stmnt.executeQuery(); + if (!result.next()) { + logger.debug("No such collection item"); + return false; + } + document[0] = XMLUtils.fromByteArray(result.getBytes(1)); + return true; + } + }; + + return exec.runRead() ? document[0] : null; } public boolean setCollectionAttribute( diff -r 77cd37a93fca -r 535e4ea2ef9b artifact-database/src/main/resources/sql/org-h2-driver.properties --- a/artifact-database/src/main/resources/sql/org-h2-driver.properties Sun Mar 06 10:03:53 2011 +0000 +++ b/artifact-database/src/main/resources/sql/org-h2-driver.properties Sun Mar 06 10:27:16 2011 +0000 @@ -48,6 +48,12 @@ (id, collection_id, artifact_id, attribute) \ VALUES (?, ?, ?, ?) +collection.item.get.attribute= \ + SELECT ci.attribute FROM collections_items ci \ + INNER JOIN collection c ON ci.collection_id = c.id \ + INNER JOIN attributes a ON ci.artifact_id = a.id \ + WHERE c.gid = ? AND a.gid = ? + # COLLECTIONS collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ') diff -r 77cd37a93fca -r 535e4ea2ef9b artifact-database/src/main/resources/sql/org-postgresql-driver.properties --- a/artifact-database/src/main/resources/sql/org-postgresql-driver.properties Sun Mar 06 10:03:53 2011 +0000 +++ b/artifact-database/src/main/resources/sql/org-postgresql-driver.properties Sun Mar 06 10:27:16 2011 +0000 @@ -48,6 +48,12 @@ (id, collection_id, artifact_id, attribute) \ VALUES (?, ?, ?, ?) +collection.item.get.attribute= \ + SELECT ci.attribute FROM collections_items ci \ + INNER JOIN collection c ON ci.collection_id = c.id \ + INNER JOIN attributes a ON ci.artifact_id = a.id \ + WHERE c.gid = ?::uuid AND a.gid = ?::uuid + # COLLECTIONS collections.id.nextval=SELECT NEXTVAL('COLLECTIONS_ID_SEQ')