comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/Datacage.java @ 992:5b467bc27eda

Datacage: Store facets of outs into datacage db at initial scan. flys-artifacts/trunk@2424 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 29 Jul 2011 13:29:29 +0000
parents c675b4a87201
children aabcca7aeb6c
comparison
equal deleted inserted replaced
991:c675b4a87201 992:5b467bc27eda
15 import de.intevation.artifactdatabase.LifetimeListener; 15 import de.intevation.artifactdatabase.LifetimeListener;
16 16
17 import de.intevation.artifactdatabase.data.StateData; 17 import de.intevation.artifactdatabase.data.StateData;
18 18
19 import de.intevation.artifactdatabase.state.Output; 19 import de.intevation.artifactdatabase.state.Output;
20 import de.intevation.artifactdatabase.state.Facet;
20 21
21 import de.intevation.artifacts.Artifact; 22 import de.intevation.artifacts.Artifact;
22 import de.intevation.artifacts.ArtifactDatabase; 23 import de.intevation.artifacts.ArtifactDatabase;
23 import de.intevation.artifacts.ArtifactDatabaseException; 24 import de.intevation.artifacts.ArtifactDatabaseException;
24 25
53 private String SQL_INSERT_ARTIFACT = "insert.artifact"; 54 private String SQL_INSERT_ARTIFACT = "insert.artifact";
54 private String SQL_ARTIFACT_DATA_ID_NEXTVAL = "artifact.data.id.nextval"; 55 private String SQL_ARTIFACT_DATA_ID_NEXTVAL = "artifact.data.id.nextval";
55 private String SQL_INSERT_ARTIFACT_DATA = "insert.artifact.data"; 56 private String SQL_INSERT_ARTIFACT_DATA = "insert.artifact.data";
56 private String SQL_OUT_ID_NEXTVALUE = "out.id.nextval"; 57 private String SQL_OUT_ID_NEXTVALUE = "out.id.nextval";
57 private String SQL_INSERT_OUT = "insert.out"; 58 private String SQL_INSERT_OUT = "insert.out";
58 59 private String SQL_FACET_ID_NEXTVAL = "facet.id.nextval";
60 private String SQL_INSERT_FACET = "insert.facet";
59 61
60 protected SQLExecutor sqlExecutor; 62 protected SQLExecutor sqlExecutor;
61 63
62 public class InitialScan 64 public class InitialScan
63 implements ArtifactDatabase.ArtifactLoadedCallback 65 implements ArtifactDatabase.ArtifactLoadedCallback
192 194
193 if (outs.isEmpty()) { 195 if (outs.isEmpty()) {
194 return; 196 return;
195 } 197 }
196 198
197 final int [] ids = new int[outs.size()]; 199 final int [] outIds = new int[outs.size()];
198
199 //final Map<Integer, List<Facet>> facets =
200 //new TreeMap<Integer, List<Facet>>();
201 200
202 SQLExecutor.Instance exec = sqlExecutor.new Instance() { 201 SQLExecutor.Instance exec = sqlExecutor.new Instance() {
203 @Override 202 @Override
204 public boolean doIt() throws SQLException { 203 public boolean doIt() throws SQLException {
205 prepareStatement(SQL_OUT_ID_NEXTVALUE); 204 prepareStatement(SQL_OUT_ID_NEXTVALUE);
206 for (int i = 0; i < ids.length; ++i) { 205 for (int i = 0; i < outIds.length; ++i) {
207 result = stmnt.executeQuery(); 206 result = stmnt.executeQuery();
208 if (!result.next()) { 207 if (!result.next()) {
209 log.error("generation of out ids failed"); 208 log.error("generation of out ids failed");
210 return false; 209 return false;
211 } 210 }
212 ids[i] = result.getInt(1); 211 outIds[i] = result.getInt(1);
213 result.close(); result = null; 212 result.close(); result = null;
214 } 213 }
215 reset(); 214 reset();
216 prepareStatement(SQL_INSERT_OUT); 215 prepareStatement(SQL_INSERT_OUT);
217 for (int i = 0; i < ids.length; ++i) { 216 for (int i = 0; i < outIds.length; ++i) {
218 Output out = outs.get(i); 217 Output out = outs.get(i);
219 stmnt.setInt(1, ids[i]); 218 stmnt.setInt(1, outIds[i]);
220 stmnt.setInt(2, artifactId); 219 stmnt.setInt(2, artifactId);
221 stmnt.setString(3, out.getName()); 220 stmnt.setString(3, out.getName());
222 setString(stmnt, 4, out.getDescription()); 221 setString(stmnt, 4, out.getDescription());
223 setString(stmnt, 5, out.getType()); 222 setString(stmnt, 5, out.getType());
224 stmnt.execute(); 223 stmnt.execute();
227 return true; 226 return true;
228 } 227 }
229 }; 228 };
230 229
231 if (!exec.runWrite()) { 230 if (!exec.runWrite()) {
232 log.error("storing artifact outs/facets failed"); 231 log.error("storing artifact outs failed");
233 } 232 return;
234 233 }
235 // TODO: Store facets 234
235 final int FACETS = numFacets(outs);
236
237 if (FACETS == 0) {
238 return;
239 }
240
241 exec = sqlExecutor.new Instance() {
242 @Override
243 public boolean doIt() throws SQLException {
244 int [] facetIds = new int[FACETS];
245 prepareStatement(SQL_FACET_ID_NEXTVAL);
246 for (int i = 0; i < facetIds.length; ++i) {
247 result = stmnt.executeQuery();
248 if (!result.next()) {
249 log.error("generation of facet ids failed");
250 return false;
251 }
252 facetIds[i] = result.getInt(1);
253 result.close(); result = null;
254 }
255 reset();
256 prepareStatement(SQL_INSERT_FACET);
257 int index = 0;
258 for (int i = 0, N = outs.size(); i < N; ++i) {
259 Output out = outs.get(i);
260 int outId = outIds[i];
261 for (Facet facet: out.getFacets()) {
262 stmnt.setInt(1, facetIds[index]);
263 stmnt.setInt(2, outId);
264 stmnt.setString(3, facet.getName());
265 stmnt.setInt(4, facet.getIndex());
266 stmnt.setString(5, "XXX"); // TODO: handle states
267 setString(stmnt, 6, facet.getDescription());
268 stmnt.execute();
269 ++index;
270 }
271 }
272 conn.commit();
273 return true;
274 }
275 };
276
277 if (!exec.runWrite()) {
278 log.error("storing facets failed");
279 }
236 } 280 }
237 281
238 protected void storeData( 282 protected void storeData(
239 final int artifactId, 283 final int artifactId,
240 FLYSArtifact artifact 284 FLYSArtifact artifact
433 SQL_INSERT_ARTIFACT = sql.get(SQL_INSERT_ARTIFACT); 477 SQL_INSERT_ARTIFACT = sql.get(SQL_INSERT_ARTIFACT);
434 SQL_ARTIFACT_DATA_ID_NEXTVAL = sql.get(SQL_ARTIFACT_DATA_ID_NEXTVAL); 478 SQL_ARTIFACT_DATA_ID_NEXTVAL = sql.get(SQL_ARTIFACT_DATA_ID_NEXTVAL);
435 SQL_INSERT_ARTIFACT_DATA = sql.get(SQL_INSERT_ARTIFACT_DATA); 479 SQL_INSERT_ARTIFACT_DATA = sql.get(SQL_INSERT_ARTIFACT_DATA);
436 SQL_OUT_ID_NEXTVALUE = sql.get(SQL_OUT_ID_NEXTVALUE); 480 SQL_OUT_ID_NEXTVALUE = sql.get(SQL_OUT_ID_NEXTVALUE);
437 SQL_INSERT_OUT = sql.get(SQL_INSERT_OUT); 481 SQL_INSERT_OUT = sql.get(SQL_INSERT_OUT);
438 } 482 SQL_FACET_ID_NEXTVAL = sql.get(SQL_FACET_ID_NEXTVAL);
439 483 SQL_INSERT_FACET = sql.get(SQL_INSERT_FACET);
440 private static void setString( 484 }
485
486 protected static final int numFacets(List<Output> outs) {
487 int sum = 0;
488 for (Output out: outs) {
489 sum += out.getFacets().size();
490 }
491 return sum;
492 }
493
494 protected static final void setString(
441 PreparedStatement stmnt, 495 PreparedStatement stmnt,
442 int index, 496 int index,
443 Object value 497 Object value
444 ) 498 )
445 throws SQLException 499 throws SQLException

http://dive4elements.wald.intevation.org