8999
|
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.util.Collection; |
|
13 |
|
14 import org.apache.log4j.Logger; |
|
15 import org.dive4elements.artifacts.CallMeta; |
|
16 import org.dive4elements.river.artifacts.model.CalculationResult; |
|
17 import org.dive4elements.river.artifacts.resources.Resources; |
|
18 import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; |
|
19 import org.dive4elements.river.exports.AbstractExporter; |
|
20 |
|
21 import au.com.bytecode.opencsv.CSVWriter; |
|
22 |
|
23 /** |
|
24 * @author Gernot Belger |
|
25 */ |
|
26 public abstract class AbstractCommonExporter<RESULT extends AbstractCalculationResult, RESULTS extends AbstractCalculationResults<RESULT>> extends AbstractExporter { |
|
27 |
|
28 /** The storage that contains the current calculation result. */ |
|
29 protected static enum ExportMode { |
|
30 pdf, csv |
|
31 } |
|
32 |
|
33 /** The log used in this exporter. */ |
|
34 protected abstract Logger getLog(); |
|
35 |
|
36 protected RESULTS data = null; |
|
37 |
9005
|
38 public final RESULTS getData() { |
8999
|
39 return this.data; |
|
40 } |
|
41 |
|
42 @Override |
9005
|
43 protected final void addData(final Object d) { |
8999
|
44 /* reset */ |
|
45 this.data = null; |
|
46 if (d instanceof CalculationResult) { |
|
47 |
|
48 final Object dat = ((CalculationResult) d).getData(); |
|
49 if (dat != null) { |
|
50 @SuppressWarnings("unchecked") |
|
51 final RESULTS result = (RESULTS) dat; |
|
52 this.data = result; |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 /** |
|
58 * Formats header with unit and label: msg [unit] (label) |
|
59 */ |
9005
|
60 protected final String msgUnitLabel(final String key, final String unit, final String label) { |
8999
|
61 final String msg = msg(key); |
|
62 return String.format("%s [%s] (%s)", msg, unit, label); |
|
63 } |
|
64 |
|
65 @Override |
9005
|
66 protected final void writeCSVData(final CSVWriter writer) { |
8999
|
67 getLog().info("writeCSVData"); |
|
68 |
|
69 /* fetch calculation results */ |
|
70 final RESULTS results = this.data; |
|
71 |
|
72 /* write as csv */ |
|
73 writeCSVGlobalMetadata(writer, results); |
|
74 writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance |
|
75 final RiverInfo river = results.getRiver(); |
|
76 // FIXME :with comment if not first result |
|
77 writeCSVHeader(writer, results, river); |
|
78 writer.writeNext(new String[] { "" }); // break line HERE to avoid redundance |
|
79 |
|
80 for (final RESULT result : results.getResults()) { |
|
81 writeCSVResult(writer, results, result); |
|
82 writer.writeNext(new String[] { "" }); // break line HERE after each resultset |
|
83 } |
|
84 |
|
85 } |
|
86 |
|
87 protected abstract void writeCSVHeader(final CSVWriter writer, final RESULTS results, final RiverInfo river); |
|
88 |
|
89 /** |
|
90 * Add metadata that is once written to the top of the file. |
|
91 */ |
|
92 protected abstract void writeCSVGlobalMetadata(final CSVWriter writer, final RESULTS results); |
|
93 |
9005
|
94 protected final void writeCSVMetaEntry(final CSVWriter writer, final String message, final Object... messageArgs) { |
8999
|
95 |
|
96 final CallMeta meta = this.context.getMeta(); |
|
97 |
|
98 writer.writeNext(new String[] { Resources.getMsg(meta, message, message, messageArgs) }); |
|
99 } |
|
100 |
9005
|
101 private final void writeCSVResult(final CSVWriter writer, final RESULTS results, final RESULT result) { |
8999
|
102 |
|
103 writeCSVResultMetadata(writer, results, result); |
9005
|
104 // wenn resultsmetadata null sind!? keine neue zeile |
|
105 // writer.writeNext(new String[] { "" }); // break line in den Implementationen, |
|
106 // weil es sein kann, dass KEINE ResultMetadata geschrieben werden; dann wäre eine Leerzeile überflüssig |
8999
|
107 |
|
108 /* now the value rows */ |
9005
|
109 final Collection<ResultRow> rows = result.getRows(); |
8999
|
110 for (final ResultRow row : rows) { |
|
111 writeCSVRow(writer, results, result, row); |
|
112 } |
|
113 } |
|
114 |
|
115 /** |
|
116 * Add metadata that is written once per result set. |
|
117 */ |
|
118 protected abstract void writeCSVResultMetadata(CSVWriter writer, RESULTS results, RESULT result); |
|
119 |
|
120 protected void writeCSVRow(final CSVWriter writer, final RESULTS results, final RESULT result, final ResultRow row) { |
|
121 getLog().debug("writeCSVFlowDepthRow"); |
|
122 |
|
123 final String[] formattedRow = formatCSVRow(results, row); |
|
124 writer.writeNext(formattedRow); |
|
125 } |
|
126 |
|
127 protected final String[] formatCSVRow(final RESULTS results, final ResultRow row) { |
|
128 return formatRow(results, row, ExportMode.csv); |
|
129 } |
|
130 |
9005
|
131 protected final MetaAndTableJRDataSource createJRData(final AbstractCalculationResults<RESULT> data) { |
8999
|
132 |
|
133 /* fetch calculation results */ |
|
134 final RESULTS results = this.data; |
|
135 |
|
136 final MetaAndTableJRDataSource source = new MetaAndTableJRDataSource(); |
|
137 |
|
138 addJRMetaData(source, results); |
|
139 |
9005
|
140 for (final RESULT result : results.getResults()) |
8999
|
141 addJRTableData(source, results, result); |
|
142 |
|
143 return source; |
|
144 } |
|
145 |
|
146 protected abstract void addJRMetaData(final MetaAndTableJRDataSource source, final RESULTS results); |
|
147 |
9005
|
148 private void addJRTableData(final MetaAndTableJRDataSource source, final RESULTS results, final RESULT result) { |
8999
|
149 |
|
150 final Collection<ResultRow> rows = result.getRows(); |
|
151 |
|
152 for (final ResultRow row : rows) { |
|
153 |
|
154 final String[] formattedRow = formatPDFRow(results, row); |
|
155 source.addData(formattedRow); |
|
156 } |
|
157 } |
|
158 |
|
159 protected abstract String[] formatRow(RESULTS results, ResultRow row, ExportMode mode); |
|
160 |
9005
|
161 private final String[] formatPDFRow(final RESULTS results, final ResultRow row) { |
8999
|
162 return formatRow(results, row, ExportMode.pdf); |
|
163 } |
|
164 |
|
165 protected abstract void writeCSVGlobalMetadataDefaults(final CSVWriter writer, final RESULTS results); |
|
166 |
|
167 protected abstract void addJRMetaDataDefaults(final MetaAndTableJRDataSource source, final RESULTS results); |
|
168 } |