view gnv-artifacts/src/main/java/de/intevation/gnv/state/MinMaxDateState.java @ 794:d0967fdee36f

Repaired JavaDoc in Export package. gnv-artifacts/trunk@876 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 31 Mar 2010 12:18:03 +0000
parents 7b81d563c8c0
children a5526908f92f
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;

/**
 * @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();
    }


    /**
     * @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();

        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 (!iv.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 (!iv.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 (!iv.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