comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/SingleInputState.java @ 860:f94a95009423

Improved the input validator: empty strings are invalid (issue251). gnv-artifacts/trunk@989 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 26 Apr 2010 13:31:11 +0000
parents 05bf8534a35a
children 9981452c7e75
comparison
equal deleted inserted replaced
859:3fbabd4803d7 860:f94a95009423
1 package de.intevation.gnv.state; 1 package de.intevation.gnv.state;
2 2
3 import de.intevation.artifacts.CallContext;
4
5 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
3 import de.intevation.gnv.geobackend.base.Result; 6 import de.intevation.gnv.geobackend.base.Result;
4
5 import de.intevation.gnv.state.describedata.DefaultSingleValueDescribeData; 7 import de.intevation.gnv.state.describedata.DefaultSingleValueDescribeData;
8 import de.intevation.gnv.state.exception.StateException;
9 import de.intevation.gnv.utils.InputValidator;
6 10
7 import java.util.ArrayList; 11 import java.util.ArrayList;
8 import java.util.Collection; 12 import java.util.Collection;
13 import java.util.Iterator;
9 import java.util.List; 14 import java.util.List;
15 import java.util.Locale;
10 16
11 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
18
19 import org.w3c.dom.Document;
12 20
13 /** 21 /**
14 * This state handles single user input. The user is allowed to select just one 22 * This state handles single user input. The user is allowed to select just one
15 * value. 23 * value.
16 * 24 *
49 describeData.add(new DefaultSingleValueDescribeData( 57 describeData.add(new DefaultSingleValueDescribeData(
50 this.dataName, value, getID())); 58 this.dataName, value, getID()));
51 59
52 return describeData; 60 return describeData;
53 } 61 }
62
63 /**
64 * This feed method needs a collection of two InputData objects. These
65 * objects' values need to be a datetime string which is turned into a Date
66 * object. Afterwards, the given dates are validated. Min and max date need
67 * to be in range of the min and max date retrieved by
68 * {@link #getDescibeData(java.lang.String)}.
69 */
70 @Override
71 public Document feed(
72 CallContext context,
73 Collection<InputData> inputData,
74 String uuid)
75 throws StateException {
76 RessourceFactory resFactory = RessourceFactory.getInstance();
77 Locale[] serverLocales = resFactory.getLocales();
78 Locale locale = context.getMeta().getPreferredLocale(
79 serverLocales);
80
81 if (inputData == null) {
82 String msg = "No input data given.";
83 log.warn(msg);
84 return feedFailure(msg);
85 }
86
87 Iterator<InputData> it = inputData.iterator();
88 InputData tmpItem = it.next();
89 InputValue inputValue = inputValues.get(tmpItem.getName());
90
91 if (inputValue == null) {
92 String msg = resFactory.getRessource(
93 locale,
94 EXCEPTION_INVALID_INPUT,
95 EXCEPTION_INVALID_INPUT);
96 log.warn(msg);
97 return feedFailure(msg);
98 }
99
100 boolean valid = InputValidator.isInputValid(
101 tmpItem.getValue(), inputValue.getType());
102
103 if (valid) {
104 String[] desc = getDescriptionForInputData(tmpItem, uuid);
105 tmpItem.setDescription(desc);
106
107 this.inputData.put(tmpItem.getName(), tmpItem);
108 return feedSuccess();
109 }
110
111 else {
112 String msg = resFactory.getRessource(
113 locale,
114 EXCEPTION_INVALID_INPUT,
115 EXCEPTION_INVALID_INPUT);
116 log.warn(msg);
117 return feedFailure(msg);
118 }
119 }
54 } 120 }
55 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 121 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org