comparison flys-artifacts/src/main/java/de/intevation/flys/exports/AbstractExporter.java @ 3786:4adc35aa655c

merged flys-artifacts/2.9.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:47 +0200
parents 118fe1cc8cc8
children a707ef048188
comparison
equal deleted inserted replaced
3719:e82acd5c86f7 3786:4adc35aa655c
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 java.text.NumberFormat;
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.artifactdatabase.state.ArtifactAndFacet;
19 import de.intevation.artifactdatabase.state.Settings;
20
21 import de.intevation.artifacts.common.ArtifactNamespaceContext;
22 import de.intevation.artifacts.common.utils.XMLUtils;
23
24 import de.intevation.flys.artifacts.resources.Resources;
25 import de.intevation.flys.collections.FLYSArtifactCollection;
26
27 import de.intevation.flys.utils.Formatter;
28
29
30 /**
31 * An abstract exporter that implements some basic methods for exporting data of
32 * artifacts.
33 *
34 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
35 */
36 public abstract class AbstractExporter implements OutGenerator {
37
38 /** The logger used in this exporter.*/
39 private static Logger logger = Logger.getLogger(AbstractExporter.class);
40
41 /* XXX: Why does AbstractExporter do not implement FacetTypes? */
42 public static String FIX_PARAMETERS = "fix_parameters";
43
44 /** The name of the CSV facet which triggers the CSV creation. */
45 public static final String FACET_CSV = "csv";
46
47 /** The name of the PDF facet which triggers the PDF creation. */
48 public static final String FACET_PDF = "pdf";
49
50 /** The default charset for the CSV export. */
51 public static final String DEFAULT_CSV_CHARSET = "UTF-8";
52
53 /** The default separator for the CSV export. */
54 public static final char DEFAULT_CSV_SEPARATOR = ',';
55
56 /** XPath that points to the desired export facet. */
57 public static final String XPATH_FACET = "/art:action/@art:type";
58
59 /** The document of the incoming out() request. */
60 protected Document request;
61
62 /** The output stream where the data should be written to. */
63 protected OutputStream out;
64
65 /** The CallContext object. */
66 protected CallContext context;
67
68 /** The selected facet. */
69 protected String facet;
70
71 /** The collection.*/
72 protected FLYSArtifactCollection collection;
73
74 /** The master artifact. */
75 protected Artifact master;
76
77
78 /**
79 * Concrete subclasses need to use this method to write their special data
80 * objects into the CSV document.
81 *
82 * @param writer The CSVWriter.
83 */
84 protected abstract void writeCSVData(CSVWriter writer) throws IOException;
85
86
87 /**
88 * Concrete subclasses need to use this method to write their special data
89 * objects into the PDF document.
90 */
91 protected abstract void writePDF(OutputStream out);
92
93
94 /**
95 * This method enables concrete subclasses to collected its own special
96 * data.
97 *
98 * @param data The artifact that stores the data that has to be
99 * exported.
100 */
101 protected abstract void addData(Object data);
102
103
104 @Override
105 public void init(Document request, OutputStream out, CallContext context) {
106 logger.debug("AbstractExporter.init");
107
108 this.request = request;
109 this.out = out;
110 this.context = context;
111 }
112
113
114 @Override
115 public void setMasterArtifact(Artifact master) {
116 this.master = master;
117 }
118
119
120 @Override
121 public void setCollection(FLYSArtifactCollection collection) {
122 this.collection = collection;
123 }
124
125
126 /**
127 * This doOut() just collects the data of multiple artifacts. Therefore, it
128 * makes use of the addData() method which enables concrete subclasses to
129 * store its data on its own. The real output creation takes place in the
130 * concrete generate() methods.
131 *
132 * @param artifactFacet The artifact and facet.
133 * The facet to add - NOTE: the facet needs to fit to the first
134 * facet inserted into this exporter. Otherwise this artifact/facet is
135 * skipped.
136 * @param attr The attr document.
137 */
138 @Override
139 public void doOut(
140 ArtifactAndFacet artifactFacet,
141 Document attr,
142 boolean visible
143 ) {
144 String name = artifactFacet.getFacetName();
145
146 logger.debug("AbstractExporter.doOut: " + name);
147
148 if (!isFacetValid(name)) {
149 logger.warn("Facet '" + name + "' not valid. No output created!");
150 return;
151 }
152
153 addData(artifactFacet.getData(context));
154 }
155
156
157 /**
158 * Generates an export based on a specified facet.
159 */
160 @Override
161 public void generate()
162 throws IOException
163 {
164 logger.debug("AbstractExporter.generate");
165
166 if (facet == null) {
167 throw new IOException("invalid (null) facet for exporter");
168 }
169
170 if (facet.equals(FACET_CSV)) {
171 generateCSV();
172 }
173 else if (facet.equals(FACET_PDF)) {
174 generatePDF();
175 }
176 else {
177 throw new IOException(
178 "invalid facet for exporter: '" + facet + "'");
179 }
180 }
181
182
183 /**
184 * Determines if the desired facet is valid for this exporter. If no facet
185 * is currently set, <i>facet</i> is set.
186 *
187 * @param facet The desired facet.
188 *
189 * @return true, if <i>facet</i> is valid, otherwise false.
190 */
191 protected boolean isFacetValid(String facet) {
192 logger.debug("AbstractExporter.isFacetValid : " + facet + " (" + getFacet() + ")" );
193
194 String thisFacet = getFacet();
195
196 if (thisFacet == null || thisFacet.length() == 0) {
197 return false;
198 }
199 else if (facet == null || facet.length() == 0) {
200 return false;
201 }
202 else {
203 return thisFacet.equals(facet);
204 }
205 }
206
207
208 /**
209 * Returns the name of the desired facet.
210 *
211 * @return the name of the desired facet.
212 */
213 protected String getFacet() {
214 if (facet == null) {
215 facet = getFacetFromRequest();
216 }
217
218 return facet;
219 }
220
221
222 /**
223 * Extracts the name of the requested facet from request document.
224 *
225 * @return the name of the requested facet.
226 */
227 protected String getFacetFromRequest() {
228 return XMLUtils.xpathString(
229 request, XPATH_FACET, ArtifactNamespaceContext.INSTANCE);
230 }
231
232
233 protected String msg(String key, String def) {
234 return Resources.getMsg(context.getMeta(), key, def);
235 }
236
237
238 /**
239 * This method starts CSV creation. It makes use of writeCSVData() which has
240 * to be implemented by concrete subclasses.
241 */
242 protected void generateCSV()
243 throws IOException
244 {
245 logger.info("AbstractExporter.generateCSV");
246
247 CSVWriter writer = new CSVWriter(
248 new OutputStreamWriter(
249 out,
250 DEFAULT_CSV_CHARSET),
251 DEFAULT_CSV_SEPARATOR);
252
253 writeCSVData(writer);
254
255 writer.close();
256 }
257
258
259 /**
260 * This method starts PDF creation.
261 */
262 protected void generatePDF()
263 throws IOException
264 {
265 logger.info("AbstractExporter.generatePDF");
266 writePDF(this.out);
267 }
268
269
270 /**
271 * Returns an instance of <i>EmptySettings</i> currently!
272 *
273 * @return an instance of <i>EmptySettings</i>.
274 */
275 public Settings getSettings() {
276 return new EmptySettings();
277 }
278
279
280 /**
281 * This method is not implemented. Override it in subclasses if those need a
282 * <i>Settings</i> object.
283 */
284 public void setSettings(Settings settings) {
285 // do nothing
286 }
287
288
289 /**
290 * Returns the number formatter for kilometer values.
291 *
292 * @return the number formatter for kilometer values.
293 */
294 protected NumberFormat getKmFormatter() {
295 return Formatter.getWaterlevelKM(context);
296 }
297
298
299 /**
300 * Returns the number formatter for W values.
301 *
302 * @return the number formatter for W values.
303 */
304 protected NumberFormat getWFormatter() {
305 return Formatter.getWaterlevelW(context);
306 }
307
308
309 /**
310 * Returns the number formatter for Q values.
311 *
312 * @return the number formatter for Q values.
313 */
314 protected NumberFormat getQFormatter() {
315 return Formatter.getWaterlevelQ(context);
316 }
317 }
318 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org