Project

General

Profile

Download (1.65 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.remote.view;
2

    
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.Map;
6

    
7
import javax.servlet.http.HttpServletRequest;
8
import javax.servlet.http.HttpServletResponse;
9

    
10
import org.apache.commons.io.FileUtils;
11
import org.apache.log4j.Logger;
12
import org.json.CDL;
13
import org.json.JSONArray;
14
import org.json.JSONObject;
15
import org.springframework.web.servlet.View;
16

    
17

    
18
/**
19
 * This class is a generic approach to generate a csv output from POJOs.
20
 * Yet there will be a problem with multidimensional structures. It is 
21
 * recommended to flatten these out before handing your data over to this
22
 * view.
23
 * <p>
24
 *<b>This is a experimental class, can be changed in the future</b>
25
 * 
26
 * 
27
 * @author a.oppermann
28
 *
29
 */
30
public class CsvFileDownloadView implements View{
31

    
32
	private File file;
33

    
34
	Logger logger = Logger.getLogger(CsvFileDownloadView.class);
35

    
36
	/**
37
	 * 
38
	 * @param file
39
	 */
40
	public CsvFileDownloadView(File file){
41
		this.file = file;
42
	}
43

    
44
	
45
	
46
	@Override
47
	public String getContentType() {
48
		return null;
49
	}
50

    
51
	@Override
52
	public void render(Map<String, ?> model, HttpServletRequest request,
53
			HttpServletResponse response) throws Exception {
54
        
55
        ArrayList<?> list = (ArrayList<?>) model.get("csv");
56
        JSONObject obj;
57
        JSONObject result = new JSONObject();
58
        for(Object o : list){
59
        	obj = new JSONObject(o);
60
        	result.append("records", obj);
61
        }
62
        JSONArray ja = result.getJSONArray("records");
63
        String csv = CDL.toString(ja);
64

    
65
        File file = getFile();
66
		FileUtils.write(file, csv);
67
	}
68

    
69
	public File getFile() {
70
		return file;
71
	}
72

    
73
	public void setFile(File file) {
74
		this.file = file;
75
	}
76
}
(2-2/10)