1
|
/**
|
2
|
* Copyright (C) 2009 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.checklist;
|
10
|
|
11
|
import java.io.ByteArrayInputStream;
|
12
|
import java.io.ByteArrayOutputStream;
|
13
|
import java.io.File;
|
14
|
import java.io.InputStreamReader;
|
15
|
import java.io.PrintWriter;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.List;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import javax.servlet.http.Cookie;
|
22
|
import javax.servlet.http.HttpServletRequest;
|
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.IService;
|
37
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
38
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultExport;
|
39
|
import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportConfiguratorRedlist;
|
40
|
import eu.etaxonomy.cdm.model.description.Feature;
|
41
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
42
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
43
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
44
|
import eu.etaxonomy.cdm.remote.controller.AbstractController;
|
45
|
import eu.etaxonomy.cdm.remote.controller.ProgressMonitorController;
|
46
|
import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
|
47
|
import eu.etaxonomy.cdm.remote.editor.UuidList;
|
48
|
|
49
|
/**
|
50
|
* @author a.oppermann
|
51
|
* @created 20.09.2012
|
52
|
*
|
53
|
*/
|
54
|
@Controller
|
55
|
@RequestMapping(value = { "/csv" })
|
56
|
public class CsvExportController extends AbstractController{
|
57
|
|
58
|
/**
|
59
|
*
|
60
|
*/
|
61
|
@Autowired
|
62
|
private ApplicationContext appContext;
|
63
|
|
64
|
@Autowired
|
65
|
private ITermService termService;
|
66
|
|
67
|
@Autowired
|
68
|
public ProgressMonitorController progressMonitorController;
|
69
|
|
70
|
private static final Logger logger = Logger.getLogger(CsvExportController.class);
|
71
|
|
72
|
/**
|
73
|
* Helper method, which allows to convert strings directly into uuids.
|
74
|
*
|
75
|
* @param binder Special DataBinder for data binding from web request parameters to JavaBean objects.
|
76
|
*/
|
77
|
@InitBinder
|
78
|
public void initBinder(WebDataBinder binder) {
|
79
|
binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
|
80
|
// binder.registerCustomEditor(NamedArea.class, new NamedAreaPropertyEditor());
|
81
|
binder.registerCustomEditor(UUID.class, new UUIDEditor());
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Fetches data from the application context and forwards the stream to the HttpServletResponse, which offers a file download.
|
86
|
*
|
87
|
* @param featureUuids List of uuids to download/select {@link Feature feature}features
|
88
|
* @param taxonNodeUuid Selected {@link Classification classification} to iterate the {@link Taxon}
|
89
|
* @param response HttpServletResponse which returns the ByteArrayOutputStream
|
90
|
*/
|
91
|
@RequestMapping(value = { "exportRedlist" }, method = { RequestMethod.POST })
|
92
|
public void doExportRedlist(
|
93
|
@RequestParam(value = "features", required = false) UuidList featureUuids,
|
94
|
@RequestParam(value = "taxonNode", required = false) String taxonNodeUuid,
|
95
|
@RequestParam(value = "area", required = false) UuidList areas,
|
96
|
@RequestParam(value = "downloadTokenValueId", required = false) String downloadTokenValueId,
|
97
|
HttpServletResponse response,
|
98
|
HttpServletRequest request) {
|
99
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
100
|
CsvTaxExportConfiguratorRedlist config = setTaxExportConfigurator(taxonNodeUuid, featureUuids, areas, byteArrayOutputStream);
|
101
|
CdmApplicationAwareDefaultExport<?> defaultExport = (CdmApplicationAwareDefaultExport<?>) appContext.getBean("defaultExport");
|
102
|
logger.info("Start export...");
|
103
|
logger.info("doExportRedlist()" + requestPathAndQuery(request));
|
104
|
defaultExport.invoke(config);
|
105
|
try {
|
106
|
/*
|
107
|
* Fetch data from the appContext and forward stream to HttpServleResponse
|
108
|
*
|
109
|
* FIXME: Large Data could be out of memory
|
110
|
*
|
111
|
* HTPP Error Break
|
112
|
*/
|
113
|
ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());//byteArrayOutputStream.toByteArray()
|
114
|
InputStreamReader isr = new InputStreamReader(bais);
|
115
|
Cookie progressCookie = new Cookie("fileDownloadToken", downloadTokenValueId);
|
116
|
progressCookie.setPath("/");
|
117
|
progressCookie.setMaxAge(60);
|
118
|
response.addCookie(progressCookie);
|
119
|
response.setContentType("text/csv; charset=utf-8");
|
120
|
response.setHeader("Content-Disposition", "attachment; filename=\""+config.getClassificationTitleCache()+".txt\"");
|
121
|
PrintWriter printWriter = response.getWriter();
|
122
|
|
123
|
int i;
|
124
|
while((i = isr.read())!= -1){
|
125
|
printWriter.write(i);
|
126
|
}
|
127
|
byteArrayOutputStream.flush();
|
128
|
isr.close();
|
129
|
byteArrayOutputStream.close();
|
130
|
printWriter.flush();
|
131
|
printWriter.close();
|
132
|
} catch (Exception e) {
|
133
|
logger.error("error generating feed", e);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
/**
|
138
|
* Cofiguration method to set the configuration details for the defaultExport in the application context.
|
139
|
*
|
140
|
* @param taxonNodeUuid pass-through the selected {@link Classification classification}
|
141
|
* @param featureUuids pass-through the selected {@link Feature feature} of a {@link Taxon}, in order to fetch it.
|
142
|
* @param areas
|
143
|
* @param byteArrayOutputStream pass-through the stream to write out the data later.
|
144
|
* @return the CsvTaxExportConfiguratorRedlist config
|
145
|
*/
|
146
|
private CsvTaxExportConfiguratorRedlist setTaxExportConfigurator(String taxonNodeUuid, UuidList featureUuids, UuidList areas, ByteArrayOutputStream byteArrayOutputStream) {
|
147
|
|
148
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
149
|
Set<UUID> taxonNodeUuids = java.util.Collections.singleton(UUID.fromString(taxonNodeUuid));
|
150
|
String destination = System.getProperty("java.io.tmpdir");
|
151
|
List<Feature> features = new ArrayList<Feature>();
|
152
|
if(featureUuids != null){
|
153
|
for(UUID uuid : featureUuids) {
|
154
|
features.add((Feature) termService.find(uuid));
|
155
|
}
|
156
|
}
|
157
|
List<NamedArea> selectedAreas = new ArrayList<NamedArea>();
|
158
|
if(areas != null){
|
159
|
for(UUID area:areas){
|
160
|
logger.info(area);
|
161
|
selectedAreas.add((NamedArea)termService.find(area));
|
162
|
}
|
163
|
}
|
164
|
|
165
|
CsvTaxExportConfiguratorRedlist config = CsvTaxExportConfiguratorRedlist.NewInstance(null, new File(destination));
|
166
|
config.setHasHeaderLines(true);
|
167
|
config.setFieldsTerminatedBy("\t");
|
168
|
config.setTaxonNodeUuids(taxonNodeUuids);
|
169
|
config.setByteArrayOutputStream(byteArrayOutputStream);
|
170
|
if(features != null)config.setFeatures(features);
|
171
|
config.setNamedAreas(selectedAreas);
|
172
|
return config;
|
173
|
}
|
174
|
|
175
|
/* (non-Javadoc)
|
176
|
* @see eu.etaxonomy.cdm.remote.controller.AbstractController#setService(eu.etaxonomy.cdm.api.service.IService)
|
177
|
*/
|
178
|
@Override
|
179
|
public void setService(IService service) {
|
180
|
// TODO Auto-generated method stub
|
181
|
|
182
|
}
|
183
|
|
184
|
}
|