Mercurial > dive4elements > gnv-client
annotate gnv-artifacts/src/main/java/de/intevation/gnv/exports/ODVExport.java @ 765:f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
gnv-artifacts/trunk@821 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Tue, 23 Mar 2010 14:09:03 +0000 |
parents | |
children | a0e63136954e |
rev | line source |
---|---|
765
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
1 /** |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
2 * |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
3 */ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
4 package de.intevation.gnv.exports; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
5 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
6 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
7 import java.util.ArrayList; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
8 import java.util.Collection; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
9 import java.util.HashMap; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
10 import java.util.Iterator; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
11 import java.util.Map; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
12 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
13 import org.apache.log4j.Logger; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
14 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
15 import au.com.bytecode.opencsv.CSVWriter; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
16 import de.intevation.gnv.geobackend.base.Result; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
17 import de.intevation.gnv.state.describedata.KeyValueDescibeData; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
18 import de.intevation.gnv.state.exception.StateException; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
19 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
20 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
21 /** |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
22 * @author Tim Englich <tim.englich@intevation.de> |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
23 * |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
24 */ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
25 public class ODVExport extends DefaultExport { |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
26 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
27 private static Logger log = Logger.getLogger(ODVExport.class); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
28 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
29 private Collection parameters = null; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
30 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
31 /** |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
32 * Constructor |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
33 * @param collector |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
34 */ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
35 public ODVExport(DataCollector collector, Collection parameters) { |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
36 super(collector); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
37 this.parameters = parameters; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
38 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
39 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
40 @Override |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
41 protected void writeData(Profile profile, Collection result, |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
42 CSVWriter writer) throws StateException { |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
43 Iterator<Result> it = result.iterator(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
44 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
45 String[] header = profile.getHeader(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
46 ArrayList<String> headerList = new ArrayList<String>(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
47 for (int i= 0; i < header.length; i++){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
48 headerList.add(header[i]); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
49 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
50 ArrayList<String> paramids = new ArrayList<String>(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
51 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
52 Map<String[], Map<String,String>> aggregatedRows = new HashMap<String[], Map<String,String>>(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
53 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
54 String currentParameterID = "N/N"; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
55 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
56 while (it.hasNext()) { |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
57 Result res = it.next(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
58 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
59 String[] key = collector.getData(res); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
60 String parameterValue = res.getString("DATAVALUE"); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
61 String parameterID = res.getString("PARAMETER"); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
62 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
63 if (!currentParameterID.equals(parameterID)){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
64 paramids.add(parameterID); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
65 headerList.add(this.findParamTitle(parameters, parameterID)); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
66 headerList.add("QF"); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
67 currentParameterID = parameterID; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
68 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
69 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
70 Map<String,String> aggregatedRow = aggregatedRows.get(key); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
71 if (aggregatedRow!= null){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
72 aggregatedRow.put(parameterID, parameterValue); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
73 }else{ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
74 Map<String,String> params = new HashMap<String, String>(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
75 params.put(parameterID, parameterValue); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
76 aggregatedRows.put(key, params); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
77 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
78 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
79 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
80 if (header != null){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
81 writer.writeNext(headerList.toArray(header)); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
82 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
83 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
84 Iterator<String[]> rows = aggregatedRows.keySet().iterator(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
85 while (rows.hasNext()){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
86 String[] row = rows.next(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
87 Map<String,String> params = aggregatedRows.get(row); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
88 ArrayList<String> rowList = new ArrayList<String>(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
89 for (int i= 0; i < row.length; i++){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
90 rowList.add(row[i]); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
91 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
92 for (int i = 0; i < paramids.size();i++){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
93 String key = paramids.get(i); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
94 String value = params.get(key); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
95 if (value == null){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
96 value = ""; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
97 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
98 rowList.add(value); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
99 rowList.add("1"); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
100 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
101 writer.writeNext(rowList.toArray(row)); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
102 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
103 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
104 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
105 protected String findParamTitle(Collection values, String id) { |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
106 log.debug("find description of dataset"); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
107 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
108 if (values != null){ |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
109 Iterator it = values.iterator(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
110 while (it.hasNext()) { |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
111 KeyValueDescibeData data = (KeyValueDescibeData) it.next(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
112 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
113 if (id.equals(data.getKey())) |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
114 return data.getValue(); |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
115 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
116 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
117 return ""; |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
118 } |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
119 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
120 |
f28a7eac542a
Integrated valid ODV-Export to the GNV-Artifacts
Tim Englich <tim.englich@intevation.de>
parents:
diff
changeset
|
121 } |