Mercurial > dive4elements > river
changeset 1004:0e8c03b69627
Datacage: completed backend listener stuff.
flys-artifacts/trunk@2444 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 01 Aug 2011 20:35:43 +0000 |
parents | c58da6dd15ed |
children | 52949da92f7a |
files | flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties flys-artifacts/src/main/resources/datacage-sql/org-postgresql-driver.properties |
diffstat | 4 files changed, 262 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog Mon Aug 01 16:53:54 2011 +0000 +++ b/flys-artifacts/ChangeLog Mon Aug 01 20:35:43 2011 +0000 @@ -1,3 +1,17 @@ +2011-08-01 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + * src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java: + Completed the backend listener stuff. + + TODO I: Added some cleanup for orphaned artifacts. + TODO II: Figure out a way to delete collections/artifacts + which are delete from backend without the + backend API. + + * src/main/resources/datacage-sql/org-h2-driver.properties, + src/main/resources/datacage-sql/org-postgresql-driver.properties: + Added needed statements. + 2011-08-01 Sascha L. Teichmann <sascha.teichmann@intevation.de> * src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Mon Aug 01 16:53:54 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Mon Aug 01 20:35:43 2011 +0000 @@ -68,6 +68,15 @@ private String SQL_UPDATE_COLLECTION_NAME = "update.collection.name"; private String SQL_DELETE_ARTIFACT_FROM_COLLECTION = "delete.artifact.from.collection"; + private String SQL_DELETE_COLLECTION_BY_GID = + "delete.collection.by.gid"; + private String SQL_DELETE_USER_BY_GID = "delete.user.by.gid"; + private String SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID = + "delete.artifact.data.by.artifact.id"; + private String SQL_DELETE_OUTS_BY_ARTIFACT_ID = + "delete.outs.by.artifact.id"; + private String SQL_DELETE_FACETS_BY_ARTIFACT_ID = + "delete.facets.by.artifacts.id"; protected SQLExecutor sqlExecutor; @@ -355,6 +364,14 @@ SQL_UPDATE_COLLECTION_NAME = sql.get(SQL_UPDATE_COLLECTION_NAME); SQL_DELETE_ARTIFACT_FROM_COLLECTION = sql.get(SQL_DELETE_ARTIFACT_FROM_COLLECTION); + SQL_DELETE_COLLECTION_BY_GID = sql.get(SQL_DELETE_COLLECTION_BY_GID); + SQL_DELETE_USER_BY_GID = sql.get(SQL_DELETE_USER_BY_GID); + SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID = + sql.get(SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID); + SQL_DELETE_OUTS_BY_ARTIFACT_ID = + sql.get(SQL_DELETE_OUTS_BY_ARTIFACT_ID); + SQL_DELETE_FACETS_BY_ARTIFACT_ID = + sql.get(SQL_DELETE_FACETS_BY_ARTIFACT_ID); } protected static final int numFacets(List<Output> outs) { @@ -453,6 +470,7 @@ log.debug("createdArtifact"); if (!(artifact instanceof FLYSArtifact)) { + log.warn("need FLYSArtifact here"); return; } @@ -466,6 +484,7 @@ prepareStatement(SQL_ARTIFACT_ID_NEXTVAL); result = stmnt.executeQuery(); if (!result.next()) { + log.error("id generation for artifact failed"); return false; } res[0] = result.getInt(1); @@ -495,38 +514,207 @@ GlobalContext context ) { log.debug("storedArtifact"); + if (!(artifact instanceof FLYSArtifact)) { + log.warn("need FLYSArtifact here"); + return; + } + + final FLYSArtifact flys = (FLYSArtifact)artifact; + + final Integer [] res = new Integer[1]; + + // check first if artifact already exists + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_ARTIFACT_BY_GID); + result = stmnt.executeQuery(); + if (!result.next()) { + // new artifact + return true; + } + res[0] = result.getInt(1); + return true; + } + }; + + if (!exec.runRead()) { + log.error("querying artifact failed"); + return; + } + + if (res[0] == null) { // new artifact + createdArtifact(artifact, backend, context); + return; + } + + // artifact already exists -> delete old data + exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_DELETE_ARTIFACT_DATA_BY_ARTIFACT_ID); + stmnt.setInt(1, res[0]); + stmnt.execute(); + prepareStatement(SQL_DELETE_FACETS_BY_ARTIFACT_ID); + stmnt.setInt(1, res[0]); + stmnt.execute(); + prepareStatement(SQL_DELETE_OUTS_BY_ARTIFACT_ID); + stmnt.setInt(1, res[0]); + stmnt.execute(); + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("deleting old artifact data failed"); + return; + } + + // write new data + storeData(res[0], flys); + storeOuts(res[0], flys, context); } public void createdUser( - User user, + final User user, Backend backend, GlobalContext context ) { log.debug("createdUser"); + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_USER_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + log.error("id generation for user failed"); + return false; + } + int uId = result.getInt(1); + reset(); + prepareStatement(SQL_INSERT_USER); + stmnt.setInt(1, uId); + stmnt.setString(2, user.identifier()); + stmnt.execute(); + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("create user failed"); + } } public void deletedUser( - String identifier, + final String identifier, Backend backend, GlobalContext context ) { log.debug("deletedUser"); + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_DELETE_USER_BY_GID); + stmnt.setString(1, identifier); + stmnt.execute(); + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("delete user failed"); + } } public void createdCollection( - ArtifactCollection collection, - Backend backend, - GlobalContext context + final ArtifactCollection collection, + Backend backend, + GlobalContext context ) { log.debug("createdCollection"); + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + String userId = collection.getUser().identifier(); + prepareStatement(SQL_USER_BY_GID); + stmnt.setString(1, userId); + result = stmnt.executeQuery(); + int uId; + if (result.next()) { + uId = result.getInt(1); + reset(); + } + else { + // need to create user first + reset(); + prepareStatement(SQL_USER_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + log.error("id generation for user failed"); + return false; + } + uId = result.getInt(1); + reset(); + prepareStatement(SQL_INSERT_USER); + stmnt.setInt(1, uId); + stmnt.setString(2, userId); + stmnt.execute(); + conn.commit(); + reset(); + } + + prepareStatement(SQL_COLLECTION_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + log.error("id generation for collection failed"); + return false; + } + int cId = result.getInt(1); + reset(); + + String identifier = collection.identifier(); + String name = collection.getName(); + + prepareStatement(SQL_INSERT_COLLECTION); + stmnt.setInt(1, cId); + stmnt.setString(2, identifier); + stmnt.setInt(3, uId); + setString(stmnt, 4, name); + stmnt.execute(); + + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("create collection failed"); + } } public void deletedCollection( - String identifier, + final String identifier, Backend backend, GlobalContext context ) { log.debug("deletedCollection"); + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_DELETE_COLLECTION_BY_GID); + stmnt.setString(1, identifier); + stmnt.execute(); + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("delete collection failed"); + } } public void changedCollectionAttribute( @@ -549,12 +737,54 @@ } public void addedArtifactToCollection( - String artifactId, - String collectionId, + final String artifactId, + final String collectionId, Backend backend, GlobalContext context ) { log.debug("addedArtifactToCollection"); + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_ARTIFACT_BY_GID); + stmnt.setString(1, artifactId); + result = stmnt.executeQuery(); + if (!result.next()) { + return false; + } + int aId = result.getInt(1); + reset(); + + prepareStatement(SQL_COLLECTION_BY_GID); + stmnt.setString(1, collectionId); + result = stmnt.executeQuery(); + if (!result.next()) { + return false; + } + int cId = result.getInt(1); + reset(); + + prepareStatement(SQL_COLLECTION_ITEM_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + return false; + } + int ciId = result.getInt(1); + reset(); + + prepareStatement(SQL_INSERT_COLLECTION_ITEM); + stmnt.setInt(1, ciId); + stmnt.setInt(2, cId); + stmnt.setInt(3, aId); + stmnt.execute(); + + conn.commit(); + return true; + } + }; + if (!exec.runWrite()) { + log.error("added artifact to collection failed"); + } } public void removedArtifactFromCollection(
--- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties Mon Aug 01 16:53:54 2011 +0000 +++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties Mon Aug 01 20:35:43 2011 +0000 @@ -20,3 +20,8 @@ update.collection.name = UPDATE collections SET name = ? WHERE gid = ? delete.artifact.from.collection = DELETE FROM collection_items WHERE collection_id = ? AND artifact_id = ? +delete.collection.by.gid = DELETE FROM collections WHERE gid = ? +delete.user.by.gid = DELETE FROM user WHERE gid = ? +delete.artifact.data.by.artifact.id = DELETE FROM artifact_data WHERE artifact_id = ? +delete.outs.by.artifact.id = DELETE FROM outs WHERE artifact_id = ? +delete.facets.by.artifact.id = DELETE FROM facets WHERE out_id IN (SELECT id FROM outs WHERE artifact_id = ?)
--- a/flys-artifacts/src/main/resources/datacage-sql/org-postgresql-driver.properties Mon Aug 01 16:53:54 2011 +0000 +++ b/flys-artifacts/src/main/resources/datacage-sql/org-postgresql-driver.properties Mon Aug 01 20:35:43 2011 +0000 @@ -20,3 +20,8 @@ update.collection.name = UPDATE collections SET name = ? WHERE gid = ?::uuid delete.artifact.from.collection = DELETE FROM collection_items WHERE collection_id = ? AND artifact_id = ? +delete.collection.by.gid = DELETE FROM collections WHERE gid = ?::uuid +delete.user.by.gid = DELETE FROM user WHERE gid = ?::uuid +delete.artifact.data.by.artifact.id = DELETE FROM artifact_data WHERE artifact_id = ? +delete.outs.by.artifact.id = DELETE FROM outs WHERE artifact_id = ? +delete.facets.by.artifact.id = DELETE FROM facets WHERE out_id IN (SELECT id FROM outs WHERE artifact_id = ?)