Merged sdd-branch to trunk.
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / csv / CsvExportController.java
1 /**
2 * Copyright (C) 2012 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.remote.controller.csv;
10
11 import java.io.ByteArrayInputStream;
12 import java.io.ByteArrayOutputStream;
13 import java.io.File;
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;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import javax.servlet.ServletOutputStream;
23 import javax.servlet.http.HttpServletResponse;
24
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;
35
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;
42
43 /**
44 * @author a.oppermann
45 * @created 20.09.2012
46 *
47 */
48 @Controller
49 @RequestMapping(value = { "/csv" })
50 public class CsvExportController{
51
52 @Autowired
53 private ApplicationContext appContext;
54
55 @Autowired
56 private ITermService termService;
57
58 private static final Logger logger = Logger.getLogger(CsvExportController.class);
59
60 @InitBinder
61 public void initBinder(WebDataBinder binder) {
62 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
63 binder.registerCustomEditor(UUID.class, new UUIDEditor());
64 }
65
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) {
71
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);
77 try {
78 /*
79 * Fetch data from the appContext and forward stream to HttpServleResponse
80 *
81 * FIXME: Large Data could be out of memory
82 *
83 * HTPP Error Break
84 */
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\"");
90
91 int i;
92 while((i = isr.read())!= -1){
93 sos.write(i);
94 }
95 byteArrayOutputStream.flush();
96 isr.close();
97 byteArrayOutputStream.close();
98 sos.flush();
99 sos.close();
100
101 } catch (Exception e) {
102 logger.error("error generating feed", e);
103 }
104 }
105
106 private CsvTaxExportConfiguratorRedlist setTaxExportConfigurator(String classificationUUID, UuidList featureUuids, ByteArrayOutputStream byteArrayOutputStream) {
107
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));
116 }
117 }
118
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);
125 return config;
126 }
127 }