ingo@1115: /* ingo@1115: * Copyright (c) 2010 by Intevation GmbH ingo@1115: * ingo@1115: * This program is free software under the LGPL (>=v2.1) ingo@1115: * Read the file LGPL.txt coming with the software for details ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1115: */ ingo@1115: ingo@725: package de.intevation.gnv.state; ingo@725: ingo@725: import java.util.Collection; ingo@784: import java.util.Date; tim@823: import java.util.GregorianCalendar; ingo@725: import java.util.Iterator; ingo@725: import java.util.Locale; ingo@725: ingo@725: import org.apache.log4j.Logger; tim@823: import org.w3c.dom.Document; ingo@725: tim@823: import de.intevation.artifacts.CallContext; tim@823: import de.intevation.gnv.artifacts.ressource.RessourceFactory; tim@823: import de.intevation.gnv.geobackend.util.DateUtils; tim@823: import de.intevation.gnv.state.describedata.DefaultMinMaxDescribeData; tim@823: import de.intevation.gnv.state.describedata.DescribeData; tim@823: import de.intevation.gnv.state.describedata.MinMaxDescribeData; tim@823: import de.intevation.gnv.state.exception.StateException; tim@823: import de.intevation.gnv.utils.InputValidator; ingo@725: ingo@725: /** ingo@796: * This state handles date input. The resulting describe document of this state ingo@796: * contains two fields in the user interface description to define a time range. ingo@796: * sascha@780: * @author Ingo Weinzierl ingo@725: */ ingo@725: public class MinMaxDateState extends MinMaxState { ingo@725: ingo@725: ingo@796: /** ingo@796: * ingo@796: */ ingo@725: public static final String EXCEPTION_DATE_REQUIRED = ingo@725: "input.is.not.valid.date.required"; ingo@725: ingo@796: /** ingo@796: * ingo@796: */ ingo@725: public static final String EXCEPTION_START_AFTER_END = ingo@725: "start.date.after.end.date"; ingo@725: ingo@796: /** ingo@796: * ingo@796: */ ingo@784: public static final String EXCEPTION_DATE_OUT_OF_RANGE = ingo@784: "date.out.of.range"; ingo@784: ingo@796: /** ingo@796: * ingo@796: */ ingo@784: public static final String EXCEPTION_MISSING_DATE = ingo@784: "missing.data.field"; ingo@784: ingo@725: private static Logger logger = Logger.getLogger(MinMaxDateState.class); ingo@725: ingo@725: ingo@796: /** ingo@796: * ingo@796: */ ingo@725: public MinMaxDateState() { ingo@725: super(); ingo@725: } ingo@725: ingo@725: ingo@725: /** ingo@796: * This feed method needs a collection of two InputData objects. These ingo@796: * objects' values need to be a datetime string which is turned into a Date ingo@796: * object. Afterwards, the given dates are validated. Min and max date need ingo@796: * to be in range of the min and max date retrieved by ingo@796: * {@link #getDescibeData(java.lang.String)}. ingo@725: */ ingo@725: @Override ingo@725: public Document feed( ingo@725: CallContext context, ingo@725: Collection inputData, ingo@725: String uuid) ingo@725: throws StateException { ingo@725: RessourceFactory resFactory = RessourceFactory.getInstance(); ingo@725: Locale[] serverLocales = resFactory.getLocales(); ingo@725: Locale locale = context.getMeta().getPreferredLocale( ingo@725: serverLocales); ingo@725: ingo@725: if (inputData == null) { ingo@725: String msg = "No input data given."; ingo@725: logger.warn(msg); ingo@725: return feedFailure(msg); ingo@725: } ingo@725: ingo@725: Iterator iter = inputData.iterator(); ingo@725: ingo@784: MinMaxDescribeData data = ingo@784: (MinMaxDescribeData) getDescibeData(uuid).get(0); ingo@784: Object min = data.getMinValue(); ingo@784: Object max = data.getMaxValue(); ingo@784: ingo@784: Object tmpMin = null; ingo@784: Object tmpMax = null; ingo@725: ingo@725: while (iter.hasNext()) { ingo@725: InputData tmp = (InputData) iter.next(); ingo@725: InputValue meta = inputValues.get(tmp.getName()); ingo@725: String type = meta.getType(); ingo@725: String value = tmp.getValue(); ingo@725: String name = tmp.getName(); ingo@725: ingo@725: if (meta == null) { ingo@725: String msg = "Input data not expected here. Data will be ignored."; ingo@725: logger.warn(msg); ingo@725: return feedFailure(msg); ingo@725: } ingo@725: ingo@796: if (!InputValidator.isInputValid(value, type)) { ingo@725: String msg = resFactory.getRessource( ingo@725: locale, EXCEPTION_DATE_REQUIRED, EXCEPTION_DATE_REQUIRED); ingo@725: logger.error(msg); ingo@725: return feedFailure(msg); ingo@725: } ingo@725: ingo@784: Date lower = null; ingo@784: if (min instanceof GregorianCalendar) { ingo@784: lower = ((GregorianCalendar)min).getTime(); ingo@784: } ingo@784: ingo@784: Date upper = null; ingo@784: if (max instanceof GregorianCalendar) { ingo@784: upper = ((GregorianCalendar)max).getTime(); ingo@784: } ingo@784: ingo@784: Date d = null; ingo@784: try { ingo@784: d = DateUtils.getDateFromString(value,DateUtils.DATE_PATTERN); ingo@784: } ingo@784: catch (Exception e) { ingo@784: logger.warn(e, e); ingo@784: } ingo@784: ingo@784: if (d == null || lower == null || upper == null) { ingo@784: String msg = resFactory.getRessource( ingo@784: locale, ingo@784: EXCEPTION_MISSING_DATE, ingo@784: EXCEPTION_MISSING_DATE); ingo@784: logger.warn(msg); ingo@784: } ingo@784: else { ingo@784: if (logger.isDebugEnabled()) { ingo@784: logger.debug("Date to validate: " + d.toString()); ingo@784: logger.debug("Lower date bound: " + lower.toString()); ingo@784: logger.debug("Upper date bound: " + upper.toString()); ingo@784: } ingo@784: ingo@796: if (!InputValidator.isDateValid(d, lower, upper)) { ingo@784: String msg = resFactory.getRessource( ingo@784: locale, ingo@784: EXCEPTION_DATE_OUT_OF_RANGE, ingo@784: EXCEPTION_DATE_OUT_OF_RANGE); ingo@784: logger.error(msg); ingo@784: return feedFailure(msg); ingo@784: } ingo@784: } ingo@784: ingo@725: if (name.equals(MINVALUEFIELDNAME)) { ingo@784: tmpMin = value; ingo@725: } ingo@725: ingo@725: if (name.equals(MAXVALUEFIELDNAME)) { ingo@784: tmpMax = value; ingo@725: } ingo@725: ingo@784: if (tmpMin != null && tmpMax != null) { ingo@796: if (!InputValidator.isInputValid((String) tmpMin, (String) tmpMax, type)) { ingo@725: String msg = resFactory.getRessource( ingo@725: locale, ingo@725: EXCEPTION_START_AFTER_END, ingo@725: EXCEPTION_START_AFTER_END); ingo@725: logger.error(msg); ingo@725: return feedFailure(msg); ingo@725: } ingo@725: } ingo@725: } ingo@725: ingo@725: DescribeData values = new DefaultMinMaxDescribeData( ingo@784: dataName, tmpMin, tmpMax, getID()); ingo@725: sascha@778: this.inputData.put(dataName, new DefaultInputData(dataName, values)); ingo@725: ingo@725: return feedSuccess(); ingo@725: } ingo@725: } ingo@796: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :