diff flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java @ 346:16161de47662

The Attributes of a collection are written into its DESCRIBE now. flys-artifacts/trunk@1748 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 26 Apr 2011 13:29:18 +0000
parents 9a0e1289bab6
children a63d8bdb2d79
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Tue Apr 26 13:17:08 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Tue Apr 26 13:29:18 2011 +0000
@@ -73,25 +73,32 @@
 
         Element collection = ec.create("artifact-collection");
         Element artifacts  = ec.create("artifacts");
+        Element attributes = ec.create("attribute");
 
         ec.addAttr(collection, "name", getName(), true);
         ec.addAttr(collection, "uuid", identifier(), true);
         ec.addAttr(collection, "creation", creation,  true);
 
         collection.appendChild(artifacts);
+        collection.appendChild(attributes);
         doc.appendChild(collection);
 
-        Document attribute = getAttribute();
-        if (attribute != null) {
-            Node child = attribute.getFirstChild();
-            collection.appendChild(doc.importNode(child, true));
-        }
-
-        ArtifactDatabase db    = context.getDatabase();
+        ArtifactDatabase db = context.getDatabase();
 
         try {
             String[] artifactUUIDs = getArtifactUUIDs(context);
 
+            Document oldAttrs = getAttribute();
+            Document attrs    = buildAttributes(
+                db, context,
+                oldAttrs,
+                artifactUUIDs);
+
+            db.setCollectionAttribute(identifier(), context.getMeta(), attrs);
+
+            Node child = attrs.getFirstChild();
+            attributes.appendChild(doc.importNode(child, true));
+
             for (String uuid: artifactUUIDs) {
                 try {
                     artifacts.appendChild(
@@ -147,6 +154,37 @@
     }
 
 
+    protected Document buildAttributes(
+        ArtifactDatabase db,
+        CallContext      context,
+        Document         oldAttr,
+        String[]         items)
+    {
+        Document doc = XMLUtils.newDocument();
+
+        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
+            doc,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        AttributeParser aParser = new AttributeParser();
+        OutputParser    oParser = new OutputParser(db, context.getMeta());
+
+        for (String uuid: items) {
+            try {
+                oParser.parse(uuid);
+            }
+            catch (ArtifactDatabaseException ade) {
+                log.warn(ade, ade);
+            }
+        }
+
+        aParser.parse(oldAttr);
+
+        return new AttributeWriter(aParser.getOuts(), oParser.getOuts()).write();
+    }
+
+
     /**
      * This method returns the list of artifact UUIDs that this collections
      * contains.
@@ -324,17 +362,17 @@
     {
         log.debug("Append artifact '" + uuid + "' to collection description");
 
-        // XXX I am not sure if it works well every time with an empty document
-        // in the describe operation of an artifact.
-        Document description = database.describe(uuid, null, context.getMeta());
-
+        // TODO
         String hash = "MYHASH";
-        // TODO
 
         Element ci   = ec.create("artifact");
         ec.addAttr(ci, "uuid", uuid, true);
         ec.addAttr(ci, "hash", hash, true);
 
+        // XXX I am not sure if it works well every time with an empty document
+        // in the describe operation of an artifact.
+        Document description = database.describe(uuid, null, context.getMeta());
+
         Node outputModes = (Node) XMLUtils.xpath(
             description,
             XPATH_ARTIFACT_OUTPUTMODES,

http://dive4elements.wald.intevation.org