comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelSelectState.java @ 3468:f37e7e8907cb

merged flys-artifacts/2.8.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:39 +0200
parents 5642a83420f2
children e8a4d2fd25cc
comparison
equal deleted inserted replaced
3387:5ffad8bde8ad 3468:f37e7e8907cb
1 package de.intevation.flys.artifacts.states;
2
3 import org.apache.log4j.Logger;
4
5 import org.w3c.dom.Element;
6
7 import de.intevation.artifacts.Artifact;
8 import de.intevation.artifacts.CallContext;
9
10 import de.intevation.artifactdatabase.data.DefaultStateData;
11 import de.intevation.artifactdatabase.data.StateData;
12
13 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
14
15 import de.intevation.flys.artifacts.FLYSArtifact;
16 import de.intevation.flys.artifacts.model.CalculationResult;
17 import de.intevation.flys.artifacts.model.WQKms;
18 import de.intevation.flys.artifacts.resources.Resources;
19 import de.intevation.flys.utils.FLYSUtils;
20 import de.intevation.flys.utils.StringUtil;
21
22
23 /**
24 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
25 */
26 public class WaterlevelSelectState extends DefaultState {
27
28 private static final Logger logger =
29 Logger.getLogger(WaterlevelSelectState.class);
30
31 public static final String SPLIT_CHAR = ";";
32
33 public static final String WINFO_WSP_STATE_ID = "state.winfo.waterlevel";
34
35 public static final String I18N_STATIC_KEY = "wsp.selected.string";
36
37
38 @Override
39 protected String getUIProvider() {
40 return "wsp_datacage_panel";
41 }
42
43
44 /**
45 * @param flys ignored
46 * @param cc ignrored
47 */
48 @Override
49 public StateData transform(
50 FLYSArtifact flys,
51 CallContext cc,
52 StateData stateData,
53 String name,
54 String val
55 ) {
56 if (!isValueValid(val)) {
57 logger.error("The given input string is not valid: '" + val + "'");
58 return null;
59 }
60
61 return new DefaultStateData(name, null, null, StringUtil.unbracket(val));
62 }
63
64
65 @Override
66 public boolean validate(Artifact artifact)
67 throws IllegalArgumentException
68 {
69 FLYSArtifact flys = (FLYSArtifact) artifact;
70
71 StateData data = flys.getData("wsp");
72
73 if (data == null) {
74 throw new IllegalArgumentException("WSP is empty");
75 }
76
77 return true;
78 }
79
80
81 @Override
82 protected Element createStaticData(
83 FLYSArtifact flys,
84 ElementCreator creator,
85 CallContext cc,
86 String name,
87 String value,
88 String type
89 ) {
90 Element dataElement = creator.create("data");
91 creator.addAttr(dataElement, "name", name, true);
92 creator.addAttr(dataElement, "type", type, true);
93
94 Element itemElement = creator.create("item");
95 creator.addAttr(itemElement, "value", value, true);
96
97 String[] labels = getLabels(cc, value);
98 Object[] obj = new Object[] { labels[0] };
99
100 String attrValue = Resources.getMsg(
101 cc.getMeta(), I18N_STATIC_KEY, I18N_STATIC_KEY, obj);
102
103 creator.addAttr(itemElement, "label", attrValue, true);
104 dataElement.appendChild(itemElement);
105
106 return dataElement;
107 }
108
109
110 /**
111 * Get name to display for selected watelerlevel (for example "Q=123")
112 * from the CalculationResult.
113 */
114 public static String[] getLabels(CallContext cc, String value) {
115 String[] parts = value.split(SPLIT_CHAR);
116
117 FLYSArtifact artifact = FLYSUtils.getArtifact(parts[0], cc);
118
119 CalculationResult rawData = (CalculationResult) artifact.compute(
120 cc,
121 null,
122 WINFO_WSP_STATE_ID,
123 ComputeType.ADVANCE,
124 false);
125
126 WQKms[] wqkms = (WQKms[]) rawData.getData();
127
128 int idx = -1;
129 try {
130 idx = Integer.parseInt(parts[2]);
131 }
132 catch (NumberFormatException nfe) { /* do nothing */ }
133
134 String name = wqkms[idx].getName();
135
136 return new String[] { StringUtil.wWrap(name) };
137 }
138
139
140 /**
141 * Validates the given String. A valid string for this state requires the
142 * format: "UUID;FACETNAME;FACETINDEX".
143 *
144 * @param value The string value requires validation.
145 *
146 * @return true, if the string applies the specified format, otherwise
147 * false.
148 */
149 public static boolean isValueValid(String value) {
150 logger.debug("Validate string: '" + value + "'");
151
152 value = StringUtil.unbracket(value);
153
154 logger.debug("Validate substring: '" + value + "'");
155
156 if (value == null || value.length() == 0) {
157 return false;
158 }
159
160 String[] parts = value.split(SPLIT_CHAR);
161
162 if (parts == null || parts.length < 3) {
163 return false;
164 }
165
166 if (parts[0] == null || parts[0].length() == 0) {
167 return false;
168 }
169
170 if (parts[1] == null || parts[1].length() == 0) {
171 return false;
172 }
173
174 try {
175 Integer.parseInt(parts[2]);
176 }
177 catch (NumberFormatException nfe) {
178 logger.error("Index is not a valid integer!", nfe);
179 }
180
181 return true;
182 }
183 }
184 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org