comparison flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java @ 2038:f73036b991e2

Bugfix: repaired broken named values in exports - the last solution had bad side effects. flys-artifacts/trunk@3515 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 21 Dec 2011 15:44:41 +0000
parents 3ebbe497d7f7
children d13be39cfd1d
comparison
equal deleted inserted replaced
2037:986820795c3b 2038:f73036b991e2
12 12
13 import org.apache.log4j.Logger; 13 import org.apache.log4j.Logger;
14 14
15 import au.com.bytecode.opencsv.CSVWriter; 15 import au.com.bytecode.opencsv.CSVWriter;
16 16
17 import de.intevation.artifacts.Artifact;
18 import de.intevation.artifacts.CallContext; 17 import de.intevation.artifacts.CallContext;
19
20 import de.intevation.artifactdatabase.state.ArtifactAndFacet;
21 18
22 import de.intevation.flys.artifacts.WINFOArtifact; 19 import de.intevation.flys.artifacts.WINFOArtifact;
23 import de.intevation.flys.artifacts.model.CalculationResult; 20 import de.intevation.flys.artifacts.model.CalculationResult;
24 import de.intevation.flys.artifacts.model.WQCKms; 21 import de.intevation.flys.artifacts.model.WQCKms;
25 import de.intevation.flys.artifacts.model.WQKms; 22 import de.intevation.flys.artifacts.model.WQKms;
98 } 95 }
99 } 96 }
100 97
101 98
102 /** 99 /**
103 * This method is used to determine, if the name of the WQKms object
104 * <i>data</i> matches a named value. In such cases, the name of <i>data</i>
105 * is reset to this named value.
106 *
107 * @param aaf The wrapper for the Artifact and Facet.
108 * @param data Needs to be a CalculationResult that contains a WQKms[]
109 * objects, otherwise nothing is done.
110 */
111 @Override
112 protected void prepareData(ArtifactAndFacet aaf, Object data) {
113 logger.debug("WaterlevelExporter.prepareData");
114
115 Artifact artifact = aaf.getArtifact();
116
117 if (!(artifact instanceof WINFOArtifact)) {
118 logger.debug("Artifact is no WINFOArtifact. Cannot prepare data.");
119 return;
120 }
121
122 if (data instanceof CalculationResult) {
123 data = ((CalculationResult) data).getData();
124
125 if (data instanceof WQKms[]) {
126 WQKms[] wqkms = (WQKms[]) data;
127
128 for (WQKms item: wqkms) {
129 prepareNamedValue((WINFOArtifact) artifact, item);
130 }
131 }
132 }
133 }
134
135
136 /**
137 * This method is used to prepare the column titles of waterlevel exports. 100 * This method is used to prepare the column titles of waterlevel exports.
138 * Titles in this export include the Q value. If a Q value matches a named 101 * Titles in this export include the Q value. If a Q value matches a named
139 * main value (as HQ100 or MNQ) this named main value should be used as 102 * main value (as HQ100 or MNQ) this named main value should be used as
140 * title. This method resets the name of the <i>wqkms</i> object if such 103 * title. This method resets the name of the <i>wqkms</i> object if such
141 * named main value fits to the chosen Q. 104 * named main value fits to the chosen Q.
142 * 105 *
143 * @param winfo A WINFO Artifact. 106 * @param winfo A WINFO Artifact.
144 * @param wqkms A WQKms object that should be prepared. 107 * @param wqkms A WQKms object that should be prepared.
145 */ 108 */
146 protected void prepareNamedValue(WINFOArtifact winfo, WQKms wqkms) { 109 protected String getColumnTitle(WINFOArtifact winfo, WQKms wqkms) {
147 logger.debug("WaterlevelExporter.prepareNamedValue"); 110 logger.debug("WaterlevelExporter.prepareNamedValue");
148 111
149 String name = wqkms.getName(); 112 String name = wqkms.getName();
150 113
151 logger.debug("Name of WQKms = '" + name + "'"); 114 logger.debug("Name of WQKms = '" + name + "'");
115
116 if (name.indexOf("W=") >= 0) {
117 return name;
118 }
152 119
153 Matcher m = NUMBERS_PATTERN.matcher(name); 120 Matcher m = NUMBERS_PATTERN.matcher(name);
154 121
155 if (m.matches()) { 122 if (m.matches()) {
156 String raw = m.group(1); 123 String raw = m.group(1);
157 124
158 try { 125 try {
159 double v = Double.valueOf(raw); 126 double v = Double.valueOf(raw);
160 127
161 String nmv = FLYSUtils.getNamedMainValue(winfo.getGauge(), v); 128 String nmv = FLYSUtils.getNamedMainValue(winfo, v);
162 129
163 if (nmv != null && nmv.length() > 0) { 130 if (nmv != null && nmv.length() > 0) {
164 nmv = FLYSUtils.stripNamedMainValue(nmv); 131 nmv = FLYSUtils.stripNamedMainValue(nmv);
165 logger.debug("Set named main value '" + nmv + "'"); 132 logger.debug("Set named main value '" + nmv + "'");
166 133
167 wqkms.setName(nmv); 134 return nmv;
168 } 135 }
169 } 136 }
170 catch (NumberFormatException nfe) { 137 catch (NumberFormatException nfe) {
171 // do nothing here 138 // do nothing here
172 } 139 }
173 } 140 }
141
142 return name;
174 } 143 }
175 144
176 145
177 @Override 146 @Override
178 protected void writeCSVData(CSVWriter writer) { 147 protected void writeCSVData(CSVWriter writer) {
267 } 236 }
268 } 237 }
269 } 238 }
270 239
271 240
241 /**
242 * This method is used to register a new column at <i>writer</i>. The name /
243 * title of the column depends on the Q or W value of <i>wqkms</i>. If a Q
244 * was selected and the Q fits to a named main value, the title is set to
245 * the named main value. Otherwise, the name returned by
246 * <i>WQKms.getName()</i> is set.
247 *
248 * @param writer The WstWriter.
249 * @param wqkms The new WST column.
250 */
272 protected void addWSTColumn(WstWriter writer, WQKms wqkms) { 251 protected void addWSTColumn(WstWriter writer, WQKms wqkms) {
273 writer.addColumn(wqkms.getName()); 252 if (master instanceof WINFOArtifact) {
253 writer.addColumn(getColumnTitle((WINFOArtifact) master, wqkms));
254 }
255 else {
256 writer.addColumn(wqkms.getName());
257 }
274 } 258 }
275 259
276 260
277 /** 261 /**
278 * Returns the number formatter for kilometer values. 262 * Returns the number formatter for kilometer values.

http://dive4elements.wald.intevation.org