comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SoundingsSelect.java @ 2712:ed612b85fb6d

Implemented SoundingsSelect.getOptions() and SoundingsSelect.getLabelFor(). flys-artifacts/trunk@4435 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 18 May 2012 07:03:38 +0000
parents cd6bcca17de6
children 150dcdefeb7d
comparison
equal deleted inserted replaced
2711:fa8ae7dbcb72 2712:ed612b85fb6d
1 package de.intevation.flys.artifacts.states; 1 package de.intevation.flys.artifacts.states;
2
3 import java.util.ArrayList;
4 import java.util.List;
2 5
3 import org.apache.log4j.Logger; 6 import org.apache.log4j.Logger;
4 7
5 import de.intevation.artifacts.Artifact; 8 import de.intevation.artifacts.Artifact;
6 import de.intevation.artifacts.CallContext; 9 import de.intevation.artifacts.CallContext;
7 10
8 import de.intevation.artifacts.common.model.KVP; 11 import de.intevation.artifacts.common.model.KVP;
9 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
10 20
11 public class SoundingsSelect extends MultiStringArrayState { 21 public class SoundingsSelect extends MultiStringArrayState {
12 22
13 public static final String SOUNDINGS = "soundings"; 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-";
14 28
15 29
16 private static final Logger logger = Logger.getLogger(SoundingsSelect.class); 30 private static final Logger logger = Logger.getLogger(SoundingsSelect.class);
17 31
18 32
34 if (!testParameterName(parameterName)) { 48 if (!testParameterName(parameterName)) {
35 throw new IllegalArgumentException( 49 throw new IllegalArgumentException(
36 "Invalid parameter for state: '" + parameterName + "'"); 50 "Invalid parameter for state: '" + parameterName + "'");
37 } 51 }
38 52
39 KVP[] kvp = new KVP[10]; 53 River river = FLYSUtils.getRiver((FLYSArtifact) artifact);
40 54
41 for (int i = 0; i < 10; i++) { 55 List<KVP<String, String>> kvp = new ArrayList<KVP<String, String>>();
42 String id = String.valueOf(i);
43 String value = "#" + id;
44 56
45 kvp[i] = new KVP(id, value); 57 appendSingles(river, kvp);
58 appendEpochs(river, kvp);
59
60 return (KVP<String,String>[]) kvp.toArray(new KVP[kvp.size()]);
61 }
62
63
64 protected void appendSingles(River river, List<KVP<String, String>> kvp) {
65 List<BedHeightSingle> singles =
66 BedHeightSingle.getBedHeightSingles(river);
67
68 if (singles != null) {
69 for (int i = 0; i < singles.size(); i++) {
70 BedHeightSingle s = singles.get(i);
71
72 String id = PREFIX_SINGLE + s.getId();
73 String value = s.getDescription();
74
75 kvp.add(new KVP(id, value));
76 }
46 } 77 }
78 }
47 79
48 return kvp; 80
81 protected void appendEpochs(River river, List<KVP<String, String>> kvp) {
82 List<BedHeightEpoch> epochs =
83 BedHeightEpoch.getBedHeightEpochs(river);
84
85 if (epochs != null) {
86 for (int i = 0; i < epochs.size(); i++) {
87 BedHeightEpoch e = epochs.get(i);
88
89 String id = PREFIX_EPOCH + e.getId();
90 String value = e.getDescription();
91
92 kvp.add(new KVP(id, value));
93 }
94 }
49 } 95 }
50 96
51 97
52 @Override 98 @Override
53 protected String getLabelFor( 99 protected String getLabelFor(
59 if (!testParameterName(parameterName)) { 105 if (!testParameterName(parameterName)) {
60 throw new IllegalArgumentException( 106 throw new IllegalArgumentException(
61 "Invalid parameter for state: '" + parameterName + "'"); 107 "Invalid parameter for state: '" + parameterName + "'");
62 } 108 }
63 109
64 return "#" + value; 110 if (value.indexOf(PREFIX_SINGLE) >= 0) {
111 return getLabelForSingle(cc, value);
112 }
113 else if (value.indexOf(PREFIX_EPOCH) >= 0) {
114 return getLabelForEpoch(cc, value);
115 }
116
117 return value;
118 }
119
120
121 protected String getLabelForSingle(CallContext cc, String value) {
122 String id = value.replace(PREFIX_SINGLE, "");
123 try {
124 BedHeightSingle s = BedHeightSingle.getBedHeightSingleById(
125 Integer.parseInt(id));
126
127 if (s != null) {
128 return s.getDescription();
129 }
130 else {
131 return "no value for '" + id + "'";
132 }
133 }
134 catch (NumberFormatException nfe) {
135 logger.warn("Could not parse id from string '" + id + "'", nfe);
136 }
137
138 return "n.A.";
139 }
140
141
142 protected String getLabelForEpoch(CallContext cc, String value) {
143 String id = value.replace(PREFIX_EPOCH, "");
144 try {
145 BedHeightEpoch e = BedHeightEpoch.getBedHeightEpochById(
146 Integer.parseInt(id));
147
148 if (e != null) {
149 return e.getDescription();
150 }
151 else {
152 return "no value for '" + id + "'";
153 }
154 }
155 catch (NumberFormatException nfe) {
156 logger.warn("Could not parse id from string '" + id + "'", nfe);
157 }
158
159 return "n.A.";
65 } 160 }
66 161
67 162
68 /** 163 /**
69 * This method might be used to test, if a parameter name is handled by this 164 * This method might be used to test, if a parameter name is handled by this
84 else { 179 else {
85 return false; 180 return false;
86 } 181 }
87 } 182 }
88 } 183 }
89 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org