Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepthminmax/FlowDepthMinMaxExporter.java @ 9006:7134a4c7d1b6
U-Info S-Info general work
author | gernotbelger |
---|---|
date | Tue, 17 Apr 2018 13:20:25 +0200 |
parents | d5802f22e4f5 |
children | 23945061daec |
rev | line source |
---|---|
8946 | 1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.artifacts.sinfo.flowdepthminmax; | |
10 | |
9006 | 11 import java.io.OutputStream; |
8946 | 12 import java.util.ArrayList; |
13 import java.util.Collection; | |
14 | |
15 import org.apache.log4j.Logger; | |
8997 | 16 import org.dive4elements.river.artifacts.common.GeneralResultType; |
9006 | 17 import org.dive4elements.river.artifacts.common.JasperReporter; |
18 import org.dive4elements.river.artifacts.common.MetaAndTableJRDataSource; | |
8997 | 19 import org.dive4elements.river.artifacts.common.ResultRow; |
8946 | 20 import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoExporter; |
8948 | 21 import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; |
8946 | 22 import org.dive4elements.river.artifacts.sinfo.util.BedHeightInfo; |
23 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
24 import org.dive4elements.river.artifacts.sinfo.util.WstInfo; | |
25 | |
26 import au.com.bytecode.opencsv.CSVWriter; | |
9006 | 27 import net.sf.jasperreports.engine.JRException; |
8946 | 28 |
29 /** | |
30 * Generates different output formats (csv, pdf) of data that resulted from a flow depths computation. | |
31 * | |
32 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
33 * @author Gernot Belger | |
34 */ | |
35 // REMARK: must be public because its registered in generators.xml | |
8948 | 36 public class FlowDepthMinMaxExporter extends AbstractSInfoExporter<FlowDepthMinMaxCalculationResult, FlowDepthMinMaxCalculationResults> { |
8946 | 37 |
38 /** The log used in this exporter. */ | |
39 private static Logger log = Logger.getLogger(FlowDepthMinMaxExporter.class); | |
40 | |
9006 | 41 private static final String JASPER_FILE = "/jasper/templates/sinfo.flowdepthminmax.jrxml"; |
8946 | 42 |
43 @Override | |
44 protected Logger getLog() { | |
45 return log; | |
46 } | |
47 | |
48 @Override | |
49 protected void writeCSVResultMetadata(final CSVWriter writer, final FlowDepthMinMaxCalculationResults results, | |
50 final FlowDepthMinMaxCalculationResult result) { | |
51 | |
8993
0adc6d04de95
SInfo - FlowDepthMinMax: choosing bed heights, not bed height min and max separately
gernotbelger
parents:
8949
diff
changeset
|
52 final BedHeightInfo sounding = result.getSounding(); |
8946 | 53 super.writeCSVSoundingMetadata(writer, sounding); |
8997 | 54 writer.writeNext(new String[] { "" }); // break line |
8946 | 55 final WstInfo wst = result.getWst(); |
56 writeCSVWaterlevelMetadata(writer, wst); | |
9006 | 57 writer.writeNext(new String[] { "" }); // break line |
8946 | 58 } |
59 | |
60 @Override | |
61 protected void writeCSVGlobalMetadata(final CSVWriter writer, final FlowDepthMinMaxCalculationResults results) { | |
62 | |
63 super.writeCSVGlobalMetadataDefaults(writer, results); | |
64 } | |
65 | |
66 /** | |
67 * Write the header, with different headings depending on whether at a | |
68 * gauge or at a location. | |
69 * | |
70 * @param river | |
71 * @param useTkh | |
72 */ | |
73 @Override | |
74 protected void writeCSVHeader(final CSVWriter writer, final FlowDepthMinMaxCalculationResults results, final RiverInfo river) { | |
75 log.info("FlowDepthExporter.writeCSVHeader"); | |
76 | |
77 final Collection<String> header = new ArrayList<>(11); | |
78 | |
8997 | 79 header.add(msg(GeneralResultType.station.getCsvHeader())); |
8948 | 80 header.add(msgUnit(SInfoResultType.flowdepthmin.getCsvHeader(), SInfoResultType.flowdepthmin.getUnit())); |
81 header.add(msgUnit(SInfoResultType.flowdepthmax.getCsvHeader(), SInfoResultType.flowdepthmax.getUnit())); | |
82 header.add(msgUnit(SInfoResultType.waterlevel.getCsvHeader(), river.getWstUnit())); | |
83 header.add(msgUnit(SInfoResultType.discharge.getCsvHeader(), SInfoResultType.discharge.getUnit())); | |
84 header.add(msg(SInfoResultType.waterlevelLabel.getCsvHeader())); | |
85 header.add(msg(SInfoResultType.gaugeLabel.getCsvHeader())); | |
86 header.add(msgUnit(SInfoResultType.meanBedHeight.getCsvHeader(), river.getWstUnit())); | |
87 header.add(msg(SInfoResultType.soundingLabel.getCsvHeader())); | |
88 header.add(msg(SInfoResultType.location.getCsvHeader())); | |
8946 | 89 |
90 writer.writeNext(header.toArray(new String[header.size()])); | |
91 } | |
92 | |
93 /** | |
94 * Format a row of a flow depth result into an array of string, both used by csv and pdf | |
95 * | |
8948 | 96 * @param result |
97 * | |
8946 | 98 * @param useTkh |
99 */ | |
8997 | 100 @Override |
101 protected String[] formatRow(final FlowDepthMinMaxCalculationResults results, final ResultRow row, final ExportMode mode) { | |
8946 | 102 |
8948 | 103 final Collection<String> lines = new ArrayList<>(10); |
8946 | 104 |
8997 | 105 lines.add(row.exportValue(this.context, GeneralResultType.station)); |
8946 | 106 |
8948 | 107 // REMARK: null check as pdf will call this with null and in that case we show all columns (to avoid multiple jasper |
108 // FIXME: does not work like this: we may have several pairs of min/max; so we need to look at all of them? | |
109 // templates) | |
110 // if (result == null || result.getMinSounding() != null) | |
111 lines.add(row.exportValue(this.context, SInfoResultType.flowdepthmin)); | |
112 // if (result == null || result.getMaxSounding() != null) | |
113 lines.add(row.exportValue(this.context, SInfoResultType.flowdepthmax)); | |
8946 | 114 |
8948 | 115 lines.add(row.exportValue(this.context, SInfoResultType.waterlevel)); |
116 lines.add(row.exportValue(this.context, SInfoResultType.discharge)); | |
117 lines.add(row.exportValue(this.context, SInfoResultType.waterlevelLabel)); | |
118 lines.add(row.exportValue(this.context, SInfoResultType.gaugeLabel)); | |
119 lines.add(row.exportValue(this.context, SInfoResultType.meanBedHeight)); | |
120 lines.add(row.exportValue(this.context, SInfoResultType.soundingLabel)); | |
121 lines.add(row.exportValue(this.context, SInfoResultType.location)); | |
8946 | 122 |
123 return lines.toArray(new String[lines.size()]); | |
124 } | |
125 | |
126 @Override | |
127 protected final void addJRMetaData(final MetaAndTableJRDataSource source, final FlowDepthMinMaxCalculationResults results) { | |
128 | |
129 /* general metadata */ | |
8997 | 130 super.addJRMetaData(source, results); |
8946 | 131 |
132 /* column headings */ | |
8997 | 133 source.addMetaData("station_header", GeneralResultType.station.getPdfHeader(this.context.getMeta())); |
8949
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
134 source.addMetaData("flowdepthmin_header", SInfoResultType.flowdepthmin.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
135 source.addMetaData("flowdepthmax_header", SInfoResultType.flowdepthmax.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
136 source.addMetaData("waterlevel_header", SInfoResultType.waterlevel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
137 source.addMetaData("discharge_header", SInfoResultType.discharge.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
138 source.addMetaData("waterlevel_name_header", SInfoResultType.waterlevelLabel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
139 source.addMetaData("gauge_header", SInfoResultType.gaugeLabel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
140 source.addMetaData("bedheight_header", SInfoResultType.meanBedHeight.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
141 source.addMetaData("sounding_name_header", SInfoResultType.soundingLabel.getPdfHeader(this.context.getMeta())); |
09e4a4909814
Distinguish between csv and pdf column headers
gernotbelger
parents:
8948
diff
changeset
|
142 source.addMetaData("location_header", SInfoResultType.location.getPdfHeader(this.context.getMeta())); |
8946 | 143 } |
144 | |
9006 | 145 @Override |
146 protected void writePDF(final OutputStream out) { | |
147 try { | |
148 final MetaAndTableJRDataSource source = createJRData(this.data); | |
149 | |
150 final JasperReporter reporter = new JasperReporter(); | |
151 reporter.addReport(JASPER_FILE, source); | |
152 reporter.exportPDF(out); | |
153 } | |
154 catch (final JRException je) { | |
155 getLog().warn("Error generating PDF Report!", je); | |
156 } | |
157 } | |
158 | |
8946 | 159 } |