9151
|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by |
|
3 * Björnsen Beratende Ingenieure GmbH |
|
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt |
|
5 * |
|
6 * This file is Free Software under the GNU AGPL (>=v3) |
|
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
8 * documentation coming with Dive4Elements River for details. |
|
9 */ |
|
10 package org.dive4elements.river.artifacts.common; |
|
11 |
|
12 import java.io.OutputStream; |
|
13 |
|
14 import org.apache.log4j.Logger; |
|
15 import org.dive4elements.artifacts.CallContext; |
|
16 import org.dive4elements.river.artifacts.model.CalculationResult; |
9195
|
17 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
9151
|
18 import org.dive4elements.river.exports.AbstractExporter; |
|
19 import org.w3c.dom.Document; |
|
20 |
|
21 import au.com.bytecode.opencsv.CSVWriter; |
9195
|
22 import net.sf.jasperreports.engine.JRException; |
9151
|
23 |
|
24 /** |
|
25 * @author Gernot Belger |
|
26 */ |
9195
|
27 public abstract class AbstractCommonExporter<RESULT extends AbstractCalculationExportableResult, RESULTS extends AbstractCalculationResults<RESULT>> extends AbstractExporter { |
9151
|
28 |
|
29 /** The log used in this exporter. */ |
|
30 private final Logger log = Logger.getLogger(getClass()); |
|
31 |
|
32 /** The log used in this exporter. */ |
|
33 protected final Logger getLog() { |
|
34 return this.log; |
|
35 } |
|
36 |
|
37 private RESULTS results = null; |
|
38 |
|
39 @Override |
|
40 public void init(final String outName, final Document request, final OutputStream out, final CallContext context) { |
|
41 super.init(outName, request, out, context); |
|
42 } |
|
43 |
|
44 @Override |
|
45 protected final void addData(final Object d) { |
|
46 /* reset */ |
|
47 this.results = null; |
|
48 |
|
49 if (d instanceof CalculationResult) { |
|
50 |
|
51 final Object dat = ((CalculationResult) d).getData(); |
|
52 if (dat != null) { |
|
53 @SuppressWarnings("unchecked") |
|
54 final RESULTS result = (RESULTS) dat; |
|
55 this.results = result; |
|
56 } |
|
57 } |
|
58 } |
|
59 |
|
60 protected final RESULTS getResults() { |
|
61 return this.results; |
|
62 } |
|
63 |
9195
|
64 @Override |
|
65 protected final void writeCSVData(final CSVWriter writer) { |
|
66 |
|
67 final ExportContextCSV exportContext = new ExportContextCSV(this.context, writer, this.results); |
|
68 |
|
69 doWriteCSVData(exportContext, this.results); |
9151
|
70 } |
|
71 |
9195
|
72 protected final void doWriteCSVData(final ExportContextCSV exportContext, final RESULTS results) { |
|
73 |
|
74 writeCSVGlobalMetadata(exportContext, results); |
|
75 |
|
76 final RiverInfo river = results.getRiver(); |
|
77 |
|
78 final Class<?> lastResultType = null; |
|
79 |
|
80 for (final AbstractCalculationExportableResult result : results.getResults()) { |
|
81 |
|
82 final Class<?> resultType = result.getClass(); |
|
83 if (lastResultType == null || lastResultType != resultType) { |
|
84 exportContext.writeBlankLine(); |
|
85 result.writeCSVHeader(exportContext, river); |
|
86 exportContext.writeBlankLine(); |
|
87 } else |
|
88 exportContext.writeCSVLine(new String[] { "#" }); |
|
89 |
|
90 result.writeCsv(exportContext); |
|
91 } |
9151
|
92 } |
|
93 |
9195
|
94 protected abstract void writeCSVGlobalMetadata(final ExportContextCSV exportContext, final RESULTS results); |
9151
|
95 |
|
96 /** |
|
97 * Formats header with unit and label: msg [unit] (label) |
|
98 */ |
|
99 // TODO: REMOVE, because it has moved to ExportContextCSV |
|
100 protected final String msgUnitLabel(final String key, final String unit, final String label) { |
|
101 final String msg = msg(key); |
|
102 return String.format("%s [%s] (%s)", msg, unit, label); |
|
103 } |
|
104 |
|
105 @Override |
|
106 protected final void writePDF(final OutputStream out) { |
|
107 doWritePdf(out, this.results); |
|
108 } |
|
109 |
9195
|
110 private final void doWritePdf(final OutputStream out, final RESULTS results) { |
|
111 |
|
112 try { |
|
113 final ExportContextPDF exportContext = new ExportContextPDF(this.context, results); |
|
114 |
|
115 final JasperReporter reporter = new JasperReporter(); |
|
116 |
|
117 for (final RESULT result : results.getResults()) { |
|
118 |
|
119 final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); |
|
120 |
|
121 writePDFGlobalMetadata(exportContext, source); |
|
122 |
|
123 final JasperDesigner design = result.addReport(exportContext, reporter, source); |
|
124 configureDesign(result, design); |
|
125 } |
|
126 |
|
127 reporter.exportPDF(out); |
|
128 } |
|
129 catch (final JRException je) { |
|
130 getLog().warn("Error generating PDF Report!", je); |
|
131 } |
|
132 } |
|
133 |
|
134 /** |
|
135 * Override to implement, does nothing by default. |
|
136 */ |
|
137 protected void configureDesign(final RESULT result, final JasperDesigner design) { |
|
138 } |
|
139 |
|
140 protected abstract void writePDFGlobalMetadata(ExportContextPDF exportContext, MetaAndTableJRDataSource source); |
9151
|
141 } |