Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/sq/SQRelationExporter.java @ 6172:58a613798386
Implement PDF export for SQ Relation
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Mon, 03 Jun 2013 17:15:09 +0200 |
parents | af13ceeba52a |
children | 6a04081a2f69 |
comparison
equal
deleted
inserted
replaced
6171:4a1af135885a | 6172:58a613798386 |
---|---|
9 package org.dive4elements.river.exports.sq; | 9 package org.dive4elements.river.exports.sq; |
10 | 10 |
11 import java.io.OutputStream; | 11 import java.io.OutputStream; |
12 import java.util.ArrayList; | 12 import java.util.ArrayList; |
13 import java.util.List; | 13 import java.util.List; |
14 import java.util.Map; | |
15 import java.util.HashMap; | |
16 import java.util.Date; | |
17 import java.util.Locale; | |
18 import java.text.DateFormat; | |
14 | 19 |
15 import org.w3c.dom.Document; | 20 import org.w3c.dom.Document; |
16 | 21 |
22 import net.sf.jasperreports.engine.JasperExportManager; | |
23 import net.sf.jasperreports.engine.JasperFillManager; | |
24 import net.sf.jasperreports.engine.JasperPrint; | |
25 import net.sf.jasperreports.engine.JRException; | |
26 | |
17 import au.com.bytecode.opencsv.CSVWriter; | 27 import au.com.bytecode.opencsv.CSVWriter; |
18 | 28 |
19 import org.dive4elements.artifacts.CallContext; | 29 import org.dive4elements.artifacts.CallContext; |
30 import org.dive4elements.artifacts.CallMeta; | |
20 | 31 |
21 import org.dive4elements.river.artifacts.model.CalculationResult; | 32 import org.dive4elements.river.artifacts.model.CalculationResult; |
22 | |
23 import org.dive4elements.river.artifacts.model.sq.SQFractionResult; | 33 import org.dive4elements.river.artifacts.model.sq.SQFractionResult; |
24 import org.dive4elements.river.artifacts.model.sq.SQResult; | 34 import org.dive4elements.river.artifacts.model.sq.SQResult; |
35 import org.dive4elements.river.artifacts.model.sq.SQRelationJRDataSource; | |
25 import org.dive4elements.river.artifacts.model.Parameters; | 36 import org.dive4elements.river.artifacts.model.Parameters; |
37 import org.dive4elements.river.artifacts.model.DateRange; | |
38 import org.dive4elements.river.artifacts.access.SQRelationAccess; | |
39 | |
40 import org.dive4elements.river.artifacts.resources.Resources; | |
41 | |
42 import org.dive4elements.river.artifacts.D4EArtifact; | |
26 | 43 |
27 import org.dive4elements.river.exports.AbstractExporter; | 44 import org.dive4elements.river.exports.AbstractExporter; |
45 | |
46 import org.dive4elements.river.utils.RiverUtils; | |
47 import org.dive4elements.river.utils.Formatter; | |
48 | |
49 import org.dive4elements.artifacts.common.utils.Config; | |
28 | 50 |
29 import org.apache.log4j.Logger; | 51 import org.apache.log4j.Logger; |
30 | 52 |
31 /** | 53 /** |
32 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 54 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
78 "export.sqrelation.csv.header.c.ferguson"; | 100 "export.sqrelation.csv.header.c.ferguson"; |
79 | 101 |
80 public static final String CSV_VARIANCE = | 102 public static final String CSV_VARIANCE = |
81 "export.sqrelation.csv.header.variance"; | 103 "export.sqrelation.csv.header.variance"; |
82 | 104 |
105 public static final String PDF_TITLE= | |
106 "export.sqrelation.pdf.title"; | |
107 | |
108 public static final String PDF_HEADER_MODE = | |
109 "export.sqrelation.pdf.mode"; | |
110 | |
111 public static final String JASPER_FILE = | |
112 "export.sqrelation.pdf.file"; | |
83 | 113 |
84 protected List<SQResult []> data; | 114 protected List<SQResult []> data; |
85 | 115 |
86 | 116 |
87 public void init(Document request, OutputStream out, CallContext cc) { | 117 public void init(Document request, OutputStream out, CallContext cc) { |
118 | 148 |
119 writeCSVHeader(writer); | 149 writeCSVHeader(writer); |
120 | 150 |
121 for (SQResult [] results: data) { | 151 for (SQResult [] results: data) { |
122 for (SQResult result: results) { | 152 for (SQResult result: results) { |
123 data2CSV(writer, result); | 153 writer.writeAll(data2StringArrays(result)); |
124 } | 154 } |
125 } | 155 } |
126 } | 156 } |
127 | 157 |
128 protected void data2CSV(CSVWriter writer, SQResult result) { | 158 protected List<String[]> data2StringArrays(SQResult result) { |
129 logger.debug("data2CSV"); | |
130 | |
131 // TODO: i18n | |
132 String km = String.valueOf(result.getKm()); | 159 String km = String.valueOf(result.getKm()); |
160 List<String[]> retval = new ArrayList<String[]>(); | |
133 | 161 |
134 for (int i = 0; i < SQResult.NUMBER_FRACTIONS; ++i) { | 162 for (int i = 0; i < SQResult.NUMBER_FRACTIONS; ++i) { |
135 SQFractionResult fraction = result.getFraction(i); | 163 SQFractionResult fraction = result.getFraction(i); |
136 | 164 |
137 String name = result.getFractionName(i); | 165 String name = result.getFractionName(i); |
140 | 168 |
141 if (parameters == null) { | 169 if (parameters == null) { |
142 continue; | 170 continue; |
143 } | 171 } |
144 | 172 |
145 double a = parameters.getValue(0, "a"); | 173 String a, b, sd, o, t; |
146 double b = parameters.getValue(0, "b"); | 174 a = Formatter.getSQRelation(context).format(parameters.getValue(0, "a")); |
147 double sd = Math.sqrt(parameters.getValue(0, "std_dev")); | 175 b = Formatter.getSQRelation(context).format(parameters.getValue(0, "b")); |
148 int o = fraction.totalNumOutliers(); | 176 sd = Formatter.getVariance(context).format(Math.sqrt(parameters.getValue(0, "std_dev"))); |
149 int t = fraction.numMeasurements() + o; | 177 |
150 | 178 o = String.valueOf(fraction.totalNumOutliers()); |
151 writer.writeNext(new String[] { | 179 t = String.valueOf(fraction.numMeasurements() + o); |
180 | |
181 retval.add(new String[] { | |
152 km, | 182 km, |
153 name, | 183 name, |
154 String.valueOf(a), | 184 a, |
155 String.valueOf(b), | 185 b, |
156 String.valueOf(t), | 186 t, |
157 String.valueOf(o), | 187 o, |
158 String.valueOf(sd) | 188 sd |
159 }); | 189 }); |
160 } | 190 } |
161 } | 191 return retval; |
162 | 192 } |
193 | |
194 | |
195 protected SQRelationJRDataSource createJRData() { | |
196 SQRelationJRDataSource source = new SQRelationJRDataSource(); | |
197 | |
198 addMetaData(source); | |
199 for (SQResult [] results: data) { | |
200 for (SQResult result: results) { | |
201 for (String[] res: data2StringArrays(result)) { | |
202 source.addData(res); | |
203 } | |
204 } | |
205 } | |
206 return source; | |
207 } | |
208 | |
209 protected void addMetaData(SQRelationJRDataSource source) { | |
210 CallMeta meta = context.getMeta(); | |
211 | |
212 D4EArtifact arti = (D4EArtifact) master; | |
213 | |
214 source.addMetaData ("river", RiverUtils.getRivername(arti)); | |
215 | |
216 Locale locale = Resources.getLocale(meta); | |
217 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale); | |
218 | |
219 source.addMetaData("date", df.format(new Date())); | |
220 | |
221 SQRelationAccess access = new SQRelationAccess(arti); | |
222 source.addMetaData("location", getKmFormatter().format(access.getLocation())); | |
223 | |
224 DateRange period = access.getPeriod(); | |
225 source.addMetaData("periods", df.format(period.getFrom()) + " - " + | |
226 df.format(period.getTo())); | |
227 | |
228 source.addMetaData("outliertest", access.getOutlierMethod()); | |
229 source.addMetaData("outliers", access.getOutliers().toString()); | |
230 | |
231 source.addMetaData("calculation", Resources.getMsg( | |
232 locale, | |
233 PDF_HEADER_MODE, | |
234 "SQRelation")); | |
235 } | |
163 | 236 |
164 @Override | 237 @Override |
165 protected void writePDF(OutputStream out) { | 238 protected void writePDF(OutputStream out) { |
166 logger.debug("writePDF"); | 239 logger.debug("write PDF"); |
167 logger.error("NOT IMPLEMENTED: writePDF"); | 240 SQRelationJRDataSource source = createJRData(); |
241 | |
242 String jasperFile = Resources.getMsg( | |
243 context.getMeta(), | |
244 JASPER_FILE, | |
245 "/jasper/sqrelation_en.jasper"); | |
246 String confPath = Config.getConfigDirectory().toString(); | |
247 | |
248 | |
249 Map parameters = new HashMap(); | |
250 parameters.put("ReportTitle", Resources.getMsg( | |
251 context.getMeta(), PDF_TITLE, "Exported Data")); | |
252 try { | |
253 JasperPrint print = JasperFillManager.fillReport( | |
254 confPath + jasperFile, | |
255 parameters, | |
256 source); | |
257 JasperExportManager.exportReportToPdfStream(print, out); | |
258 } | |
259 catch(JRException je) { | |
260 logger.warn("Error generating PDF Report!", je); | |
261 } | |
168 } | 262 } |
169 } | 263 } |
170 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : | 264 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |