comparison flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 1972:3c3e81fca092

Added a CollectionDescriptionHelper that helps generating DESCRIBE documents for Collections. flys-artifacts/trunk@3391 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 13 Dec 2011 08:43:48 +0000
parents 37a7b3841565
children 0b466bd4ab24
comparison
equal deleted inserted replaced
1971:741d2067cfe1 1972:3c3e81fca092
1 package de.intevation.flys.collections; 1 package de.intevation.flys.collections;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.io.OutputStream; 4 import java.io.OutputStream;
5 import java.util.ArrayList; 5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.HashMap; 6 import java.util.HashMap;
8 import java.util.List; 7 import java.util.List;
9 import java.util.Map; 8 import java.util.Map;
10 9
11 import javax.xml.xpath.XPathConstants; 10 import javax.xml.xpath.XPathConstants;
77 */ 76 */
78 @Override 77 @Override
79 public Document describe(CallContext context) { 78 public Document describe(CallContext context) {
80 log.debug("FLYSArtifactCollection.describe: " + identifier); 79 log.debug("FLYSArtifactCollection.describe: " + identifier);
81 80
82 Document doc = XMLUtils.newDocument(); 81 CollectionDescriptionHelper helper = new CollectionDescriptionHelper(
83 82 getName(), identifier(), getCreationTime(), getTTL(),
84 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( 83 context);
85 doc,
86 ArtifactNamespaceContext.NAMESPACE_URI,
87 ArtifactNamespaceContext.NAMESPACE_PREFIX);
88
89 Date creationTime = getCreationTime();
90 String creation = creationTime != null
91 ? Long.toString(creationTime.getTime())
92 : "";
93
94 Element collection = ec.create("artifact-collection");
95 Element artifacts = ec.create("artifacts");
96
97 ec.addAttr(collection, "name", getName(), true);
98 ec.addAttr(collection, "uuid", identifier(), true);
99 ec.addAttr(collection, "creation", creation, true);
100 ec.addAttr(collection, "ttl", String.valueOf(getTTL()), true);
101
102 collection.appendChild(artifacts);
103 doc.appendChild(collection);
104 84
105 ArtifactDatabase db = context.getDatabase(); 85 ArtifactDatabase db = context.getDatabase();
106 Document oldAttrs = getAttribute(); 86 Document oldAttrs = getAttribute();
107 87
108 try { 88 try {
109 String[] aUUIDs = getArtifactUUIDs(context); 89 String[] aUUIDs = getArtifactUUIDs(context);
110 Node newAttr = mergeAttributes(db, context, oldAttrs, aUUIDs); 90 Node newAttr = mergeAttributes(db, context, oldAttrs, aUUIDs);
111 91
112 collection.appendChild(doc.importNode(newAttr, true)); 92 helper.setAttribute(newAttr);
113 93
114 // Make it an empty array if null. 94 // Make it an empty array if null.
115 if (aUUIDs == null) { 95 if (aUUIDs == null) {
116 aUUIDs = new String[] {}; 96 aUUIDs = new String[] {};
117 } 97 }
118 98
119 for (String uuid: aUUIDs) { 99 for (String uuid: aUUIDs) {
120 try { 100 helper.addArtifact(uuid);
121 artifacts.appendChild(
122 buildArtifactNode(db, uuid, context, ec));
123 }
124 catch (ArtifactDatabaseException dbe) {
125 log.warn(dbe, dbe);
126 }
127 } 101 }
128 } 102 }
129 catch (ArtifactDatabaseException ade) { 103 catch (ArtifactDatabaseException ade) {
130 log.error(ade, ade); 104 log.error("Error while merging attribute documents.", ade);
131 105
132 collection.appendChild( 106 helper.setAttribute(oldAttrs);
133 doc.importNode(oldAttrs.getFirstChild(), true)); 107 }
134 } 108
109 Document doc = helper.toXML();
110 log.debug("HELPER DOC: " + XMLUtils.toString(doc));
135 111
136 return doc; 112 return doc;
137 } 113 }
138 114
139 115
352 log.error("ArtifactDatabaseException!", ade); 328 log.error("ArtifactDatabaseException!", ade);
353 } 329 }
354 330
355 return dataProviders; 331 return dataProviders;
356 } 332 }
357 333
334
358 /** 335 /**
359 * @return masterartifact or null if exception/not found. 336 * @return masterartifact or null if exception/not found.
360 */ 337 */
361 protected FLYSArtifact getMasterArtifact(CallContext context) 338 protected FLYSArtifact getMasterArtifact(CallContext context)
362 { 339 {
363 try { 340 try {
364 ArtifactDatabase db = context.getDatabase(); 341 ArtifactDatabase db = context.getDatabase();
365 CallMeta callMeta = context.getMeta(); 342 CallMeta callMeta = context.getMeta();
366 Document document = db.getCollectionsMasterArtifact( 343 Document document = db.getCollectionsMasterArtifact(
367 identifier(), callMeta); 344 identifier(), callMeta);
368 345
369 String masterUUID = XMLUtils.xpathString( 346 String masterUUID = XMLUtils.xpathString(
370 document, XPATH_MASTER_UUID, ArtifactNamespaceContext.INSTANCE); 347 document, XPATH_MASTER_UUID, ArtifactNamespaceContext.INSTANCE);
371 FLYSArtifact masterArtifact = 348 FLYSArtifact masterArtifact =
372 (FLYSArtifact) getArtifact(masterUUID, context); 349 (FLYSArtifact) getArtifact(masterUUID, context);
373 return masterArtifact; 350 return masterArtifact;
394 FLYSContext flysContext = FLYSUtils.getFlysContext(context); 371 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
395 StateEngine engine = (StateEngine) flysContext.get( 372 StateEngine engine = (StateEngine) flysContext.get(
396 FLYSContext.STATE_ENGINE_KEY); 373 FLYSContext.STATE_ENGINE_KEY);
397 374
398 FLYSArtifact masterArtifact = getMasterArtifact(context); 375 FLYSArtifact masterArtifact = getMasterArtifact(context);
399 376
400 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( 377 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
401 doc, 378 doc,
402 ArtifactNamespaceContext.NAMESPACE_URI, 379 ArtifactNamespaceContext.NAMESPACE_URI,
403 ArtifactNamespaceContext.NAMESPACE_PREFIX); 380 ArtifactNamespaceContext.NAMESPACE_PREFIX);
404 381
405 AttributeParser aParser = new AttributeParser(); 382 AttributeParser aParser = new AttributeParser();
406 OutputParser oParser = new OutputParser(db, context); 383 OutputParser oParser = new OutputParser(db, context);
407 384
408 if (uuids != null) { 385 if (uuids != null) {
409 for (String uuid: uuids) { 386 for (String uuid: uuids) {
410 try { 387 try {
411 oParser.parse(uuid); 388 oParser.parse(uuid);
412 } 389 }
828 return null; 805 return null;
829 } 806 }
830 807
831 808
832 /** 809 /**
833 * Create the Artifacts Node that contains outputmode and statedata.
834 * @param uuid uuid of the artifact.
835 */
836 protected Element buildArtifactNode(
837 ArtifactDatabase database,
838 String uuid,
839 CallContext context,
840 XMLUtils.ElementCreator ec)
841 throws ArtifactDatabaseException
842 {
843 log.debug("Append artifact '" + uuid + "' to collection description");
844
845 // TODO
846 String hash = "MYHASH";
847
848 Element ci = ec.create("artifact");
849 ec.addAttr(ci, "uuid", uuid, true);
850 ec.addAttr(ci, "hash", hash, true);
851
852 // XXX I am not sure if it works well every time with an empty document
853 // in the describe operation of an artifact.
854 Document description = database.describe(uuid, null, context.getMeta());
855
856 // Add outputmode element(s).
857 Node outputModes = (Node) XMLUtils.xpath(
858 description,
859 XPATH_ARTIFACT_OUTPUTMODES,
860 XPathConstants.NODE,
861 ArtifactNamespaceContext.INSTANCE);
862
863 if (outputModes != null) {
864 Document doc = ci.getOwnerDocument();
865 ci.appendChild(doc.importNode(outputModes, true));
866 }
867
868 // Add state-data element(s).
869 Node dataNode = ci.appendChild(
870 ci.getOwnerDocument().createElement("art:data-items"));
871
872 NodeList dataNodes = (NodeList) XMLUtils.xpath(
873 description,
874 XPATH_ARTIFACT_STATE_DATA,
875 XPathConstants.NODESET,
876 ArtifactNamespaceContext.INSTANCE);
877
878 if (dataNodes != null) {
879 Document doc = ci.getOwnerDocument();
880 for (int i = 0; i < dataNodes.getLength(); i++) {
881 dataNode.appendChild(doc.importNode(dataNodes.item(i), true));
882 }
883 }
884
885 return ci;
886 }
887
888
889 /**
890 * Inner class to structure/order the themes of a chart. 810 * Inner class to structure/order the themes of a chart.
891 */ 811 */
892 private static class ThemeList { 812 private static class ThemeList {
893 private Logger logger = Logger.getLogger(ThemeList.class); 813 private Logger logger = Logger.getLogger(ThemeList.class);
894 protected Map<Integer, ManagedFacet> themes; 814 protected Map<Integer, ManagedFacet> themes;

http://dive4elements.wald.intevation.org