2 * Copyright (C) 2012 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.remote
.controller
.csv
;
11 import java
.io
.ByteArrayInputStream
;
12 import java
.io
.ByteArrayOutputStream
;
14 import java
.io
.InputStreamReader
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
20 import java
.util
.UUID
;
22 import javax
.servlet
.ServletOutputStream
;
23 import javax
.servlet
.http
.HttpServletResponse
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
27 import org
.springframework
.beans
.propertyeditors
.UUIDEditor
;
28 import org
.springframework
.context
.ApplicationContext
;
29 import org
.springframework
.stereotype
.Controller
;
30 import org
.springframework
.web
.bind
.WebDataBinder
;
31 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
32 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
33 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
34 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
36 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
37 import eu
.etaxonomy
.cdm
.io
.common
.CdmApplicationAwareDefaultExport
;
38 import eu
.etaxonomy
.cdm
.io
.csv
.redlist
.out
.CsvTaxExportConfiguratorRedlist
;
39 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
40 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDListPropertyEditor
;
41 import eu
.etaxonomy
.cdm
.remote
.editor
.UuidList
;
49 @RequestMapping(value
= { "/csv" })
50 public class CsvExportController
{
53 private ApplicationContext appContext
;
56 private ITermService termService
;
58 private static final Logger logger
= Logger
.getLogger(CsvExportController
.class);
61 public void initBinder(WebDataBinder binder
) {
62 binder
.registerCustomEditor(UuidList
.class, new UUIDListPropertyEditor());
63 binder
.registerCustomEditor(UUID
.class, new UUIDEditor());
66 @RequestMapping(value
= { "exportRedlist" }, method
= { RequestMethod
.POST
})
67 public void doExportRedlist(
68 @RequestParam(value
= "features", required
= false) UuidList featureUuids
,
69 @RequestParam(value
= "combobox", required
= false) String classificationUUID
,
70 HttpServletResponse response
) {
72 ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();
73 CsvTaxExportConfiguratorRedlist config
= setTaxExportConfigurator(classificationUUID
, featureUuids
, byteArrayOutputStream
);
74 CdmApplicationAwareDefaultExport
<?
> defaultExport
= (CdmApplicationAwareDefaultExport
<?
>) appContext
.getBean("defaultExport");
75 logger
.info("Start export...");
76 defaultExport
.invoke(config
);
79 * Fetch data from the appContext and forward stream to HttpServleResponse
81 * FIXME: Large Data could be out of memory
85 ByteArrayInputStream bais
= new ByteArrayInputStream(byteArrayOutputStream
.toByteArray());
86 InputStreamReader isr
= new InputStreamReader(bais
, "UTF-8");
87 ServletOutputStream sos
= response
.getOutputStream();
88 response
.setContentType("text/csv");
89 response
.setHeader("Content-Disposition", "attachment; filename=\""+config
.getClassificationTitleCache()+".txt\"");
92 while((i
= isr
.read())!= -1){
95 byteArrayOutputStream
.flush();
97 byteArrayOutputStream
.close();
101 } catch (Exception e
) {
102 logger
.error("error generating feed", e
);
106 private CsvTaxExportConfiguratorRedlist
setTaxExportConfigurator(String classificationUUID
, UuidList featureUuids
, ByteArrayOutputStream byteArrayOutputStream
) {
108 @SuppressWarnings({ "unchecked", "rawtypes" })
109 Set
<UUID
> classificationUUIDS
= new HashSet
110 (Arrays
.asList(new UUID
[] {UUID
.fromString(classificationUUID
)}));
111 String destination
= System
.getProperty("java.io.tmpdir");
112 List
<Feature
> features
= new ArrayList
<Feature
>();
113 if(featureUuids
!= null){
114 for(UUID uuid
: featureUuids
) {
115 features
.add((Feature
) termService
.find(uuid
));
119 CsvTaxExportConfiguratorRedlist config
= CsvTaxExportConfiguratorRedlist
.NewInstance(null, new File(destination
));
120 config
.setHasHeaderLines(true);
121 config
.setFieldsTerminatedBy("\t");
122 config
.setClassificationUuids(classificationUUIDS
);
123 config
.setByteArrayOutputStream(byteArrayOutputStream
);
124 if(features
!= null)config
.setFeatures(features
);