view flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java @ 132:8be4a837f20a

Added support for the flys backend and an example how to use it. flys-artifacts/trunk@1498 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 17 Mar 2011 11:46:43 +0000
parents 206312c2aa76
children f3dfa188d8b2
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;
import de.intevation.flys.artifacts.resources.Resources;


/**
 * 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 context 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);

        appendStaticUI(creator, staticUI, context);

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

        ui.appendChild(dynamic);
        ui.appendChild(staticUI);
        root.appendChild(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,
        CallContext context)
    {
        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);
            String name = Resources.getMsg(
                context.getMeta(),
                d.getName(),
                d.getName());

            Element dataElement = cr.create("data");
            cr.addAttr(dataElement, "name", name, 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