diff 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
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Tue Dec 13 09:10:48 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Tue Dec 13 11:55:47 2011 +0000
@@ -83,11 +83,14 @@
             context);
 
         ArtifactDatabase db = context.getDatabase();
-        Document oldAttrs   = getAttribute();
+
+        Document        oldAttrs = getAttribute();
+        AttributeParser parser   = new AttributeParser(oldAttrs);
 
         try {
-            String[] aUUIDs = getArtifactUUIDs(context);
-            Node newAttr    = mergeAttributes(db, context, oldAttrs, aUUIDs);
+            String[]            aUUIDs  = getArtifactUUIDs(context);
+            CollectionAttribute newAttr = mergeAttributes(
+                db, context, parser, aUUIDs);
 
             helper.setAttribute(newAttr);
 
@@ -103,13 +106,10 @@
         catch (ArtifactDatabaseException ade) {
             log.error("Error while merging attribute documents.", ade);
 
-            helper.setAttribute(oldAttrs);
+            helper.setAttribute(parser.getCollectionAttribute());
         }
 
-        Document doc = helper.toXML();
-        log.debug("HELPER DOC: " + XMLUtils.toString(doc));
-
-        return doc;
+        return helper.toXML();
     }
 
 
@@ -119,16 +119,19 @@
      *
      * @param uuids Artifact uuids.
      */
-    protected Node mergeAttributes(
+    protected CollectionAttribute mergeAttributes(
         ArtifactDatabase db,
         CallContext      context,
-        Document         oldAttrs,
-        String[]         uuids)
-    {
-        Document doc = buildOutAttributes(db, context, oldAttrs, uuids);
-        Node newAttr = doc.getFirstChild();
+        AttributeParser  oldParser,
+        String[]         uuids
+    ) {
+        CollectionAttribute cAttribute =
+            buildOutAttributes(db, context, oldParser, uuids);
 
-        newAttr = mergeLoadedRecommendations(oldAttrs, newAttr);
+        cAttribute.setLoadedRecommendations(
+            getLoadedRecommendations(oldParser.getAttributeDocument()));
+
+        Document doc = cAttribute.toXML();
 
         try {
             // Save the merged document into database.
@@ -138,7 +141,7 @@
             log.error(adb, adb);
         }
 
-        return newAttr;
+        return cAttribute;
     }
 
 
@@ -148,19 +151,14 @@
      * because mergeAttributes() only merges the art:outputs nodes - all
      * other nodes are skiped.
      */
-    protected Node mergeLoadedRecommendations(Document oldAttrs, Node newAttrs){
+    protected Node getLoadedRecommendations(Document oldAttrs) {
         Element loadedRecoms = (Element) XMLUtils.xpath(
             oldAttrs,
             XPATH_LOADED_RECOMMENDATIONS,
             XPathConstants.NODE,
             ArtifactNamespaceContext.INSTANCE);
 
-        if (loadedRecoms != null) {
-            Document doc = newAttrs.getOwnerDocument();
-            newAttrs.appendChild(doc.importNode(loadedRecoms, true));
-        }
-
-        return newAttrs;
+        return loadedRecoms;
     }
 
 
@@ -360,10 +358,10 @@
      * Return merged output document.
      * @param uuids List of artifact uuids.
      */
-    protected Document buildOutAttributes(
+    protected CollectionAttribute buildOutAttributes(
         ArtifactDatabase db,
         CallContext      context,
-        Document         oldAttr,
+        AttributeParser  aParser,
         String[]         uuids)
     {
         Document doc = XMLUtils.newDocument();
@@ -379,8 +377,7 @@
             ArtifactNamespaceContext.NAMESPACE_URI,
             ArtifactNamespaceContext.NAMESPACE_PREFIX);
 
-        AttributeParser aParser = new AttributeParser();
-        OutputParser    oParser = new OutputParser(db, context);
+        OutputParser oParser = new OutputParser(db, context);
 
         if (uuids != null) {
             for (String uuid: uuids) {
@@ -392,8 +389,8 @@
                 }
             }
         }
-    
-        aParser.parse(oldAttr);
+
+        aParser.parse();
 
         return new AttributeWriter(
             db,
@@ -417,12 +414,14 @@
     protected Document getAttribute(CallContext context, String output)
     throws    ArtifactDatabaseException
     {
-        Document attr = buildOutAttributes(
+        CollectionAttribute cAttr = buildOutAttributes(
             context.getDatabase(),
             context,
-            getAttribute(),
+            new AttributeParser(getAttribute()),
             getArtifactUUIDs(context));
 
+        Document attr = cAttr.toXML();
+
         Node out = (Node) XMLUtils.xpath(
             attr,
             "art:attribute/art:outputs/art:output[@name='" + output + "']",

http://dive4elements.wald.intevation.org