view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RiverSelect.java @ 116:47a4bc7a9ddf

Replaced the 'special' attribute of the DESCRIBE with a 'uiprovider' attribute. flys-artifacts/trunk@1347 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 01 Mar 2011 17:18:08 +0000
parents b51e92fef704
children b18aebd1342f
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

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

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

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

import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.data.StateData;
import de.intevation.artifactdatabase.state.AbstractState;

import de.intevation.flys.artifacts.model.River;
import de.intevation.flys.artifacts.model.RiverFactory;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class RiverSelect extends AbstractState {

    /** The logger used in this class. */
    private static Logger logger = Logger.getLogger(RiverSelect.class);

    /**
     * The default constructor that initializes an empty State object.
     */
    public RiverSelect() {
        super(null, null);
    }


    /**
     * Initialize the state based on the state node in the configuration.
     *
     * @param config The state configuration node.
     */
    public void setup(Node config) {
        super.setup(config);
    }


    /**
     * This method creates the UI part of the artifact's describe document. The
     * ui part consists of a static - representing previous inserted data - and
     * a dynamic part - representing data that is necessary to be inserted in
     * the current state.
     *
     * @param document The describe document.
     * @param root The root node of the describe document.
     * @param context The CallContext.
     * @param uuid The uuid of the artifact.
     */
    public void describe(
        Document    document,
        Node        root,
        CallContext context,
        String      uuid)
    {
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            document,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

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

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

        // TODO Add the static data
        logger.error("TODO: ADD STATIC DATA TO DESCRIBE DOCUMENT.");

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

        Map<String, StateData> data = getData();

        if (data != null) {
            Collection<StateData> items = data.values();

            for (StateData item: items) {
                Element select = ProtocolUtils.createArtNode(
                    creator, "select",
                    new String[] { "uiprovider" },
                    new String[] { "select_with_map" });

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

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

                label.setTextContent("River");

                select.appendChild(label);
                select.appendChild(choices);

                List<River> rivers = RiverFactory.getRivers();

                for (River river: rivers) {
                    choices.appendChild(createRiverItem(creator, river));
                }

                dynamicUI.appendChild(select);
            }
        }

        ui.appendChild(staticUI);
        ui.appendChild(dynamicUI);

        root.appendChild(ui);
    }


    /**
     * This method creates a node that represents a river item. This node
     * contains the label and the value that describe the river.
     *
     * @param cr The ElementCreator.
     * @param river The river.
     *
     * @return the element that contains the information about the river.
     */
    protected Element createRiverItem(XMLUtils.ElementCreator cr, River river) {
        Element item  = ProtocolUtils.createArtNode(cr, "item", null, null);
        Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
        Element value = ProtocolUtils.createArtNode(cr, "value", null, null);

        label.setTextContent(river.getName());
        value.setTextContent(river.getName());

        item.appendChild(label);
        item.appendChild(value);

        return item;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org