comparison gnv-artifacts/src/main/java/de/intevation/gnv/exports/DefaultExport.java @ 230:f68ffbe974a0

Implemented an exporter for odv and csv exports. gnv-artifacts/trunk@298 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 06 Nov 2009 10:56:18 +0000
parents
children 2ceb4bf51cba
comparison
equal deleted inserted replaced
229:a610c0a01afc 230:f68ffbe974a0
1 package de.intevation.gnv.exports;
2
3 import org.apache.log4j.Logger;
4
5 import au.com.bytecode.opencsv.CSVWriter;
6
7 import java.util.List;
8 import java.util.Iterator;
9 import java.util.Collection;
10
11 import java.io.UnsupportedEncodingException;
12 import java.io.IOException;
13 import java.io.OutputStreamWriter;
14 import java.io.OutputStream;
15
16 import de.intevation.gnv.exports.Export.Profile;
17
18 import de.intevation.gnv.geobackend.base.Result;
19 import de.intevation.gnv.geobackend.base.ResultDescriptor;
20
21 import de.intevation.gnv.transition.exception.TransitionException;
22
23 /**
24 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
25 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
26 */
27 public class DefaultExport
28 implements Export
29 {
30 private static Logger log = Logger.getLogger(DefaultExport.class);
31
32 protected void writeData(
33 Profile profile,
34 Collection result,
35 CSVWriter writer
36 ) {
37 log.debug("create content for export.");
38 Iterator<Result> it = result.iterator();
39
40 ResultDescriptor rd = null;
41 int [] indices = null;
42
43 String[] entries = new String[profile.numberColumns()];
44
45 while (it.hasNext()) {
46 Result res = it.next();
47
48 if (rd == null) {
49 rd = res.getResultDescriptor();
50 String [] names = new String[entries.length];
51 for (int i = 0; i < names.length; ++i) {
52 names[i] = profile.getHeader(i);
53 }
54 indices = rd.getColumnIndices(names);
55 }
56 for (int i = 0; i < entries.length; ++i) {
57 entries[i] = profile.toString(
58 i, res.getString(indices[i]));
59 }
60 writer.writeNext(entries);
61 }
62 }
63
64 public void create(
65 Profile profile,
66 OutputStream outputStream,
67 Collection result
68 )
69 throws
70 IOException,
71 UnsupportedEncodingException,
72 TransitionException
73 {
74 if (result == null) {
75 String msg = "No data given for generation of " +
76 profile.getType() + " file.";
77 log.error(msg);
78 throw new TransitionException(msg);
79 }
80
81 CSVWriter writer = new CSVWriter(
82 new OutputStreamWriter(
83 outputStream,
84 profile.getEncoding()),
85 profile.getSeparator(),
86 profile.getQuoteCharacter(),
87 profile.getEscapeCharacter());
88
89 writeData(profile, result, writer);
90
91 writer.close();
92 }
93 }

http://dive4elements.wald.intevation.org