view flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java @ 22:a85bac235069

Implemented code to parse the UIProvider that should be used for a current state. flys-client/trunk@1336 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 22 Feb 2011 17:29:51 +0000
parents f8a5f2c5e2b7
children 88c530c25968
line wrap: on
line source
package de.intevation.flys.client.server;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPathConstants;

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

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

import de.intevation.flys.client.shared.model.ArtifactDescription;
import de.intevation.flys.client.shared.model.Data;
import de.intevation.flys.client.shared.model.DataItem;
import de.intevation.flys.client.shared.model.DefaultData;
import de.intevation.flys.client.shared.model.DefaultDataItem;
import de.intevation.flys.client.shared.model.DefaultArtifactDescription;


/**
 * This factory class helps creating an {@link ArtifactDescription} based on the
 * DESCRIBE document of an artifact returned by the artifact server. Use the
 * {@link createArtifactDescription(org.w3c.dom.Document)} method with the
 * DESCRIBE document to create such an {@link ArtifactDescription}.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ArtifactDescriptionFactory {

    public static final String XPATH_STATE_NAME = "@art:name";

    public static final String XPATH_UIPROVIDER = "@art:uiprovider";

    /**
     * This method creates the {@link ArtifactDescription} of the DESCRIBE
     * document <i>doc</i>.
     *
     * @param doc A DESCRIBE document.
     *
     * @return the {@link ArtifactDescription}.
     */
    public static ArtifactDescription createArtifactDescription(Document doc) {
        System.out.println("ArtifactDescriptionFactory - create()");

        Node currentState = ClientProtocolUtils.getCurrentState(doc);
        Node staticNode   = ClientProtocolUtils.getStaticUI(doc);
        Node dynamicNode  = ClientProtocolUtils.getDynamicUI(doc);

        String state = (String) XMLUtils.xpath(
            currentState,
            XPATH_STATE_NAME,
            XPathConstants.STRING,
            ArtifactNamespaceContext.INSTANCE);
        System.out.println("Current state name: " + state);

        Data currentData = extractCurrentData(dynamicNode);

        // TODO parse the static ui part
        return new DefaultArtifactDescription(null, currentData, state, null);
    }


    /**
     * This method extracts the data that the user is able to enter in the
     * current state of the artifact.
     *
     * @param dynamicNode The dynamic node of the DESCRIBE document.
     *
     * @return A {@link Data} object that represents the data which might be
     * entered by the user in the current state or null, if no data might be
     * entered.
     */
    protected static Data extractCurrentData(Node dynamicNode) {
        System.out.println("ArtifactDescriptionFactory - extractCurrentData()");

        Node     data    = ClientProtocolUtils.getSelectNode(dynamicNode);
        NodeList choices = ClientProtocolUtils.getItemNodes(data);
        String   label   = ClientProtocolUtils.getLabel(data);

        DataItem[] dataItems = extractCurrentDataItems(choices);
        String uiProvider    = extractUIProvider(data);

        return new DefaultData(label, null, null, dataItems, uiProvider);
    }


    /**
     * This method extract the {@link DataItem}s of the DESCRIBE document.
     *
     * @param items The items in the DESCRIBE document.
     *
     * @return the {@link DataItem}s.
     */
    protected static DataItem[] extractCurrentDataItems(NodeList items) {
        System.out.println(
            "ArtifactDescriptionFactory - extractCurrentDataItems()");

        if (items == null || items.getLength() == 0) {
            System.out.println("No data items found.");
            return null;
        }

        int count = items.getLength();

        List<DataItem> dataItems = new ArrayList<DataItem>(count);

        for (int i = 0; i < count; i++) {
            Node item    = items.item(i);
            String label = ClientProtocolUtils.getLabel(item);
            String value = ClientProtocolUtils.getValue(item);

            dataItems.add(new DefaultDataItem(label, null, value));
        }

        return (DataItem[]) dataItems.toArray(new DataItem[count]);
    }


    /**
     * This method extracts the UIProvider specified by the data node.
     *
     * @param data The data node.
     *
     * @return the UIProvider that is specified in the data node.
     */
    protected static String extractUIProvider(Node data) {
        return (String) XMLUtils.xpath(
            data,
            XPATH_UIPROVIDER,
            XPathConstants.STRING,
            ArtifactNamespaceContext.INSTANCE);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org