diff flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.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
children 8378683fa07a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/AttributeWriter.java	Tue Apr 26 13:29:18 2011 +0000
@@ -0,0 +1,144 @@
+package de.intevation.flys.collections;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import de.intevation.artifacts.ArtifactNamespaceContext;
+
+import de.intevation.artifactdatabase.state.Facet;
+import de.intevation.artifactdatabase.state.Output;
+
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
+
+import de.intevation.flys.artifacts.model.ManagedFacet;
+
+
+public class AttributeWriter {
+
+    protected Map<String, Output> oldAttr;
+    protected Map<String, Output> newAttr;
+
+    private static Logger logger = Logger.getLogger(AttributeWriter.class);
+
+
+    public AttributeWriter(
+        Map<String, Output> oldAttr,
+        Map<String, Output> newAttr)
+    {
+        this.oldAttr = oldAttr;
+        this.newAttr = newAttr;
+    }
+
+
+    protected Document write() {
+        Document doc = XMLUtils.newDocument();
+
+        ElementCreator cr = new ElementCreator(
+            doc,
+            ArtifactNamespaceContext.NAMESPACE_URI,
+            ArtifactNamespaceContext.NAMESPACE_PREFIX);
+
+        Iterator<String> iter = newAttr.keySet().iterator();
+
+        while (iter.hasNext()) {
+            String outName = iter.next();
+
+            Output a = newAttr.get(outName);
+            Output b = oldAttr.get(outName);
+
+            writeOutput(doc, cr, a, b);
+        }
+
+        return doc;
+    }
+
+
+    protected void writeOutput(
+        Document       doc,
+        ElementCreator cr,
+        Output         a,
+        Output         b)
+    {
+        Element output = cr.create("output");
+        cr.addAttr(output, "name", a.getName());
+
+        doc.appendChild(output);
+
+        List<Facet> facetsA = a.getFacets();
+        List<Facet> facetsB = null;
+
+        if (b != null) {
+            facetsB = b.getFacets();
+        }
+
+        writeFacets(doc, cr, output, facetsA, facetsB);
+    }
+
+
+    protected void writeFacets(
+        Document       doc,
+        ElementCreator cr,
+        Element        output,
+        List<Facet>    a,
+        List<Facet>    b)
+    {
+        int num = a.size();
+
+        for (int i = 0; i < num; i++) {
+            ManagedFacet fA = (ManagedFacet) a.get(i);
+
+            if (!mergeFacets(doc, cr, output, fA, b)) {
+                writeFacet(doc, cr, output, fA);
+            }
+        }
+    }
+
+
+    protected boolean mergeFacets(
+        Document       doc,
+        ElementCreator cr,
+        Element        output,
+        ManagedFacet   a,
+        List<Facet>    list)
+    {
+        String name = a.getName();
+
+        if (list == null) {
+            return false;
+        }
+
+        for (Facet facet: list) {
+            if (name.equals(facet.getName())) {
+                writeFacet(doc, cr, output, (ManagedFacet) facet);
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
+    protected void writeFacet(
+        Document       doc,
+        ElementCreator cr,
+        Node           output,
+        ManagedFacet   f)
+    {
+        Element theme = cr.create("theme");
+        cr.addAttr(theme, "artifact", f.getArtifact(), true);
+        cr.addAttr(theme, "facet", f.getName(), true);
+        cr.addAttr(theme, "pos", Integer.toString(f.getPosition()), true);
+        cr.addAttr(theme, "active", Integer.toString(f.getActive()), true);
+
+        output.appendChild(theme);
+    }
+}
+

http://dive4elements.wald.intevation.org