view flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/CalculationSelect.java @ 2089:0da8874bd378

Added initial state to map artifact to be able to advance and step back. The map artifact overrides describe() to have the complete UI information in the describe response document. flys-artifacts/trunk@3613 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 06 Jan 2012 12:02:10 +0000
parents be06dbc2ed1d
children 4d57d456e261
line wrap: on
line source
package de.intevation.flys.artifacts.states;

import org.apache.log4j.Logger;

import org.w3c.dom.Element;

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

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

import de.intevation.artifactdatabase.data.StateData;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.resources.Resources;

/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class CalculationSelect extends DefaultState {

    /** The logger that is used in this class. */
    private static Logger logger = Logger.getLogger(CalculationSelect.class);


    public static final String FIELD_MODE = "calculation_mode";

    /** Constant value for the reference line calculation. */
    public static final String CALCULATION_SURFACE_CURVE =
        "calc.surface.curve";

    /** Constant value for the differences calculation. */
    public static final String CALCULATION_DURATION_CURVE =
        "calc.duration.curve";

    /** Constant value for the flood map calculation. */
    public static final String CALCULATION_FLOOD_MAP =
        "calc.flood.map";

    /** Constant value for the profile calculation. */
    public static final String CALCULATION_DISCHARGE_LONGITUDINAL_CURVE =
        "calc.discharge.longitudinal.section";

    /** Constant value for the state discharge curve calculation. */
    public static final String CALCULATION_DISCHARGE_CURVE =
        "calc.discharge.curve";

    /** Constant value for the state w differences calculation. */
    public static final String CALCULATION_W_DIFFERENCES =
        "calc.w.differences";

    /** Constant value for the state reference curve calculation. */
    public static final String CALCULATION_REFERENCE_CURVE =
        "calc.reference.curve";

    /** An array that holds all available calculation modes. */
    public static final String[] CALCULATIONS = {
        CALCULATION_SURFACE_CURVE,
        CALCULATION_FLOOD_MAP,
        CALCULATION_DISCHARGE_CURVE,
        CALCULATION_DURATION_CURVE,
        CALCULATION_DISCHARGE_LONGITUDINAL_CURVE,
        CALCULATION_W_DIFFERENCES,
        CALCULATION_REFERENCE_CURVE };


    /** Error message that is thrown if no mode has been chosen. */
    public static final String ERROR_NO_CALCULATION_MODE =
        "error_feed_no_calculation_mode";

    /** Error message that is thrown if an invalid calculation mode has been
     * chosen. */
    public static  final String ERROR_INVALID_CALCULATION_MODE =
        "error_feed_invalid_calculation_mode";


    public CalculationSelect() {
    }


    @Override
    protected Element[] createItems(
        XMLUtils.ElementCreator cr,
        Artifact    artifact,
        String      name,
        CallContext context)
    {
        CallMeta meta   = context.getMeta();
        Element[] calcs = new Element[CALCULATIONS.length];

        int i = 0;

        for (String calc: CALCULATIONS) {
            calcs[i++] = createItem(
                cr, new String[] {
                    Resources.getMsg(meta, calc, calc),
                    calc
                });
        }

        return calcs;
    }


    @Override
    public boolean validate(Artifact artifact)
    throws IllegalArgumentException
    {
        logger.debug("CalculationSelect.validate");
        FLYSArtifact flys = (FLYSArtifact) artifact;

        StateData data = getData(flys, FIELD_MODE);
        String    calc = (data != null) ? (String) data.getValue() : null;

        if (calc == null) {
            throw new IllegalArgumentException(ERROR_NO_CALCULATION_MODE);
        }

        calc = calc.trim().toLowerCase();

        for (String mode: CALCULATIONS) {
            if (mode.equals(calc)) {
                return true;
            }
        }

        throw new IllegalArgumentException(ERROR_INVALID_CALCULATION_MODE);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org