Mercurial > dive4elements > river
annotate gwt-client/src/main/java/org/dive4elements/river/client/server/CSVExportServiceImpl.java @ 9189:13a0c4be17ea
bom for csv export added
author | gernotbelger |
---|---|
date | Thu, 28 Jun 2018 14:11:12 +0200 |
parents | 238fc722f87a |
children |
rev | line source |
---|---|
5861
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
2 * Software engineering by Intevation GmbH |
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
3 * |
5993
ea9eef426962
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5861
diff
changeset
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
5861
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
5993
ea9eef426962
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5861
diff
changeset
|
6 * documentation coming with Dive4Elements River for details. |
5861
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
7 */ |
172338b1407f
GWT client: Added copyright header.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
8 |
5835
821a02bbfb4e
Fixed internal java dependencies
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5834
diff
changeset
|
9 package org.dive4elements.river.client.server; |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
10 |
9189 | 11 import java.io.IOException; |
12 import java.io.InputStream; | |
13 import java.io.InputStreamReader; | |
14 import java.io.Reader; | |
1477
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
15 import java.util.ArrayList; |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
16 import java.util.List; |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
17 |
9189 | 18 import org.apache.log4j.Logger; |
19 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
20 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
21 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
22 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | |
23 import org.dive4elements.river.client.client.services.CSVExportService; | |
24 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
25 import org.w3c.dom.Document; |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
26 import org.w3c.dom.Element; |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
27 |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
28 import com.google.gwt.user.server.rpc.RemoteServiceServlet; |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
29 |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
30 import au.com.bytecode.opencsv.CSVReader; |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
31 |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
32 /** |
263
f56523bf4c55
Changed the empty table message for helper and data tables.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
259
diff
changeset
|
33 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
34 */ |
9189 | 35 public class CSVExportServiceImpl extends RemoteServiceServlet implements CSVExportService { |
36 private static final Logger log = Logger.getLogger(CSVExportServiceImpl.class); | |
1367
ab8eb2f544f2
Replaced stdout and stderr logging with log4j loggers in server classes.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
550
diff
changeset
|
37 |
9189 | 38 public static final String ERROR_NO_EXPORT_FOUND = "error_no_export_found"; |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
39 |
9189 | 40 @Override |
41 public List<String[]> getCSV(final String locale, final String uuid, final String name) throws ServerException { | |
8203
238fc722f87a
sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents:
7036
diff
changeset
|
42 log.info("CSVExportServiceImpl.getCSV"); |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
43 |
9189 | 44 final String url = getServletContext().getInitParameter("server-url"); |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
45 |
9189 | 46 final Document requestDoc = XMLUtils.newDocument(); |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
47 |
9189 | 48 final XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(requestDoc, ArtifactNamespaceContext.NAMESPACE_URI, |
49 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
50 | |
51 final Element action = ec.create("action"); | |
258
5091675676d6
Fixed attributes/namespaces in the XML document that is used to fetch the CSV export of WINFO computations.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
256
diff
changeset
|
52 ec.addAttr(action, "type", "csv", true); |
5091675676d6
Fixed attributes/namespaces in the XML document that is used to fetch the CSV export of WINFO computations.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
256
diff
changeset
|
53 ec.addAttr(action, "name", name, true); |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
54 |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
55 requestDoc.appendChild(action); |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
56 |
9189 | 57 final HttpClient client = new HttpClientImpl(url, locale); |
1477
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
58 |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
59 try { |
9189 | 60 final InputStream in = client.collectionOut(requestDoc, uuid, "export"); |
61 final Reader reader = new InputStreamReader(in, "UTF-8"); | |
62 final CSVReader csvReader = new CSVReader(reader, ';'); | |
258
5091675676d6
Fixed attributes/namespaces in the XML document that is used to fetch the CSV export of WINFO computations.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
256
diff
changeset
|
63 |
9189 | 64 final byte[] BOM = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }; |
65 final char bomChar = new String(BOM, "UTF-8").charAt(0); | |
66 final StringBuilder bomCharCheckBuilder = new StringBuilder().append(bomChar).append("#"); | |
1477
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
67 |
9189 | 68 final List<String[]> lines = new ArrayList<String[]>(); |
69 String[] line = null; | |
1477
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
70 while ((line = csvReader.readNext()) != null) { |
1480
1f2f08211a5f
Skip empty lines in CSV service.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1477
diff
changeset
|
71 if (line != null) { |
9189 | 72 if (line.length > 0 && !line[0].startsWith("#") && !line[0].startsWith(bomCharCheckBuilder.toString())) { |
1480
1f2f08211a5f
Skip empty lines in CSV service.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1477
diff
changeset
|
73 if (line[0].replace("'", "").length() > 0) { |
1f2f08211a5f
Skip empty lines in CSV service.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1477
diff
changeset
|
74 lines.add(line); |
1f2f08211a5f
Skip empty lines in CSV service.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1477
diff
changeset
|
75 } |
1477
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
76 } |
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
77 } |
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
78 } |
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
79 |
6e694603cde1
Improved the CSVExportService to ignore lines that begin with a '#' character.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
1425
diff
changeset
|
80 return lines; |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
81 } |
9189 | 82 catch (final IOException ce) { |
8203
238fc722f87a
sed 's/logger/log/g' src/**/*.java
Sascha L. Teichmann <teichmann@intevation.de>
parents:
7036
diff
changeset
|
83 log.error(ce.getLocalizedMessage()); |
256
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
84 } |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
85 |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
86 throw new ServerException(ERROR_NO_EXPORT_FOUND); |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
87 } |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
88 } |
5e1c1b7d6516
Added table and service for calculation output to UI.
Raimund Renkert <raimund.renkert@intevation.de>
parents:
diff
changeset
|
89 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |