Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractCommonExporter.java @ 9455:5f81d3f7b82b
pdf export disabled flag
author | gernotbelger |
---|---|
date | Thu, 23 Aug 2018 16:32:53 +0200 |
parents | 217e8e59c386 |
children |
rev | line source |
---|---|
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 | |
9353
217e8e59c386
sinfo flowdepth multiple pairs allowed; bugfix arrayOutOfRange hasTkh==false && useTkh==true
gernotbelger
parents:
9200
diff
changeset
|
78 Class<?> lastResultType = null; |
9195 | 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 | |
9353
217e8e59c386
sinfo flowdepth multiple pairs allowed; bugfix arrayOutOfRange hasTkh==false && useTkh==true
gernotbelger
parents:
9200
diff
changeset
|
90 lastResultType = resultType; |
9195 | 91 result.writeCsv(exportContext); |
92 } | |
9151 | 93 } |
94 | |
9195 | 95 protected abstract void writeCSVGlobalMetadata(final ExportContextCSV exportContext, final RESULTS results); |
9151 | 96 |
97 @Override | |
98 protected final void writePDF(final OutputStream out) { | |
99 doWritePdf(out, this.results); | |
100 } | |
101 | |
9195 | 102 private final void doWritePdf(final OutputStream out, final RESULTS results) { |
103 | |
104 try { | |
105 final ExportContextPDF exportContext = new ExportContextPDF(this.context, results); | |
106 | |
107 final JasperReporter reporter = new JasperReporter(); | |
108 | |
109 for (final RESULT result : results.getResults()) { | |
9455 | 110 if (result.pdfExportEnabled()) { // TODO: only works for multiple result: if singleresult is disabled, ugly errors occur, 'cause no pdf will be |
111 // created at all | |
9195 | 112 |
9455 | 113 final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); |
9195 | 114 |
9455 | 115 writePDFGlobalMetadata(exportContext, source); |
116 | |
117 final JasperDesigner design = result.addReport(exportContext, reporter, source); | |
118 configureDesign(result, design); | |
119 } | |
9195 | 120 } |
121 | |
122 reporter.exportPDF(out); | |
123 } | |
124 catch (final JRException je) { | |
125 getLog().warn("Error generating PDF Report!", je); | |
126 } | |
127 } | |
128 | |
129 /** | |
130 * Override to implement, does nothing by default. | |
131 */ | |
132 protected void configureDesign(final RESULT result, final JasperDesigner design) { | |
133 } | |
134 | |
135 protected abstract void writePDFGlobalMetadata(ExportContextPDF exportContext, MetaAndTableJRDataSource source); | |
9151 | 136 } |