Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java @ 9195:a4121ec450d6
'ca.'-issue
ExportContextCSV+PDF separated
uinfo.inundationduration url export
author | gernotbelger |
---|---|
date | Fri, 29 Jun 2018 14:52:54 +0200 |
parents | bb929b444ea5 |
children | 6393e05a9610 |
comparison
equal
deleted
inserted
replaced
9191:f692f5a0536a | 9195:a4121ec450d6 |
---|---|
12 import java.io.OutputStream; | 12 import java.io.OutputStream; |
13 | 13 |
14 import org.apache.log4j.Logger; | 14 import org.apache.log4j.Logger; |
15 import org.dive4elements.artifacts.CallContext; | 15 import org.dive4elements.artifacts.CallContext; |
16 import org.dive4elements.river.artifacts.model.CalculationResult; | 16 import org.dive4elements.river.artifacts.model.CalculationResult; |
17 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; | |
17 import org.dive4elements.river.exports.AbstractExporter; | 18 import org.dive4elements.river.exports.AbstractExporter; |
18 import org.w3c.dom.Document; | 19 import org.w3c.dom.Document; |
19 | 20 |
20 import au.com.bytecode.opencsv.CSVWriter; | 21 import au.com.bytecode.opencsv.CSVWriter; |
22 import net.sf.jasperreports.engine.JRException; | |
21 | 23 |
22 /** | 24 /** |
23 * @author Gernot Belger | 25 * @author Gernot Belger |
24 */ | 26 */ |
25 public abstract class AbstractCommonExporter<RESULTS extends AbstractCalculationResults> extends AbstractExporter { | 27 public abstract class AbstractCommonExporter<RESULT extends AbstractCalculationExportableResult, RESULTS extends AbstractCalculationResults<RESULT>> extends AbstractExporter { |
26 | 28 |
27 /** The log used in this exporter. */ | 29 /** The log used in this exporter. */ |
28 private final Logger log = Logger.getLogger(getClass()); | 30 private final Logger log = Logger.getLogger(getClass()); |
29 | 31 |
30 /** The log used in this exporter. */ | 32 /** The log used in this exporter. */ |
32 return this.log; | 34 return this.log; |
33 } | 35 } |
34 | 36 |
35 private RESULTS results = null; | 37 private RESULTS results = null; |
36 | 38 |
37 private ExportContextPDF helper; | |
38 | |
39 @Override | 39 @Override |
40 public void init(final String outName, final Document request, final OutputStream out, final CallContext context) { | 40 public void init(final String outName, final Document request, final OutputStream out, final CallContext context) { |
41 super.init(outName, request, out, context); | 41 super.init(outName, request, out, context); |
42 | |
43 this.helper = new ExportContextPDF(context); // bleibt so asynchron zu ExportContextCSV - ist so beabsichtigt | |
44 } | |
45 | |
46 protected final ExportContextPDF getHelper() { | |
47 return this.helper; | |
48 } | 42 } |
49 | 43 |
50 @Override | 44 @Override |
51 protected final void addData(final Object d) { | 45 protected final void addData(final Object d) { |
52 /* reset */ | 46 /* reset */ |
65 | 59 |
66 protected final RESULTS getResults() { | 60 protected final RESULTS getResults() { |
67 return this.results; | 61 return this.results; |
68 } | 62 } |
69 | 63 |
70 protected final AbstractCalculationResults getResults2() { | 64 @Override |
71 return this.results; | 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); | |
72 } | 70 } |
73 | 71 |
74 @Override | 72 protected final void doWriteCSVData(final ExportContextCSV exportContext, final RESULTS results) { |
75 protected final void writeCSVData(final CSVWriter writer) { | 73 |
76 doWriteCSVData(writer, this.results); | 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 } | |
77 } | 92 } |
78 | 93 |
79 protected abstract void doWriteCSVData(CSVWriter writer, RESULTS results); | 94 protected abstract void writeCSVGlobalMetadata(final ExportContextCSV exportContext, final RESULTS results); |
80 | 95 |
81 /** | 96 /** |
82 * Formats header with unit and label: msg [unit] (label) | 97 * Formats header with unit and label: msg [unit] (label) |
83 */ | 98 */ |
84 // TODO: REMOVE, because it has moved to ExportContextCSV | 99 // TODO: REMOVE, because it has moved to ExportContextCSV |
90 @Override | 105 @Override |
91 protected final void writePDF(final OutputStream out) { | 106 protected final void writePDF(final OutputStream out) { |
92 doWritePdf(out, this.results); | 107 doWritePdf(out, this.results); |
93 } | 108 } |
94 | 109 |
95 protected abstract void doWritePdf(OutputStream out, RESULTS results); | 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); | |
96 } | 141 } |