reintegrating redlist branch into trunk with minor fixes for the csv exporter. These...
[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.model.taxon.Classification;
41 import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
42 import eu.etaxonomy.cdm.remote.editor.UuidList;
43
44 /**
45 * @author a.oppermann
46 * @created 20.09.2012
47 *
48 */
49 @Controller
50 @RequestMapping(value = { "/csv" })
51 public class CsvExportController{
52
53 @Autowired
54 private ApplicationContext appContext;
55
56 @Autowired
57 private ITermService termService;
58
59 private static final Logger logger = Logger.getLogger(CsvExportController.class);
60
61 @InitBinder
62 public void initBinder(WebDataBinder binder) {
63 binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
64 binder.registerCustomEditor(UUID.class, new UUIDEditor());
65 }
66
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) {
72
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);
78 try {
79 /*
80 * Fetch data from the appContext and forward stream to HttpServleResponse
81 *
82 * FIXME: Large Data could be out of memory
83 *
84 * HTPP Error Break
85 */
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\"");
91
92 int i;
93 while((i = isr.read())!= -1){
94 sos.write(i);
95 }
96 byteArrayOutputStream.flush();
97 isr.close();
98 byteArrayOutputStream.close();
99 sos.flush();
100 sos.close();
101
102 } catch (Exception e) {
103 logger.error("error generating feed", e);
104 }
105 }
106
107 private CsvTaxExportConfiguratorRedlist setTaxExportConfigurator(String classificationUUID, UuidList featureUuids, ByteArrayOutputStream byteArrayOutputStream) {
108
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));
117 }
118 }
119
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);
126 return config;
127 }
128 }