comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/DischargeState.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/DischargeState.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.states;
2
3 import java.util.List;
4
5 import org.apache.log4j.Logger;
6
7 import org.dive4elements.artifacts.Artifact;
8 import org.dive4elements.artifacts.CallContext;
9
10 import org.dive4elements.artifacts.common.model.KVP;
11
12 import org.dive4elements.river.model.DischargeZone;
13 import org.dive4elements.river.model.River;
14
15 import org.dive4elements.river.artifacts.FLYSArtifact;
16 import org.dive4elements.river.utils.FLYSUtils;
17
18
19 public class DischargeState extends MultiIntArrayState {
20
21 public static final String MAIN_CHANNEL = "main_channel";
22 public static final String TOTAL_CHANNEL = "total_channel";
23
24
25 private static final Logger logger = Logger.getLogger(DischargeState.class);
26
27
28 /** Let client display a matrix. */
29 @Override
30 public String getUIProvider() {
31 return "parameter-matrix";
32 }
33
34
35 /**
36 * This method fetches all DischargeZones for a given river (extracted from
37 * <i>artifact</i>) and returns a KVP[] where the key is the ID of the
38 * DischargeZone and the value is a string that consists of lower discharge
39 * and upper discharge.
40 *
41 * @param artifact Needs to be a FLYSArtifact that provides river
42 * information.
43 * @param parameterName The name of a parameter.
44 *
45 * @return a KVP[].
46 */
47 @Override
48 protected KVP<Integer, String>[] getOptions(
49 Artifact artifact,
50 String parameterName
51 )
52 throws IllegalArgumentException
53 {
54 if (!testParameterName(parameterName)) {
55 throw new IllegalArgumentException(
56 "Invalid parameter for state: '" + parameterName + "'");
57 }
58
59 List<DischargeZone> zones = getDischargeZones(artifact);
60
61 KVP[] kvp = new KVP[zones.size()];
62
63 for (int i = 0, Z = zones.size(); i < Z; i++) {
64 DischargeZone zone = zones.get(i);
65
66 String lower = zone.getLowerDischarge();
67 String upper = zone.getUpperDischarge();
68
69 if (lower.equals(upper)) {
70 kvp[i] = new KVP(zone.getId(), lower);
71 }
72 else {
73 kvp[i] = new KVP(zone.getId(), lower + " - " + upper);
74 }
75 }
76
77 return kvp;
78 }
79
80
81 @Override
82 protected String getLabelFor(
83 CallContext cc,
84 String parameterName,
85 int value
86 ) throws IllegalArgumentException
87 {
88 if (!testParameterName(parameterName)) {
89 throw new IllegalArgumentException(
90 "Invalid parameter for state: '" + parameterName + "'");
91 }
92
93 DischargeZone zone = DischargeZone.getDischargeZoneById(value);
94
95 if (zone == null) {
96 throw new IllegalArgumentException(
97 "Invalid id for DischargeZone: '" + value + "'");
98 }
99
100 String lo = zone.getLowerDischarge();
101 String hi = zone.getUpperDischarge();
102
103 return hi != null && !lo.equals(hi)
104 ? lo + " - " + hi
105 : lo;
106 }
107
108
109 /**
110 * This method might be used to test, if a parameter name is handled by this
111 * state.
112 *
113 * @param parameterName The name of a parameter.
114 *
115 * @return true, if parameterName is one of <i>MAIN_CHANNEL</i> or
116 * <i>TOTAL_CHANNEL</i>. Otherwise false.
117 */
118 protected boolean testParameterName(String parameterName) {
119 if (parameterName == null || parameterName.length() == 0) {
120 return false;
121 }
122 else if (parameterName.equals(MAIN_CHANNEL)) {
123 return true;
124 }
125 else if (parameterName.equals(TOTAL_CHANNEL)) {
126 return true;
127 }
128 else {
129 return false;
130 }
131 }
132
133
134 /**
135 * Returns all discharge zones for a given river. The river information is
136 * extracted from <i>artifact</i> using FLYSUtils.getRiver().
137 *
138 * @param artifact Needs to be a FLYSArtifact that stores a rivername.
139 *
140 * @return a list of DischargeZones.
141 *
142 * @throws IllegalArgumentException if no river information is provided by
143 * <i>artifact</i>.
144 */
145 protected List<DischargeZone> getDischargeZones(Artifact artifact)
146 throws IllegalArgumentException
147 {
148 River river = FLYSUtils.getRiver((FLYSArtifact) artifact);
149
150 if (river == null) {
151 throw new IllegalArgumentException("No river found");
152 }
153
154 List<DischargeZone> zones = DischargeZone.getDischargeZones(river);
155
156 logger.debug("Found " + zones.size() + " DischargeZones.");
157
158 return zones;
159 }
160 }
161 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org