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