comparison flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 1976:0b466bd4ab24

Introduced a CollectionAttribute class that stores the information provided by the Collection's attribute document. flys-artifacts/trunk@3400 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 13 Dec 2011 11:55:47 +0000
parents 3c3e81fca092
children a7c437c9547e
comparison
equal deleted inserted replaced
1975:b30e1710df1d 1976:0b466bd4ab24
81 CollectionDescriptionHelper helper = new CollectionDescriptionHelper( 81 CollectionDescriptionHelper helper = new CollectionDescriptionHelper(
82 getName(), identifier(), getCreationTime(), getTTL(), 82 getName(), identifier(), getCreationTime(), getTTL(),
83 context); 83 context);
84 84
85 ArtifactDatabase db = context.getDatabase(); 85 ArtifactDatabase db = context.getDatabase();
86 Document oldAttrs = getAttribute(); 86
87 Document oldAttrs = getAttribute();
88 AttributeParser parser = new AttributeParser(oldAttrs);
87 89
88 try { 90 try {
89 String[] aUUIDs = getArtifactUUIDs(context); 91 String[] aUUIDs = getArtifactUUIDs(context);
90 Node newAttr = mergeAttributes(db, context, oldAttrs, aUUIDs); 92 CollectionAttribute newAttr = mergeAttributes(
93 db, context, parser, aUUIDs);
91 94
92 helper.setAttribute(newAttr); 95 helper.setAttribute(newAttr);
93 96
94 // Make it an empty array if null. 97 // Make it an empty array if null.
95 if (aUUIDs == null) { 98 if (aUUIDs == null) {
101 } 104 }
102 } 105 }
103 catch (ArtifactDatabaseException ade) { 106 catch (ArtifactDatabaseException ade) {
104 log.error("Error while merging attribute documents.", ade); 107 log.error("Error while merging attribute documents.", ade);
105 108
106 helper.setAttribute(oldAttrs); 109 helper.setAttribute(parser.getCollectionAttribute());
107 } 110 }
108 111
109 Document doc = helper.toXML(); 112 return helper.toXML();
110 log.debug("HELPER DOC: " + XMLUtils.toString(doc));
111
112 return doc;
113 } 113 }
114 114
115 115
116 /** 116 /**
117 * Merge the current art:outputs nodes with the the outputs provided by the 117 * Merge the current art:outputs nodes with the the outputs provided by the
118 * artifacts in the Collection. 118 * artifacts in the Collection.
119 * 119 *
120 * @param uuids Artifact uuids. 120 * @param uuids Artifact uuids.
121 */ 121 */
122 protected Node mergeAttributes( 122 protected CollectionAttribute mergeAttributes(
123 ArtifactDatabase db, 123 ArtifactDatabase db,
124 CallContext context, 124 CallContext context,
125 Document oldAttrs, 125 AttributeParser oldParser,
126 String[] uuids) 126 String[] uuids
127 { 127 ) {
128 Document doc = buildOutAttributes(db, context, oldAttrs, uuids); 128 CollectionAttribute cAttribute =
129 Node newAttr = doc.getFirstChild(); 129 buildOutAttributes(db, context, oldParser, uuids);
130 130
131 newAttr = mergeLoadedRecommendations(oldAttrs, newAttr); 131 cAttribute.setLoadedRecommendations(
132 getLoadedRecommendations(oldParser.getAttributeDocument()));
133
134 Document doc = cAttribute.toXML();
132 135
133 try { 136 try {
134 // Save the merged document into database. 137 // Save the merged document into database.
135 db.setCollectionAttribute(identifier(), context.getMeta(), doc); 138 db.setCollectionAttribute(identifier(), context.getMeta(), doc);
136 } 139 }
137 catch (ArtifactDatabaseException adb) { 140 catch (ArtifactDatabaseException adb) {
138 log.error(adb, adb); 141 log.error(adb, adb);
139 } 142 }
140 143
141 return newAttr; 144 return cAttribute;
142 } 145 }
143 146
144 147
145 /** 148 /**
146 * Merge the recommendations which have already been loaded from the old 149 * Merge the recommendations which have already been loaded from the old
147 * attribute document into the new attribute document. This is necessary, 150 * attribute document into the new attribute document. This is necessary,
148 * because mergeAttributes() only merges the art:outputs nodes - all 151 * because mergeAttributes() only merges the art:outputs nodes - all
149 * other nodes are skiped. 152 * other nodes are skiped.
150 */ 153 */
151 protected Node mergeLoadedRecommendations(Document oldAttrs, Node newAttrs){ 154 protected Node getLoadedRecommendations(Document oldAttrs) {
152 Element loadedRecoms = (Element) XMLUtils.xpath( 155 Element loadedRecoms = (Element) XMLUtils.xpath(
153 oldAttrs, 156 oldAttrs,
154 XPATH_LOADED_RECOMMENDATIONS, 157 XPATH_LOADED_RECOMMENDATIONS,
155 XPathConstants.NODE, 158 XPathConstants.NODE,
156 ArtifactNamespaceContext.INSTANCE); 159 ArtifactNamespaceContext.INSTANCE);
157 160
158 if (loadedRecoms != null) { 161 return loadedRecoms;
159 Document doc = newAttrs.getOwnerDocument();
160 newAttrs.appendChild(doc.importNode(loadedRecoms, true));
161 }
162
163 return newAttrs;
164 } 162 }
165 163
166 164
167 @Override 165 @Override
168 public void out( 166 public void out(
358 356
359 /** 357 /**
360 * Return merged output document. 358 * Return merged output document.
361 * @param uuids List of artifact uuids. 359 * @param uuids List of artifact uuids.
362 */ 360 */
363 protected Document buildOutAttributes( 361 protected CollectionAttribute buildOutAttributes(
364 ArtifactDatabase db, 362 ArtifactDatabase db,
365 CallContext context, 363 CallContext context,
366 Document oldAttr, 364 AttributeParser aParser,
367 String[] uuids) 365 String[] uuids)
368 { 366 {
369 Document doc = XMLUtils.newDocument(); 367 Document doc = XMLUtils.newDocument();
370 368
371 FLYSContext flysContext = FLYSUtils.getFlysContext(context); 369 FLYSContext flysContext = FLYSUtils.getFlysContext(context);
377 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( 375 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
378 doc, 376 doc,
379 ArtifactNamespaceContext.NAMESPACE_URI, 377 ArtifactNamespaceContext.NAMESPACE_URI,
380 ArtifactNamespaceContext.NAMESPACE_PREFIX); 378 ArtifactNamespaceContext.NAMESPACE_PREFIX);
381 379
382 AttributeParser aParser = new AttributeParser(); 380 OutputParser oParser = new OutputParser(db, context);
383 OutputParser oParser = new OutputParser(db, context);
384 381
385 if (uuids != null) { 382 if (uuids != null) {
386 for (String uuid: uuids) { 383 for (String uuid: uuids) {
387 try { 384 try {
388 oParser.parse(uuid); 385 oParser.parse(uuid);
390 catch (ArtifactDatabaseException ade) { 387 catch (ArtifactDatabaseException ade) {
391 log.warn(ade, ade); 388 log.warn(ade, ade);
392 } 389 }
393 } 390 }
394 } 391 }
395 392
396 aParser.parse(oldAttr); 393 aParser.parse();
397 394
398 return new AttributeWriter( 395 return new AttributeWriter(
399 db, 396 db,
400 aParser.getOuts(), 397 aParser.getOuts(),
401 aParser.getFacets(), 398 aParser.getFacets(),
415 * @return the attribute for the desired output type. 412 * @return the attribute for the desired output type.
416 */ 413 */
417 protected Document getAttribute(CallContext context, String output) 414 protected Document getAttribute(CallContext context, String output)
418 throws ArtifactDatabaseException 415 throws ArtifactDatabaseException
419 { 416 {
420 Document attr = buildOutAttributes( 417 CollectionAttribute cAttr = buildOutAttributes(
421 context.getDatabase(), 418 context.getDatabase(),
422 context, 419 context,
423 getAttribute(), 420 new AttributeParser(getAttribute()),
424 getArtifactUUIDs(context)); 421 getArtifactUUIDs(context));
422
423 Document attr = cAttr.toXML();
425 424
426 Node out = (Node) XMLUtils.xpath( 425 Node out = (Node) XMLUtils.xpath(
427 attr, 426 attr,
428 "art:attribute/art:outputs/art:output[@name='" + output + "']", 427 "art:attribute/art:outputs/art:output[@name='" + output + "']",
429 XPathConstants.NODE, 428 XPathConstants.NODE,

http://dive4elements.wald.intevation.org