view flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java @ 1058:c37084f31c84

Implemented a MapInfo service that returns some basic information to create maps for specific rivers. flys-artifacts/trunk@2528 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 23 Aug 2011 07:53:41 +0000
parents 9a77a9adbb36
children fdb0f4ef96f0
line wrap: on
line source
package de.intevation.flys.artifacts;

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

import org.apache.log4j.Logger;

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

import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.ArtifactFactory;
import de.intevation.artifacts.ArtifactNamespaceContext;
import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.CallMeta;

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

import de.intevation.flys.artifacts.states.DefaultState;
import de.intevation.artifactdatabase.ProtocolUtils;
import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifactdatabase.state.DefaultOutput;
import de.intevation.artifactdatabase.state.Output;
import de.intevation.artifacts.common.utils.XMLUtils;

/**
 * Artifact to access names of Points Of Interest along a segment of a river.
 */
public class MainValuesArtifact
extends      StaticFLYSArtifact
implements   Facet {

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

    /** The name of the artifact. */
    public static final String ARTIFACT_NAME = "annotation";
   
    public MainValuesArtifact() {
        logger.warn("MainValuesArtifact.MainValuesartifact()");
    }

    /**
     * Gets called from factory, to set things up.
     * Do nothing.
     */
    @Override
    public void setup(
        String          identifier,
        ArtifactFactory factory,
        Object          context,
        CallMeta        callMeta,
        Document        data)
    {
        logger.warn("MainValuesArtifact.setup");
        ; 
    }

    /**
     * Create the description of this MainValuesArtiface-instance.
     *
     * @param data    some data.
     * @param context the CallContext.
     *
     * @return the description of this artifact.
     */
    @Override
    public Document describe(Document data, CallContext context) {
        logger.debug("MainValuesArtifact.describe");

        if (logger.isDebugEnabled()) {
            dumpArtifact();
        }

        FLYSContext flysContext = getFlysContext(context);

        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);

        ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash());

        Element name = ProtocolUtils.createArtNode(
            creator, "name",
            new String[] { "value" },
            new String[] { getName() });

        Element outs = ProtocolUtils.createArtNode(
            creator, "outputmodes", null, null);
        appendOutputModes(description, outs, context);

        root.appendChild(name);
        root.appendChild(outs);

        return description;
    }


    /**
     * Append outputmode elements to given document.
     *
     * @param doc Document to add outputmodes to.
     * @param outs Element to add outputmode elements to.
     * @param context The given CallContext (mostly for internationalization).
     */
    protected void appendOutputModes(
        Document    doc,
        Element     outs,
        CallContext context)
    {
        // TODO outputmodes
        XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        FLYSContext flysContext = getFlysContext(context);

        for (int i = 0; i < 1; i++) {
            logger.debug("Append output mode.");

            // TODO create outputs.
            /*
            List<Output> list = state.getOutputs();
            if (list == null || list.size() == 0) {
                logger.debug("-> No output modes for this state.");
                continue;
            }*/
            List<Facet>  fs = new ArrayList<Facet>();
            DefaultOutput mainValuesOutput = new DefaultOutput(
                    "discharge_curve", "output.discharge_curve", "image/png",
                    fs,
                    "chart");

            // Create facets.
            if (fs == null || fs.size() == 0) {
                logger.debug("No facets found.");
                continue;
            }
            logger.debug("Built stateless facets.");
            // TODO remember issues here.
            //List<Output> generated = generateOutputs(list, fs);
            //ProtocolUtils.appendOutputModes(doc, outs, generated);
        }

        try {
            DefaultState cur = (DefaultState) getCurrentState(context);
            if (cur.validate(this)) {
                List<Output> list = cur.getOutputs();
                if (list != null && list.size() > 0) {
                    logger.debug(
                        "Append output modes for state: " + cur.getID());

                    List<Facet>  fs = facets.get(cur.getID());
                    if (fs != null && fs.size() > 0) {
                        List<Output> generated = generateOutputs(list, fs);

                        logger.debug("Found " + fs.size() + " current facets.");
                        if (!generated.isEmpty()) {
                            ProtocolUtils.appendOutputModes(
                                doc, outs, generated);
                        }
                        else{
                            logger.debug("Cannot append output to generated document.");
                        }
                    }
                    else {
                        logger.debug("No facets found for the current state.");
                    }
                }
            }
        }
        catch (IllegalArgumentException iae) {
            // state is not valid, so we do not append its outputs.
        }
    }


    /* FACET IMPLEMENTATION  */


    // TODO implement; what is index used for?
    /**
     * Returns the index of this facet.
     *
     * @return the index of this facet.
     */
    public int getIndex() {
        return 0;
    }


    /**
     * Returns the name of this facet.
     *
     * @return the name of this facet.
     */
    public String getName() {
        // TODO define, static
        return "FACETNAME";
    }


    /**
     * Returns the description of this facet.
     *
     * @return the description of this facet.
     */
    public String getDescription() {
        return null;
    }


    /**
     * Returns the data this facet requires.
     *
     * @param artifact The owner artifact.
     * @param context The CallContext.
     *
     * @return the data.
     */
    public Object getData(Artifact artifact, CallContext context) {
        return null;
    }


    /**
     * Write the internal representation of a facet to a node.
     *
     * @param doc A Document.
     *
     * @return the representation as Node.
     */
    public Node toXML(Document doc) {
        return null;
    }
 
}

http://dive4elements.wald.intevation.org