Mercurial > dive4elements > framework
diff artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 176:a0eff2227588
Added code to add artifacts to collections.
artifacts/trunk@1402 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 04 Mar 2011 17:51:15 +0000 |
parents | 16e6e661e6bf |
children | 77cd37a93fca |
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java Fri Mar 04 17:11:40 2011 +0000 +++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java Fri Mar 04 17:51:15 2011 +0000 @@ -155,6 +155,15 @@ public static final String SQL_DELETE_COLLECTION = SQL.get("delete.collection"); + public static final String SQL_COLLECTION_CHECK_ARTIFACT = + SQL.get("collection.check.artifact"); + + public static final String SQL_COLLECTION_ITEMS_ID_NEXTVAL = + SQL.get("collection.check.artifact"); + + public static final String SQL_COLLECTION_ITEMS_INSERT = + SQL.get("collection.items.insert"); + /** The singleton.*/ protected static Backend instance; @@ -1113,11 +1122,87 @@ } public boolean addCollectionArtifact( - String collectionId, - String artifactId + final String collectionId, + final String artifactId, + final Document attribute ) { - // TODO: Implement me! - return false; + if (!StringUtils.checkUUID(collectionId)) { + logger.debug("Invalid collection id: '" + collectionId + "'"); + return false; + } + + if (!StringUtils.checkUUID(artifactId)) { + logger.debug("Invalid artifact id: '" + artifactId + "'"); + return false; + } + + SQLExecutor exec = new SQLExecutor() { + public boolean doIt() throws SQLException { + // fetch artifact id + prepareStatement(SQL_GET_ID); + stmnt.setString(1, artifactId); + result = stmnt.executeQuery(); + if (!result.next()) { + logger.debug("No such artifact: " + artifactId); + return false; + } + int aid = result.getInt(1); + reset(); + + // fetch collection id + prepareStatement(SQL_COLLECTIONS_ID_BY_GID); + stmnt.setString(1, collectionId); + result = stmnt.executeQuery(); + if (!result.next()) { + logger.debug("No such collection: " + collectionId); + } + int cid = result.getInt(1); + reset(); + + // check if artifact is already in collection + prepareStatement(SQL_COLLECTION_CHECK_ARTIFACT); + stmnt.setInt(1, aid); + stmnt.setInt(2, cid); + result = stmnt.executeQuery(); + if (result.next()) { + logger.debug("artifact already in collection"); + return false; + } + reset(); + + // fetch fresh id for new collection item + prepareStatement(SQL_COLLECTION_ITEMS_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + logger.debug("no collection item id generated"); + return false; + } + int ci_id = result.getInt(1); + reset(); + + // insert new collection item + prepareStatement(SQL_COLLECTION_ITEMS_INSERT); + stmnt.setInt(1, ci_id); + stmnt.setInt(2, cid); + stmnt.setInt(3, aid); + + byte [] data = attribute != null + ? XMLUtils.toByteArray(attribute) + : null; + + if (data == null) { + stmnt.setNull(4, Types.BINARY); + } + else { + stmnt.setBytes(4, data); + } + stmnt.execute(); + conn.commit(); + + return true; + } + }; + return exec.runWrite(); } public boolean removeCollectionArtifact(