Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/DeltaWtExporter.java @ 3216:a46910e61dfb
FixA: Moved DeltaWtExporter to fixings exporter package.
flys-artifacts/trunk@4839 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sat, 30 Jun 2012 19:44:39 +0000 |
parents | |
children | 79dd823733e2 |
comparison
equal
deleted
inserted
replaced
3215:750e98fc8b76 | 3216:a46910e61dfb |
---|---|
1 package de.intevation.flys.exports.fixings; | |
2 | |
3 import au.com.bytecode.opencsv.CSVWriter; | |
4 | |
5 import de.intevation.artifacts.CallContext; | |
6 import de.intevation.artifacts.CallMeta; | |
7 | |
8 import de.intevation.flys.artifacts.model.CalculationResult; | |
9 | |
10 import de.intevation.flys.artifacts.model.fixings.AnalysisPeriod; | |
11 import de.intevation.flys.artifacts.model.fixings.FixResult; | |
12 import de.intevation.flys.artifacts.model.fixings.QWD; | |
13 | |
14 import de.intevation.flys.artifacts.resources.Resources; | |
15 | |
16 import de.intevation.flys.exports.AbstractExporter; | |
17 | |
18 import de.intevation.flys.utils.Formatter; | |
19 import de.intevation.flys.utils.KMIndex; | |
20 | |
21 import java.io.IOException; | |
22 import java.io.OutputStream; | |
23 | |
24 import java.text.DateFormat; | |
25 import java.text.NumberFormat; | |
26 | |
27 import java.util.ArrayList; | |
28 import java.util.Date; | |
29 import java.util.List; | |
30 import java.util.Locale; | |
31 | |
32 import org.apache.log4j.Logger; | |
33 | |
34 import org.w3c.dom.Document; | |
35 | |
36 public class DeltaWtExporter | |
37 extends AbstractExporter | |
38 { | |
39 private static Logger log = Logger.getLogger(DeltaWtExporter.class); | |
40 | |
41 public static final String CSV_KM_HEADER = | |
42 "export.fixings.deltawt.csv.header.km"; | |
43 | |
44 public static final String CSV_DELTA_W_HEADER = | |
45 "export.fixings.deltawt.csv.header.deltaw"; | |
46 | |
47 public static final String CSV_T_HEADER = | |
48 "export.fixings.deltawt.csv.header.t"; | |
49 | |
50 public static final String CSV_T_DESC_HEADER = | |
51 "export.fixings.deltawt.csv.header.description"; | |
52 | |
53 public static final String DEFAULT_CSV_KM_HEADER = "km"; | |
54 | |
55 public static final String DEFAULT_CSV_DELTA_W_HEADER = "\u0394 W [cm]"; | |
56 | |
57 public static final String DEFAULT_CSV_T_HEADER = "Datum"; | |
58 | |
59 public static final String DEFAULT_CSV_T_DESC_HEADER = "Beschreibung"; | |
60 | |
61 protected List<KMIndex<AnalysisPeriod []>> analysisPeriods; | |
62 | |
63 protected List<KMIndex<QWD[]>> referenceEvents; | |
64 | |
65 @Override | |
66 public void init(Document request, OutputStream out, CallContext context) { | |
67 log.debug("DeltaWtExporter.init"); | |
68 super.init(request, out, context); | |
69 analysisPeriods = new ArrayList<KMIndex<AnalysisPeriod []>>(); | |
70 referenceEvents = new ArrayList<KMIndex<QWD[]>>(); | |
71 } | |
72 | |
73 @Override | |
74 public void generate() throws IOException { | |
75 log.debug("DeltaWtExporter.generate"); | |
76 if (facet == null) { | |
77 throw new IOException("invalid (null) facet for exporter"); | |
78 } | |
79 else if (facet.equals(FACET_CSV)) { | |
80 generateCSV(); | |
81 } | |
82 else if (facet.equals(FACET_PDF)) { | |
83 generatePDF(); | |
84 } | |
85 else { | |
86 throw new IOException("invalid facet (" + facet + ") for exporter"); | |
87 } | |
88 } | |
89 | |
90 @Override | |
91 protected void addData(Object d) { | |
92 log.debug("DeltaWtExporter.addData"); | |
93 if (!(d instanceof CalculationResult)) { | |
94 log.warn("Invalid data type"); | |
95 return; | |
96 } | |
97 | |
98 Object data = ((CalculationResult)d).getData(); | |
99 if (!(data instanceof FixResult)) { | |
100 log.warn("Invalid data stored in result."); | |
101 } | |
102 FixResult result = (FixResult)data; | |
103 analysisPeriods.add(result.getAnalysisPeriods()); | |
104 referenceEvents.add(result.getReferenced()); | |
105 } | |
106 | |
107 @Override | |
108 protected void writeCSVData(CSVWriter writer) throws IOException { | |
109 | |
110 boolean debug = log.isDebugEnabled(); | |
111 | |
112 writeCSVHeader(writer); | |
113 | |
114 NumberFormat kmF = getKMFormatter(); | |
115 NumberFormat dwF = getDeltaWFormatter(); | |
116 | |
117 CallMeta meta = context.getMeta(); | |
118 Locale locale = Resources.getLocale(meta); | |
119 DateFormat dF = DateFormat.getDateInstance(DateFormat.SHORT, locale); | |
120 | |
121 for (KMIndex<QWD[]> reference: referenceEvents) { | |
122 | |
123 for (KMIndex.Entry<QWD[]> kmEntry: reference) { | |
124 | |
125 String kmS = kmF.format(kmEntry.getKm()); | |
126 for (QWD qwd: kmEntry.getValue()) { | |
127 double deltaW = qwd.getDeltaW(); | |
128 Date date = qwd.getDate(); | |
129 String desc = qwd.getDescription(); | |
130 | |
131 String deltaWS = dwF.format(deltaW); | |
132 String dateS = dF.format(date); | |
133 | |
134 writer.writeNext(new String[] { | |
135 kmS, | |
136 deltaWS, | |
137 dateS, | |
138 desc != null ? desc : "" }); | |
139 } | |
140 } | |
141 } | |
142 | |
143 if (debug) { | |
144 log.debug("AnalysisPeriods: " + analysisPeriods.size()); | |
145 } | |
146 | |
147 for (KMIndex<AnalysisPeriod []> periods: analysisPeriods) { | |
148 | |
149 for (KMIndex.Entry<AnalysisPeriod []> kmEntry: periods) { | |
150 | |
151 String kmS = kmF.format(kmEntry.getKm()); | |
152 | |
153 for (AnalysisPeriod period: kmEntry.getValue()) { | |
154 QWD [] qwds = period.getQWDs(); | |
155 if (qwds != null) { | |
156 for (QWD qwd: qwds) { | |
157 double deltaW = qwd.getDeltaW(); | |
158 Date date = qwd.getDate(); | |
159 String desc = qwd.getDescription(); | |
160 | |
161 String deltaWS = dwF.format(deltaW); | |
162 String dateS = dF.format(date); | |
163 | |
164 writer.writeNext(new String[] { | |
165 kmS, | |
166 deltaWS, | |
167 dateS, | |
168 desc != null ? desc : "" }); | |
169 } | |
170 } | |
171 } | |
172 } | |
173 } | |
174 writer.flush(); | |
175 } | |
176 | |
177 | |
178 protected NumberFormat getKMFormatter() { | |
179 return Formatter.getFixDeltaWKM(context); | |
180 } | |
181 | |
182 protected NumberFormat getDeltaWFormatter() { | |
183 return Formatter.getFixDeltaWDeltaW(context); | |
184 } | |
185 | |
186 protected void writeCSVHeader(CSVWriter writer) { | |
187 log.debug("DeltaWtExporter.writeCSVHeader"); | |
188 | |
189 writer.writeNext(new String[] { | |
190 msg(CSV_KM_HEADER, DEFAULT_CSV_KM_HEADER), | |
191 msg(CSV_DELTA_W_HEADER, DEFAULT_CSV_DELTA_W_HEADER), | |
192 msg(CSV_T_HEADER, DEFAULT_CSV_T_HEADER), | |
193 msg(CSV_T_DESC_HEADER, DEFAULT_CSV_T_DESC_HEADER) | |
194 }); | |
195 } | |
196 | |
197 @Override | |
198 protected void writePDF(OutputStream out) { | |
199 // TODO: Implement me! | |
200 } | |
201 } | |
202 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |