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: tim@335: package de.intevation.gnv.state; tim@335: tim@335: import java.util.ArrayList; tim@335: import java.util.Collection; ingo@607: import java.util.Date; ingo@607: import java.util.GregorianCalendar; ingo@607: import java.util.Iterator; ingo@470: import java.util.List; tim@1096: import java.util.Locale; tim@335: tim@335: import org.apache.log4j.Logger; ingo@607: import org.w3c.dom.Document; ingo@607: import org.w3c.dom.Element; ingo@607: import org.w3c.dom.Node; tim@335: sascha@1117: import de.intevation.artifacts.common.utils.XMLUtils; tim@823: import de.intevation.artifacts.CallContext; tim@823: import de.intevation.artifacts.CallMeta; tim@823: import de.intevation.gnv.artifacts.ressource.RessourceFactory; tim@823: import de.intevation.gnv.geobackend.base.Result; 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; tim@823: tim@335: /** ingo@796: * This state handles input of a min and max value and validates the user input. ingo@796: * The min value needs to be equal or smaller than the max value, otherwise the ingo@796: * input results in an error. ingo@796: * sascha@780: * @author Tim Englich sascha@780: * @author Ingo Weinzierl tim@335: */ tim@335: public class MinMaxState extends StateBase { tim@335: tim@335: /** tim@335: * the logger, used to log exceptions and additonaly information tim@335: */ tim@335: private static Logger log = Logger.getLogger(MinMaxState.class); tim@335: tim@1096: /** tim@1096: * Key to lookup the localized exceptionmessage in the ResourceFiles. tim@1096: */ tim@1096: public static final String EXCEPTION_INVALID_MIN_MAX_INPUT = tim@1096: "input.is.not.valid.minmax"; tim@1096: tim@1096: /** tim@335: * Constructor tim@335: */ tim@335: public MinMaxState() { tim@335: super(); tim@335: } tim@335: tim@335: /** ingo@796: * The objects returned by the database are searched for two fields with ingo@796: * 'MIN' and 'MAX' as names. These objects are stored and used to be ingo@796: * displayed in the gui to give the user an orientation of the range he is ingo@796: * able to insert. tim@335: */ tim@335: @Override ingo@607: protected List purifyResult( ingo@607: Collection result, String uuid) ingo@607: { ingo@607: List describeData = new ArrayList(); ingo@607: tim@335: if (result != null && result.size() == 1) { tim@335: Result value = result.iterator().next(); ingo@473: DescribeData values = new DefaultMinMaxDescribeData( ingo@607: dataName, ingo@607: value.getObject("MIN"), ingo@473: value.getObject("MAX"), ingo@473: getID()); tim@335: describeData.add(values); tim@335: } else { tim@335: log.warn("Result cannot be handled as MinMax Resultset"); tim@335: } ingo@607: ingo@607: return describeData; tim@335: } tim@335: ingo@607: ingo@607: @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: ingo@725: if (inputData == null) { ingo@725: String msg = "No input data given."; ingo@725: log.warn(msg); ingo@725: return feedFailure(msg); ingo@725: } ingo@607: ingo@607: InputValidator iv = new InputValidator(); ingo@607: Iterator iter = inputData.iterator(); ingo@607: ingo@607: Object min = null; ingo@607: Object max = null; ingo@607: ingo@607: while (iter.hasNext()) { ingo@607: InputData tmp = (InputData) iter.next(); ingo@607: InputValue meta = inputValues.get(tmp.getName()); ingo@725: String type = meta.getType(); ingo@607: String value = tmp.getValue(); ingo@607: String name = tmp.getName(); ingo@607: ingo@607: if (meta == null) { ingo@725: String msg = "Input data not expected here. Data will be ignored."; ingo@725: log.warn(msg); ingo@725: return feedFailure(msg); ingo@607: } ingo@607: ingo@796: boolean valid = InputValidator.isInputValid(value, type); ingo@607: if (!valid) { ingo@607: String msg = "Input is not valid for this state."; ingo@607: log.error(msg); ingo@725: return feedFailure(msg); ingo@607: } ingo@607: ingo@607: if (name.equals(MINVALUEFIELDNAME)) { ingo@607: min = value; ingo@607: } ingo@607: ingo@607: if (name.equals(MAXVALUEFIELDNAME)) { ingo@607: max = value; ingo@607: } ingo@725: ingo@725: if (min != null && max != null) { tim@1096: if (!InputValidator.isInputValid((String) min, tim@1096: (String) max, tim@1096: type)) { tim@1096: Locale[] serverLocales = resFactory.getLocales(); tim@1096: Locale locale = context.getMeta() tim@1096: .getPreferredLocale( tim@1096: serverLocales); tim@1096: String msg = resFactory.getRessource(locale, tim@1096: EXCEPTION_INVALID_MIN_MAX_INPUT, tim@1096: EXCEPTION_INVALID_MIN_MAX_INPUT); ingo@725: log.error(msg); ingo@725: return feedFailure(msg); ingo@725: } ingo@725: } ingo@607: } ingo@607: sascha@778: ingo@607: ingo@725: DescribeData values = new DefaultMinMaxDescribeData( ingo@725: dataName, min, max, getID()); ingo@725: sascha@778: this.inputData.put(dataName, new DefaultInputData(dataName, values)); ingo@725: ingo@725: return feedSuccess(); ingo@607: } ingo@607: ingo@607: ingo@607: @Override ingo@607: protected void appendToStaticNode( ingo@607: XMLUtils.ElementCreator artCreator, ingo@607: XMLUtils.ElementCreator creator, ingo@607: Document document, ingo@607: Node staticNode, ingo@608: CallMeta callMeta ingo@607: ) { ingo@607: InputData data = inputData.get(dataName); ingo@607: ingo@607: if (data == null) { ingo@607: return; ingo@607: } ingo@607: ingo@607: MinMaxDescribeData minMax = (MinMaxDescribeData) data.getObject(); ingo@607: ingo@607: Object min = minMax.getMinValue(); ingo@607: Object max = minMax.getMaxValue(); ingo@607: if (min instanceof GregorianCalendar) { ingo@607: Date d = ((GregorianCalendar) min).getTime(); ingo@607: min = DateUtils.getPatternedDateAmer(d); ingo@607: } ingo@607: ingo@607: if (max instanceof GregorianCalendar) { ingo@607: Date d = ((GregorianCalendar) max).getTime(); ingo@607: max = DateUtils.getPatternedDateAmer(d); ingo@607: } ingo@607: ingo@607: Element groupNode = creator.create("group"); ingo@607: artCreator.addAttr(groupNode, "state", minMax.getState(), true); ingo@607: ingo@607: creator.addAttr(groupNode, "ref", minMax.getName()); ingo@607: Element groupNodeLableNode = creator.create("label"); ingo@607: groupNodeLableNode.setTextContent(RessourceFactory ingo@607: .getInstance().getRessource( ingo@607: callMeta.getLanguages(), ingo@607: minMax.getName(), ingo@607: minMax.getName())); ingo@607: groupNode.appendChild(groupNodeLableNode); ingo@607: ingo@607: Element inputMinNode = creator.create("input"); ingo@607: creator.addAttr(inputMinNode, "ref", MINVALUEFIELDNAME); ingo@607: Element inputMinLableNode = creator.create("label"); ingo@607: inputMinLableNode.setTextContent(RessourceFactory ingo@607: .getInstance().getRessource( ingo@607: callMeta.getLanguages(), MINVALUEFIELDNAME, ingo@607: MINVALUEFIELDNAME)); ingo@607: inputMinNode.appendChild(inputMinLableNode); ingo@607: ingo@607: Element inputMinValueNode = creator.create("value"); ingo@607: inputMinValueNode.setTextContent(min.toString()); ingo@607: inputMinNode.appendChild(inputMinValueNode); ingo@607: ingo@607: Element inputMaxNode = creator.create("input"); ingo@607: creator.addAttr(inputMaxNode, "ref", MAXVALUEFIELDNAME); ingo@607: Element inputMaxLableNode = creator.create("label"); ingo@607: inputMaxLableNode.setTextContent(RessourceFactory ingo@607: .getInstance().getRessource( ingo@607: callMeta.getLanguages(), MAXVALUEFIELDNAME, ingo@607: MAXVALUEFIELDNAME)); ingo@607: inputMaxNode.appendChild(inputMaxLableNode); ingo@607: ingo@607: Element inputMaxValueNode = creator.create("value"); ingo@607: inputMaxValueNode.setTextContent(max.toString()); ingo@607: inputMaxNode.appendChild(inputMaxValueNode); ingo@607: ingo@607: groupNode.appendChild(inputMinNode); ingo@607: groupNode.appendChild(inputMaxNode); ingo@607: ingo@607: staticNode.appendChild(groupNode); ingo@607: } tim@335: } ingo@796: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :