comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/WaterlevelSelectState.java @ 5831:bd047b71ab37

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

http://dive4elements.wald.intevation.org