view flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 127:21d791e21aca

Introduced a state to select the calculation mode. flys-artifacts/trunk@1467 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 14 Mar 2011 15:47:10 +0000
parents b7a9557957d1
children 206312c2aa76
line wrap: on
line source
package de.intevation.flys.artifacts;

import java.util.List;
import java.util.Set;

import javax.xml.xpath.XPathConstants;

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.CallContext;

import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.data.StateData;
import de.intevation.artifactdatabase.state.State;
import de.intevation.artifactdatabase.state.StateEngine;
import de.intevation.artifactdatabase.transition.TransitionEngine;

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

import de.intevation.flys.artifacts.context.FLYSContext;


/**
 * The default WINFO artifact.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class WINFOArtifact extends FLYSArtifact {

    /** The logger for this class */
    private static Logger logger = Logger.getLogger(WINFOArtifact.class);


    /** The name of the artifact.*/
    public static final String ARTIFACT_NAME = "winfo";

    /** XPath */
    public static final String XPATH_STATIC_UI ="/art:result/art:ui/art:static";


    /**
     * The default constructor.
     */
    public WINFOArtifact() {
    }


    /**
     * This method returns a description of this artifact.
     *
     * @param data Some data.
     * @param CallContext The CallContext.
     *
     * @return the description of this artifact.
     */
    public Document describe(Document data, CallContext context) {
        logger.debug("Describe: the current state is: " + getCurrentStateId());

        FLYSContext flysContext = null;
        if (context instanceof FLYSContext) {
            flysContext = (FLYSContext) context;
        }
        else {
            flysContext = (FLYSContext) context.globalContext();
        }

        StateEngine stateEngine = (StateEngine) flysContext.get(
            FLYSContext.STATE_ENGINE_KEY);

        TransitionEngine transitionEngine = (TransitionEngine) flysContext.get(
            FLYSContext.TRANSITION_ENGINE_KEY);

        List<State> reachable = transitionEngine.getReachableStates(
            getCurrentState(context), stateEngine);

        Document description            = XMLUtils.newDocument();
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            description,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element root = ProtocolUtils.createRootNode(creator);
        description.appendChild(root);

        State current = getCurrentState(context);

        ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());
        ProtocolUtils.appendState(creator, root, current);
        ProtocolUtils.appendReachableStates(creator, root, reachable);

        Element ui = ProtocolUtils.createArtNode(
            creator, "ui", null, null);

        Element staticUI  = ProtocolUtils.createArtNode(
            creator, "static", null, null);

        Element dynamic = current.describe(
            description,
            root,
            context,
            identifier());

        ui.appendChild(dynamic);
        root.appendChild(ui);

        appendStaticUI(creator, ui);

        return description;
    }


    /**
     * Returns the name of the concrete artifact.
     *
     * @return the name of the concrete artifact.
     */
    public String getName() {
        return ARTIFACT_NAME;
    }


    /**
     * This method appends the static data - that has already been inserted by
     * the user - to the static node of the DESCRIBE document.
     *
     * @param cr The ElementCreator that is used to create new elements.
     * @param ui The static ui node.
     */
    protected void appendStaticUI(XMLUtils.ElementCreator cr, Node ui) {
        Set<String> keys = data.keySet();

        // XXX This just handles single selection string values. If we need more
        // complex (maybe multiselect) objects we should introduce a data
        // structure for this - this structure should have a method that returns
        // a <data> node that contains its items.
        for (String k: keys) {
            logger.debug("The key = " + k);
            StateData d = getData(k);

            Element dataElement = cr.create("data");
            cr.addAttr(dataElement, "name", (String) d.getName(), true);
            cr.addAttr(dataElement, "type", d.getType(), true);

            Element itemElement = cr.create("item");
            cr.addAttr(itemElement, "value", (String) d.getValue(), true);
            // TODO Description (human readable) is missing

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

http://dive4elements.wald.intevation.org