comparison flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 350:2465dc296395

The OutGenerator gets the theme of a facet while output creation. flys-artifacts/trunk@1754 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 28 Apr 2011 08:14:20 +0000
parents a63d8bdb2d79
children 8378683fa07a
comparison
equal deleted inserted replaced
349:6167ae622ce0 350:2465dc296395
22 import de.intevation.artifacts.ArtifactDatabaseException; 22 import de.intevation.artifacts.ArtifactDatabaseException;
23 import de.intevation.artifacts.ArtifactNamespaceContext; 23 import de.intevation.artifacts.ArtifactNamespaceContext;
24 import de.intevation.artifacts.CallContext; 24 import de.intevation.artifacts.CallContext;
25 import de.intevation.artifacts.CallMeta; 25 import de.intevation.artifacts.CallMeta;
26 26
27 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
27 import de.intevation.artifacts.common.utils.XMLUtils; 28 import de.intevation.artifacts.common.utils.XMLUtils;
28 29
29 import de.intevation.artifactdatabase.Backend; 30 import de.intevation.artifactdatabase.Backend;
30 import de.intevation.artifactdatabase.Backend.PersistentArtifact; 31 import de.intevation.artifactdatabase.Backend.PersistentArtifact;
31 import de.intevation.artifactdatabase.DefaultArtifactCollection; 32 import de.intevation.artifactdatabase.DefaultArtifactCollection;
32 33
33 import de.intevation.flys.artifacts.context.FLYSContext; 34 import de.intevation.flys.artifacts.context.FLYSContext;
34 import de.intevation.flys.artifacts.model.ManagedFacet; 35 import de.intevation.flys.artifacts.model.ManagedFacet;
35 import de.intevation.flys.exports.OutGenerator; 36 import de.intevation.flys.exports.OutGenerator;
37 import de.intevation.flys.themes.Theme;
38 import de.intevation.flys.themes.ThemeFactory;
36 39
37 40
38 /** 41 /**
39 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 42 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
40 */ 43 */
140 } 143 }
141 144
142 generator.init(format, out, context); 145 generator.init(format, out, context);
143 146
144 try { 147 try {
145 doOut(generator, getAttribute(context, name), context); 148 doOut(generator, name, getAttribute(context, name), context);
146 } 149 }
147 catch (ArtifactDatabaseException adbe) { 150 catch (ArtifactDatabaseException adbe) {
148 log.error(adbe, adbe); 151 log.error(adbe, adbe);
149 } 152 }
150 } 153 }
152 155
153 /** 156 /**
154 * This method creates the concrete output. 157 * This method creates the concrete output.
155 * 158 *
156 * @param generator The OutGenerator that creates the output. 159 * @param generator The OutGenerator that creates the output.
160 * @param outputName The name of the requested output.
157 * @param attributes The collection's attributes for this concrete output 161 * @param attributes The collection's attributes for this concrete output
158 * type. 162 * type.
159 * @param context The context object. 163 * @param context The context object.
160 */ 164 */
161 protected void doOut( 165 protected void doOut(
162 OutGenerator generator, 166 OutGenerator generator,
167 String outName,
163 Document attributes, 168 Document attributes,
164 CallContext context) 169 CallContext context)
165 throws IOException 170 throws IOException
166 { 171 {
167 log.debug("FLYSArtifactCollection.doOut"); 172 log.debug("FLYSArtifactCollection.doOut: " + outName);
168 173
169 ThemeList themeList = new ThemeList(attributes); 174 ThemeList themeList = new ThemeList(attributes);
170 175
171 int size = themeList.size(); 176 int size = themeList.size();
172 log.debug("Output will contain " + size + " elements."); 177 log.debug("Output will contain " + size + " elements.");
187 log.debug("... facet: " + theme.getName()); 192 log.debug("... facet: " + theme.getName());
188 } 193 }
189 194
190 generator.doOut( 195 generator.doOut(
191 getArtifact(art, context), 196 getArtifact(art, context),
192 getArtifactAttribute(art, context)); 197 getFacetThemeFromAttribute(
198 art,
199 outName,
200 theme.getName(),
201 context));
193 } 202 }
194 } 203 }
195 catch (ArtifactDatabaseException ade) { 204 catch (ArtifactDatabaseException ade) {
196 log.error(ade, ade); 205 log.error(ade, ade);
197 } 206 }
337 /** 346 /**
338 * Returns the attribute that belongs to an artifact stored in this 347 * Returns the attribute that belongs to an artifact stored in this
339 * collection. 348 * collection.
340 * 349 *
341 * @param uuid The Artifact's uuid. 350 * @param uuid The Artifact's uuid.
351 * @param outname The name of the requested output.
352 * @param facet The name of the requested facet.
342 * @param context The CallContext. 353 * @param context The CallContext.
343 * 354 *
344 * @return an attribute in form of a document. 355 * @return an attribute in form of a document.
345 */ 356 */
346 protected Document getArtifactAttribute(String uuid, CallContext context) 357 protected Document getFacetThemeFromAttribute(
358 String uuid,
359 String outName,
360 String facet,
361 CallContext context)
347 throws ArtifactDatabaseException 362 throws ArtifactDatabaseException
348 { 363 {
349 log.debug("FLYSArtifactCollection.getArtifactAttribute"); 364 log.debug("FLYSArtifactCollection.getFacetThemeFromAttribute");
350 365
351 // TODO FILL ME 366 ArtifactDatabase db = context.getDatabase();
352 return null; 367 CallMeta meta = context.getMeta();
368
369 FLYSContext flysContext = context instanceof FLYSContext
370 ? (FLYSContext) context
371 : (FLYSContext) context.globalContext();
372
373 Map<String, String> mappings = (Map<String, String>)
374 flysContext.get(FLYSContext.THEME_MAPPING);
375
376 String themeName = mappings.get(facet);
377
378 Document attr = db.getCollectionItemAttribute(identifier(), uuid, meta);
379
380 if (attr == null) {
381 attr = initItemAttribute(uuid, facet, context);
382
383 if (attr == null) {
384 return null;
385 }
386 }
387
388 log.debug("Search attribute of collection item: " + uuid);
389
390 Node tmp = (Node) XMLUtils.xpath(
391 attr,
392 "/art:attribute",
393 XPathConstants.NODE,
394 ArtifactNamespaceContext.INSTANCE);
395
396 if (tmp == null) {
397 log.warn("No attribute found. Operation failed.");
398 return null;
399 }
400
401 log.debug("Search theme '" + themeName + "' in attribute.");
402
403 Node theme = (Node) XMLUtils.xpath(
404 tmp,
405 "art:themes/theme[@name='" + themeName + "']",
406 XPathConstants.NODE,
407 ArtifactNamespaceContext.INSTANCE);
408
409 if (theme == null) {
410 log.warn("Could not find the theme in attribute of: " + uuid);
411
412 Theme t = getThemeForFacet(uuid, facet, context);
413
414 if (t == null) {
415 log.warn("No theme found for facet: " + facet);
416 return null;
417 }
418
419 addThemeToAttribute(uuid, attr, t, context);
420 theme = t.toXML().getFirstChild();
421 }
422
423 Document doc = XMLUtils.newDocument();
424 doc.appendChild(doc.importNode(theme, true));
425
426 return doc;
427 }
428
429
430 /**
431 * Adds the theme of a facet to a CollectionItem's attribute.
432 *
433 * @param uuid The uuid of the artifact.
434 * @param attr The current attribute of an artifact.
435 * @param t The theme to add.
436 * @param context The CallContext.
437 */
438 protected void addThemeToAttribute(
439 String uuid,
440 Document attr,
441 Theme t,
442 CallContext context)
443 {
444 log.debug("FLYSArtifactCollection.addThemeToAttribute: " + uuid);
445
446 if (t == null) {
447 log.warn("Theme is empty - cancel adding it to attribute!");
448 return;
449 }
450
451 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
452 attr,
453 ArtifactNamespaceContext.NAMESPACE_URI,
454 ArtifactNamespaceContext.NAMESPACE_PREFIX);
455
456 Node tmp = (Node) XMLUtils.xpath(
457 attr,
458 "/art:attribute",
459 XPathConstants.NODE,
460 ArtifactNamespaceContext.INSTANCE);
461
462 if (tmp == null) {
463 tmp = ec.create("attribute");
464 attr.appendChild(tmp);
465 }
466
467 Node themes = (Node) XMLUtils.xpath(
468 tmp,
469 "art:themes",
470 XPathConstants.NODE,
471 ArtifactNamespaceContext.INSTANCE);
472
473 if (themes == null) {
474 themes = ec.create("themes");
475 tmp.appendChild(themes);
476 }
477
478 themes.appendChild(attr.importNode(t.toXML().getFirstChild(), true));
479
480 try {
481 setCollectionItemAttribute(uuid, attr, context);
482
483 log.debug("Successfully added theme to item attribute.");
484 }
485 catch (ArtifactDatabaseException e) {
486 // do nothing
487 log.warn("Cannot set attribute of item: " + uuid);
488 }
489 }
490
491
492 /**
493 * Initializes the attribute of an collection item with the theme of a
494 * specific facet.
495 *
496 * @param uuid The uuid of an artifact.
497 * @param facet The name of a facet.
498 * @param context The CallContext.
499 *
500 * @param the new attribute.
501 */
502 protected Document initItemAttribute(
503 String uuid,
504 String facet,
505 CallContext context)
506 {
507 log.info("FLYSArtifactCollection.initItemAttribute");
508
509 Theme t = getThemeForFacet(uuid, facet, context);
510
511 if (t == null) {
512 log.info("Could not find theme for facet. Cancel initialization.");
513 return null;
514 }
515
516 Document attr = XMLUtils.newDocument();
517
518 addThemeToAttribute(uuid, attr, t, context);
519
520 return attr;
521 }
522
523
524 /**
525 * Sets the attribute of a CollectionItem specified by <i>uuid</i> to a new
526 * value <i>attr</i>.
527 *
528 * @param uuid The uuid of the CollectionItem.
529 * @param attr The new attribute for the CollectionItem.
530 * @param context The CallContext.
531 */
532 public void setCollectionItemAttribute(
533 String uuid,
534 Document attr,
535 CallContext context)
536 throws ArtifactDatabaseException
537 {
538 Document doc = ClientProtocolUtils.newSetItemAttributeDocument(
539 uuid,
540 attr);
541
542 if (doc == null) {
543 log.warn("Cannot set item attribute: No attribute found.");
544 return;
545 }
546
547 ArtifactDatabase db = context.getDatabase();
548 CallMeta meta = context.getMeta();
549
550 db.setCollectionItemAttribute(identifier(), uuid, doc, meta);
551 }
552
553
554 /**
555 * Returns the theme of a specific facet.
556 *
557 * @param uuid The uuid of an artifact.
558 * @param facet The name of the facet.
559 * @param context The CallContext object.
560 *
561 * @return the desired theme.
562 */
563 protected Theme getThemeForFacet(
564 String uuid,
565 String facet,
566 CallContext context)
567 {
568 log.info("FLYSArtifactCollection.getThemeForFacet: " + facet);
569
570 FLYSContext flysContext = context instanceof FLYSContext
571 ? (FLYSContext) context
572 : (FLYSContext) context.globalContext();
573
574 return ThemeFactory.getTheme(flysContext, facet);
353 } 575 }
354 576
355 577
356 /** 578 /**
357 * Returns the OutGenerator for a specified <i>type</i>. 579 * Returns the OutGenerator for a specified <i>type</i>.

http://dive4elements.wald.intevation.org