view gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java @ 747:a5e860f17a52

Added an new State wich will handle the Display of Coordinatevalues and fetch given Inputparameters from the Presettings and copy them to the InputValues. gnv-artifacts/trunk@787 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 16 Mar 2010 13:02:56 +0000
parents dde7f51dbe1e
children 9a828e5a2390
line wrap: on
line source
package de.intevation.gnv.state;

import de.intevation.artifactdatabase.XMLUtils;

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

import de.intevation.gnv.artifacts.ressource.RessourceFactory;

import de.intevation.gnv.geobackend.base.Result;

import de.intevation.gnv.geobackend.util.DateUtils;

import de.intevation.gnv.state.describedata.DefaultMinMaxDescribeData;
import de.intevation.gnv.state.describedata.DescribeData;
import de.intevation.gnv.state.describedata.MinMaxDescribeData;

import de.intevation.gnv.state.exception.StateException;

import de.intevation.gnv.utils.InputValidator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;

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

/**
 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
 */
public class MinMaxDateState extends MinMaxState {


    public static final String EXCEPTION_DATE_REQUIRED =
        "input.is.not.valid.date.required";

    public static final String EXCEPTION_START_AFTER_END =
        "start.date.after.end.date";

    private static Logger logger = Logger.getLogger(MinMaxDateState.class);


    public MinMaxDateState() {
        super();
    }


    /**
     * @see de.intevation.gnv.state.StateBase#feed(java.util.Collection,
     *      java.lang.String)
     */
    @Override
    public Document feed(
        CallContext           context,
        Collection<InputData> inputData,
        String                uuid)
    throws StateException {
        RessourceFactory resFactory = RessourceFactory.getInstance();
        Locale[] serverLocales      = resFactory.getLocales();
        Locale locale               = context.getMeta().getPreferredLocale(
            serverLocales);

        if (inputData == null) {
            String msg = "No input data given.";
            logger.warn(msg);
            return feedFailure(msg);
        }

        InputValidator iv = new InputValidator();
        Iterator iter     = inputData.iterator();

        Object min = null;
        Object max = null;

        while (iter.hasNext()) {
            InputData  tmp   = (InputData) iter.next();
            InputValue meta  = inputValues.get(tmp.getName());
            String     type  = meta.getType();
            String     value = tmp.getValue();
            String     name  = tmp.getName();

            if (meta == null) {
                String msg = "Input data not expected here. Data will be ignored.";
                logger.warn(msg);
                return feedFailure(msg);
            }

            boolean valid = iv.isInputValid(value, type);
            if (!valid) {
                String msg = resFactory.getRessource(
                    locale, EXCEPTION_DATE_REQUIRED, EXCEPTION_DATE_REQUIRED);
                logger.error(msg);
                return feedFailure(msg);
            }

            if (name.equals(MINVALUEFIELDNAME)) {
                min = value;
            }

            if (name.equals(MAXVALUEFIELDNAME)) {
                max = value;
            }

            if (min != null && max != null) {
                if (!iv.isInputValid((String) min, (String) max, type)) {
                    String msg = resFactory.getRessource(
                        locale,
                        EXCEPTION_START_AFTER_END,
                        EXCEPTION_START_AFTER_END);
                    logger.error(msg);
                    return feedFailure(msg);
                }
            }
        }

        DescribeData values = new DefaultMinMaxDescribeData(
            dataName, min, max, getID());

        this.inputData.put(dataName, new DefaultInputData(dataName, values)); 

        return feedSuccess();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org