Project

General

Profile

« Previous | Next » 

Revision 2eedcd33

Added by Patrick Plitzner almost 8 years ago

#5890 Rename classification parameters to taxon node

View differences:

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.Arrays;
18
import java.util.HashSet;
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.IService;
39
import eu.etaxonomy.cdm.api.service.ITermService;
40
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultExport;
41
import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportConfiguratorRedlist;
42
import eu.etaxonomy.cdm.model.description.Feature;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44
import eu.etaxonomy.cdm.model.taxon.Classification;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.remote.controller.AbstractController;
47
import eu.etaxonomy.cdm.remote.controller.ProgressMonitorController;
48
import eu.etaxonomy.cdm.remote.editor.UUIDListPropertyEditor;
49
import eu.etaxonomy.cdm.remote.editor.UuidList;
50

  
51
/**
52
 * @author a.oppermann
53
 * @created 20.09.2012
54
 * 
55
 */
56
@Controller
57
@RequestMapping(value = { "/csv" })
58
public class CsvExportController extends AbstractController{
59

  
60
	/**
61
	 * 
62
	 */
63
	@Autowired
64
	private ApplicationContext appContext;
65
	
66
	@Autowired 
67
	private ITermService termService;
68
	
69
	@Autowired
70
	public ProgressMonitorController progressMonitorController;
71
	
72
	private static final Logger logger = Logger.getLogger(CsvExportController.class);
73
	
74
	/**
75
	 * Helper method, which allows to convert strings directly into uuids.
76
	 * 
77
	 * @param binder Special DataBinder for data binding from web request parameters to JavaBean objects.
78
	 */
79
    @InitBinder
80
    public void initBinder(WebDataBinder binder) {
81
        binder.registerCustomEditor(UuidList.class, new UUIDListPropertyEditor());
82
//        binder.registerCustomEditor(NamedArea.class, new NamedAreaPropertyEditor());
83
        binder.registerCustomEditor(UUID.class, new UUIDEditor());
84
    }
85

  
86
    /**
87
     * Fetches data from the application context and forwards the stream to the HttpServletResponse, which offers a file download.
88
     *
89
     * @param featureUuids List of uuids to download/select {@link Feature feature}features
90
     * @param classificationUUID Selected {@link Classification classification} to iterate the {@link Taxon}
91
     * @param response HttpServletResponse which returns the ByteArrayOutputStream
92
     */
93
	@RequestMapping(value = { "exportRedlist" }, method = { RequestMethod.POST })
94
	public void doExportRedlist(
95
			@RequestParam(value = "features", required = false) UuidList featureUuids,
96
			@RequestParam(value = "classification", required = false) String classificationUUID,
97
            @RequestParam(value = "area", required = false) UuidList areas,
98
			@RequestParam(value = "downloadTokenValueId", required = false) String downloadTokenValueId,
99
			HttpServletResponse response,
100
			HttpServletRequest request) {
101
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
102
		CsvTaxExportConfiguratorRedlist config = setTaxExportConfigurator(classificationUUID, featureUuids, areas, byteArrayOutputStream);
103
		CdmApplicationAwareDefaultExport<?> defaultExport = (CdmApplicationAwareDefaultExport<?>) appContext.getBean("defaultExport");
104
		logger.info("Start export...");
105
		logger.info("doExportRedlist()" + requestPathAndQuery(request));
106
		defaultExport.invoke(config);
107
		try {
108
			/*
109
			 *  Fetch data from the appContext and forward stream to HttpServleResponse
110
			 *  
111
			 *  FIXME: Large Data could be out of memory
112
			 *  
113
			 *  HTPP Error Break
114
			 */
115
			ByteArrayInputStream bais = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());//byteArrayOutputStream.toByteArray()
116
			InputStreamReader isr = new InputStreamReader(bais);
117
			Cookie progressCookie = new Cookie("fileDownloadToken", downloadTokenValueId);
118
			progressCookie.setPath("/");
119
			progressCookie.setMaxAge(60);
120
			response.addCookie(progressCookie);
121
			response.setContentType("text/csv; charset=utf-8");
122
			response.setHeader("Content-Disposition", "attachment; filename=\""+config.getClassificationTitleCache()+".txt\"");
123
			PrintWriter printWriter = response.getWriter();
124

  
125
			int i;
126
			while((i = isr.read())!= -1){
127
				printWriter.write(i);
128
			}
129
			byteArrayOutputStream.flush();
130
			isr.close();
131
			byteArrayOutputStream.close();
132
			printWriter.flush();
133
			printWriter.close();
134
		} catch (Exception e) {
135
			logger.error("error generating feed", e);
136
		}
137
	}
138

  
139
	/**
140
	 * Cofiguration method to set the configuration details for the defaultExport in the application context.
141
	 * 
142
	 * @param classificationUUID pass-through the selected {@link Classification classification}
143
	 * @param featureUuids pass-through the selected {@link Feature feature} of a {@link Taxon}, in order to fetch it.
144
	 * @param areas 
145
	 * @param byteArrayOutputStream pass-through the stream to write out the data later.
146
	 * @return the CsvTaxExportConfiguratorRedlist config
147
	 */
148
	private CsvTaxExportConfiguratorRedlist setTaxExportConfigurator(String classificationUUID, UuidList featureUuids, UuidList areas, ByteArrayOutputStream byteArrayOutputStream) {
149

  
150
		@SuppressWarnings({ "unchecked", "rawtypes" })
151
		Set<UUID> classificationUUIDS = new HashSet
152
		(Arrays.asList(new UUID[] {UUID.fromString(classificationUUID)}));
153
		String destination = System.getProperty("java.io.tmpdir");
154
		List<Feature> features = new ArrayList<Feature>();
155
		if(featureUuids != null){
156
			for(UUID uuid : featureUuids) {
157
				features.add((Feature) termService.find(uuid));
158
			}
159
		}
160
		List<NamedArea> selectedAreas = new ArrayList<NamedArea>();
161
		if(areas != null){
162
			for(UUID area:areas){
163
				logger.info(area);
164
				selectedAreas.add((NamedArea)termService.find(area));
165
			}
166
		}
167

  
168
		CsvTaxExportConfiguratorRedlist config = CsvTaxExportConfiguratorRedlist.NewInstance(null, new File(destination));
169
		config.setHasHeaderLines(true);
170
		config.setFieldsTerminatedBy("\t");
171
		config.setClassificationUuids(classificationUUIDS);
172
		config.setByteArrayOutputStream(byteArrayOutputStream);
173
		if(features != null)config.setFeatures(features);
174
        config.setNamedAreas(selectedAreas);
175
		return config;
176
	}
177
	
178
	/* (non-Javadoc)
179
	 * @see eu.etaxonomy.cdm.remote.controller.AbstractController#setService(eu.etaxonomy.cdm.api.service.IService)
180
	 */
181
	@Override
182
	public void setService(IService service) {
183
		// TODO Auto-generated method stub
184
		
185
	}
186
	
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 = "classification", 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
	
187 184
}

Also available in: Unified diff