comparison artifacts/src/main/java/org/dive4elements/river/artifacts/GaugeDischargeArtifact.java @ 6693:88bb0c794833

issue1391: Enable GaugeDischarge artifact to directly load a table by its ID This allows us to use the GaugeDischargeArtifact for any discharge tables that we have in our database. The name of the created facet is taken from the ids string as is also usual in the WMS artifacts.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 29 Jul 2013 12:19:57 +0200
parents 20a32dbdbb59
children 8d5ca5175038
comparison
equal deleted inserted replaced
6692:ce691d22cee3 6693:88bb0c794833
10 10
11 import java.util.ArrayList; 11 import java.util.ArrayList;
12 import java.util.Arrays; 12 import java.util.Arrays;
13 import java.util.List; 13 import java.util.List;
14 import java.util.Map; 14 import java.util.Map;
15 import java.util.HashMap;
15 16
16 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
17 18
18 import org.w3c.dom.Document; 19 import org.w3c.dom.Document;
19 20
33 34
34 import org.dive4elements.river.artifacts.model.DischargeTables; 35 import org.dive4elements.river.artifacts.model.DischargeTables;
35 36
36 import org.dive4elements.river.model.Gauge; 37 import org.dive4elements.river.model.Gauge;
37 import org.dive4elements.river.model.River; 38 import org.dive4elements.river.model.River;
39 import org.dive4elements.river.model.DischargeTable;
38 40
39 import org.dive4elements.river.utils.RiverUtils; 41 import org.dive4elements.river.utils.RiverUtils;
40 42
41 43
42 /** 44 /**
50 private static Logger logger = Logger.getLogger(GaugeDischargeArtifact.class); 52 private static Logger logger = Logger.getLogger(GaugeDischargeArtifact.class);
51 53
52 /** The name of the artifact. */ 54 /** The name of the artifact. */
53 public static final String ARTIFACT_NAME = "gaugedischarge"; 55 public static final String ARTIFACT_NAME = "gaugedischarge";
54 56
57 /** The name a facet should have */
58 protected String facetWishName;
55 59
56 /** 60 /**
57 * Trivial Constructor. 61 * Trivial Constructor.
58 */ 62 */
59 public GaugeDischargeArtifact() { 63 public GaugeDischargeArtifact() {
77 { 81 {
78 logger.debug("GaugeDischargeArtifact.setup"); 82 logger.debug("GaugeDischargeArtifact.setup");
79 String ids = StaticD4EArtifact.getDatacageIDValue(data); 83 String ids = StaticD4EArtifact.getDatacageIDValue(data);
80 addStringData("ids", ids); 84 addStringData("ids", ids);
81 logger.debug("id for gaugedischarge: " + ids); 85 logger.debug("id for gaugedischarge: " + ids);
86 String[] splitIds = ids.split(";");
87 /* We assume that if an id's string with a ; is given that the
88 * format is <gauge_name>;<discharge_table_id>;<facet_desc>
89 * so that a specific discharge table can be selected */
90 if (splitIds.length > 2) {
91 facetWishName = splitIds[2];
92 }
82 super.setup(identifier, factory, context, callMeta, data); 93 super.setup(identifier, factory, context, callMeta, data);
83 } 94 }
84 95
85 96
86 /** Return the name of this artifact. */ 97 /** Return the name of this artifact. */
119 } 130 }
120 131
121 132
122 /** Get the Gauges name which came with datacage data-document. */ 133 /** Get the Gauges name which came with datacage data-document. */
123 public String getGaugeName() { 134 public String getGaugeName() {
124 return this.getDataAsString("ids"); 135 if (getDataAsString("ids") == null) {
136 return null;
137 }
138 return getDataAsString("ids").split(";")[0];
125 } 139 }
126 140
127 141
128 /** Get the Gauges which came with datacage data-document. */ 142 /** Get the Gauges which came with datacage data-document. */
129 public Gauge getGauge() { 143 public Gauge getGauge() {
164 names[i] = gauges.get(i).getName(); 178 names[i] = gauges.get(i).getName();
165 logger.debug("getDischargeCurveData: name " + names[i]); 179 logger.debug("getDischargeCurveData: name " + names[i]);
166 } 180 }
167 */ 181 */
168 182
169 DischargeTables dt = new DischargeTables(river.getName(), getDataAsString("ids")); 183 Map<String, double [][]> map;
170 184
171 Map<String, double [][]> map = dt.getValues(); 185 String[] ids = getDataAsString("ids").split(";");
186 if (ids.length > 1) {
187 /* We assume that if an id's string with a ; is given that the
188 * format is <gauge_name>;<discharge_table_id>;<facet_desc>
189 * so that a specific discharge table can be selected */
190 int tableId = 0;
191 try {
192 tableId = Integer.parseInt(ids[1]);
193 } catch (NumberFormatException e) {
194 logger.error("Discharge tables ids string is wrong." +
195 " Fromat is <gauge_name>;<discharge_table_id>;<facet_desc>" +
196 " Fix your Datacage!");
197 // Let's rather break down completly then show the wrong data.
198 return null;
199 }
200 DischargeTable table = DischargeTable.getDischargeTableById(tableId);
201 map = new HashMap<String, double [][]>();
202 map.put(getGaugeName(), DischargeTables.loadDischargeTableValues(table));
203 } else {
204 DischargeTables dt = new DischargeTables(river.getName(), getGaugeName());
205 map = dt.getValues();
206 }
172 207
173 ArrayList<WQKms> res = new ArrayList<WQKms>(); 208 ArrayList<WQKms> res = new ArrayList<WQKms>();
174 209
175 Gauge gauge = river.determineGaugeByName(this.getDataAsString("ids")); 210 Gauge gauge = river.determineGaugeByName(getGaugeName());
176 211
177 String name = getGaugeName(); 212 String name = getGaugeName();
178 double [][] values = map.get(name); 213 double [][] values = map.get(name);
179 if (values == null) { 214 if (values == null) {
180 logger.error("No values for this gauge / discharge found."); 215 logger.error("No values for this gauge / discharge found.");
189 224
190 return new CalculationResult( 225 return new CalculationResult(
191 res.toArray(new WQKms[res.size()]), 226 res.toArray(new WQKms[res.size()]),
192 new Calculation()); 227 new Calculation());
193 } 228 }
229
230 /** Gets the facet wish name.
231 *
232 * This is a hack to enable setting the name of the facet / theme in the
233 * UI from the datacage setting. */
234 public String getFacetWishName() {
235 return facetWishName;
236 }
194 } 237 }
195 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 238 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org