Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/collections/D4EArtifactCollection.java @ 6976:c47f1a1d0f0f
D4EArtifactCollection: Do not push CallContext and reference to db around.
Instead, store a reference initially.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 06 Sep 2013 15:44:02 +0200 |
parents | 8bcc120e64ae |
children | 0a337f0005c2 |
comparison
equal
deleted
inserted
replaced
6975:8bcc120e64ae | 6976:c47f1a1d0f0f |
---|---|
72 "/art:artifact-collection/art:artifact/@art:uuid"; | 72 "/art:artifact-collection/art:artifact/@art:uuid"; |
73 | 73 |
74 public static final String XPATH_LOADED_RECOMMENDATIONS = | 74 public static final String XPATH_LOADED_RECOMMENDATIONS = |
75 "/art:attribute/art:loaded-recommendations"; | 75 "/art:attribute/art:loaded-recommendations"; |
76 | 76 |
77 private CallContext context; | |
78 | |
79 private ArtifactDatabase db; | |
80 | |
81 protected CallContext getContext() { | |
82 return this.context; | |
83 } | |
84 | |
85 protected ArtifactDatabase getArtifactDB() { | |
86 return this.db; | |
87 } | |
88 | |
89 protected void setContext(CallContext context) { | |
90 this.context = context; | |
91 this.db = context.getDatabase(); | |
92 } | |
77 | 93 |
78 /** | 94 /** |
79 * Create and return description Document for this collection. | 95 * Create and return description Document for this collection. |
80 */ | 96 */ |
81 @Override | 97 @Override |
82 public Document describe(CallContext context) { | 98 public Document describe(CallContext context) { |
83 log.debug("D4EArtifactCollection.describe: " + identifier); | 99 log.debug("D4EArtifactCollection.describe: " + identifier); |
84 | 100 |
101 setContext(context); | |
102 | |
85 CollectionDescriptionHelper helper = new CollectionDescriptionHelper( | 103 CollectionDescriptionHelper helper = new CollectionDescriptionHelper( |
86 getName(), identifier(), getCreationTime(), getTTL(), | 104 getName(), identifier(), getCreationTime(), getTTL(), |
87 context); | 105 context); |
88 | 106 |
89 ArtifactDatabase db = context.getDatabase(); | |
90 | 107 |
91 Document oldAttrs = getAttribute(); | 108 Document oldAttrs = getAttribute(); |
92 AttributeParser parser = new AttributeParser(oldAttrs); | 109 AttributeParser parser = new AttributeParser(oldAttrs); |
93 | 110 |
94 try { | 111 try { |
95 String[] aUUIDs = getArtifactUUIDs(context); | 112 String[] aUUIDs = getArtifactUUIDs(); |
96 | 113 |
97 oldAttrs = removeAttributes(oldAttrs, context); | 114 oldAttrs = removeAttributes(oldAttrs); |
98 parser = new AttributeParser(oldAttrs); | 115 parser = new AttributeParser(oldAttrs); |
99 | 116 |
100 CollectionAttribute newAttr = mergeAttributes( | 117 CollectionAttribute newAttr = mergeAttributes(parser, aUUIDs); |
101 db, context, parser, aUUIDs); | 118 |
102 | 119 if (checkOutputSettings(newAttr)) { |
103 if (checkOutputSettings(newAttr, context)) { | 120 saveCollectionAttribute(newAttr); |
104 saveCollectionAttribute(db, context, newAttr); | |
105 } | 121 } |
106 | 122 |
107 helper.setAttribute(newAttr); | 123 helper.setAttribute(newAttr); |
108 | 124 |
109 if (aUUIDs != null) { | 125 if (aUUIDs != null) { |
127 * artifacts in the Collection. | 143 * artifacts in the Collection. |
128 * | 144 * |
129 * @param uuids Artifact uuids. | 145 * @param uuids Artifact uuids. |
130 */ | 146 */ |
131 protected CollectionAttribute mergeAttributes( | 147 protected CollectionAttribute mergeAttributes( |
132 ArtifactDatabase db, | |
133 CallContext context, | |
134 AttributeParser oldParser, | 148 AttributeParser oldParser, |
135 String[] uuids | 149 String[] uuids |
136 ) { | 150 ) { |
137 CollectionAttribute cAttribute = | 151 CollectionAttribute cAttribute = |
138 buildOutAttributes(db, context, oldParser, uuids); | 152 buildOutAttributes(oldParser, uuids); |
139 | 153 |
140 if (cAttribute == null) { | 154 if (cAttribute == null) { |
141 log.warn("mergeAttributes: cAttribute == null"); | 155 log.warn("mergeAttributes: cAttribute == null"); |
142 return null; | 156 return null; |
143 } | 157 } |
144 | 158 |
145 cAttribute.setLoadedRecommendations( | 159 cAttribute.setLoadedRecommendations( |
146 getLoadedRecommendations(oldParser.getAttributeDocument())); | 160 getLoadedRecommendations(oldParser.getAttributeDocument())); |
147 | 161 |
148 saveCollectionAttribute(db, context, cAttribute); | 162 saveCollectionAttribute(cAttribute); |
149 | 163 |
150 return cAttribute; | 164 return cAttribute; |
151 } | 165 } |
152 | 166 |
153 | 167 |
154 protected Document removeAttributes(Document attrs, CallContext context) { | 168 /** |
169 * Remove those output-elements which have a name that does | |
170 * not appear in master artifacts out-list. | |
171 * @param attr[in,out] Document to clean and return. | |
172 * @return param attr. | |
173 */ | |
174 protected Document removeAttributes(Document attrs) { | |
155 Node outs = (Node) XMLUtils.xpath( | 175 Node outs = (Node) XMLUtils.xpath( |
156 attrs, | 176 attrs, |
157 "/art:attribute/art:outputs", | 177 "/art:attribute/art:outputs", |
158 XPathConstants.NODE, | 178 XPathConstants.NODE, |
159 ArtifactNamespaceContext.INSTANCE); | 179 ArtifactNamespaceContext.INSTANCE); |
165 ArtifactNamespaceContext.INSTANCE); | 185 ArtifactNamespaceContext.INSTANCE); |
166 | 186 |
167 if (nodes != null) { | 187 if (nodes != null) { |
168 for (int i = 0; i < nodes.getLength(); i++) { | 188 for (int i = 0; i < nodes.getLength(); i++) { |
169 Element e = (Element)nodes.item(i); | 189 Element e = (Element)nodes.item(i); |
170 if(!outputExists(e.getAttribute("name"), context)) { | 190 if(!outputExists(e.getAttribute("name"))) { |
171 outs.removeChild(e); | 191 outs.removeChild(e); |
172 } | 192 } |
173 } | 193 } |
174 } | 194 } |
175 return attrs; | 195 return attrs; |
180 * True if current MasterArtifact has given output. | 200 * True if current MasterArtifact has given output. |
181 * @param name Name of the output of interest. | 201 * @param name Name of the output of interest. |
182 * @param context current context | 202 * @param context current context |
183 * @return true if current master artifact has given output. | 203 * @return true if current master artifact has given output. |
184 */ | 204 */ |
185 protected boolean outputExists(String name, CallContext context) { | 205 protected boolean outputExists(String name) { |
186 D4EArtifact master = getMasterArtifact(context); | 206 D4EArtifact master = getMasterArtifact(); |
187 List<Output> outList = master.getOutputs(context); | 207 List<Output> outList = master.getOutputs(getContext()); |
188 | 208 |
189 for (Output o : outList) { | 209 for (Output o : outList) { |
190 if (name.equals(o.getName())) { | 210 if (name.equals(o.getName())) { |
191 return true; | 211 return true; |
192 } | 212 } |
201 * database. | 221 * database. |
202 * | 222 * |
203 * @return true, if the transaction was successful, otherwise false. | 223 * @return true, if the transaction was successful, otherwise false. |
204 */ | 224 */ |
205 protected boolean saveCollectionAttribute( | 225 protected boolean saveCollectionAttribute( |
206 ArtifactDatabase db, | |
207 CallContext context, | |
208 CollectionAttribute attribute | 226 CollectionAttribute attribute |
209 ) { | 227 ) { |
210 log.info("Save new CollectionAttribute into database."); | 228 log.info("Save new CollectionAttribute into database."); |
211 | 229 |
212 Document doc = attribute.toXML(); | 230 Document doc = attribute.toXML(); |
213 | 231 |
214 try { | 232 try { |
215 // Save the merged document into database. | 233 // Save the merged document into database. |
216 db.setCollectionAttribute(identifier(), context.getMeta(), doc); | 234 getArtifactDB().setCollectionAttribute(identifier(), getContext().getMeta(), doc); |
217 | 235 |
218 log.info("Saving CollectionAttribute was successful."); | 236 log.info("Saving CollectionAttribute was successful."); |
219 | 237 |
220 return true; | 238 return true; |
221 } | 239 } |
252 * @param cc The CallContext. | 270 * @param cc The CallContext. |
253 * | 271 * |
254 * @return true, if the CollectionAttribute was modified, otherwise false. | 272 * @return true, if the CollectionAttribute was modified, otherwise false. |
255 */ | 273 */ |
256 protected boolean checkOutputSettings( | 274 protected boolean checkOutputSettings( |
257 CollectionAttribute attribute, | 275 CollectionAttribute attribute |
258 CallContext cc | |
259 ) { | 276 ) { |
260 boolean modified = false; | 277 boolean modified = false; |
261 | 278 |
262 Map<String, Output> outputMap = attribute != null | 279 Map<String, Output> outputMap = attribute != null |
263 ? attribute.getOutputs() | 280 ? attribute.getOutputs() |
279 Settings settings = output.getSettings(); | 296 Settings settings = output.getSettings(); |
280 | 297 |
281 if (settings == null) { | 298 if (settings == null) { |
282 log.debug("No Settings set for Output '" + outName + "'."); | 299 log.debug("No Settings set for Output '" + outName + "'."); |
283 output.setSettings( | 300 output.setSettings( |
284 createInitialOutputSettings(cc, attribute, outName)); | 301 createInitialOutputSettings(attribute, outName)); |
285 | 302 |
286 modified = true; | 303 modified = true; |
287 } | 304 } |
288 } | 305 } |
289 | 306 |
300 * @param out The name of the output. | 317 * @param out The name of the output. |
301 * | 318 * |
302 * @return a default Settings object for the specified Output. | 319 * @return a default Settings object for the specified Output. |
303 */ | 320 */ |
304 protected Settings createInitialOutputSettings( | 321 protected Settings createInitialOutputSettings( |
305 CallContext cc, | |
306 CollectionAttribute attr, | 322 CollectionAttribute attr, |
307 String out | 323 String out |
308 ) { | 324 ) { |
309 OutGenerator outGen = RiverContext.getOutGenerator(cc, out, null); | 325 OutGenerator outGen = RiverContext.getOutGenerator(getContext(), out, null); |
310 | 326 |
311 if (outGen == null) { | 327 if (outGen == null) { |
312 return null; | 328 return null; |
313 } | 329 } |
314 | 330 |
315 // XXX NOTE: the outGen is not able to process its generate() operation, | 331 // XXX NOTE: the outGen is not able to process its generate() operation, |
316 // because it has no OutputStream set! | 332 // because it has no OutputStream set! |
317 outGen.init(XMLUtils.newDocument(), null, cc); | 333 outGen.init(XMLUtils.newDocument(), null, getContext()); |
318 prepareMasterArtifact(outGen, cc); | 334 prepareMasterArtifact(outGen); |
319 | 335 |
320 try { | 336 try { |
321 Document outAttr = getAttribute(cc, attr, out); | 337 Document outAttr = getAttribute(attr, out); |
322 OutputHelper helper = new OutputHelper(identifier()); | 338 OutputHelper helper = new OutputHelper(identifier()); |
323 helper.doOut(outGen, out, out, outAttr, cc); | 339 helper.doOut(outGen, out, out, outAttr, getContext()); |
324 } | 340 } |
325 catch (ArtifactDatabaseException adbe) { | 341 catch (ArtifactDatabaseException adbe) { |
326 log.error(adbe, adbe); | 342 log.error(adbe, adbe); |
327 } | 343 } |
328 catch (IOException ioe) { | 344 catch (IOException ioe) { |
340 OutputStream out, | 356 OutputStream out, |
341 CallContext context) | 357 CallContext context) |
342 throws IOException | 358 throws IOException |
343 { | 359 { |
344 boolean debug = log.isDebugEnabled(); | 360 boolean debug = log.isDebugEnabled(); |
361 | |
362 setContext(context); | |
345 | 363 |
346 long reqBegin = System.currentTimeMillis(); | 364 long reqBegin = System.currentTimeMillis(); |
347 | 365 |
348 if (debug) { | 366 if (debug) { |
349 log.debug(XMLUtils.toString(format)); | 367 log.debug(XMLUtils.toString(format)); |
400 } | 418 } |
401 | 419 |
402 generator.init(format, out, context); | 420 generator.init(format, out, context); |
403 generator.setSettings(settings); | 421 generator.setSettings(settings); |
404 generator.setCollection(this); | 422 generator.setCollection(this); |
405 prepareMasterArtifact(generator, context); | 423 prepareMasterArtifact(generator); |
406 | 424 |
407 try { | 425 try { |
408 Document attr = getAttribute(context, cAttr, name); | 426 Document attr = getAttribute(cAttr, name); |
409 OutputHelper helper = new OutputHelper(identifier()); | 427 OutputHelper helper = new OutputHelper(identifier()); |
410 if (name.equals("sq_overview")) { | 428 if (name.equals("sq_overview")) { |
411 helper.doOut(generator, name, subtype, format, context); | 429 helper.doOut(generator, name, subtype, format, context); |
412 } | 430 } |
413 helper.doOut(generator, name, subtype, attr, context); | 431 helper.doOut(generator, name, subtype, attr, context); |
426 | 444 |
427 /** | 445 /** |
428 * Sets the master Artifact at the given <i>generator</i>. | 446 * Sets the master Artifact at the given <i>generator</i>. |
429 * | 447 * |
430 * @param generator The generator that gets a master Artifact. | 448 * @param generator The generator that gets a master Artifact. |
431 * @param cc The CallContext. | 449 */ |
432 */ | 450 protected void prepareMasterArtifact(OutGenerator generator |
433 protected void prepareMasterArtifact(OutGenerator generator, CallContext cc | |
434 ) { | 451 ) { |
435 // Get master artifact. | 452 // Get master artifact. |
436 D4EArtifact master = getMasterArtifact(cc); | 453 D4EArtifact master = getMasterArtifact(); |
437 if (master != null) { | 454 if (master != null) { |
438 log.debug("Set master Artifact to uuid: " + master.identifier()); | 455 log.debug("Set master Artifact to uuid: " + master.identifier()); |
439 generator.setMasterArtifact(master); | 456 generator.setMasterArtifact(master); |
440 } | 457 } |
441 else { | 458 else { |
445 | 462 |
446 | 463 |
447 /** | 464 /** |
448 * @return masterartifact or null if exception/not found. | 465 * @return masterartifact or null if exception/not found. |
449 */ | 466 */ |
450 protected D4EArtifact getMasterArtifact(CallContext context) | 467 protected D4EArtifact getMasterArtifact() |
451 { | 468 { |
452 try { | 469 try { |
453 ArtifactDatabase db = context.getDatabase(); | 470 ArtifactDatabase db = getArtifactDB(); |
454 CallMeta callMeta = context.getMeta(); | 471 CallMeta callMeta = getContext().getMeta(); |
455 Document document = db.getCollectionsMasterArtifact( | 472 Document document = db.getCollectionsMasterArtifact( |
456 identifier(), callMeta); | 473 identifier(), callMeta); |
457 | 474 |
458 String masterUUID = XMLUtils.xpathString( | 475 String masterUUID = XMLUtils.xpathString( |
459 document, XPATH_MASTER_UUID, ArtifactNamespaceContext.INSTANCE); | 476 document, XPATH_MASTER_UUID, ArtifactNamespaceContext.INSTANCE); |
460 D4EArtifact masterArtifact = | 477 D4EArtifact masterArtifact = |
461 (D4EArtifact) getArtifact(masterUUID, context); | 478 (D4EArtifact) getArtifact(masterUUID); |
462 return masterArtifact; | 479 return masterArtifact; |
463 } | 480 } |
464 catch (ArtifactDatabaseException ade) { | 481 catch (ArtifactDatabaseException ade) { |
465 log.error(ade, ade); | 482 log.error(ade, ade); |
466 } | 483 } |
471 /** | 488 /** |
472 * Return merged output document. | 489 * Return merged output document. |
473 * @param uuids List of artifact uuids. | 490 * @param uuids List of artifact uuids. |
474 */ | 491 */ |
475 protected CollectionAttribute buildOutAttributes( | 492 protected CollectionAttribute buildOutAttributes( |
476 ArtifactDatabase db, | |
477 CallContext context, | |
478 AttributeParser aParser, | 493 AttributeParser aParser, |
479 String[] uuids) | 494 String[] uuids) |
480 { | 495 { |
481 RiverContext flysContext = RiverUtils.getFlysContext(context); | 496 RiverContext flysContext = RiverUtils.getFlysContext(context); |
482 StateEngine engine = (StateEngine) flysContext.get( | 497 StateEngine engine = (StateEngine) flysContext.get( |
485 if (engine == null) { | 500 if (engine == null) { |
486 log.error("buildOutAttributes: engine == null"); | 501 log.error("buildOutAttributes: engine == null"); |
487 return null; | 502 return null; |
488 } | 503 } |
489 | 504 |
490 D4EArtifact masterArtifact = getMasterArtifact(context); | 505 D4EArtifact masterArtifact = getMasterArtifact(); |
491 | 506 |
492 if (masterArtifact == null) { | 507 if (masterArtifact == null) { |
493 log.debug("buildOutAttributes: masterArtifact == null"); | 508 log.debug("buildOutAttributes: masterArtifact == null"); |
494 return null; | 509 return null; |
495 } | 510 } |
496 | 511 |
497 OutputParser oParser = new OutputParser(db, context); | 512 OutputParser oParser = new OutputParser( |
513 getArtifactDB(), | |
514 getContext()); | |
498 | 515 |
499 if (uuids != null) { | 516 if (uuids != null) { |
500 for (String uuid: uuids) { | 517 for (String uuid: uuids) { |
501 try { | 518 try { |
502 oParser.parse(uuid); | 519 oParser.parse(uuid); |
508 } | 525 } |
509 | 526 |
510 aParser.parse(); | 527 aParser.parse(); |
511 | 528 |
512 AttributeWriter aWriter = new AttributeWriter( | 529 AttributeWriter aWriter = new AttributeWriter( |
513 db, | 530 getArtifactDB(), |
514 aParser.getCollectionAttribute(), | 531 aParser.getCollectionAttribute(), |
515 aParser.getOuts(), | 532 aParser.getOuts(), |
516 aParser.getFacets(), | 533 aParser.getFacets(), |
517 oParser.getOuts(), | 534 oParser.getOuts(), |
518 oParser.getFacets(), | 535 oParser.getFacets(), |
531 * @param output The name of the desired output type. | 548 * @param output The name of the desired output type. |
532 * | 549 * |
533 * @return the attribute for the desired output type. | 550 * @return the attribute for the desired output type. |
534 */ | 551 */ |
535 protected Document getAttribute( | 552 protected Document getAttribute( |
536 CallContext context, | |
537 CollectionAttribute cAttr, | 553 CollectionAttribute cAttr, |
538 String output) | 554 String output) |
539 throws ArtifactDatabaseException | 555 throws ArtifactDatabaseException |
540 { | 556 { |
541 Document attr = cAttr.toXML(); | 557 Document attr = cAttr.toXML(); |
570 * @param context The CallContext that is necessary to get information about | 586 * @param context The CallContext that is necessary to get information about |
571 * the ArtifactDatabase. | 587 * the ArtifactDatabase. |
572 * | 588 * |
573 * @return a list of uuids. | 589 * @return a list of uuids. |
574 */ | 590 */ |
575 protected String[] getArtifactUUIDs(CallContext context) | 591 protected String[] getArtifactUUIDs() |
576 throws ArtifactDatabaseException | 592 throws ArtifactDatabaseException |
577 { | 593 { |
578 log.debug("D4EArtifactCollection.getArtifactUUIDs"); | 594 log.debug("D4EArtifactCollection.getArtifactUUIDs"); |
579 | 595 |
580 ArtifactDatabase db = context.getDatabase(); | 596 ArtifactDatabase db = getArtifactDB(); |
581 CallMeta meta = context.getMeta(); | 597 CallMeta meta = getContext().getMeta(); |
582 | 598 |
583 Document itemList = db.listCollectionArtifacts(identifier(), meta); | 599 Document itemList = db.listCollectionArtifacts(identifier(), meta); |
584 NodeList items = (NodeList) XMLUtils.xpath( | 600 NodeList items = (NodeList) XMLUtils.xpath( |
585 itemList, | 601 itemList, |
586 XPATH_COLLECTION_ITEMS, | 602 XPATH_COLLECTION_ITEMS, |
617 * @param uuid The Artifact's uuid. | 633 * @param uuid The Artifact's uuid. |
618 * @param context The CallContext. | 634 * @param context The CallContext. |
619 * | 635 * |
620 * @return an Artifact. | 636 * @return an Artifact. |
621 */ | 637 */ |
622 protected Artifact getArtifact(String uuid, CallContext context) | 638 protected Artifact getArtifact(String uuid) |
623 throws ArtifactDatabaseException | 639 throws ArtifactDatabaseException |
624 { | 640 { |
625 log.debug("D4EArtifactCollection.getArtifact"); | 641 log.debug("D4EArtifactCollection.getArtifact"); |
626 | 642 |
627 Backend backend = Backend.getInstance(); | 643 Backend backend = Backend.getInstance(); |
637 * @param context The CallContext | 653 * @param context The CallContext |
638 * | 654 * |
639 * @return a list of artifacts matching this name. | 655 * @return a list of artifacts matching this name. |
640 */ | 656 */ |
641 public List<Artifact> getArtifactsByName(String name, CallContext context) | 657 public List<Artifact> getArtifactsByName(String name, CallContext context) |
658 { | |
659 setContext(context); | |
660 return getArtifactsByName(name); | |
661 } | |
662 | |
663 | |
664 /** | |
665 * Returns artifacts with name name. | |
666 * | |
667 * @param name The Artifact name to search | |
668 * | |
669 * @return a list of artifacts matching this name. | |
670 */ | |
671 protected List<Artifact> getArtifactsByName(String name) | |
642 { | 672 { |
643 log.debug("Searching for Artifacts: " + name); | 673 log.debug("Searching for Artifacts: " + name); |
644 List<Artifact> ret = new ArrayList<Artifact>(); | 674 List<Artifact> ret = new ArrayList<Artifact>(); |
645 try { | 675 try { |
646 for (String uuid: getArtifactUUIDs(context)) { | 676 for (String uuid: getArtifactUUIDs()) { |
647 D4EArtifact subArt = (D4EArtifact)getArtifact(uuid, context); | 677 D4EArtifact subArt = (D4EArtifact) getArtifact(uuid); |
648 if (subArt.getName() != null && subArt.getName().equals(name)) { | 678 if (subArt.getName() != null && subArt.getName().equals(name)) { |
649 ret.add(subArt); | 679 ret.add(subArt); |
650 } | 680 } |
651 } | 681 } |
652 } catch (ArtifactDatabaseException e) { | 682 } catch (ArtifactDatabaseException e) { |