view flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java @ 945:59ae2a823e73

Use the Facet.toXML() method to write facet elements into the DESCRIBE document of the Collection. flys-artifacts/trunk@2360 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 19 Jul 2011 10:37:57 +0000
parents f4439e015278
children ff6ce301c472
line wrap: on
line source
package de.intevation.flys.artifacts;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;

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

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactNamespaceContext;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.Hook;

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


public class CollectionMonitor implements Hook {

    public static final String XPATH_STATES = "output-defaults/state";
    public static final String XPATH_RESULT = "/art:result";


    protected Map<String, List<String>> states;


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


    @Override
    public void setup(Node cfg) {
        Element config = (Element) cfg;
        String  xlink  = config.getAttribute("xlink:href");
        xlink          = Config.replaceConfigDir(xlink);

        File file = new File(xlink);

        if (file == null || !file.exists()) {
            logger.error("The config file '" + xlink + "' does not exist.");
            return;
        }

        Document outputDefaults = XMLUtils.parseDocument(file);

        NodeList states = (NodeList) XMLUtils.xpath(
            outputDefaults,
            XPATH_STATES,
            XPathConstants.NODESET);

        int len = states != null ? states.getLength() : 0;

        this.states = new HashMap<String, List<String>>(len);

        for (int i = 0; i < len; i++) {
            Element state = (Element) states.item(i);

            String stateId = state.getAttribute("id");
            String factory = state.getAttribute("artifact-factory");

            if (stateId != null && factory != null) {
                List<String> factories = this.states.get(stateId);

                if (factories == null) {
                    factories = new ArrayList<String>();
                    this.states.put(stateId, factories);
                }

                factories.add(factory);
            }
        }
    }


    @Override
    public void execute(Artifact artifact, CallContext context, Document doc) {
        FLYSArtifact flys = (FLYSArtifact) artifact;

        String stateId = flys.getCurrentStateId();

        List<String> factories = states.get(stateId);

        if (factories == null || factories.isEmpty()) {
            return;
        }

        logger.info("Found " + factories.size() + " recommended artifacts.");

        appendRecommendations(doc, factories);
    }


    protected void appendRecommendations(Document doc, List<String> factories) {
        Element result = (Element) XMLUtils.xpath(
            doc,
            XPATH_RESULT,
            XPathConstants.NODE,
            ArtifactNamespaceContext.INSTANCE);

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

        Element recommended = creator.create("recommended-artifacts");

        for (String factory: factories) {
            Element fac = creator.create("artifact-factory");
            creator.addAttr(fac, "name", factory);

            recommended.appendChild(fac);
        }

        result.appendChild(recommended);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org