Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java @ 391:5d6988836f01
Added an exporter to export the computed data of a duration curve.
flys-artifacts/trunk@1814 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 03 May 2011 17:37:56 +0000 |
parents | 69d05357c177 |
children | 340dc41a7ea3 |
comparison
equal
deleted
inserted
replaced
390:a67748ad4d61 | 391:5d6988836f01 |
---|---|
1 package de.intevation.flys.exports; | 1 package de.intevation.flys.exports; |
2 | 2 |
3 import java.io.IOException; | |
4 import java.io.OutputStream; | 3 import java.io.OutputStream; |
5 import java.io.OutputStreamWriter; | |
6 import java.util.ArrayList; | 4 import java.util.ArrayList; |
7 import java.util.List; | 5 import java.util.List; |
8 | 6 |
9 import org.w3c.dom.Document; | 7 import org.w3c.dom.Document; |
10 | 8 |
20 | 18 |
21 | 19 |
22 /** | 20 /** |
23 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | 21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
24 */ | 22 */ |
25 public class WaterlevelExporter implements OutGenerator { | 23 public class WaterlevelExporter extends AbstractExporter { |
26 | 24 |
27 /** The logger used in this exporter.*/ | 25 /** The logger used in this exporter.*/ |
28 private static Logger logger = Logger.getLogger(WaterlevelExporter.class); | 26 private static Logger logger = Logger.getLogger(WaterlevelExporter.class); |
29 | 27 |
30 | |
31 public static final String WATERLEVEL_FACET_CSV = "waterlevel_export.csv"; | |
32 | |
33 public static final String DEFAULT_CSV_CHARSET = "UTF-8"; | |
34 | |
35 public static final char DEFAULT_CSV_SEPARATOR = ','; | |
36 | |
37 | |
38 /** The document of the incoming out() request.*/ | |
39 protected Document request; | |
40 | |
41 /** The output stream where the data should be written to.*/ | |
42 protected OutputStream out; | |
43 | |
44 /** The CallContext object.*/ | |
45 protected CallContext context; | |
46 | |
47 /** The selected facet.*/ | |
48 protected String facet; | |
49 | 28 |
50 /** The storage that contains all WQKms objects for the different facets.*/ | 29 /** The storage that contains all WQKms objects for the different facets.*/ |
51 protected List<WQKms[]> data; | 30 protected List<WQKms[]> data; |
52 | 31 |
53 | 32 |
54 public void init(Document request, OutputStream out, CallContext context) { | 33 public void init(Document request, OutputStream out, CallContext context) { |
55 logger.debug("WaterlevelExporter.init"); | 34 logger.debug("WaterlevelExporter.init"); |
56 | 35 |
57 this.request = request; | 36 super.init(request, out, context); |
58 this.out = out; | 37 |
59 this.context = context; | 38 this.data = new ArrayList<WQKms[]>(); |
60 this.data = new ArrayList<WQKms[]>(); | |
61 } | 39 } |
62 | 40 |
63 | 41 |
64 /** | 42 protected void addData(Artifact artifact) { |
65 * This doOut() just collects the data of multiple artifacts. The real data | |
66 * generation takes place in the concrete generate() methods. | |
67 * | |
68 * @param artifact The artifact. | |
69 * @param facet The facet to add - NOTE: the facet needs to fit to the first | |
70 * facet inserted into this exporter. Otherwise this artifact/facet is | |
71 * skipped. | |
72 * @param attr The attr document. | |
73 */ | |
74 public void doOut(Artifact artifact, String facet, Document attr) { | |
75 logger.debug("WaterlevelExporter.doOut: " + facet); | |
76 | |
77 if (!isFacetValid(facet)) { | |
78 logger.warn("Facet '" + facet + "' not valid. No output created!"); | |
79 return; | |
80 } | |
81 | |
82 data.add(getWaterlevelData(artifact)); | 43 data.add(getWaterlevelData(artifact)); |
83 } | |
84 | |
85 | |
86 public void generate() | |
87 throws IOException | |
88 { | |
89 if (facet != null && facet.equals(WATERLEVEL_FACET_CSV)) { | |
90 generateCSV(); | |
91 } | |
92 else { | |
93 throw new IOException("invalid facet for exporter."); | |
94 } | |
95 } | |
96 | |
97 | |
98 /** | |
99 * Determines if the desired facet is valid for this exporter. If no facet | |
100 * is currently set, <i>facet</i> is set. | |
101 * | |
102 * @param facet The desired facet. | |
103 * | |
104 * @return true, if <i>facet</i> is valid, otherwise false. | |
105 */ | |
106 protected boolean isFacetValid(String facet) { | |
107 logger.debug("WaterlevelExporter.isFacetValid"); | |
108 | |
109 if (facet == null || facet.length() == 0) { | |
110 return false; | |
111 } | |
112 else if (this.facet == null || this.facet.length() == 0) { | |
113 logger.debug("Set the facet of this export: " + facet); | |
114 this.facet = facet; | |
115 | |
116 return true; | |
117 } | |
118 else { | |
119 return this.facet.equals(facet); | |
120 } | |
121 } | 44 } |
122 | 45 |
123 | 46 |
124 /** | 47 /** |
125 * Returns the waterlevel data computed by the WINFOArtifact. | 48 * Returns the waterlevel data computed by the WINFOArtifact. |
136 | 59 |
137 return wqkms; | 60 return wqkms; |
138 } | 61 } |
139 | 62 |
140 | 63 |
141 protected void generateCSV() | 64 protected void writeCSVData(CSVWriter writer) { |
142 throws IOException | 65 logger.info("WaterlevelExporter.writeData"); |
143 { | |
144 logger.info("WaterlevelExporter.generateCSV"); | |
145 | |
146 CSVWriter writer = new CSVWriter( | |
147 new OutputStreamWriter( | |
148 out, | |
149 DEFAULT_CSV_CHARSET), | |
150 DEFAULT_CSV_SEPARATOR); | |
151 | 66 |
152 for (WQKms[] tmp: data) { | 67 for (WQKms[] tmp: data) { |
153 for (WQKms wqkms: tmp) { | 68 for (WQKms wqkms: tmp) { |
154 wQKms2CSV(writer, wqkms); | 69 wQKms2CSV(writer, wqkms); |
155 } | 70 } |
156 } | 71 } |
157 | |
158 writer.close(); | |
159 } | 72 } |
160 | 73 |
161 | 74 |
162 protected void wQKms2CSV(CSVWriter writer, WQKms wqkms) { | 75 protected void wQKms2CSV(CSVWriter writer, WQKms wqkms) { |
163 logger.debug("WaterlevelExporter.wQKms2CSV"); | 76 logger.debug("WaterlevelExporter.wQKms2CSV"); |