ingo@1976: package de.intevation.flys.collections;
ingo@1976: 
ingo@1976: import java.util.ArrayList;
ingo@1976: import java.util.HashMap;
ingo@1976: import java.util.List;
ingo@1976: import java.util.Map;
ingo@1976: import java.util.Set;
ingo@1976: 
ingo@1976: import org.w3c.dom.Document;
ingo@1976: import org.w3c.dom.Element;
ingo@1976: import org.w3c.dom.Node;
ingo@1976: 
ingo@1976: import org.apache.log4j.Logger;
ingo@1976: 
ingo@1976: import de.intevation.artifacts.ArtifactNamespaceContext;
ingo@1976: 
ingo@1976: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@1976: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
ingo@1976: 
ingo@1976: import de.intevation.artifactdatabase.state.DefaultOutput;
ingo@1976: import de.intevation.artifactdatabase.state.Facet;
ingo@1976: import de.intevation.artifactdatabase.state.Output;
ingo@1979: import de.intevation.artifactdatabase.state.Settings;
ingo@1976: 
ingo@1976: 
ingo@1976: public class CollectionAttribute {
ingo@1976: 
ingo@1976:     private static final Logger logger =
ingo@1976:         Logger.getLogger(CollectionAttribute.class);
ingo@1976: 
ingo@1976: 
ingo@1976:     protected ElementCreator ec;
ingo@1976: 
ingo@1976:     protected Map<String, Output> outputMap;
ingo@1976: 
ingo@1976:     protected Node loadedRecommendations;
ingo@1976: 
ingo@1976: 
ingo@1976:     public CollectionAttribute() {
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     public void addOutput(String key, Output output) {
ingo@1976:         if (outputMap == null) {
ingo@1976:             outputMap = new HashMap<String, Output>();
ingo@1976:         }
ingo@1976: 
ingo@1976:         if (key != null && key.length() > 0 && output != null) {
ingo@1976:             outputMap.put(
ingo@1976:                 key,
ingo@1976:                 new DefaultOutput(
ingo@1976:                     output.getName(),
ingo@1976:                     output.getDescription(),
ingo@1976:                     output.getMimeType(),
ingo@1976:                     new ArrayList<Facet>(),
ingo@1976:                     output.getType()));
ingo@1976:         }
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@2079:     public void cleanEmptyOutputs() {
ingo@2079:         if (outputMap == null) {
ingo@2079:             return;
ingo@2079:         }
ingo@2079: 
ingo@2079:         List<String> removeUs = new ArrayList<String>();
ingo@2079: 
ingo@2079:         Set<Map.Entry<String, Output>> entries = outputMap.entrySet();
ingo@2079:         for (Map.Entry<String, Output> entry: entries) {
ingo@2079:             Output o = entry.getValue();
ingo@2079: 
ingo@2079:             List<Facet> facets = o.getFacets();
ingo@2079:             if (facets == null || facets.isEmpty()) {
ingo@2079:                 removeUs.add(entry.getKey());
ingo@2079:             }
ingo@2079:         }
ingo@2079: 
ingo@2079:         for (String key: removeUs) {
ingo@2079:             outputMap.remove(key);
ingo@2079:         }
ingo@2079:     }
ingo@2079: 
ingo@2079: 
ingo@1993:     public void setSettings(String outputKey, Settings settings) {
ingo@1993:         if (settings == null) {
ingo@1993:             logger.warn("Tried to set empty Settings for '" + outputKey + "'");
ingo@1993:             return;
ingo@1993:         }
ingo@1993: 
ingo@1993:         if (outputMap == null) {
ingo@1993:             logger.warn("Tried to add facet but no Outputs are existing yet.");
ingo@1993:             return;
ingo@1993:         }
ingo@1993: 
ingo@1993:         Output output = outputMap.get(outputKey);
ingo@1993: 
ingo@1993:         if (output == null) {
ingo@1993:             logger.warn("Tried to add facet for unknown Output: " + outputKey);
ingo@1993:             return;
ingo@1993:         }
ingo@1993: 
ingo@1993:         output.setSettings(settings);
ingo@1993:     }
ingo@1993: 
ingo@1993: 
ingo@1976:     public void addFacet(String outputKey, Facet facet) {
ingo@1976:         if (facet == null) {
ingo@1976:             logger.warn("Tried to add empty facet.");
ingo@1976:             return;
ingo@1976:         }
ingo@1976: 
ingo@1976:         if (outputMap == null) {
ingo@1976:             logger.warn("Tried to add facet but no Outputs are existing yet.");
ingo@1976:             return;
ingo@1976:         }
ingo@1976: 
ingo@1976:         Output output = outputMap.get(outputKey);
ingo@1976: 
ingo@1976:         if (output == null) {
ingo@1976:             logger.warn("Tried to add facet for unknown Output: " + outputKey);
ingo@1976:             return;
ingo@1976:         }
ingo@1976: 
ingo@1976:         logger.debug("Add facet for '" + outputKey + "': " + facet.getName());
ingo@1976:         output.addFacet(facet);
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     public void setLoadedRecommendations(Node loadedRecommendations) {
ingo@1976:         // TODO Replace this Node with a Java class object.
ingo@1976:         this.loadedRecommendations = loadedRecommendations;
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1993:     public void clearFacets(String outputKey) {
ingo@1993:         if (outputKey == null || outputKey.length() == 0) {
ingo@1993:             logger.warn("Tried to clear Facets, but no Output key specified!");
ingo@1993:             return;
ingo@1993:         }
ingo@1993: 
ingo@1993:         if (outputMap == null) {
ingo@1993:             logger.warn("Tried to clear Facets, but no Outputs existing!");
ingo@1993:             return;
ingo@1993:         }
ingo@1993: 
ingo@1993:         Output output = outputMap.get(outputKey);
ingo@1993:         if (output == null) {
ingo@1993:             logger.warn("Tried to clear Facets for unknown Out: " + outputKey);
ingo@1993:             return;
ingo@1993:         }
ingo@1993: 
ingo@1993:         output.setFacets(new ArrayList<Facet>());
ingo@1993:     }
ingo@1993: 
ingo@1993: 
ingo@1976:     public Document toXML() {
ingo@1976:         Document doc = XMLUtils.newDocument();
ingo@1976: 
ingo@1976:         ec = new ElementCreator(
ingo@1976:             doc,
ingo@1976:             ArtifactNamespaceContext.NAMESPACE_URI,
ingo@1976:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@1976: 
ingo@1976:         Element root = ec.create("attribute");
ingo@1976: 
ingo@1976:         appendOutputs(root);
ingo@1976:         appendLoadedRecommendations(root);
ingo@1976: 
ingo@1976:         doc.appendChild(root);
ingo@1976: 
ingo@1976:         return doc;
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     public Map<String, Output> getOutputs() {
ingo@1976:         return outputMap;
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     public Output getOutput(String name) {
ingo@1976:         if (name == null || name.length() == 0) {
ingo@1976:             logger.warn("No Output name specified.");
ingo@1976:             return null;
ingo@1976:         }
ingo@1976: 
ingo@1976:         if (outputMap == null || outputMap.size() == 0) {
ingo@1976:             logger.warn("Tried to retrieve Output, but no Outputs existing.");
ingo@1976:             return null;
ingo@1976:         }
ingo@1976: 
ingo@1976:         return outputMap.get(name);
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     public List<Facet> getFacets(String output) {
ingo@1976:         if (output == null || output.length() == 0) {
ingo@1976:             logger.warn("No Output name specified.");
ingo@1976:             return new ArrayList<Facet>();
ingo@1976:         }
ingo@1976: 
ingo@1976:         if (outputMap == null) {
ingo@1976:             logger.warn("Tried to retrieve facets, but no Outputs existing.");
ingo@1976:             return new ArrayList<Facet>();
ingo@1976:         }
ingo@1976: 
ingo@1976:         Output o = outputMap.get(output);
ingo@1976: 
ingo@1976:         if (o == null) {
ingo@1976:             logger.warn("No Output '" + output + "' existing.");
ingo@1976:             return new ArrayList<Facet>();
ingo@1976:         }
ingo@1976: 
ingo@1976:         return o.getFacets();
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     public List<Facet> getFacets() {
ingo@1976:         List<Facet> allFacets = new ArrayList<Facet>();
ingo@1976: 
ingo@1976:         if (outputMap == null || outputMap.size() == 0) {
ingo@1976:             logger.warn("No Outputs existing.");
ingo@1976:             return allFacets;
ingo@1976:         }
ingo@1976: 
ingo@1976:         Set<String> outputNames = outputMap.keySet();
ingo@1976: 
ingo@1976:         for (String outputName: outputNames) {
ingo@1976:             allFacets.addAll(getFacets(outputName));
ingo@1976:         }
ingo@1976: 
ingo@1976:         return allFacets;
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     protected void appendOutputs(Element root) {
ingo@1976:         if (outputMap == null || outputMap.size() == 0) {
ingo@1976:             logger.warn("No outputs to append.");
ingo@1976:             return;
ingo@1976:         }
ingo@1976: 
ingo@1976:         logger.debug("Append " + outputMap.size() + " Output Elements.");
ingo@1976: 
ingo@1976:         Element outputsEl = ec.create("outputs");
ingo@1976: 
ingo@1976:         Set<Map.Entry<String, Output>> entrySet = outputMap.entrySet();
ingo@1976: 
ingo@1976:         for (Map.Entry<String, Output> entry: entrySet) {
ingo@1976:             appendOutput(outputsEl, entry.getKey(), entry.getValue());
ingo@1976:         }
ingo@1976: 
ingo@1976:         root.appendChild(outputsEl);
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     protected void appendOutput(Element root, String name, Output output) {
ingo@1976:         if (name == null || name.length() == 0 || output == null) {
ingo@1976:             logger.warn("Tried to appendOutput, but Output is invalid.");
ingo@1976:             return;
ingo@1976:         }
ingo@1976: 
ingo@1976:         logger.debug("Append Output Element for '" + name + "'");
ingo@1976: 
ingo@1976:         Element outputEl = ec.create("output");
ingo@1976:         ec.addAttr(outputEl, "name", name);
ingo@1976: 
ingo@1979:         appendSettings(outputEl, output.getSettings());
ingo@1976:         appendFacets(outputEl, output.getFacets());
ingo@1976: 
ingo@1976:         root.appendChild(outputEl);
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1979:     protected void appendSettings(Element root, Settings settings) {
ingo@1979:         if (settings == null) {
ingo@1979:             logger.warn("Tried to append Settings, but Settings is empty!");
ingo@1979:             return;
ingo@1979:         }
ingo@1979: 
ingo@1979:         settings.toXML(root);
ingo@1979:     }
ingo@1979: 
ingo@1979: 
ingo@1976:     protected void appendFacets(Element root, List<Facet> facets) {
ingo@1976:         if (facets == null || facets.size() == 0) {
ingo@1976:             logger.warn("Tried to append 0 Facets.");
ingo@1976:             return;
ingo@1976:         }
ingo@1976: 
ingo@1976:         Document owner = root.getOwnerDocument();
ingo@1976: 
ingo@1976:         logger.debug("Append " + facets.size() + " facets.");
ingo@1976: 
ingo@1976:         for (Facet facet: facets) {
ingo@1976:             Node facetNode = facet.toXML(owner);
ingo@1976: 
ingo@1976:             if (facetNode != null) {
ingo@1976:                 root.appendChild(facetNode);
ingo@1976:             }
ingo@1976:         }
ingo@1976:     }
ingo@1976: 
ingo@1976: 
ingo@1976:     protected void appendLoadedRecommendations(Element root) {
ingo@1976:         if (loadedRecommendations == null) {
ingo@1976:             logger.debug("No loaded recommendations existing yet.");
ingo@1976:             return;
ingo@1976:         }
ingo@1976: 
ingo@1976:         Document owner = root.getOwnerDocument();
ingo@1976: 
ingo@1976:         root.appendChild(owner.importNode(loadedRecommendations, true));
ingo@1976:     }
ingo@1976: }
ingo@1976: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :