Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/uinfo/inundationduration/InundationDurationExporter.java @ 9150:23945061daec
gigantic refactoring: exporter, result, results
to support multiple jaspers -> collisions
author | gernotbelger |
---|---|
date | Thu, 14 Jun 2018 16:56:31 +0200 |
parents | 41f4bc83aa7a |
children |
comparison
equal
deleted
inserted
replaced
9149:5be112fba832 | 9150:23945061daec |
---|---|
8 * documentation coming with Dive4Elements River for details. | 8 * documentation coming with Dive4Elements River for details. |
9 */ | 9 */ |
10 package org.dive4elements.river.artifacts.uinfo.inundationduration; | 10 package org.dive4elements.river.artifacts.uinfo.inundationduration; |
11 | 11 |
12 import java.io.OutputStream; | 12 import java.io.OutputStream; |
13 import java.util.ArrayList; | |
14 import java.util.Collection; | |
15 | 13 |
16 import org.apache.log4j.Logger; | 14 import org.dive4elements.river.artifacts.common.AbstractCalculationExportableResult; |
17 import org.dive4elements.river.artifacts.common.GeneralResultType; | 15 import org.dive4elements.river.artifacts.common.AbstractCommonExporter; |
16 import org.dive4elements.river.artifacts.common.ExportContextCSV; | |
18 import org.dive4elements.river.artifacts.common.I18NStrings; | 17 import org.dive4elements.river.artifacts.common.I18NStrings; |
19 import org.dive4elements.river.artifacts.common.JasperReporter; | 18 import org.dive4elements.river.artifacts.common.JasperReporter; |
20 import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource; | 19 import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource; |
21 import org.dive4elements.river.artifacts.common.ResultRow; | |
22 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; | |
23 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | 20 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
24 import org.dive4elements.river.artifacts.uinfo.commons.AbstractUInfoExporter; | |
25 | 21 |
26 import au.com.bytecode.opencsv.CSVWriter; | 22 import au.com.bytecode.opencsv.CSVWriter; |
27 import net.sf.jasperreports.engine.JRException; | 23 import net.sf.jasperreports.engine.JRException; |
28 | 24 |
29 /** | 25 /** |
30 * @author Domenico Nardi Tironi | 26 * @author Domenico Nardi Tironi |
31 * | 27 * |
32 */ | 28 */ |
33 public class InundationDurationExporter extends AbstractUInfoExporter<InundationDurationCalculationResult, InundationDurationCalculationResults> { | 29 public class InundationDurationExporter extends AbstractCommonExporter<InundationDurationCalculationResults> { |
34 | |
35 /** The log used in this exporter. */ | |
36 private static Logger log = Logger.getLogger(InundationDurationExporter.class); | |
37 | |
38 private static final String JASPER_FILE = "/jasper/templates/uinfo.inundationduration.jrxml"; | |
39 | 30 |
40 @Override | 31 @Override |
41 protected Logger getLog() { | 32 protected void doWritePdf(final OutputStream out, final InundationDurationCalculationResults results) { |
42 return log; | 33 // TODO: Move to super |
43 } | |
44 | |
45 @Override | |
46 protected void writeCSVGlobalMetadata(final CSVWriter writer, final InundationDurationCalculationResults results) { | |
47 log.info("SalixLineExporter.writeCSVMeta"); | |
48 | |
49 super.writeCSVGlobalMetadataDefaults(writer, results); | |
50 | |
51 // "# Höhensystem des Flusses: " | |
52 writeCSVMetaEntry(writer, I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, results.getRiver().getWstUnit()); | |
53 | |
54 writer.writeNext(new String[] { "" }); // break line | |
55 } | |
56 | |
57 @Override | |
58 protected void writeCSVResultMetadata(final CSVWriter writer, final InundationDurationCalculationResults results, | |
59 final InundationDurationCalculationResult result) { | |
60 | |
61 /* | |
62 * final BedHeightInfo sounding = result.getSounding(); | |
63 * super.writeCSVSoundingMetadata(writer, sounding); | |
64 * final WstInfo wst = result.getWst(); | |
65 * super.writeCSVWaterlevelMetadata(writer, wst); | |
66 */ | |
67 } | |
68 | |
69 /** | |
70 * Write the header, with different headings depending on whether at a | |
71 * gauge or at a location. | |
72 * | |
73 * @param river | |
74 * @param useTkh | |
75 */ | |
76 @Override | |
77 protected void writeCSVHeader(final CSVWriter writer, final InundationDurationCalculationResults results, final RiverInfo river) { | |
78 log.info("FlowDepthExporter.writeCSVHeader"); | |
79 | |
80 final Collection<String> header = new ArrayList<>(11); | |
81 | |
82 header.add(msg(GeneralResultType.station.getCsvHeader())); | |
83 header.add(msgUnit(SInfoResultType.flowdepth.getCsvHeader(), SInfoResultType.flowdepth.getUnit())); | |
84 | |
85 header.add(msgUnit(SInfoResultType.waterlevel.getCsvHeader(), river.getWstUnit())); | |
86 header.add(msgUnit(SInfoResultType.discharge.getCsvHeader(), SInfoResultType.discharge.getUnit())); | |
87 header.add(msg(SInfoResultType.waterlevelLabel.getCsvHeader())); | |
88 header.add(msg(SInfoResultType.gaugeLabel.getCsvHeader())); | |
89 header.add(msgUnit(SInfoResultType.meanBedHeight.getCsvHeader(), river.getWstUnit())); | |
90 header.add(msg(SInfoResultType.soundingLabel.getCsvHeader())); | |
91 header.add(msg(SInfoResultType.location.getCsvHeader())); | |
92 | |
93 writer.writeNext(header.toArray(new String[header.size()])); | |
94 } | |
95 | |
96 /** | |
97 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
98 * | |
99 * @param pdf | |
100 * | |
101 * @param useTkh | |
102 */ | |
103 @Override | |
104 protected String[] formatRow(final InundationDurationCalculationResults results, final ResultRow row, final ExportMode mode) { | |
105 | |
106 final Collection<String> lines = new ArrayList<>(11); | |
107 | |
108 lines.add(row.exportValue(this.context, GeneralResultType.station)); | |
109 lines.add(row.exportValue(this.context, SInfoResultType.flowdepth)); | |
110 | |
111 lines.add(row.exportValue(this.context, SInfoResultType.waterlevel)); | |
112 lines.add(row.exportValue(this.context, SInfoResultType.discharge)); | |
113 lines.add(row.exportValue(this.context, SInfoResultType.waterlevelLabel)); | |
114 lines.add(row.exportValue(this.context, SInfoResultType.gaugeLabel)); | |
115 lines.add(row.exportValue(this.context, SInfoResultType.meanBedHeight)); | |
116 lines.add(row.exportValue(this.context, SInfoResultType.soundingLabel)); | |
117 lines.add(row.exportValue(this.context, SInfoResultType.location)); | |
118 | |
119 return lines.toArray(new String[lines.size()]); | |
120 } | |
121 | |
122 @Override | |
123 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final InundationDurationCalculationResults results) { | |
124 | |
125 /* general metadata */ | |
126 super.addJRMetaData(source, results); | |
127 | |
128 /* column headings */ | |
129 source.addMetaData("station_header", GeneralResultType.station.getPdfHeader(this.context.getMeta())); | |
130 | |
131 } | |
132 | |
133 @Override | |
134 protected void writePDF(final OutputStream out) { | |
135 try { | 34 try { |
136 final MetaAndTableJRDataSource source = createJRData(this.data); | 35 final ExportContextCSV exportContextCSV = new ExportContextCSV(this.context, null); |
137 | 36 |
138 final JasperReporter reporter = new JasperReporter(); | 37 final JasperReporter reporter = new JasperReporter(); |
139 reporter.addReport(JASPER_FILE, source); | 38 |
39 for (final AbstractCalculationExportableResult<InundationDurationCalculationResults> result : results.getResults()) { | |
40 final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); | |
41 getHelper().addJRMetaDataUSINFO(source, results); | |
42 | |
43 result.addReport(exportContextCSV, results, reporter, source); | |
44 } | |
45 | |
140 reporter.exportPDF(out); | 46 reporter.exportPDF(out); |
141 } | 47 } |
142 catch (final JRException je) { | 48 catch (final JRException je) { |
143 getLog().warn("Error generating PDF Report!", je); | 49 getLog().warn("Error generating PDF Report!", je); |
144 } | 50 } |
145 } | 51 } |
146 | 52 |
53 @Override | |
54 protected void doWriteCSVData(final CSVWriter writer, final InundationDurationCalculationResults results) { | |
55 // TODO: Diesen Ablauf in super? -> leichte abweichung | |
56 | |
57 // TODO: move results into context? | |
58 final ExportContextCSV exportContextCSV = new ExportContextCSV(this.context, writer); | |
59 | |
60 getLog().info("writeCSVData"); | |
61 | |
62 final RiverInfo river = results.getRiver(); | |
63 | |
64 /* write as csv */ | |
65 exportContextCSV.writeCSVGlobalMetadataDefaults(results); // ggf auslagern innerhalb dieser Klasse | |
66 exportContextCSV.writeCSVMetaEntry(I18NStrings.CSV_META_HEIGHT_UNIT_RIVER, river.getWstUnit()); | |
67 // writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance | |
68 | |
69 final Class<?> lastResultType = null; | |
70 | |
71 for (final AbstractCalculationExportableResult<InundationDurationCalculationResults> result : results.getResults()) { | |
72 | |
73 final Class<?> resultType = result.getClass(); | |
74 if (lastResultType == null || lastResultType != resultType) { | |
75 exportContextCSV.writeBlankLine(); | |
76 result.writeCSVHeader(exportContextCSV, results, river); | |
77 exportContextCSV.writeBlankLine(); | |
78 } else | |
79 exportContextCSV.writeCSVLine(new String[] { "#" }); | |
80 | |
81 result.writeCsv(exportContextCSV, results); | |
82 } | |
83 | |
84 } | |
147 } | 85 } |