view artifacts/src/main/java/org/dive4elements/river/artifacts/SedimentYieldArtifact.java @ 7324:24023630f693

issue1345: Fix sediment load facet type, adjust theme name.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 14 Oct 2013 17:41:19 +0200
parents d7b2c14a9693
children 6bc884bab32e
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.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;

import org.dive4elements.artifactdatabase.state.DefaultOutput;
import org.dive4elements.artifactdatabase.state.Facet;
import org.dive4elements.artifactdatabase.state.FacetActivity;
import org.dive4elements.artifactdatabase.state.State;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.ArtifactFactory;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoad;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFacet;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFactory;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadResult;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.artifacts.states.StaticState;

import org.dive4elements.river.artifacts.model.FacetTypes;


/** Artifact to access sediment yield measurements. */
// TODO Naming: would SedimentLoadArtifact be more precise?
public class SedimentYieldArtifact
extends      StaticD4EArtifact
implements   FacetTypes
{
    /** The logger for this class. */
    private static Logger logger =
        Logger.getLogger(SedimentYieldArtifact.class);

    /** Artifact key name. */
    private static final String NAME = "sedimentyield";

    /** Spawn only inactive facets. */
    static {
        // TODO: Move to configuration.
        FacetActivity.Registry.getInstance()
            .register(NAME, FacetActivity.INACTIVE);
    }

    /** Need to give the state an id. */
    public static final String STATIC_STATE_NAME =
        "state.sedimentyield.static";

    /** One and only state to be in. */
    protected transient State state = null;

    protected String DATA_NAME = "ID";

    /**
     * Trivial Constructor.
     */
    public SedimentYieldArtifact() {
        logger.debug("SedimentYieldArtifact.SedimentYieldArtifact");
    }


    /** Get artifact key name. */
    @Override
    public String getName() {
        return NAME;
    }


    private Object getSedimentLoad() {
        logger.debug("SedimentYieldArtifact.getSedimentLoad");
        String id = getDataAsString(DATA_NAME);
        String river = getDataAsString("river");

        // TODO use cache if possible
        SedimentLoad myLoad = SedimentLoadFactory.getSedimentLoadWithDataUncached(id, river);
        return new CalculationResult(
            new SedimentLoadResult[] {
                new SedimentLoadResult(1983,2042,myLoad)
            }, new Calculation());
    }


    /** Create a new state with bogus output. */
    protected State spawnState() {
        state = new StaticState(STATIC_STATE_NAME) {

            public Object staticCompute(List<Facet> facets) {
                 return getSedimentLoad();
            }
        };
        List<Facet> fs = getFacets(STATIC_STATE_NAME);
        DefaultOutput output = new DefaultOutput(
            "general",
            "general",
            "image/png",
            fs,
            "chart");

        state.getOutputs().add(output);

        return state;
    }


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

        // Refactor? this happens at another place, too
        // Store id, yield yields.
        state = new StaticState(STATIC_STATE_NAME) {

            public Object staticCompute(List<Facet> facets) {
                 return getSedimentLoad();
            }
        };
        if (logger.isDebugEnabled()) {
            logger.debug(XMLUtils.toString(data));
        }

        List<Facet> fs = new ArrayList<Facet>();
        String code = getDatacageIDValue(data);

        if (code != null) {
            String fraction = SedimentLoadFactory.getSedimentYieldFractionName(Integer.valueOf(code));
            String fractionName = Resources.getMsg(callMeta, fraction, "-");
            Date[] dates = SedimentLoadFactory.getSedimentYieldTimes(Integer.valueOf(code));
            Calendar date = Calendar.getInstance();
            date.setTime(dates[0]);
            String name = fractionName + " - " + date.get(Calendar.YEAR);
            if (dates[1] != null) {
                date.setTime(dates[1]);
                name += " - " + date.get(Calendar.YEAR);
            }

            String facetType = "";
            if (fraction.equals("coarse")) {
                facetType = SEDIMENT_LOAD_COARSE;
            }
            else if (fraction.equals("sand")) {
                facetType = SEDIMENT_LOAD_SAND;
            }
            else if (fraction.equals("fine_middle")) {
                facetType = SEDIMENT_LOAD_FINEMIDDLE;
            }
            else if (fraction.equals("susp_sand")) {
                facetType = SEDIMENT_LOAD_SUSP_SAND;
            }
            else if (fraction.equals("susp_sand_bed")) {
                facetType = SEDIMENT_LOAD_SUSP_SAND_BED;
            }
            else if (fraction.equals("suspended_sediment")) {
                facetType = SEDIMENT_LOAD_SUSP_SEDIMENT;
            }
            else {
                logger.error("Do not know fraction type " + fraction);
            }

            Facet facet = new SedimentLoadFacet(
                0,
                facetType,
                name,
                //????
                ComputeType.ADVANCE, state.getID(), "hash"
                );
            fs.add(facet);
            addFacets(state.getID(), fs);
            addStringData(DATA_NAME, code);
        }

        spawnState();
        super.setup(identifier, factory, context, callMeta, data);
    }


    /**
     * 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(getState());
        return states;
    }


    /**
     * Get the "current" state (there is but one).
     * @param cc ignored.
     * @return the "current" (only possible) state.
     */
    @Override
    public State getCurrentState(Object cc) {
        return getState();
    }


    /**
     * Get the only possible state.
     * @return the state.
     */
    protected State getState() {
        return getState(null, null);
    }


    /**
     * Get the state.
     * @param context ignored.
     * @param stateID ignored.
     * @return the state.
     */
    @Override
    protected State getState(Object context, String stateID) {
        return (state != null)
            ? state
            : spawnState();
    }


    /**
     * Called via setup. Overridden to avoid cloning all data.
     *
     * @param artifact The master-artifact.
     */
    @Override
    protected void initialize(
        Artifact artifact,
        Object context,
        CallMeta meta)
    {
        logger.debug("SedimentYieldArtifact.initialize");
        importData((D4EArtifact) artifact, "river");
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org