teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5861: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5861: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.server; raimund@256: ingo@1477: import java.util.ArrayList; raimund@256: import java.util.List; raimund@256: raimund@256: import java.io.Reader; raimund@256: import java.io.InputStream; raimund@256: import java.io.InputStreamReader; raimund@256: import java.io.IOException; raimund@256: raimund@256: import org.w3c.dom.Document; raimund@256: import org.w3c.dom.Element; raimund@256: ingo@1367: import org.apache.log4j.Logger; ingo@1367: raimund@256: import com.google.gwt.user.server.rpc.RemoteServiceServlet; raimund@256: raimund@256: import au.com.bytecode.opencsv.CSVReader; raimund@256: teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; raimund@256: teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; raimund@256: teichmann@5835: import org.dive4elements.river.client.shared.exceptions.ServerException; teichmann@5835: import org.dive4elements.river.client.client.services.CSVExportService; raimund@256: raimund@256: raimund@256: /** raimund@263: * @author Raimund Renkert raimund@256: */ raimund@256: public class CSVExportServiceImpl raimund@256: extends RemoteServiceServlet raimund@256: implements CSVExportService raimund@256: { ingo@1367: private static final Logger logger = ingo@1367: Logger.getLogger(CSVExportServiceImpl.class); ingo@1367: ingo@1367: raimund@256: public static final String ERROR_NO_EXPORT_FOUND = raimund@256: "error_no_export_found"; raimund@256: ingo@550: public List getCSV( raimund@256: String locale, raimund@256: String uuid, raimund@256: String name) raimund@256: throws ServerException raimund@256: { ingo@1367: logger.info("CSVExportServiceImpl.getCSV"); raimund@256: raimund@1425: String url = getServletContext().getInitParameter("server-url"); raimund@1425: raimund@256: Document requestDoc = XMLUtils.newDocument(); raimund@256: raimund@256: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( raimund@256: requestDoc, raimund@256: ArtifactNamespaceContext.NAMESPACE_URI, raimund@256: ArtifactNamespaceContext.NAMESPACE_PREFIX); raimund@256: ingo@258: Element action = ec.create("action"); ingo@258: ec.addAttr(action, "type", "csv", true); ingo@258: ec.addAttr(action, "name", name, true); raimund@256: raimund@256: requestDoc.appendChild(action); raimund@256: raimund@256: HttpClient client = new HttpClientImpl(url, locale); ingo@1477: raimund@256: try { raimund@256: InputStream in = client.collectionOut(requestDoc, uuid, "export"); ingo@258: Reader reader = new InputStreamReader (in, "UTF-8"); raimund@256: CSVReader csvReader = new CSVReader (reader); ingo@258: ingo@1477: List lines = new ArrayList(); ingo@1477: String[] line = null; ingo@1477: ingo@1477: while ((line = csvReader.readNext()) != null) { ingo@1480: if (line != null) { ingo@1480: if (!line[0].startsWith("#") && line.length > 0) { ingo@1480: if (line[0].replace("'", "").length() > 0) { ingo@1480: lines.add(line); ingo@1480: } ingo@1477: } ingo@1477: } ingo@1477: } ingo@1477: ingo@1477: return lines; raimund@256: } raimund@256: catch (IOException ce) { ingo@1367: logger.error(ce.getLocalizedMessage()); raimund@256: } raimund@256: raimund@256: throw new ServerException(ERROR_NO_EXPORT_FOUND); raimund@256: } raimund@256: } raimund@256: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :