view artifacts/src/main/java/org/dive4elements/river/artifacts/AbstractStaticStateArtifact.java @ 8755:30b1ddadf275

(issue1801) Unify reference gauge finding code The basic way as described in the method comment of the determineRefGauge method is now used in the WINFOArtifact, MainValuesService and RiverUtils.getGauge method. RiverUtils.getGauge previously just returned the first gauge found. While this is now a behavior change I believe that it is always more correct then the undeterministic behavior of the previous implmenentation.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 24 Jun 2015 14:07:26 +0200
parents e4606eae8ea5
children 5e38e2924c07
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.artifacts;

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

import org.apache.log4j.Logger;

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

import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.ArtifactNamespaceContext;

import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;

import org.dive4elements.artifactdatabase.ProtocolUtils;
import org.dive4elements.artifactdatabase.data.StateData;
import org.dive4elements.artifactdatabase.state.State;

import org.dive4elements.river.artifacts.states.StaticState;

/**
 * A abstract baseclass for Artifacts which are using only one static state.
 *
 * This class is intended to be used without the config/stateengine to generate
 * the static state.
 *
 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
 */
public abstract class AbstractStaticStateArtifact extends StaticD4EArtifact {

    private transient StaticState staticstate;

    private static final Logger log =
        Logger.getLogger(AbstractStaticStateArtifact.class);

    /**
     * Get a list containing the one and only State.
     * @param  context ignored.
     * @return list with one and only state.
     */
    @Override
    protected List<State> getStates(Object context) {
        ArrayList<State> states = new ArrayList<State>();
        states.add(getStaticState());
        return states;
    }


    /**
     * Get the "current" state.
     * @param cc ignored.
     * @return always the set static state.
     */
    @Override
    public State getCurrentState(Object cc) {
        return getStaticState();
    }

    /**
     * A child class must override this method to set its static state
     */
    protected abstract void initStaticState();

    protected void setStaticState(StaticState state) {
        this.staticstate = state;
    }

    protected StaticState getStaticState() {
        if (staticstate == null) {
            initStaticState();
        }
        return staticstate;
    }

    /**
     * Get the state.
     * @param context ignored.
     * @param stateID ignored.
     * @return the state.
     */
    @Override
    protected State getState(Object context, String stateID) {
        return getStaticState();
    }

    @Override
    public Document describe(Document data, CallContext cc) {
        log.debug("Describe artifact: " + identifier());

        Document desc = XMLUtils.newDocument();

        ElementCreator creator = new ElementCreator(
            desc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        Element root = ProtocolUtils.createRootNode(creator);
        desc.appendChild(root);

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

        root.appendChild(name);
        root.appendChild(createOutputModes(cc, desc, creator));

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

        // Add the data to an anonymous state.
        Collection<StateData> datas = getAllData();
        if (datas.size() > 0) {
            Element ui = creator.create("ui");
            Element staticE = creator.create("static");

            StaticState current = getStaticState();
            Element state = current.describeStatic(this, desc, root, cc, null);
            staticE.appendChild(state);

            for (StateData dataItem : datas) {
                Element itemelent = creator.create("data");
                creator.addAttr(itemelent, "name", dataItem.getName(), true);
                creator.addAttr(itemelent, "type", dataItem.getType(), true);
                state.appendChild(itemelent);
                Element valuement = creator.create("item");
                creator.addAttr(valuement, "label", dataItem.getDescription(), true);
                creator.addAttr(valuement, "value", dataItem.getValue().toString(), true);
                itemelent.appendChild(valuement);
            }

            ui.appendChild(staticE);
            root.appendChild(ui);
        }

        return desc;
    }
}

http://dive4elements.wald.intevation.org