# HG changeset patch # User Sascha L. Teichmann # Date 1312216488 0 # Node ID db86c665ab6e543991f1dce6b91cb5fd56823702 # Parent a640944b5dd760a6d77cb101471918e3356e2a7c Datacage: Pass global context to datacage if calls come from backend listener. flys-artifacts/trunk@2442 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r a640944b5dd7 -r db86c665ab6e flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Mon Aug 01 15:58:37 2011 +0000 +++ b/flys-artifacts/ChangeLog Mon Aug 01 16:34:48 2011 +0000 @@ -1,3 +1,12 @@ +2011-08-01 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/artifacts/datacage/DatacageBackendListener.java: + Call datacage with the global context. This is needed to access the state engine. + + * src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java: Changed + signatures to take the global context, too. Create artifacts via backend listener + interface. + 2011-08-01 Sascha L. Teichmann * src/main/java/de/intevation/flys/artifacts/datacage/DatacageBackendListener.java: diff -r a640944b5dd7 -r db86c665ab6e flys-artifacts/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 15:58:37 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Mon Aug 01 16:34:48 2011 +0000 @@ -15,7 +15,6 @@ import de.intevation.artifactdatabase.db.SQLExecutor; import de.intevation.artifactdatabase.LifetimeListener; -import de.intevation.artifactdatabase.BackendListener; import de.intevation.artifactdatabase.Backend; import de.intevation.artifactdatabase.data.StateData; @@ -36,7 +35,7 @@ import org.w3c.dom.Document; public class Datacage -implements LifetimeListener, BackendListener +implements LifetimeListener { private static Logger log = Logger.getLogger(Datacage.class); @@ -191,153 +190,9 @@ storeData(aId, artifact); - storeOuts(aId, artifact); + storeOuts(aId, artifact, context); } - protected void storeOuts( - final int artifactId, - final FLYSArtifact artifact - ) { - final List outs = artifact.getCurrentOutputs(context); - - if (outs.isEmpty()) { - return; - } - - final int [] outIds = new int[outs.size()]; - - SQLExecutor.Instance exec = sqlExecutor.new Instance() { - @Override - public boolean doIt() throws SQLException { - prepareStatement(SQL_OUT_ID_NEXTVALUE); - for (int i = 0; i < outIds.length; ++i) { - result = stmnt.executeQuery(); - if (!result.next()) { - log.error("generation of out ids failed"); - return false; - } - outIds[i] = result.getInt(1); - result.close(); result = null; - } - reset(); - prepareStatement(SQL_INSERT_OUT); - for (int i = 0; i < outIds.length; ++i) { - Output out = outs.get(i); - stmnt.setInt(1, outIds[i]); - stmnt.setInt(2, artifactId); - stmnt.setString(3, out.getName()); - setString(stmnt, 4, out.getDescription()); - setString(stmnt, 5, out.getType()); - stmnt.execute(); - } - conn.commit(); - return true; - } - }; - - if (!exec.runWrite()) { - log.error("storing artifact outs failed"); - return; - } - - final int FACETS = numFacets(outs); - - if (FACETS == 0) { - return; - } - - exec = sqlExecutor.new Instance() { - @Override - public boolean doIt() throws SQLException { - int [] facetIds = new int[FACETS]; - prepareStatement(SQL_FACET_ID_NEXTVAL); - for (int i = 0; i < facetIds.length; ++i) { - result = stmnt.executeQuery(); - if (!result.next()) { - log.error("generation of facet ids failed"); - return false; - } - facetIds[i] = result.getInt(1); - result.close(); result = null; - } - reset(); - prepareStatement(SQL_INSERT_FACET); - int index = 0; - for (int i = 0, N = outs.size(); i < N; ++i) { - Output out = outs.get(i); - int outId = outIds[i]; - for (Facet facet: out.getFacets()) { - stmnt.setInt(1, facetIds[index]); - stmnt.setInt(2, outId); - stmnt.setString(3, facet.getName()); - stmnt.setInt(4, facet.getIndex()); - stmnt.setString(5, "XXX"); // TODO: handle states - setString(stmnt, 6, facet.getDescription()); - stmnt.execute(); - ++index; - } - } - conn.commit(); - return true; - } - }; - - if (!exec.runWrite()) { - log.error("storing facets failed"); - } - } - - protected void storeData( - final int artifactId, - FLYSArtifact artifact - ) { - final Collection data = artifact.getAllData(); - - if (data.isEmpty()) { - return; - } - - SQLExecutor.Instance exec = sqlExecutor.new Instance() { - @Override - public boolean doIt() throws SQLException { - int [] ids = new int[data.size()]; - prepareStatement(SQL_ARTIFACT_DATA_ID_NEXTVAL); - - for (int i = 0; i < ids.length; ++i) { - result = stmnt.executeQuery(); - if (!result.next()) { - log.error("generating id for artifact data failed"); - return false; - } - ids[i] = result.getInt(1); - result.close(); result = null; - } - reset(); - prepareStatement(SQL_INSERT_ARTIFACT_DATA); - - int i = 0; - for (StateData sd: data) { - int id = ids[i++]; - stmnt.setInt(1, id); - stmnt.setInt(2, artifactId); - // XXX: Where come the nulls from? - String type = sd.getType(); - if (type == null) type = "String"; - stmnt.setString(3, type); - stmnt.setString(4, sd.getName()); - setString(stmnt, 5, sd.getValue()); - stmnt.execute(); - } - - conn.commit(); - return true; - } - }; - - if (!exec.runWrite()) { - log.error("storing artifact data failed"); - } - } protected void storeCollectionItem( final Integer collectionId, @@ -580,87 +435,284 @@ log.debug("systemDown"); } - @Override public void setup(GlobalContext globalContext) { log.debug("setup"); } - @Override - public void createdArtifact(Artifact artifact, Backend backend) { + public void createdArtifact( + Artifact artifact, + Backend backend, + GlobalContext context + ) { log.debug("createdArtifact"); + + if (!(artifact instanceof FLYSArtifact)) { + return; + } + + final FLYSArtifact flys = (FLYSArtifact)artifact; + + final int [] res = new int[1]; + + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_ARTIFACT_ID_NEXTVAL); + result = stmnt.executeQuery(); + if (!result.next()) { + return false; + } + res[0] = result.getInt(1); + reset(); + prepareStatement(SQL_INSERT_ARTIFACT); + stmnt.setInt (1, res[0]); + stmnt.setString(2, flys.identifier()); + stmnt.setString(3, flys.getCurrentStateId()); + stmnt.execute(); + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("storing of artifact failed."); + return; + } + + storeData(res[0], flys); + storeOuts(res[0], flys, context); } - @Override - public void storedArtifact(Artifact artifact, Backend backend) { + public void storedArtifact( + Artifact artifact, + Backend backend, + GlobalContext context + ) { log.debug("storedArtifact"); } - @Override - public void createdUser(User user, Backend backend) { + public void createdUser( + User user, + Backend backend, + GlobalContext context + ) { log.debug("createdUser"); } - @Override - public void deletedUser(String identifier, Backend backend) { + public void deletedUser( + String identifier, + Backend backend, + GlobalContext context + ) { log.debug("deletedUser"); } - @Override public void createdCollection( ArtifactCollection collection, - Backend backend + Backend backend, + GlobalContext context ) { log.debug("createdCollection"); } - @Override - public void deletedCollection(String identifier, Backend backend) { + public void deletedCollection( + String identifier, + Backend backend, + GlobalContext context + ) { log.debug("deletedCollection"); } - @Override public void changedCollectionAttribute( - String identifier, - Document document, - Backend backend + String identifier, + Document document, + Backend backend, + GlobalContext context ) { log.debug("changedCollectionAttribute"); } - @Override public void changedCollectionItemAttribute( - String collectionId, - String artifactId, - Document document, - Backend backend + String collectionId, + String artifactId, + Document document, + Backend backend, + GlobalContext context ) { log.debug("changedCollectionItemAttribute"); } - @Override public void addedArtifactToCollection( - String artifactId, - String collectionId, - Backend backend + String artifactId, + String collectionId, + Backend backend, + GlobalContext context ) { log.debug("addedArtifactToCollection"); } - @Override public void removedArtifactFromCollection( - String artifactId, - String collectionId, - Backend backend + String artifactId, + String collectionId, + Backend backend, + GlobalContext context ) { log.debug("removedArtifactFromCollection"); } - @Override public void setCollectionName( - String collectionId, - String name + String collectionId, + String name, + GlobalContext context ) { log.debug("setCollectionName"); } + + protected void storeData( + final int artifactId, + FLYSArtifact artifact + ) { + final Collection data = artifact.getAllData(); + + if (data.isEmpty()) { + return; + } + + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + int [] ids = new int[data.size()]; + prepareStatement(SQL_ARTIFACT_DATA_ID_NEXTVAL); + + for (int i = 0; i < ids.length; ++i) { + result = stmnt.executeQuery(); + if (!result.next()) { + log.error("generating id for artifact data failed"); + return false; + } + ids[i] = result.getInt(1); + result.close(); result = null; + } + reset(); + prepareStatement(SQL_INSERT_ARTIFACT_DATA); + + int i = 0; + for (StateData sd: data) { + int id = ids[i++]; + stmnt.setInt(1, id); + stmnt.setInt(2, artifactId); + // XXX: Where come the nulls from? + String type = sd.getType(); + if (type == null) type = "String"; + stmnt.setString(3, type); + stmnt.setString(4, sd.getName()); + setString(stmnt, 5, sd.getValue()); + stmnt.execute(); + } + + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("storing artifact data failed"); + } + } + + protected void storeOuts( + final int artifactId, + final FLYSArtifact artifact, + GlobalContext context + ) { + final List outs = artifact.getCurrentOutputs(context); + + if (outs.isEmpty()) { + return; + } + + final int [] outIds = new int[outs.size()]; + + SQLExecutor.Instance exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + prepareStatement(SQL_OUT_ID_NEXTVALUE); + for (int i = 0; i < outIds.length; ++i) { + result = stmnt.executeQuery(); + if (!result.next()) { + log.error("generation of out ids failed"); + return false; + } + outIds[i] = result.getInt(1); + result.close(); result = null; + } + reset(); + prepareStatement(SQL_INSERT_OUT); + for (int i = 0; i < outIds.length; ++i) { + Output out = outs.get(i); + stmnt.setInt(1, outIds[i]); + stmnt.setInt(2, artifactId); + stmnt.setString(3, out.getName()); + setString(stmnt, 4, out.getDescription()); + setString(stmnt, 5, out.getType()); + stmnt.execute(); + } + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("storing artifact outs failed"); + return; + } + + final int FACETS = numFacets(outs); + + if (FACETS == 0) { + return; + } + + exec = sqlExecutor.new Instance() { + @Override + public boolean doIt() throws SQLException { + int [] facetIds = new int[FACETS]; + prepareStatement(SQL_FACET_ID_NEXTVAL); + for (int i = 0; i < facetIds.length; ++i) { + result = stmnt.executeQuery(); + if (!result.next()) { + log.error("generation of facet ids failed"); + return false; + } + facetIds[i] = result.getInt(1); + result.close(); result = null; + } + reset(); + prepareStatement(SQL_INSERT_FACET); + int index = 0; + for (int i = 0, N = outs.size(); i < N; ++i) { + Output out = outs.get(i); + int outId = outIds[i]; + for (Facet facet: out.getFacets()) { + stmnt.setInt(1, facetIds[index]); + stmnt.setInt(2, outId); + stmnt.setString(3, facet.getName()); + stmnt.setInt(4, facet.getIndex()); + stmnt.setString(5, "XXX"); // TODO: handle states + setString(stmnt, 6, facet.getDescription()); + stmnt.execute(); + ++index; + } + } + conn.commit(); + return true; + } + }; + + if (!exec.runWrite()) { + log.error("storing facets failed"); + } + } + } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r a640944b5dd7 -r db86c665ab6e flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/DatacageBackendListener.java --- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/DatacageBackendListener.java Mon Aug 01 15:58:37 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/DatacageBackendListener.java Mon Aug 01 16:34:48 2011 +0000 @@ -24,10 +24,10 @@ log.debug("new DatacageBackendListener"); } - protected BackendListener getListener() { + protected Datacage getDatacage() { Object listener = context.get(Datacage.DATACAGE_KEY); - return listener instanceof BackendListener - ? (BackendListener)listener + return listener instanceof Datacage + ? (Datacage)listener : null; } @@ -35,7 +35,7 @@ public void setup(GlobalContext context) { log.debug("setup"); this.context = context; - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { l.setup(context); } @@ -44,36 +44,36 @@ @Override public void createdArtifact(Artifact artifact, Backend backend) { log.debug("createdArtifact"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.createdArtifact(artifact, backend); + l.createdArtifact(artifact, backend, context); } } @Override public void storedArtifact(Artifact artifact, Backend backend) { log.debug("storedArtifact"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.storedArtifact(artifact, backend); + l.storedArtifact(artifact, backend, context); } } @Override public void createdUser(User user, Backend backend) { log.debug("createdUser"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.createdUser(user, backend); + l.createdUser(user, backend, context); } } @Override public void deletedUser(String identifier, Backend backend) { log.debug("deletedUser"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.deletedUser(identifier, backend); + l.deletedUser(identifier, backend, context); } } @@ -83,18 +83,18 @@ Backend backend ) { log.debug("createdCollection"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.createdCollection(collection, backend); + l.createdCollection(collection, backend, context); } } @Override public void deletedCollection(String identifier, Backend backend) { log.debug("deletedCollection"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.deletedCollection(identifier, backend); + l.deletedCollection(identifier, backend, context); } } @@ -105,9 +105,10 @@ Backend backend ) { log.debug("changedCollectionAttribute"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.changedCollectionAttribute(identifier, document, backend); + l.changedCollectionAttribute( + identifier, document, backend, context); } } @@ -119,10 +120,10 @@ Backend backend ) { log.debug("changedCollectionItemAttribute"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { l.changedCollectionItemAttribute( - collectionId, artifactId, document, backend); + collectionId, artifactId, document, backend, context); } } @@ -133,10 +134,10 @@ Backend backend ) { log.debug("addedArtifactToCollection"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { l.addedArtifactToCollection( - artifactId, collectionId, backend); + artifactId, collectionId, backend, context); } } @@ -147,10 +148,10 @@ Backend backend ) { log.debug("removedArtifactFromCollection"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { l.removedArtifactFromCollection( - artifactId, collectionId, backend); + artifactId, collectionId, backend, context); } } @@ -160,9 +161,9 @@ String name ) { log.debug("setCollectionName"); - BackendListener l = getListener(); + Datacage l = getDatacage(); if (l != null) { - l.setCollectionName(collectionId, name); + l.setCollectionName(collectionId, name, context); } } }