Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/exports/fixings/ParametersExporter.java @ 3432:9c39a25aa849
FixA: Embed german and english translations into the overview stylesheet.
flys-artifacts/trunk@5092 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sat, 21 Jul 2012 12:50:48 +0000 |
parents | ed9689fbcd26 |
children | 35a6c9a49a76 |
rev | line source |
---|---|
3217
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.exports.fixings; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import au.com.bytecode.opencsv.CSVWriter; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import de.intevation.artifacts.CallContext; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 import de.intevation.flys.artifacts.model.CalculationResult; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import de.intevation.flys.artifacts.model.Parameters; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 |
3415
e3c7a3228bc2
FixA: Renamed FixResult to FixAnalysisResult.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3223
diff
changeset
|
10 import de.intevation.flys.artifacts.model.fixings.FixAnalysisResult; |
3217
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 import de.intevation.flys.artifacts.resources.Resources; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 import de.intevation.flys.exports.AbstractExporter; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import java.io.IOException; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 import java.io.OutputStream; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 import java.text.NumberFormat; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 import java.util.ArrayList; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 import java.util.List; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 import org.apache.log4j.Logger; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 import org.w3c.dom.Document; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 public class ParametersExporter |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 extends AbstractExporter |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 private static Logger log = Logger.getLogger(DeltaWtExporter.class); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 protected List<Parameters> parametersList; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 @Override |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 public void init(Document request, OutputStream out, CallContext context) { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 log.debug("ParametersExporter.init"); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 super.init(request, out, context); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 parametersList = new ArrayList<Parameters>(); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 @Override |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 protected void addData(Object d) { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 log.debug("ParametersExporter.addData"); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 if (!(d instanceof CalculationResult)) { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 log.warn("Invalid data type"); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 return; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 Object data = ((CalculationResult)d).getData(); |
3415
e3c7a3228bc2
FixA: Renamed FixResult to FixAnalysisResult.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3223
diff
changeset
|
51 if (!(data instanceof FixAnalysisResult)) { |
3217
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 log.warn("Invalid data stored in result."); |
3417
ed9689fbcd26
FixA: Prevent NPE when exporting parameters.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3415
diff
changeset
|
53 return; |
3217
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 } |
3417
ed9689fbcd26
FixA: Prevent NPE when exporting parameters.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3415
diff
changeset
|
55 |
3415
e3c7a3228bc2
FixA: Renamed FixResult to FixAnalysisResult.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3223
diff
changeset
|
56 FixAnalysisResult result = (FixAnalysisResult)data; |
3217
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 parametersList.add(result.getParameters()); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 @Override |
3223
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
61 public void generate() |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
62 throws IOException |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
63 { |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
64 log.debug("ParametersExporter.generate"); |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
65 |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
66 if (facet == null) { |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
67 throw new IOException("invalid (null) facet for exporter"); |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
68 } |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
69 |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
70 if (facet.equals(FIX_PARAMETERS)) { |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
71 generateCSV(); |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
72 } |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
73 else { |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
74 throw new IOException( |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
75 "invalid facet for exporter: '" + facet + "'"); |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
76 } |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
77 } |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
78 |
282c9da923ab
FixA: flys/issue689: Export CSV for facet 'fix_parameters'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3217
diff
changeset
|
79 @Override |
3217
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 protected void writeCSVData(final CSVWriter writer) throws IOException { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 if (parametersList.isEmpty()) { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 return; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
85 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
86 Parameters parameters = parametersList.get(0); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 writer.writeNext(parameters.getColumnNames()); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
89 final int numColumns = parameters.getNumberColumns(); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
90 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
91 parameters.visit(new Parameters.Visitor() { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 String [] row = new String[numColumns]; |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
95 NumberFormat format = NumberFormat.getInstance( |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
96 Resources.getLocale(context.getMeta())); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 @Override |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
99 public void visit(double [] data) { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
100 for (int i = 0; i < data.length; ++i) { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
101 row[i] = format.format(data[i]); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
102 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
103 writer.writeNext(row); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
104 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
105 }, new double[numColumns]); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
106 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
107 writer.flush(); |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
108 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
109 |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
110 @Override |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
111 protected void writePDF(OutputStream out) { |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
112 // TODO: Implement me! |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
113 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 } |
79dd823733e2
FixA: Added parameter exporter.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |