comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/SingleInputState.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 package de.intevation.gnv.state;
10
11 import de.intevation.artifacts.CallContext;
12
13 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
14 import de.intevation.gnv.geobackend.base.Result;
15 import de.intevation.gnv.state.describedata.DefaultSingleValueDescribeData;
16 import de.intevation.gnv.state.exception.StateException;
17 import de.intevation.gnv.utils.InputValidator;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Locale;
24
25 import org.apache.log4j.Logger;
26
27 import org.w3c.dom.Document;
28
29 /**
30 * This state handles single user input. The user is allowed to select just one
31 * value.
32 *
33 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
34 *
35 */
36 public class SingleInputState extends StateBase {
37
38 /**
39 * the logger, used to log exceptions and additonaly information
40 */
41 private static Logger log = Logger.getLogger(SingleInputState.class);
42
43 private static final long serialVersionUID = -6169497306324917318L;
44
45 /**
46 * Constructor
47 */
48 public SingleInputState() {
49 }
50
51
52 @Override
53 protected List<Object> purifyResult(Collection<Result> result, String uuid) {
54 log.debug("SingleInputState.purifyResult");
55 List<Object> describeData = new ArrayList<Object>();
56
57 String value = null;
58 if (result != null && result.size() == 1) {
59 Result tmpItem = result.iterator().next();
60 value = tmpItem.getObject("MAX").toString();
61 } else {
62 value = "";
63 }
64
65 describeData.add(new DefaultSingleValueDescribeData(
66 this.dataName, value, getID()));
67
68 return describeData;
69 }
70
71 /**
72 * This feed method needs a collection of two InputData objects. These
73 * objects' values need to be a datetime string which is turned into a Date
74 * object. Afterwards, the given dates are validated. Min and max date need
75 * to be in range of the min and max date retrieved by
76 * {@link #getDescibeData(java.lang.String)}.
77 */
78 @Override
79 public Document feed(
80 CallContext context,
81 Collection<InputData> inputData,
82 String uuid)
83 throws StateException {
84 RessourceFactory resFactory = RessourceFactory.getInstance();
85 Locale[] serverLocales = resFactory.getLocales();
86 Locale locale = context.getMeta().getPreferredLocale(
87 serverLocales);
88
89 if (inputData == null) {
90 String msg = "No input data given.";
91 log.warn(msg);
92 return feedFailure(msg);
93 }
94
95 Iterator<InputData> it = inputData.iterator();
96 InputData tmpItem = it.next();
97 InputValue inputValue = inputValues.get(tmpItem.getName());
98
99 if (inputValue == null) {
100 String msg = resFactory.getRessource(
101 locale,
102 EXCEPTION_INVALID_INPUT,
103 EXCEPTION_INVALID_INPUT);
104 log.warn(msg);
105 return feedFailure(msg);
106 }
107
108 boolean valid = InputValidator.isInputValid(
109 tmpItem.getValue(), inputValue.getType());
110
111 if (valid) {
112 String[] desc = getDescriptionForInputData(tmpItem, context, uuid);
113 tmpItem.setDescription(desc);
114
115 this.inputData.put(tmpItem.getName(), tmpItem);
116 return feedSuccess();
117 }
118
119 else {
120 String msg = resFactory.getRessource(
121 locale,
122 EXCEPTION_INVALID_INPUT,
123 EXCEPTION_INVALID_INPUT);
124 log.warn(msg);
125 return feedFailure(msg);
126 }
127 }
128 }
129 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org