comparison artifacts/src/main/java/org/dive4elements/river/exports/DurationCurveExporter.java @ 9312:740d65e4aa14

Q [m³/s] one message
author gernotbelger
date Thu, 26 Jul 2018 15:54:20 +0200
parents 2b0ff11cef3f
children
comparison
equal deleted inserted replaced
9311:7c7f73e5e01e 9312:740d65e4aa14
7 */ 7 */
8 8
9 package org.dive4elements.river.exports; 9 package org.dive4elements.river.exports;
10 10
11 import java.io.OutputStream; 11 import java.io.OutputStream;
12 import java.text.DateFormat;
12 import java.text.NumberFormat; 13 import java.text.NumberFormat;
13 import java.util.ArrayList; 14 import java.util.ArrayList;
15 import java.util.Date;
16 import java.util.HashMap;
14 import java.util.List; 17 import java.util.List;
18 import java.util.Locale;
15 import java.util.Map; 19 import java.util.Map;
16 import java.util.HashMap;
17 import java.util.Date;
18 import java.text.DateFormat;
19 import java.util.Locale;
20 20
21 import org.apache.log4j.Logger; 21 import org.apache.log4j.Logger;
22 import org.dive4elements.artifacts.CallMeta;
23 import org.dive4elements.artifacts.common.utils.Config;
24 import org.dive4elements.river.artifacts.D4EArtifact;
25 import org.dive4elements.river.artifacts.access.RangeAccess;
26 import org.dive4elements.river.artifacts.access.RiverAccess;
27 import org.dive4elements.river.artifacts.model.CalculationResult;
28 import org.dive4elements.river.artifacts.model.WKmsJRDataSource;
29 import org.dive4elements.river.artifacts.model.WQDay;
30 import org.dive4elements.river.artifacts.resources.Resources;
31 import org.dive4elements.river.utils.Formatter;
32 import org.dive4elements.river.utils.RiverUtils;
22 33
23 import au.com.bytecode.opencsv.CSVWriter; 34 import au.com.bytecode.opencsv.CSVWriter;
24 35 import net.sf.jasperreports.engine.JRException;
25 import net.sf.jasperreports.engine.JasperExportManager; 36 import net.sf.jasperreports.engine.JasperExportManager;
26 import net.sf.jasperreports.engine.JasperFillManager; 37 import net.sf.jasperreports.engine.JasperFillManager;
27 import net.sf.jasperreports.engine.JasperPrint; 38 import net.sf.jasperreports.engine.JasperPrint;
28 import net.sf.jasperreports.engine.JRException;
29
30 import org.dive4elements.artifacts.CallMeta;
31 import org.dive4elements.artifacts.common.utils.Config;
32
33 import org.dive4elements.river.artifacts.D4EArtifact;
34
35 import org.dive4elements.river.artifacts.access.RiverAccess;
36 import org.dive4elements.river.artifacts.access.RangeAccess;
37 import org.dive4elements.river.artifacts.model.WQDay;
38 import org.dive4elements.river.artifacts.model.CalculationResult;
39 import org.dive4elements.river.artifacts.model.WKmsJRDataSource;
40 import org.dive4elements.river.artifacts.resources.Resources;
41
42 import org.dive4elements.river.utils.RiverUtils;
43 import org.dive4elements.river.utils.Formatter;
44
45 39
46 /** 40 /**
47 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 41 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
48 */ 42 */
49 public class DurationCurveExporter extends AbstractExporter { 43 public class DurationCurveExporter extends AbstractExporter {
50 44
51 /** The log used in this exporter. */ 45 /** The log used in this exporter. */
52 private static Logger log = Logger.getLogger(DurationCurveExporter.class); 46 private static Logger log = Logger.getLogger(DurationCurveExporter.class);
53 47
54 48 public static final String CSV_DURATION_HEADER = "export.duration.curve.csv.header.duration";
55 public static final String CSV_DURATION_HEADER = 49
56 "export.duration.curve.csv.header.duration"; 50 public static final String CSV_W_HEADER = "export.duration.curve.csv.header.w";
57 51
58 public static final String CSV_W_HEADER = 52 public static final String CSV_Q_HEADER = "common.export.csv.header.q";
59 "export.duration.curve.csv.header.w";
60
61 public static final String CSV_Q_HEADER =
62 "export.duration.curve.csv.header.q";
63 53
64 public static final String PDF_HEADER_MODE = "export.duration.pdf.mode"; 54 public static final String PDF_HEADER_MODE = "export.duration.pdf.mode";
65 public static final String JASPER_FILE = "export.duration.pdf.file"; 55 public static final String JASPER_FILE = "export.duration.pdf.file";
66 56
67 /** The storage that contains all WQKms objects for the different facets. */ 57 /** The storage that contains all WQKms objects for the different facets. */
68 protected List<WQDay> data; 58 protected List<WQDay> data;
69 59
70 public DurationCurveExporter() { 60 public DurationCurveExporter() {
71 data = new ArrayList<WQDay>(); 61 this.data = new ArrayList<>();
72 } 62 }
73 63
74 @Override 64 @Override
75 protected void addData(Object d) { 65 protected void addData(Object d) {
76 if (d instanceof CalculationResult) { 66 if (d instanceof CalculationResult) {
77 d = ((CalculationResult)d).getData(); 67 d = ((CalculationResult) d).getData();
78 if (d instanceof WQDay) { 68 if (d instanceof WQDay) {
79 data.add((WQDay)d); 69 this.data.add((WQDay) d);
80 } 70 }
81 } 71 }
82 } 72 }
83 73
84 74 @Override
85 protected void writeCSVData(CSVWriter writer) { 75 protected void writeCSVData(final CSVWriter writer) {
86 log.info("DurationCurveExporter.writeData"); 76 log.info("DurationCurveExporter.writeData");
87 77
88 writeCSVHeader(writer); 78 writeCSVHeader(writer);
89 79
90 for (WQDay wqday: data) { 80 for (final WQDay wqday : this.data) {
91 wQDay2CSV(writer, wqday); 81 wQDay2CSV(writer, wqday);
92 } 82 }
93 } 83 }
94 84
95 85 protected void writeCSVHeader(final CSVWriter writer) {
96 protected void writeCSVHeader(CSVWriter writer) {
97 log.info("DurationCurveExporter.writeCSVHeader"); 86 log.info("DurationCurveExporter.writeCSVHeader");
98 87
99 String unit = new RiverAccess((D4EArtifact)master) 88 final String unit = new RiverAccess((D4EArtifact) this.master).getRiver().getWstUnit().getName();
100 .getRiver().getWstUnit().getName(); 89
101 90 writer.writeNext(new String[] { msg(CSV_W_HEADER, new Object[] { unit }), msg(CSV_Q_HEADER), msg(CSV_DURATION_HEADER) });
102 writer.writeNext(new String[] { 91 }
103 msg(CSV_W_HEADER, new Object[] { unit }), 92
104 msg(CSV_Q_HEADER), 93 protected void wQDay2CSV(final CSVWriter writer, final WQDay wqday) {
105 msg(CSV_DURATION_HEADER)
106 });
107 }
108
109
110 protected void wQDay2CSV(CSVWriter writer, WQDay wqday) {
111 log.debug("DurationCurveExporter.wQDay2CSV"); 94 log.debug("DurationCurveExporter.wQDay2CSV");
112 95
113 int size = wqday.size(); 96 final int size = wqday.size();
114 97
115 NumberFormat wf = getWFormatter(); 98 final NumberFormat wf = getWFormatter();
116 NumberFormat qf = getQFormatter(); 99 final NumberFormat qf = getQFormatter();
117 NumberFormat df = getDFormatter(); 100 final NumberFormat df = getDFormatter();
118 101
119 if (wqday.isIncreasing()) { 102 if (wqday.isIncreasing()) {
120 for (int i = size-1; i >= 0; i --) { 103 for (int i = size - 1; i >= 0; i--) {
121 writer.writeNext(new String[] { 104 writer.writeNext(new String[] { wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), df.format(wqday.getDay(i)) });
122 wf.format(wqday.getW(i)), 105 }
123 qf.format(wqday.getQ(i)), 106 } else {
124 df.format(wqday.getDay(i)) 107 for (int i = 0; i < size; i++) {
125 }); 108 writer.writeNext(new String[] { wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), df.format(wqday.getDay(i)) });
126 } 109 }
127 } 110 }
128 else { 111 }
129 for (int i = 0; i < size; i ++) {
130 writer.writeNext(new String[] {
131 wf.format(wqday.getW(i)),
132 qf.format(wqday.getQ(i)),
133 df.format(wqday.getDay(i))
134 });
135 }
136 }
137 }
138
139 112
140 /** 113 /**
141 * Returns the number formatter for W values. 114 * Returns the number formatter for W values.
142 * 115 *
143 * @return the number formatter for W values. 116 * @return the number formatter for W values.
144 */ 117 */
145 @Override 118 @Override
146 protected NumberFormat getWFormatter() { 119 protected NumberFormat getWFormatter() {
147 return Formatter.getDurationW(context); 120 return Formatter.getDurationW(this.context);
148 } 121 }
149
150 122
151 /** 123 /**
152 * Returns the number formatter for Q values. 124 * Returns the number formatter for Q values.
153 * 125 *
154 * @return the number formatter for Q values. 126 * @return the number formatter for Q values.
155 */ 127 */
156 @Override 128 @Override
157 protected NumberFormat getQFormatter() { 129 protected NumberFormat getQFormatter() {
158 return Formatter.getDurationQ(context); 130 return Formatter.getDurationQ(this.context);
159 } 131 }
160
161 132
162 /** 133 /**
163 * Returns the number formatter for duration values. 134 * Returns the number formatter for duration values.
164 * 135 *
165 * @return the number formatter for duration values. 136 * @return the number formatter for duration values.
166 */ 137 */
167 protected NumberFormat getDFormatter() { 138 protected NumberFormat getDFormatter() {
168 return Formatter.getDurationD(context); 139 return Formatter.getDurationD(this.context);
169 } 140 }
170 141
171 142 @Override
172 @Override 143 protected void writePDF(final OutputStream out) {
173 protected void writePDF(OutputStream out) { 144 final WKmsJRDataSource source = createJRData();
174 WKmsJRDataSource source = createJRData(); 145
175 146 final String jasperFile = Resources.getMsg(this.context.getMeta(), JASPER_FILE, "/jasper/duration_en.jasper");
176 String jasperFile = Resources.getMsg( 147 final String confPath = Config.getConfigDirectory().toString();
177 context.getMeta(), 148
178 JASPER_FILE, 149 final Map parameters = new HashMap();
179 "/jasper/duration_en.jasper");
180 String confPath = Config.getConfigDirectory().toString();
181
182 Map parameters = new HashMap();
183 parameters.put("ReportTitle", "Exported Data"); 150 parameters.put("ReportTitle", "Exported Data");
184 try { 151 try {
185 JasperPrint print = JasperFillManager.fillReport( 152 final JasperPrint print = JasperFillManager.fillReport(confPath + jasperFile, parameters, source);
186 confPath + jasperFile,
187 parameters,
188 source);
189 JasperExportManager.exportReportToPdfStream(print, out); 153 JasperExportManager.exportReportToPdfStream(print, out);
190 } 154 }
191 catch(JRException je) { 155 catch (final JRException je) {
192 log.warn("Error generating PDF Report!"); 156 log.warn("Error generating PDF Report!");
193 je.printStackTrace(); 157 je.printStackTrace();
194 } 158 }
195 } 159 }
196 160
197 protected WKmsJRDataSource createJRData() { 161 protected WKmsJRDataSource createJRData() {
198 WKmsJRDataSource source = new WKmsJRDataSource(); 162 final WKmsJRDataSource source = new WKmsJRDataSource();
199 163
200 addMetaData(source); 164 addMetaData(source);
201 for (WQDay wqday: data) { 165 for (final WQDay wqday : this.data) {
202 addWQDayData(source, wqday); 166 addWQDayData(source, wqday);
203 } 167 }
204 168
205 return source; 169 return source;
206 } 170 }
207 171
208 172 protected void addMetaData(final WKmsJRDataSource source) {
209 protected void addMetaData(WKmsJRDataSource source) { 173 final CallMeta meta = this.context.getMeta();
210 CallMeta meta = context.getMeta(); 174
211 175 final D4EArtifact flys = (D4EArtifact) this.master;
212 D4EArtifact flys = (D4EArtifact) master; 176
213 177 source.addMetaData("river", RiverUtils.getRivername(flys));
214 source.addMetaData ("river", RiverUtils.getRivername(flys)); 178
215 179 final Locale locale = Resources.getLocale(meta);
216 Locale locale = Resources.getLocale(meta); 180 final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
217 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
218 181
219 source.addMetaData("date", df.format(new Date())); 182 source.addMetaData("date", df.format(new Date()));
220 183
221 RangeAccess rangeAccess = new RangeAccess(flys); 184 final RangeAccess rangeAccess = new RangeAccess(flys);
222 double[] kms = rangeAccess.getKmRange(); 185 final double[] kms = rangeAccess.getKmRange();
223 source.addMetaData("range", String.valueOf(kms[0])); 186 source.addMetaData("range", String.valueOf(kms[0]));
224 187
225 source.addMetaData("calculation", Resources.getMsg( 188 source.addMetaData("calculation", Resources.getMsg(locale, PDF_HEADER_MODE, "Duration"));
226 locale, 189 }
227 PDF_HEADER_MODE, 190
228 "Duration")); 191 protected void addWQDayData(final WKmsJRDataSource source, final WQDay wqday) {
229 } 192 final int size = wqday.size();
230 193
231 protected void addWQDayData(WKmsJRDataSource source, WQDay wqday) { 194 final NumberFormat wf = getWFormatter();
232 int size = wqday.size(); 195 final NumberFormat qf = getQFormatter();
233 196 final NumberFormat df = getDFormatter();
234 NumberFormat wf = getWFormatter();
235 NumberFormat qf = getQFormatter();
236 NumberFormat df = getDFormatter();
237 197
238 if (wqday.isIncreasing()) { 198 if (wqday.isIncreasing()) {
239 for (int i = size-1; i >= 0; i --) { 199 for (int i = size - 1; i >= 0; i--) {
240 source.addData(new String[] { 200 source.addData(new String[] { "", wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), "", "", "", df.format(wqday.getDay(i)) });
241 "", 201 }
242 wf.format(wqday.getW(i)), 202 } else {
243 qf.format(wqday.getQ(i)), 203 for (int i = 0; i < size; i++) {
244 "", "", "", 204 source.addData(new String[] { "", wf.format(wqday.getW(i)), qf.format(wqday.getQ(i)), "", "", "", df.format(wqday.getDay(i)) });
245 df.format(wqday.getDay(i))
246 });
247 }
248 }
249 else {
250 for (int i = 0; i < size; i ++) {
251 source.addData(new String[] {
252 "",
253 wf.format(wqday.getW(i)),
254 qf.format(wqday.getQ(i)),
255 "", "", "",
256 df.format(wqday.getDay(i))
257 });
258 } 205 }
259 } 206 }
260 } 207 }
261 } 208 }
262 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 209 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org