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
.model
.taxon
.Classification
;
41 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDListPropertyEditor
;
42 import eu
.etaxonomy
.cdm
.remote
.editor
.UuidList
;
50 @RequestMapping(value
= { "/csv" })
51 public class CsvExportController
{
54 private ApplicationContext appContext
;
57 private ITermService termService
;
59 private static final Logger logger
= Logger
.getLogger(CsvExportController
.class);
62 public void initBinder(WebDataBinder binder
) {
63 binder
.registerCustomEditor(UuidList
.class, new UUIDListPropertyEditor());
64 binder
.registerCustomEditor(UUID
.class, new UUIDEditor());
67 @RequestMapping(value
= { "exportRedlist" }, method
= { RequestMethod
.POST
})
68 public void doExportRedlist(
69 @RequestParam(value
= "features", required
= false) UuidList featureUuids
,
70 @RequestParam(value
= "combobox", required
= false) String classificationUUID
,
71 HttpServletResponse response
) {
73 ByteArrayOutputStream byteArrayOutputStream
= new ByteArrayOutputStream();
74 CsvTaxExportConfiguratorRedlist config
= setTaxExportConfigurator(classificationUUID
, featureUuids
, byteArrayOutputStream
);
75 CdmApplicationAwareDefaultExport
<?
> defaultExport
= (CdmApplicationAwareDefaultExport
<?
>) appContext
.getBean("defaultExport");
76 logger
.info("Start export...");
77 defaultExport
.invoke(config
);
80 * Fetch data from the appContext and forward stream to HttpServleResponse
82 * FIXME: Large Data could be out of memory
86 ByteArrayInputStream bais
= new ByteArrayInputStream(byteArrayOutputStream
.toByteArray());
87 InputStreamReader isr
= new InputStreamReader(bais
, "UTF-8");
88 ServletOutputStream sos
= response
.getOutputStream();
89 response
.setContentType("text/csv");
90 response
.setHeader("Content-Disposition", "attachment; filename=\""+config
.getClassificationTitleCache()+".txt\"");
93 while((i
= isr
.read())!= -1){
96 byteArrayOutputStream
.flush();
98 byteArrayOutputStream
.close();
102 } catch (Exception e
) {
103 logger
.error("error generating feed", e
);
107 private CsvTaxExportConfiguratorRedlist
setTaxExportConfigurator(String classificationUUID
, UuidList featureUuids
, ByteArrayOutputStream byteArrayOutputStream
) {
109 @SuppressWarnings({ "unchecked", "rawtypes" })
110 Set
<UUID
> classificationUUIDS
= new HashSet
111 (Arrays
.asList(new UUID
[] {UUID
.fromString(classificationUUID
)}));
112 String destination
= System
.getProperty("java.io.tmpdir");
113 List
<Feature
> features
= new ArrayList
<Feature
>();
114 if(featureUuids
!= null){
115 for(UUID uuid
: featureUuids
) {
116 features
.add((Feature
) termService
.find(uuid
));
120 CsvTaxExportConfiguratorRedlist config
= CsvTaxExportConfiguratorRedlist
.NewInstance(null, new File(destination
));
121 config
.setHasHeaderLines(true);
122 config
.setFieldsTerminatedBy("\t");
123 config
.setClassificationUuids(classificationUUIDS
);
124 config
.setByteArrayOutputStream(byteArrayOutputStream
);
125 if(features
!= null)config
.setFeatures(features
);