view flys-artifacts/src/main/java/de/intevation/flys/collections/CollectionAttribute.java @ 2089:0da8874bd378

Added initial state to map artifact to be able to advance and step back. The map artifact overrides describe() to have the complete UI information in the describe response document. flys-artifacts/trunk@3613 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 06 Jan 2012 12:02:10 +0000
parents ca6ccf722c24
children 2a8919e0ed28
line wrap: on
line source
package de.intevation.flys.collections;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.apache.log4j.Logger;

import de.intevation.artifacts.ArtifactNamespaceContext;

import de.intevation.artifacts.common.utils.XMLUtils;
import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;

import de.intevation.artifactdatabase.state.DefaultOutput;
import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifactdatabase.state.Output;
import de.intevation.artifactdatabase.state.Settings;


public class CollectionAttribute {

    private static final Logger logger =
        Logger.getLogger(CollectionAttribute.class);


    protected ElementCreator ec;

    protected Map<String, Output> outputMap;

    protected Node loadedRecommendations;


    public CollectionAttribute() {
    }


    public void addOutput(String key, Output output) {
        if (outputMap == null) {
            outputMap = new HashMap<String, Output>();
        }

        if (key != null && key.length() > 0 && output != null) {
            outputMap.put(
                key,
                new DefaultOutput(
                    output.getName(),
                    output.getDescription(),
                    output.getMimeType(),
                    new ArrayList<Facet>(),
                    output.getType()));
        }
    }


    public void cleanEmptyOutputs() {
        if (outputMap == null) {
            return;
        }

        List<String> removeUs = new ArrayList<String>();

        Set<Map.Entry<String, Output>> entries = outputMap.entrySet();
        for (Map.Entry<String, Output> entry: entries) {
            Output o = entry.getValue();

            List<Facet> facets = o.getFacets();
            if (facets == null || facets.isEmpty()) {
                removeUs.add(entry.getKey());
            }
        }

        for (String key: removeUs) {
            outputMap.remove(key);
        }
    }


    public void setSettings(String outputKey, Settings settings) {
        if (settings == null) {
            logger.warn("Tried to set empty Settings for '" + outputKey + "'");
            return;
        }

        if (outputMap == null) {
            logger.warn("Tried to add facet but no Outputs are existing yet.");
            return;
        }

        Output output = outputMap.get(outputKey);

        if (output == null) {
            logger.warn("Tried to add facet for unknown Output: " + outputKey);
            return;
        }

        output.setSettings(settings);
    }


    public void addFacet(String outputKey, Facet facet) {
        if (facet == null) {
            logger.warn("Tried to add empty facet.");
            return;
        }

        if (outputMap == null) {
            logger.warn("Tried to add facet but no Outputs are existing yet.");
            return;
        }

        Output output = outputMap.get(outputKey);

        if (output == null) {
            logger.warn("Tried to add facet for unknown Output: " + outputKey);
            return;
        }

        logger.debug("Add facet for '" + outputKey + "': " + facet.getName());
        output.addFacet(facet);
    }


    public void setLoadedRecommendations(Node loadedRecommendations) {
        // TODO Replace this Node with a Java class object.
        this.loadedRecommendations = loadedRecommendations;
    }


    public void clearFacets(String outputKey) {
        if (outputKey == null || outputKey.length() == 0) {
            logger.warn("Tried to clear Facets, but no Output key specified!");
            return;
        }

        if (outputMap == null) {
            logger.warn("Tried to clear Facets, but no Outputs existing!");
            return;
        }

        Output output = outputMap.get(outputKey);
        if (output == null) {
            logger.warn("Tried to clear Facets for unknown Out: " + outputKey);
            return;
        }

        output.setFacets(new ArrayList<Facet>());
    }


    public Document toXML() {
        Document doc = XMLUtils.newDocument();

        ec = new ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element root = ec.create("attribute");

        appendOutputs(root);
        appendLoadedRecommendations(root);

        doc.appendChild(root);

        return doc;
    }


    public Map<String, Output> getOutputs() {
        return outputMap;
    }


    public Output getOutput(String name) {
        if (name == null || name.length() == 0) {
            logger.warn("No Output name specified.");
            return null;
        }

        if (outputMap == null || outputMap.size() == 0) {
            logger.warn("Tried to retrieve Output, but no Outputs existing.");
            return null;
        }

        return outputMap.get(name);
    }


    public List<Facet> getFacets(String output) {
        if (output == null || output.length() == 0) {
            logger.warn("No Output name specified.");
            return new ArrayList<Facet>();
        }

        if (outputMap == null) {
            logger.warn("Tried to retrieve facets, but no Outputs existing.");
            return new ArrayList<Facet>();
        }

        Output o = outputMap.get(output);

        if (o == null) {
            logger.warn("No Output '" + output + "' existing.");
            return new ArrayList<Facet>();
        }

        return o.getFacets();
    }


    public List<Facet> getFacets() {
        List<Facet> allFacets = new ArrayList<Facet>();

        if (outputMap == null || outputMap.size() == 0) {
            logger.warn("No Outputs existing.");
            return allFacets;
        }

        Set<String> outputNames = outputMap.keySet();

        for (String outputName: outputNames) {
            allFacets.addAll(getFacets(outputName));
        }

        return allFacets;
    }


    protected void appendOutputs(Element root) {
        if (outputMap == null || outputMap.size() == 0) {
            logger.warn("No outputs to append.");
            return;
        }

        logger.debug("Append " + outputMap.size() + " Output Elements.");

        Element outputsEl = ec.create("outputs");

        Set<Map.Entry<String, Output>> entrySet = outputMap.entrySet();

        for (Map.Entry<String, Output> entry: entrySet) {
            appendOutput(outputsEl, entry.getKey(), entry.getValue());
        }

        root.appendChild(outputsEl);
    }


    protected void appendOutput(Element root, String name, Output output) {
        if (name == null || name.length() == 0 || output == null) {
            logger.warn("Tried to appendOutput, but Output is invalid.");
            return;
        }

        logger.debug("Append Output Element for '" + name + "'");

        Element outputEl = ec.create("output");
        ec.addAttr(outputEl, "name", name);

        appendSettings(outputEl, output.getSettings());
        appendFacets(outputEl, output.getFacets());

        root.appendChild(outputEl);
    }


    protected void appendSettings(Element root, Settings settings) {
        if (settings == null) {
            logger.warn("Tried to append Settings, but Settings is empty!");
            return;
        }

        settings.toXML(root);
    }


    protected void appendFacets(Element root, List<Facet> facets) {
        if (facets == null || facets.size() == 0) {
            logger.warn("Tried to append 0 Facets.");
            return;
        }

        Document owner = root.getOwnerDocument();

        logger.debug("Append " + facets.size() + " facets.");

        for (Facet facet: facets) {
            Node facetNode = facet.toXML(owner);

            if (facetNode != null) {
                root.appendChild(facetNode);
            }
        }
    }


    protected void appendLoadedRecommendations(Element root) {
        if (loadedRecommendations == null) {
            logger.debug("No loaded recommendations existing yet.");
            return;
        }

        Document owner = root.getOwnerDocument();

        root.appendChild(owner.importNode(loadedRecommendations, true));
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org