# HG changeset patch # User Sascha L. Teichmann # Date 1311946169 0 # Node ID 5b467bc27eda67c109355d1d5c897eaebb712026 # Parent c675b4a872011bf550e822ec0e3eec453bd120af Datacage: Store facets of outs into datacage db at initial scan. flys-artifacts/trunk@2424 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c675b4a87201 -r 5b467bc27eda flys-artifacts/ChangeLog --- a/flys-artifacts/ChangeLog Fri Jul 29 11:24:41 2011 +0000 +++ b/flys-artifacts/ChangeLog Fri Jul 29 13:29:29 2011 +0000 @@ -1,3 +1,13 @@ +2011-07-29 Sascha L. Teichmann + + * doc/conf/datacage.sql: Removed artifact_id from facet because there + is a link via out_id -> outs.id: artifact_id -> artifacts to + find the corresponding artifact. + + * src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java, + src/main/resources/datacage-sql/org-h2-driver.properties: Store + facets of outs into datacage db at initial scan. + 2011-07-29 Sascha L. Teichmann * src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java, diff -r c675b4a87201 -r 5b467bc27eda flys-artifacts/doc/conf/datacage.sql --- a/flys-artifacts/doc/conf/datacage.sql Fri Jul 29 11:24:41 2011 +0000 +++ b/flys-artifacts/doc/conf/datacage.sql Fri Jul 29 13:29:29 2011 +0000 @@ -57,11 +57,10 @@ CREATE TABLE facets ( id INT PRIMARY KEY NOT NULL, - artifact_id INT NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE, - out_id INT NOT NULL REFERENCES outs(id) ON DELETE CASCADE, + out_id INT NOT NULL REFERENCES outs(id) ON DELETE CASCADE, name VARCHAR(256) NOT NULL, - num INT NOT NULL, - state VARCHAR(256) NOT NULL, + num INT NOT NULL, + state VARCHAR(256) NOT NULL, description VARCHAR(256), UNIQUE (artifact_id, out_id, num, name) ); diff -r c675b4a87201 -r 5b467bc27eda 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 Fri Jul 29 11:24:41 2011 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java Fri Jul 29 13:29:29 2011 +0000 @@ -17,6 +17,7 @@ import de.intevation.artifactdatabase.data.StateData; import de.intevation.artifactdatabase.state.Output; +import de.intevation.artifactdatabase.state.Facet; import de.intevation.artifacts.Artifact; import de.intevation.artifacts.ArtifactDatabase; @@ -55,7 +56,8 @@ private String SQL_INSERT_ARTIFACT_DATA = "insert.artifact.data"; private String SQL_OUT_ID_NEXTVALUE = "out.id.nextval"; private String SQL_INSERT_OUT = "insert.out"; - + private String SQL_FACET_ID_NEXTVAL = "facet.id.nextval"; + private String SQL_INSERT_FACET = "insert.facet"; protected SQLExecutor sqlExecutor; @@ -194,29 +196,26 @@ return; } - final int [] ids = new int[outs.size()]; - - //final Map> facets = - //new TreeMap>(); + 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 < ids.length; ++i) { + for (int i = 0; i < outIds.length; ++i) { result = stmnt.executeQuery(); if (!result.next()) { log.error("generation of out ids failed"); return false; } - ids[i] = result.getInt(1); + outIds[i] = result.getInt(1); result.close(); result = null; } reset(); prepareStatement(SQL_INSERT_OUT); - for (int i = 0; i < ids.length; ++i) { + for (int i = 0; i < outIds.length; ++i) { Output out = outs.get(i); - stmnt.setInt(1, ids[i]); + stmnt.setInt(1, outIds[i]); stmnt.setInt(2, artifactId); stmnt.setString(3, out.getName()); setString(stmnt, 4, out.getDescription()); @@ -229,10 +228,55 @@ }; if (!exec.runWrite()) { - log.error("storing artifact outs/facets failed"); + log.error("storing artifact outs failed"); + return; } - // TODO: Store facets + 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( @@ -435,9 +479,19 @@ SQL_INSERT_ARTIFACT_DATA = sql.get(SQL_INSERT_ARTIFACT_DATA); SQL_OUT_ID_NEXTVALUE = sql.get(SQL_OUT_ID_NEXTVALUE); SQL_INSERT_OUT = sql.get(SQL_INSERT_OUT); + SQL_FACET_ID_NEXTVAL = sql.get(SQL_FACET_ID_NEXTVAL); + SQL_INSERT_FACET = sql.get(SQL_INSERT_FACET); } - private static void setString( + protected static final int numFacets(List outs) { + int sum = 0; + for (Output out: outs) { + sum += out.getFacets().size(); + } + return sum; + } + + protected static final void setString( PreparedStatement stmnt, int index, Object value diff -r c675b4a87201 -r 5b467bc27eda flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties --- a/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties Fri Jul 29 11:24:41 2011 +0000 +++ b/flys-artifacts/src/main/resources/datacage-sql/org-h2-driver.properties Fri Jul 29 13:29:29 2011 +0000 @@ -14,3 +14,5 @@ insert.artifact.data = INSERT INTO artifact_data (id, artifact_id, kind, k, v) VALUES (?, ?, ?, ?, ?) out.id.nextval = SELECT NEXTVAL('OUTS_ID_SEQ') insert.out = INSERT INTO outs (id, artifact_id, name, description, out_type) VALUES (?, ?, ?, ?, ?) +facet.id.nextval = SELECT NEXTVAL('FACETS_ID_SEQ') +insert.facet = INSERT INTO facets (id, out_id, name, num, state, description) VALUES (?, ?, ?, ?, ?, ?)