Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/states/HistoricalDischargeComputeState.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/HistoricalDischargeComputeState.java@1b73b731f7bd |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.artifacts.states; | |
2 | |
3 import java.text.NumberFormat; | |
4 | |
5 import java.util.List; | |
6 | |
7 import org.apache.log4j.Logger; | |
8 import org.w3c.dom.Element; | |
9 | |
10 import org.dive4elements.artifactdatabase.state.Facet; | |
11 import org.dive4elements.artifacts.Artifact; | |
12 import org.dive4elements.artifacts.CallContext; | |
13 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; | |
14 import org.dive4elements.river.artifacts.FLYSArtifact; | |
15 import org.dive4elements.river.artifacts.WINFOArtifact; | |
16 import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess; | |
17 import org.dive4elements.river.artifacts.access.HistoricalDischargeAccess.EvaluationMode; | |
18 import org.dive4elements.river.artifacts.model.CalculationResult; | |
19 import org.dive4elements.river.artifacts.model.DataFacet; | |
20 import org.dive4elements.river.artifacts.model.FacetTypes; | |
21 import org.dive4elements.river.artifacts.model.HistoricalDischargeCurveFacet; | |
22 import org.dive4elements.river.artifacts.model.HistoricalDischargeData; | |
23 import org.dive4elements.river.artifacts.model.HistoricalDischargeDifferenceFacet; | |
24 import org.dive4elements.river.artifacts.model.HistoricalDischargeFacet; | |
25 import org.dive4elements.river.artifacts.model.HistoricalDischargeWQFacet; | |
26 import org.dive4elements.river.artifacts.model.HistoricalWQKms; | |
27 import org.dive4elements.river.artifacts.model.HistoricalWQTimerange; | |
28 import org.dive4elements.river.artifacts.model.ReportFacet; | |
29 import org.dive4elements.river.artifacts.model.WQKms; | |
30 import org.dive4elements.river.artifacts.model.WQTimerange; | |
31 import org.dive4elements.river.artifacts.resources.Resources; | |
32 import org.dive4elements.river.model.TimeInterval; | |
33 | |
34 | |
35 /** | |
36 * State to calculate historical discharge curves. | |
37 * | |
38 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
39 */ | |
40 public class HistoricalDischargeComputeState | |
41 extends DefaultState | |
42 implements FacetTypes { | |
43 | |
44 private static final Logger logger = Logger | |
45 .getLogger(HistoricalDischargeComputeState.class); | |
46 | |
47 public static final String DEFAULT_UNIT = "cm"; | |
48 public static final String I18N_WQ_CURVE_FACET_RANGE = "historical_discharge.wq.curve_range"; | |
49 public static final String I18N_WQ_CURVE_FACET_SINCE = "historical_discharge.wq.curve_since"; | |
50 | |
51 @Override | |
52 protected void appendItems(Artifact artifact, ElementCreator creator, | |
53 String name, CallContext context, Element select) { | |
54 // TODO IMPLEMENT ME | |
55 } | |
56 | |
57 @Override | |
58 public Object computeAdvance(FLYSArtifact artifact, String hash, | |
59 CallContext context, List<Facet> facets, Object old) { | |
60 logger.debug("HistoricalDischargeComputeState.computeAdvance"); | |
61 | |
62 WINFOArtifact winfo = (WINFOArtifact) artifact; | |
63 | |
64 CalculationResult res = old instanceof CalculationResult ? (CalculationResult) old | |
65 : winfo.getHistoricalDischargeData(); | |
66 | |
67 if (facets == null) { | |
68 return res; | |
69 } | |
70 | |
71 if (res.getReport().hasProblems()) { | |
72 facets.add(new ReportFacet(ComputeType.ADVANCE, hash, id)); | |
73 } | |
74 | |
75 HistoricalDischargeData data = (HistoricalDischargeData) res.getData(); | |
76 HistoricalDischargeAccess access = new HistoricalDischargeAccess( | |
77 artifact); | |
78 | |
79 WQTimerange[] wqts = (WQTimerange[]) data.getWQTimeranges(); | |
80 if (wqts != null && wqts.length > 0) { | |
81 facets.add(new DataFacet(CSV, "CSV data", ComputeType.ADVANCE, | |
82 hash, id)); | |
83 | |
84 facets.add(new DataFacet(PDF, "PDF data", ComputeType.ADVANCE, | |
85 hash, id)); | |
86 | |
87 prepareFacets(facets, wqts, access); | |
88 } | |
89 | |
90 WQKms[] wqs = (WQKms[]) data.getWQs(); | |
91 if (wqs != null && wqs.length > 0) { | |
92 prepareWQFacets(context, facets, wqs, access, hash); | |
93 } | |
94 | |
95 return res; | |
96 } | |
97 | |
98 protected void prepareFacets(List<Facet> facets, WQTimerange[] wqts, | |
99 HistoricalDischargeAccess access) { | |
100 int i = 0; | |
101 | |
102 for (WQTimerange wqt : wqts) { | |
103 logger.debug("Prepare facet for: " + wqt.getName()); | |
104 | |
105 EvaluationMode evalMode = access.getEvaluationMode(); | |
106 if (evalMode == EvaluationMode.W) { | |
107 facets.add(new HistoricalDischargeFacet(i, | |
108 HISTORICAL_DISCHARGE_Q, createFacetTitle(wqt))); | |
109 | |
110 if (wqt instanceof HistoricalWQTimerange) { | |
111 logger | |
112 .debug("Create another facet for historical differences."); | |
113 | |
114 // TODO CREATE BETTER TITLE FOR FACETS (issue1180) | |
115 facets.add(new HistoricalDischargeDifferenceFacet(i, | |
116 HISTORICAL_DISCHARGE_Q_DIFF, "DIFF: " + wqt.getName())); | |
117 } | |
118 } | |
119 else { | |
120 facets.add(new HistoricalDischargeFacet(i, | |
121 HISTORICAL_DISCHARGE_W, createFacetTitle(wqt))); | |
122 | |
123 if (wqt instanceof HistoricalWQTimerange) { | |
124 logger | |
125 .debug("Create another facet for historical differences."); | |
126 | |
127 // TODO CREATE BETTER TITLE FOR FACETS | |
128 facets.add(new HistoricalDischargeDifferenceFacet(i, | |
129 HISTORICAL_DISCHARGE_W_DIFF, "DIFF: " + wqt.getName())); | |
130 } | |
131 } | |
132 | |
133 i++; | |
134 } | |
135 } | |
136 | |
137 protected void prepareWQFacets(CallContext cc, List<Facet> facets, | |
138 WQKms[] wqs, HistoricalDischargeAccess access, String hash) { | |
139 double[] ws = access.getWs(); | |
140 double[] qs = access.getQs(); | |
141 | |
142 NumberFormat format = NumberFormat.getInstance( | |
143 Resources.getLocale(cc.getMeta())); | |
144 for (int k = 0; k < ws.length; k++) { | |
145 facets.add(new HistoricalDischargeWQFacet(k, | |
146 HISTORICAL_DISCHARGE_WQ_W, "W=" + format.format(ws[k]), ComputeType.ADVANCE, | |
147 hash, getID(), ws[k])); | |
148 } | |
149 | |
150 for (int k = 0; k < qs.length; k++) { | |
151 facets.add(new HistoricalDischargeWQFacet(k, | |
152 HISTORICAL_DISCHARGE_WQ_Q, "Q=" + format.format(qs[k]), ComputeType.ADVANCE, | |
153 hash, getID(), qs[k])); | |
154 } | |
155 | |
156 int idx = 0; | |
157 for (WQKms wqkms : wqs) { | |
158 String title = null; | |
159 if (wqkms instanceof HistoricalWQKms) { | |
160 title = createDischargeCurveTitle(cc, (HistoricalWQKms) wqkms); | |
161 } | |
162 else { | |
163 title = wqkms.getName(); | |
164 } | |
165 | |
166 facets.add(new HistoricalDischargeCurveFacet(idx, | |
167 HISTORICAL_DISCHARGE_WQ_CURVE, title, ComputeType.ADVANCE, | |
168 getID(), hash)); | |
169 idx++; | |
170 } | |
171 } | |
172 | |
173 /** Create title for a Discharge-curve, including date. */ | |
174 protected String createDischargeCurveTitle(CallContext cc, | |
175 HistoricalWQKms wqkms) { | |
176 TimeInterval timeInterval = wqkms.getTimeInterval(); | |
177 | |
178 if (timeInterval.getStopTime() != null) { | |
179 return Resources.format(cc.getMeta(), I18N_WQ_CURVE_FACET_RANGE, | |
180 I18N_WQ_CURVE_FACET_RANGE, timeInterval.getStartTime(), | |
181 timeInterval.getStopTime()); | |
182 } | |
183 else { | |
184 return Resources.format(cc.getMeta(), I18N_WQ_CURVE_FACET_SINCE, | |
185 I18N_WQ_CURVE_FACET_SINCE, timeInterval.getStartTime()); | |
186 } | |
187 } | |
188 | |
189 | |
190 /** Create string for facets name/description. */ | |
191 protected String createFacetTitle(WQTimerange wqt) { | |
192 String name = wqt.getName(); | |
193 | |
194 return name != null && name.indexOf("W") >= 0 ? createFacetTitleW(wqt) | |
195 : createFacetTitleQ(wqt); | |
196 } | |
197 | |
198 protected String createFacetTitleW(WQTimerange wqt) { | |
199 String name = wqt.getName(); | |
200 return name + " " + DEFAULT_UNIT; | |
201 } | |
202 | |
203 protected String createFacetTitleQ(WQTimerange wqt) { | |
204 return wqt.getName(); | |
205 } | |
206 } | |
207 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |