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