comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/DischargeState.java @ 3318:dbe2f85bf160

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

http://dive4elements.wald.intevation.org