view flys-artifacts/src/main/java/de/intevation/flys/artifacts/CollectionMonitor.java @ 938:bd3683453928

Debugged the water fill algorithm. flys-artifacts/trunk@2330 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 14 Jul 2011 14:11:29 +0000
parents 9e813e9137a5
children f4439e015278
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.ArtifactDatabase;
import de.intevation.artifacts.ArtifactDatabaseException;
import de.intevation.artifacts.ArtifactNamespaceContext;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.Hook;

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


public class CollectionMonitor implements Hook {

    public static final String XPATH_UUID = "/art:result/art:uuid/@art:value";
    public static final String XPATH_HASH = "/art:result/art:hash/@art:value";


    public class LoadArtifact {
        public String factory;
        public List<String> parameters;

        public LoadArtifact(String factory) {
            this.factory    = factory;
            this.parameters = new ArrayList<String>();
        }

        public void addParameter(String parameter) {
            parameters.add(parameter);
        }
    } // end of class LoadArtifact


    public static final String XPATH_STATES = "output-defaults/state";


    protected Map<String, List<LoadArtifact>> 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<LoadArtifact>>(len);

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

            String             stateId   = state.getAttribute("id");
            List<LoadArtifact> artifacts = parseLoadArtifacts(state);

            if (artifacts != null) {
                this.states.put(stateId, artifacts);
            }
        }
    }


    protected List<LoadArtifact> parseLoadArtifacts(Element state) {
        NodeList artifacts = (NodeList) XMLUtils.xpath(
            state, "artifact", XPathConstants.NODESET);

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

        List<LoadArtifact> loadArtifacts = new ArrayList<LoadArtifact>(len);

        for (int i = 0; i < len; i++) {
            LoadArtifact la = parseLoadArtifact((Element) artifacts.item(i));

            if (la != null) {
                loadArtifacts.add(la);
            }
        }

        return loadArtifacts;
    }


    protected LoadArtifact parseLoadArtifact(Element art) {
        String factory = art.getAttribute("factory");

        LoadArtifact artifact = new LoadArtifact(factory);

        NodeList parameters = (NodeList) XMLUtils.xpath(
            art, "parameter", XPathConstants.NODESET);

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

        for (int i = 0; i < len; i++) {
            Element parameter = (Element) parameters.item(i);
            artifact.addParameter(parameter.getAttribute("name"));
        }

        return artifact;
    }


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

        String stateId = flys.getCurrentStateId();

        List<LoadArtifact> loadArtifacts = states.get(stateId);

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

        ArtifactDatabase db = context.getDatabase();
        List<String> uuids  = new ArrayList<String>();

        for (LoadArtifact rawArtifact: loadArtifacts) {
            String[] art = createArtifact(db, context, rawArtifact);

            if (art != null && art.length >= 2) {
                Document feed   = prepareFeed(
                    artifact, rawArtifact, art[0], art[1]);

                boolean success = initializeArtifact(db, context, feed, art[0]);

                if (success) {
                    uuids.add(art[0]);
                }
            }
        }

        // TODO ADD UUIDS TO DESCRIBE
    }


    protected String[] createArtifact(
        ArtifactDatabase db,
        CallContext      cc,
        LoadArtifact     raw)
    {
        Document create = ClientProtocolUtils.newCreateDocument(raw.factory);

        try {
            Document result = db.createArtifactWithFactory(
                raw.factory, cc.getMeta(), create);

            String uuid = XMLUtils.xpathString(
                result, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);
            String hash = XMLUtils.xpathString(
                result, XPATH_HASH, ArtifactNamespaceContext.INSTANCE);

            return new String[] {uuid, hash};
        }
        catch (ArtifactDatabaseException ade) {
            logger.error(ade, ade);
        }

        return null;
    }


    protected Document prepareFeed(
        Artifact     artifact,
        LoadArtifact rawArtifact,
        String       uuid,
        String       hash)
    {
        FLYSArtifact flys = (FLYSArtifact) artifact;

        String[][] data = new String[rawArtifact.parameters.size()][2];

        for (int i = 0, len = rawArtifact.parameters.size(); i < len; i++) {
            String param = rawArtifact.parameters.get(i);
            String value = flys.getDataAsString(param);

            if (value != null) {
                data[i][0] = param;
                data[i][1] = value;
            }
        }

        return ClientProtocolUtils.newFeedDocument(uuid, hash, data);
    }


    protected boolean initializeArtifact(
        ArtifactDatabase db,
        CallContext      cc,
        Document         feed,
        String           uuid)
    {
        try {
            db.feed(uuid, feed, cc.getMeta());

            return true;
        }
        catch (ArtifactDatabaseException adbe) {
            logger.error(adbe, adbe);
        }

        return false;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org