comparison flys-artifacts/src/main/java/de/intevation/flys/exports/WaterlevelExporter.java @ 389:69d05357c177

Added an exporter (OutGenerator) for waterlevels which currently supports CSV exports. flys-artifacts/trunk@1810 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 03 May 2011 16:36:21 +0000
parents
children 5d6988836f01
comparison
equal deleted inserted replaced
388:cc6840cbe503 389:69d05357c177
1 package de.intevation.flys.exports;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.io.OutputStreamWriter;
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.w3c.dom.Document;
10
11 import org.apache.log4j.Logger;
12
13 import au.com.bytecode.opencsv.CSVWriter;
14
15 import de.intevation.artifacts.Artifact;
16 import de.intevation.artifacts.CallContext;
17
18 import de.intevation.flys.artifacts.WINFOArtifact;
19 import de.intevation.flys.artifacts.model.WQKms;
20
21
22 /**
23 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
24 */
25 public class WaterlevelExporter implements OutGenerator {
26
27 /** The logger used in this exporter.*/
28 private static Logger logger = Logger.getLogger(WaterlevelExporter.class);
29
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
50 /** The storage that contains all WQKms objects for the different facets.*/
51 protected List<WQKms[]> data;
52
53
54 public void init(Document request, OutputStream out, CallContext context) {
55 logger.debug("WaterlevelExporter.init");
56
57 this.request = request;
58 this.out = out;
59 this.context = context;
60 this.data = new ArrayList<WQKms[]>();
61 }
62
63
64 /**
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));
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 }
122
123
124 /**
125 * Returns the waterlevel data computed by the WINFOArtifact.
126 *
127 * @param artifact The WINFOArtifact.
128 *
129 * @return the computed waterlevel data.
130 */
131 protected WQKms[] getWaterlevelData(Artifact artifact) {
132 WINFOArtifact winfoArtifact = (WINFOArtifact) artifact;
133 WQKms[] wqkms = winfoArtifact.getWaterlevelData();
134
135 logger.debug("Got " + wqkms.length + " WQKms objects.");
136
137 return wqkms;
138 }
139
140
141 protected void generateCSV()
142 throws IOException
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
152 for (WQKms[] tmp: data) {
153 for (WQKms wqkms: tmp) {
154 wQKms2CSV(writer, wqkms);
155 }
156 }
157
158 writer.close();
159 }
160
161
162 protected void wQKms2CSV(CSVWriter writer, WQKms wqkms) {
163 logger.debug("WaterlevelExporter.wQKms2CSV");
164
165 int size = wqkms.size();
166 double[] result = new double[3];
167
168 for (int i = 0; i < size; i ++) {
169 result = wqkms.get(i, result);
170
171 writer.writeNext(new String[] {
172 Double.toString(result[2]),
173 Double.toString(result[0]),
174 Double.toString(result[1])
175 });
176 }
177 }
178 }
179 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org