Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/WaterlevelSelectState.java @ 1178:1b432c260e97
Enabled the FloodMapState to fetch waterlevel data from external Artifact.
flys-artifacts/trunk@2764 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 15 Sep 2011 15:36:05 +0000 |
parents | 23eb122c8ec1 |
children | 1aba1a75beb2 |
comparison
equal
deleted
inserted
replaced
1177:70b8ac64404b | 1178:1b432c260e97 |
---|---|
1 package de.intevation.flys.artifacts.states; | 1 package de.intevation.flys.artifacts.states; |
2 | |
3 import org.apache.log4j.Logger; | |
4 | |
5 import de.intevation.artifacts.Artifact; | |
6 import de.intevation.artifacts.CallContext; | |
7 | |
8 import de.intevation.artifactdatabase.data.DefaultStateData; | |
9 import de.intevation.artifactdatabase.data.StateData; | |
10 | |
11 import de.intevation.flys.artifacts.FLYSArtifact; | |
12 import de.intevation.flys.utils.FLYSUtils; | |
13 | |
2 | 14 |
3 /** | 15 /** |
4 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
5 */ | 17 */ |
6 public class WaterlevelSelectState extends DefaultState { | 18 public class WaterlevelSelectState extends DefaultState { |
7 | 19 |
20 private static final Logger logger = | |
21 Logger.getLogger(WaterlevelSelectState.class); | |
22 | |
23 public static final String SPLIT_CHAR = ";"; | |
24 | |
25 | |
8 @Override | 26 @Override |
9 protected String getUIProvider() { | 27 protected String getUIProvider() { |
10 return "wsp_datacage_panel"; | 28 return "wsp_datacage_panel"; |
11 } | 29 } |
30 | |
31 | |
32 @Override | |
33 public StateData transform( | |
34 FLYSArtifact flys, | |
35 CallContext cc, | |
36 String name, | |
37 String val | |
38 ) { | |
39 if (!isValueValid(val)) { | |
40 logger.error("The given input string is not valid: '" + val + "'"); | |
41 return null; | |
42 } | |
43 | |
44 return new DefaultStateData(name, null, null, strip(val)); | |
45 } | |
46 | |
47 | |
48 @Override | |
49 public boolean validate(Artifact artifact) | |
50 throws IllegalArgumentException | |
51 { | |
52 FLYSArtifact flys = (FLYSArtifact) artifact; | |
53 | |
54 StateData data = flys.getData("wsp"); | |
55 | |
56 if (data == null) { | |
57 throw new IllegalArgumentException("WSP is empty"); | |
58 } | |
59 | |
60 return true; | |
61 } | |
62 | |
63 | |
64 public static String strip(String value) { | |
65 int start = value.indexOf("["); | |
66 int end = value.indexOf("]"); | |
67 | |
68 if (start < 0 || end < 0) { | |
69 return value; | |
70 } | |
71 | |
72 value = value.substring(start+1, end); | |
73 | |
74 return value; | |
75 } | |
76 | |
77 | |
78 /** | |
79 * Validates the given String. A valid string for this state requires the | |
80 * format: "UUID;FACETNAME;FACETINDEX". | |
81 * | |
82 * @param value The string value requires validation. | |
83 * | |
84 * @return true, if the string applies the specified format, otherwise | |
85 * false. | |
86 */ | |
87 public static boolean isValueValid(String value) { | |
88 logger.debug("Validate string: '" + value + "'"); | |
89 | |
90 value = strip(value); | |
91 | |
92 logger.debug("Validate substring: '" + value + "'"); | |
93 | |
94 if (value == null || value.length() == 0) { | |
95 return false; | |
96 } | |
97 | |
98 String[] parts = value.split(SPLIT_CHAR); | |
99 | |
100 if (parts == null || parts.length < 3) { | |
101 return false; | |
102 } | |
103 | |
104 if (parts[0] == null || parts[0].length() == 0) { | |
105 return false; | |
106 } | |
107 | |
108 if (parts[1] == null || parts[1].length() == 0) { | |
109 return false; | |
110 } | |
111 | |
112 try { | |
113 Integer.parseInt(parts[2]); | |
114 } | |
115 catch (NumberFormatException nfe) { | |
116 logger.error("Index is not a valid integer!", nfe); | |
117 } | |
118 | |
119 return true; | |
120 } | |
12 } | 121 } |
13 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 122 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |