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.Arrays;
|
18
|
import java.util.Collections;
|
19
|
import java.util.List;
|
20
|
import java.util.Set;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import javax.servlet.http.Cookie;
|
24
|
import javax.servlet.http.HttpServletRequest;
|
25
|
import javax.servlet.http.HttpServletResponse;
|
26
|
|
27
|
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
|
28
|
import org.springframework.beans.factory.annotation.Autowired;
|
29
|
import org.springframework.beans.propertyeditors.UUIDEditor;
|
30
|
import org.springframework.context.ApplicationContext;
|
31
|
import org.springframework.stereotype.Controller;
|
32
|
import org.springframework.web.bind.WebDataBinder;
|
33
|
import org.springframework.web.bind.annotation.InitBinder;
|
34
|
import org.springframework.web.bind.annotation.RequestMapping;
|
35
|
import org.springframework.web.bind.annotation.RequestMethod;
|
36
|
import org.springframework.web.bind.annotation.RequestParam;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.service.IClassificationService;
|
39
|
import eu.etaxonomy.cdm.api.service.IService;
|
40
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
41
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
42
|
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
|
43
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
44
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
45
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultExport;
|
46
|
import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportConfiguratorRedlist;
|
47
|
import eu.etaxonomy.cdm.model.description.Feature;
|
48
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
49
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
50
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
51
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
52
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
53
|
import eu.etaxonomy.cdm.remote.controller.AbstractController;
|
54
|
import eu.etaxonomy.cdm.remote.controller.ProgressMonitorController;
|
55
|
import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
|
56
|
import eu.etaxonomy.cdm.remote.editor.UuidList;
|
57
|
|
58
|
/**
|
59
|
* @author a.oppermann
|
60
|
* @since 20.09.2012
|
61
|
*
|
62
|
*/
|
63
|
@Controller
|
64
|
@RequestMapping(value = { "/csv" })
|
65
|
public class CsvExportController extends AbstractController{
|
66
|
|
67
|
@Autowired
|
68
|
private ApplicationContext appContext;
|
69
|
|
70
|
@Autowired
|
71
|
private ITermService termService;
|
72
|
|
73
|
@Autowired
|
74
|
private IClassificationService classificationService;
|
75
|
|
76
|
@Autowired
|
77
|
private ITaxonService taxonService;
|
78
|
|
79
|
@Autowired
|
80
|
public ProgressMonitorController progressMonitorController;
|
81
|
|
82
|
private static final Logger logger = LogManager.getLogger(CsvExportController.class);
|
83
|
|
84
|
|
85
|
private static final List<String> TAXON_WITH_NODES_INIT_STRATEGY = Arrays.asList(new String []{
|
86
|
"taxonNodes.$",
|
87
|
"taxonNodes.classification.$",
|
88
|
"taxonNodes.childNodes.$"
|
89
|
});
|
90
|
|
91
|
private static final List<String> CLASSIFICATION_INIT_STRATEGY =
|
92
|
Arrays.asList(new String[]{"rootNode"});
|
93
|
|
94
|
/**
|
95
|
* Helper method, which allows to convert strings directly into uuids.
|
96
|
*
|
97
|
* @param binder Special DataBinder for data binding from web request parameters to JavaBean objects.
|
98
|
*/
|
99
|
@InitBinder
|
100
|
public void initBinder(WebDataBinder binder) {
|
101
|
binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
|
102
|
// binder.registerCustomEditor(NamedArea.class, new NamedAreaPropertyEditor());
|
103
|
binder.registerCustomEditor(UUID.class, new UUIDEditor());
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Fetches data from the application context and forwards the stream to the HttpServletResponse,
|
108
|
* which offers a file download.
|
109
|
*
|
110
|
* @param featureUuids List of uuids to download/select {@link Feature feature}features
|
111
|
* @param taxonName the selected taxon name
|
112
|
* @param classificationUuid the uuid of the selected classification
|
113
|
* @param response HttpServletResponse which returns the ByteArrayOutputStream
|
114
|
*/
|
115
|
@RequestMapping(value = { "exportRedlist" }, method = { RequestMethod.POST })
|
116
|
public void doExportRedlist(
|
117
|
@RequestParam(value = "features", required = false) UuidList featureUuids,
|
118
|
@RequestParam(value = "classificationUuid", required = false) String classificationUuid,
|
119
|
@RequestParam(value = "taxonName", required = false) String taxonName,
|
120
|
@RequestParam(value = "area", required = false) UuidList areas,
|
121
|
@RequestParam(value = "downloadTokenValueId", required = false) String downloadTokenValueId,
|
122
|
HttpServletResponse response,
|
123
|
HttpServletRequest request) {
|
124
|
|
125
|
boolean includeUnpublished = NO_UNPUBLISHED;
|
126
|
Classification classification = classificationService.load(UUID.fromString(classificationUuid), CLASSIFICATION_INIT_STRATEGY);
|
127
|
UUID taxonNodeUuid = classification.getRootNode().getUuid();
|
128
|
|
129
|
if(CdmUtils.isNotBlank(taxonName)){
|
130
|
MatchingTaxonConfigurator config = new MatchingTaxonConfigurator();
|
131
|
config.setIncludeUnpublished(includeUnpublished);
|
132
|
config.setClassificationUuid(UUID.fromString(classificationUuid));
|
133
|
config.setTaxonNameTitle(taxonName);
|
134
|
|
135
|
List<TaxonBase> taxaByName = taxonService.findTaxaByName(config);
|
136
|
for (TaxonBase<?> taxonBase : taxaByName) {
|
137
|
if(taxonBase.isInstanceOf(Taxon.class)){
|
138
|
TaxonNode taxonNode = classification.getNode(HibernateProxyHelper.deproxy(
|
139
|
taxonService.load(taxonBase.getUuid(),TAXON_WITH_NODES_INIT_STRATEGY), Taxon.class));
|
140
|
if(taxonNode!=null){
|
141
|
taxonNodeUuid = taxonNode.getUuid();
|
142
|
break;
|
143
|
}
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
|
148
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
149
|
CsvTaxExportConfiguratorRedlist config = setTaxExportConfigurator(taxonNodeUuid, featureUuids, areas, byteArrayOutputStream);
|
150
|
CdmApplicationAwareDefaultExport<CsvTaxExportConfiguratorRedlist> defaultExport =
|
151
|
(CdmApplicationAwareDefaultExport<CsvTaxExportConfiguratorRedlist>) appContext.getBean("defaultExport");
|
152
|
logger.info("Start export...");
|
153
|
logger.info("doExportRedlist()" + requestPathAndQuery(request));
|
154
|
defaultExport.invoke(config);
|
155
|
try {
|
156
|
/*
|
157
|
* Fetch data from the appContext and forward stream to HttpServleResponse
|
158
|
*
|
159
|
* FIXME: Large Data could be out of memory
|
160
|
*
|
161
|
* HTPP Error Break
|
162
|
*/
|
163
|
ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());//byteArrayOutputStream.toByteArray()
|
164
|
InputStreamReader isr = new InputStreamReader(bais);
|
165
|
Cookie progressCookie = new Cookie("fileDownloadToken", downloadTokenValueId);
|
166
|
progressCookie.setPath("/");
|
167
|
progressCookie.setMaxAge(60);
|
168
|
response.addCookie(progressCookie);
|
169
|
response.setContentType("text/csv; charset=utf-8");
|
170
|
response.setHeader("Content-Disposition", "attachment; filename=\""+config.getClassificationTitleCache()+".txt\"");
|
171
|
PrintWriter printWriter = response.getWriter();
|
172
|
|
173
|
int i;
|
174
|
while((i = isr.read())!= -1){
|
175
|
printWriter.write(i);
|
176
|
}
|
177
|
byteArrayOutputStream.flush();
|
178
|
isr.close();
|
179
|
byteArrayOutputStream.close();
|
180
|
printWriter.flush();
|
181
|
printWriter.close();
|
182
|
} catch (Exception e) {
|
183
|
logger.error("error generating feed", e);
|
184
|
}
|
185
|
}
|
186
|
|
187
|
/**
|
188
|
* Cofiguration method to set the configuration details for the defaultExport in the application context.
|
189
|
*
|
190
|
* @param taxonNodeUuid pass-through the selected {@link Classification classification}
|
191
|
* @param featureUuids pass-through the selected {@link Feature feature} of a {@link Taxon}, in order to fetch it.
|
192
|
* @param areas
|
193
|
* @param byteArrayOutputStream pass-through the stream to write out the data later.
|
194
|
* @return the CsvTaxExportConfiguratorRedlist config
|
195
|
*/
|
196
|
private CsvTaxExportConfiguratorRedlist setTaxExportConfigurator(UUID taxonNodeUuid, UuidList featureUuids, UuidList areas, ByteArrayOutputStream byteArrayOutputStream) {
|
197
|
|
198
|
Set<UUID> taxonNodeUuids = Collections.singleton(taxonNodeUuid);
|
199
|
String destination = System.getProperty("java.io.tmpdir");
|
200
|
List<Feature> features = new ArrayList<>();
|
201
|
if(featureUuids != null){
|
202
|
for(UUID uuid : featureUuids) {
|
203
|
features.add((Feature) termService.find(uuid));
|
204
|
}
|
205
|
}
|
206
|
List<NamedArea> selectedAreas = new ArrayList<>();
|
207
|
if(areas != null){
|
208
|
for(UUID area:areas){
|
209
|
logger.info(area);
|
210
|
selectedAreas.add((NamedArea)termService.find(area));
|
211
|
}
|
212
|
}
|
213
|
|
214
|
CsvTaxExportConfiguratorRedlist config = CsvTaxExportConfiguratorRedlist.NewInstance(null, new File(destination));
|
215
|
config.setHasHeaderLines(true);
|
216
|
config.setFieldsTerminatedBy("\t");
|
217
|
config.setTaxonNodeUuids(taxonNodeUuids);
|
218
|
config.setByteArrayOutputStream(byteArrayOutputStream);
|
219
|
config.setFeatures(features);
|
220
|
config.setNamedAreas(selectedAreas);
|
221
|
return config;
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public void setService(IService service) {}
|
226
|
|
227
|
}
|