view gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java @ 822:ac1b9f00603f

Added more Javadoc. Removed obsolet TODO-Flags. Added missing Files to Revision 907 gnv-artifacts/trunk@909 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 13 Apr 2010 08:17:00 +0000
parents a5526908f92f
children 499cfbbb61bc
line wrap: on
line source
package de.intevation.gnv.state;

import de.intevation.artifacts.CallContext;

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

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.GregorianCalendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;

/**
 * This state handles date input. The resulting describe document of this state
 * contains two fields in the user interface description to define a time range.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
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";

    /**
     *
     */
    public static final String EXCEPTION_DATE_OUT_OF_RANGE =
        "date.out.of.range";

    /**
     *
     */
    public static final String EXCEPTION_MISSING_DATE =
        "missing.data.field";

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


    /**
     *
     */
    public MinMaxDateState() {
        super();
    }


    /**
     * This feed method needs a collection of two InputData objects. These
     * objects' values need to be a datetime string which is turned into a Date
     * object. Afterwards, the given dates are validated. Min and max date need
     * to be in range of the min and max date retrieved by
     * {@link #getDescibeData(java.lang.String)}.
     */
    @Override
    @SuppressWarnings({"static-access", "static-access", "static-access"})
    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);
        }

        Iterator iter     = inputData.iterator();

        MinMaxDescribeData data =
            (MinMaxDescribeData) getDescibeData(uuid).get(0);
        Object min = data.getMinValue();
        Object max = data.getMaxValue();

        Object tmpMin = null;
        Object tmpMax = 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);
            }

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

            Date lower = null;
            if (min instanceof GregorianCalendar) {
                lower = ((GregorianCalendar)min).getTime();
            }

            Date upper = null;
            if (max instanceof GregorianCalendar) {
                upper = ((GregorianCalendar)max).getTime();
            }

            Date d = null;
            try {
                d = DateUtils.getDateFromString(value,DateUtils.DATE_PATTERN);
            }
            catch (Exception e) {
                logger.warn(e, e);
            }

            if (d == null || lower == null || upper == null) {
                String msg = resFactory.getRessource(
                    locale,
                    EXCEPTION_MISSING_DATE,
                    EXCEPTION_MISSING_DATE);
                logger.warn(msg);
            }
            else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Date to validate: " + d.toString());
                    logger.debug("Lower date bound: " + lower.toString());
                    logger.debug("Upper date bound: " + upper.toString());
                }

                if (!InputValidator.isDateValid(d, lower, upper)) {
                    String msg = resFactory.getRessource(
                        locale,
                        EXCEPTION_DATE_OUT_OF_RANGE,
                        EXCEPTION_DATE_OUT_OF_RANGE);
                    logger.error(msg);
                    return feedFailure(msg);
                }
            }

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

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

            if (tmpMin != null && tmpMax != null) {
                if (!InputValidator.isInputValid((String) tmpMin, (String) tmpMax, 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, tmpMin, tmpMax, 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