comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SoundingsSelect.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 200e70f31f6f
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.states;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import de.intevation.artifacts.Artifact;
9 import de.intevation.artifacts.CallContext;
10
11 import de.intevation.artifacts.common.model.KVP;
12
13 import de.intevation.flys.model.BedHeightEpoch;
14 import de.intevation.flys.model.BedHeightSingle;
15 import de.intevation.flys.model.River;
16
17 import de.intevation.flys.artifacts.FLYSArtifact;
18 import de.intevation.flys.utils.FLYSUtils;
19
20
21 public class SoundingsSelect extends MultiStringArrayState {
22
23 public static final String SOUNDINGS = "soundings";
24
25 public static final String PREFIX_SINGLE = "single-";
26
27 public static final String PREFIX_EPOCH = "epoch-";
28
29
30 private static final Logger logger = Logger.getLogger(SoundingsSelect.class);
31
32
33 @Override
34 public String getUIProvider() {
35 return "parameter-matrix";
36 }
37
38
39 @Override
40 protected KVP<String, String>[] getOptions(
41 Artifact artifact,
42 String parameterName
43 )
44 throws IllegalArgumentException
45 {
46 logger.debug("Get options for parameter: '" + parameterName + "'");
47
48 if (!testParameterName(parameterName)) {
49 throw new IllegalArgumentException(
50 "Invalid parameter for state: '" + parameterName + "'");
51 }
52
53 River river = FLYSUtils.getRiver((FLYSArtifact) artifact);
54 double lo = ((FLYSArtifact) artifact).getDataAsDouble("ld_from");
55 double hi = ((FLYSArtifact) artifact).getDataAsDouble("ld_to");
56
57 double kmLo = Math.min(lo, hi);
58 double kmHi = Math.max(lo, hi);
59
60 List<KVP<String, String>> kvp = new ArrayList<KVP<String, String>>();
61
62 appendSingles(river, kmLo, kmHi, kvp);
63 appendEpochs(river, kmLo, kmHi, kvp);
64
65 return (KVP<String,String>[]) kvp.toArray(new KVP[kvp.size()]);
66 }
67
68
69 protected void appendSingles(
70 River river,
71 double kmLo,
72 double kmHi,
73 List<KVP<String, String>> kvp
74 ) {
75 List<BedHeightSingle> singles =
76 BedHeightSingle.getBedHeightSingles(river, kmLo, kmHi);
77
78 if (singles != null) {
79 for (int i = 0, S = singles.size(); i < S; i++) {
80 BedHeightSingle s = singles.get(i);
81
82 String id = PREFIX_SINGLE + s.getId();
83 String value = s.getDescription();
84
85 kvp.add(new KVP(id, value));
86 }
87 }
88 }
89
90
91 protected void appendEpochs(
92 River river,
93 double kmLo,
94 double kmHi,
95 List<KVP<String, String>> kvp
96 ) {
97 List<BedHeightEpoch> epochs =
98 BedHeightEpoch.getBedHeightEpochs(river, kmLo, kmHi);
99
100 if (epochs != null) {
101 for (int i = 0, E = epochs.size(); i < E; i++) {
102 BedHeightEpoch e = epochs.get(i);
103
104 String id = PREFIX_EPOCH + e.getId();
105 String value = e.getDescription();
106
107 kvp.add(new KVP(id, value));
108 }
109 }
110 }
111
112
113 @Override
114 protected String getLabelFor(
115 CallContext cc,
116 String parameterName,
117 String value
118 ) throws IllegalArgumentException
119 {
120 if (!testParameterName(parameterName)) {
121 throw new IllegalArgumentException(
122 "Invalid parameter for state: '" + parameterName + "'");
123 }
124
125 if (value.indexOf(PREFIX_SINGLE) >= 0) {
126 return getLabelForSingle(cc, value);
127 }
128 else if (value.indexOf(PREFIX_EPOCH) >= 0) {
129 return getLabelForEpoch(cc, value);
130 }
131
132 return value;
133 }
134
135
136 protected String getLabelForSingle(CallContext cc, String value) {
137 String id = value.replace(PREFIX_SINGLE, "");
138 try {
139 BedHeightSingle s = BedHeightSingle.getBedHeightSingleById(
140 Integer.parseInt(id));
141
142 if (s != null) {
143 return s.getDescription();
144 }
145 else {
146 return "no value for '" + id + "'";
147 }
148 }
149 catch (NumberFormatException nfe) {
150 logger.warn("Could not parse id from string '" + id + "'", nfe);
151 }
152
153 return "n.A.";
154 }
155
156
157 protected String getLabelForEpoch(CallContext cc, String value) {
158 String id = value.replace(PREFIX_EPOCH, "");
159 try {
160 BedHeightEpoch e = BedHeightEpoch.getBedHeightEpochById(
161 Integer.parseInt(id));
162
163 if (e != null) {
164 return e.getDescription();
165 }
166 else {
167 return "no value for '" + id + "'";
168 }
169 }
170 catch (NumberFormatException nfe) {
171 logger.warn("Could not parse id from string '" + id + "'", nfe);
172 }
173
174 return "n.A.";
175 }
176
177
178 /**
179 * This method might be used to test, if a parameter name is handled by this
180 * state.
181 *
182 * @param parameterName The name of a parameter.
183 *
184 * @return true, if parameterName is one of <i>MAIN_CHANNEL</i> or
185 * <i>TOTAL_CHANNEL</i>. Otherwise false.
186 */
187 protected boolean testParameterName(String parameterName) {
188 if (parameterName == null || parameterName.length() == 0) {
189 return false;
190 }
191 else if (parameterName.equals(SOUNDINGS)) {
192 return true;
193 }
194 else {
195 return false;
196 }
197 }
198 }
199 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org