Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.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 | |
children | 046bd86ae41d |
comparison
equal
deleted
inserted
replaced
390:a67748ad4d61 | 391:5d6988836f01 |
---|---|
1 package de.intevation.flys.exports; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.OutputStream; | |
5 import java.io.OutputStreamWriter; | |
6 | |
7 import org.w3c.dom.Document; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import au.com.bytecode.opencsv.CSVWriter; | |
12 | |
13 import de.intevation.artifacts.Artifact; | |
14 import de.intevation.artifacts.CallContext; | |
15 | |
16 | |
17 /** | |
18 * An abstract exporter that implements some basic methods for exporting data of | |
19 * artifacts. | |
20 * | |
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
22 */ | |
23 public abstract class AbstractExporter implements OutGenerator { | |
24 | |
25 /** The logger used in this exporter.*/ | |
26 private static Logger logger = Logger.getLogger(AbstractExporter.class); | |
27 | |
28 | |
29 /** The name of the CSV facet which triggers the CSV creation.*/ | |
30 public static final String FACET_CSV = "csv"; | |
31 | |
32 /** The default charset for the CSV export.*/ | |
33 public static final String DEFAULT_CSV_CHARSET = "UTF-8"; | |
34 | |
35 /** The default separator for the CSV export.*/ | |
36 public static final char DEFAULT_CSV_SEPARATOR = ','; | |
37 | |
38 | |
39 /** The document of the incoming out() request.*/ | |
40 protected Document request; | |
41 | |
42 /** The output stream where the data should be written to.*/ | |
43 protected OutputStream out; | |
44 | |
45 /** The CallContext object.*/ | |
46 protected CallContext context; | |
47 | |
48 /** The selected facet.*/ | |
49 protected String facet; | |
50 | |
51 | |
52 /** | |
53 * Concrete subclasses need to use this method to write their special data | |
54 * objects into the CSV document. | |
55 * | |
56 * @param writer The CSVWriter. | |
57 */ | |
58 protected abstract void writeCSVData(CSVWriter writer); | |
59 | |
60 | |
61 /** | |
62 * This method enables concrete subclasses to collected its own special | |
63 * data. | |
64 * | |
65 * @param artifacts The artifact that stores the data that has to be | |
66 * exported. | |
67 */ | |
68 protected abstract void addData(Artifact artifact); | |
69 | |
70 | |
71 public void init(Document request, OutputStream out, CallContext context) { | |
72 logger.debug("AbstractExporter.init"); | |
73 | |
74 this.request = request; | |
75 this.out = out; | |
76 this.context = context; | |
77 } | |
78 | |
79 | |
80 /** | |
81 * This doOut() just collects the data of multiple artifacts. Therefore, it | |
82 * makes use of the addData() method which enables concrete subclasses to | |
83 * store its data on its own. The real output creation takes place in the | |
84 * concrete generate() methods. | |
85 * | |
86 * @param artifact The artifact. | |
87 * @param facet The facet to add - NOTE: the facet needs to fit to the first | |
88 * facet inserted into this exporter. Otherwise this artifact/facet is | |
89 * skipped. | |
90 * @param attr The attr document. | |
91 */ | |
92 public void doOut(Artifact artifact, String facet, Document attr) { | |
93 logger.debug("AbstractExporter.doOut: " + facet); | |
94 | |
95 if (!isFacetValid(facet)) { | |
96 logger.warn("Facet '" + facet + "' not valid. No output created!"); | |
97 return; | |
98 } | |
99 | |
100 addData(artifact); | |
101 } | |
102 | |
103 | |
104 /** | |
105 * Generates an export based on a specified facet. | |
106 */ | |
107 public void generate() | |
108 throws IOException | |
109 { | |
110 logger.debug("AbstractExporter.generate"); | |
111 | |
112 if (facet != null && facet.equals(FACET_CSV)) { | |
113 generateCSV(); | |
114 } | |
115 else { | |
116 throw new IOException("invalid facet for exporter."); | |
117 } | |
118 } | |
119 | |
120 | |
121 /** | |
122 * Determines if the desired facet is valid for this exporter. If no facet | |
123 * is currently set, <i>facet</i> is set. | |
124 * | |
125 * @param facet The desired facet. | |
126 * | |
127 * @return true, if <i>facet</i> is valid, otherwise false. | |
128 */ | |
129 protected boolean isFacetValid(String facet) { | |
130 logger.debug("AbstractExporter.isFacetValid"); | |
131 | |
132 if (facet == null || facet.length() == 0) { | |
133 return false; | |
134 } | |
135 else if (this.facet == null || this.facet.length() == 0) { | |
136 logger.debug("Set the facet of this export: " + facet); | |
137 this.facet = facet; | |
138 | |
139 return true; | |
140 } | |
141 else { | |
142 return this.facet.equals(facet); | |
143 } | |
144 } | |
145 | |
146 | |
147 /** | |
148 * This method starts CSV creation. It makes use of writeCSVData() which has | |
149 * to be implemented by concrete subclasses. | |
150 */ | |
151 protected void generateCSV() | |
152 throws IOException | |
153 { | |
154 logger.info("AbstractExporter.generateCSV"); | |
155 | |
156 CSVWriter writer = new CSVWriter( | |
157 new OutputStreamWriter( | |
158 out, | |
159 DEFAULT_CSV_CHARSET), | |
160 DEFAULT_CSV_SEPARATOR); | |
161 | |
162 writeCSVData(writer); | |
163 | |
164 writer.close(); | |
165 } | |
166 } | |
167 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |