view artifact-database/src/main/java/de/intevation/artifactdatabase/ProtocolUtils.java @ 241:4253995c970e

Fix for flys/issue8 (part 3) artifacts/trunk@1676 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 13 Apr 2011 10:57:45 +0000
parents 4bb6bfaca393
children 5ed1afd81569
line wrap: on
line source
/*
 * Copyright (c) 2011 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */
package de.intevation.artifactdatabase;

import java.util.List;

import org.w3c.dom.Element;

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

import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifactdatabase.state.Output;
import de.intevation.artifactdatabase.state.State;


/**
 * This class provides methods that help creating the artifact protocol
 * documents describe, feed, advance and out.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ProtocolUtils {

    /**
     * It should not be necessary to create instances of this class.
     */
    private ProtocolUtils() {}


    /**
     * This method creates a node that might be used for the artifact protocol.
     *
     * @param creator The ElementCreator that is used to create the node.
     * @param nodeName The node name.
     * @param attrName The names of optional attributes.
     * @param value The values for the optional attributes.
     *
     * @return the created node.
     */
    public static Element createArtNode(
        XMLUtils.ElementCreator creator,
        String nodeName, String[] attrName, String[] value)
    {
        Element typeNode = creator.create(nodeName);

        if (attrName != null && value != null) {
            for (int i = 0; i < attrName.length; i++) {
                if (i < value.length) {
                    creator.addAttr(typeNode, attrName[i], value[i], true);
                }
                else {
                    break;
                }
            }
        }

        return typeNode;
    }


    /**
     * This method creates the root node for all artifact protocol documents.
     *
     * @param creator The ElementCreator used to create new elements.
     *
     * @return the root node for the artifact protocol document.
     */
    public static Element createRootNode(XMLUtils.ElementCreator creator) {
        return createArtNode(creator, "result", null, null);
    }


    /**
     * This method appends the three necessary nodes <i>type</i>, <i>uuid</i>
     * and <i>hash</i> of the describe document to <i>root</i> node.
     *
     * @param creator The ElementCreator that is used to create new nodes.
     * @param root The root node of the describe document.
     * @param uuid The UUID of the artifact.
     * @param hash The hash if the artifact.
     */
    public static void appendDescribeHeader(
        XMLUtils.ElementCreator creator, Element root, String uuid, String hash)
    {
        root.appendChild(createArtNode(
            creator,
            "type",
            new String[] {"name"},
            new String[] {"describe"}));

        root.appendChild(createArtNode(
            creator,
            "uuid",
            new String[] {"value"},
            new String[] {uuid}));

        root.appendChild(createArtNode(
            creator,
            "hash",
            new String[] {"value"},
            new String[] {hash}));
    }


    /**
     * This method appends a node that describes the current state to
     * <i>root</i>.
     *
     * @param creator The ElementCreator used to create new elements.
     * @param root The parent node for new elements.
     * @param state The state to be appended.
     */
    public static void appendState(
        XMLUtils.ElementCreator creator, Element root, State state)
    {
        root.appendChild(createArtNode(
            creator, "state",
            new String[] { "description", "name" },
            new String[] { state.getDescription(), state.getID() }));
    }


    /**
     * This method appends a node with reachable states to <i>root</i>.
     *
     * @param creator The ElementCreator used to create new elements.
     * @param root The parent node for new elements.
     * @param states The reachable states to be appended.
     */
    public static void appendReachableStates(
        XMLUtils.ElementCreator creator,
        Element                 root,
        List<State>             states)
    {
        Element reachable = createArtNode(
            creator, "reachable-states", null, null);

        for (State s: states) {
            appendState(creator, reachable, s);
        }

        root.appendChild(reachable);
    }


    /**
     * This method appends a node for each Output in the <i>outputs</i> list to
     * <i>out</i>. Note: an output node includes its provided facets!
     *
     * @param creator The ElementCreator used to create new elements.
     * @param out The parent node for new elements.
     * @param outputs The list of reachable outputs.
     */
    public static void appendOutputModes(
        XMLUtils.ElementCreator creator,
        Element                 out,
        List<Output>            outputs)
    {
        for (Output o: outputs) {
            Element newOut = createArtNode(
                creator,
                "output",
                new String[] {"name", "description", "mime-type"},
                new String[] {o.getName(),o.getDescription(),o.getMimeType()});

            Element facets = createArtNode(creator, "facets", null, null);
            appendFacets(creator, facets, o.getFacets());

            newOut.appendChild(facets);
            out.appendChild(newOut);
        }
    }


    /**
     * This method appends a node for each Facet in the <i>facets</i> list to
     * <i>facet</i>.
     *
     * @param creator The ElementCreator used to create new elements.
     * @param facet The root node for new elements.
     * @param facets The list of facets.
     */
    public static void appendFacets(
        XMLUtils.ElementCreator creator,
        Element                 facet,
        List<Facet>             facets)
    {
        if (facets == null || facets.size() == 0) {
            return;
        }

        for (Facet f: facets) {
            Element newFacet = createArtNode(
                creator,
                "facet",
                new String[] { "name", "description" },
                new String[] { f.getName(), f.getDescription() });

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

http://dive4elements.wald.intevation.org