KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > list > export > CsvExportController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Jan-Frederic Linde [jfl at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.list.export;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.riotfamily.riot.dao.ListParams;
36 import org.riotfamily.riot.editor.EditorDefinitionUtils;
37 import org.riotfamily.riot.editor.EditorRepository;
38 import org.riotfamily.riot.editor.ListDefinition;
39 import org.riotfamily.riot.list.ListConfig;
40 import org.riotfamily.riot.list.support.ListParamsImpl;
41 import org.springframework.beans.BeanWrapper;
42 import org.springframework.beans.BeanWrapperImpl;
43 import org.springframework.web.bind.ServletRequestUtils;
44 import org.springframework.web.servlet.ModelAndView;
45 import org.springframework.web.servlet.mvc.Controller;
46
47 public class CsvExportController implements Controller {
48
49     private EditorRepository editorRepository;
50
51     private String JavaDoc encoding = "UTF-8";
52
53     private String JavaDoc delimiter = ";";
54
55     public CsvExportController(EditorRepository editorRepository) {
56         this.editorRepository = editorRepository;
57     }
58
59     public void setDelimiter(String JavaDoc delimiter) {
60         this.delimiter = delimiter;
61     }
62
63     public void setEncoding(String JavaDoc encoding) {
64         this.encoding = encoding;
65     }
66
67     public ModelAndView handleRequest(HttpServletRequest JavaDoc request,
68             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
69
70         generateCsv(request, response);
71         return null;
72     }
73
74     public void generateCsv(HttpServletRequest JavaDoc request,
75             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
76
77         String JavaDoc listId = ServletRequestUtils.getStringParameter(request, "listId");
78         String JavaDoc commandId = ServletRequestUtils.getStringParameter(request, "commandId");
79         String JavaDoc parentId = ServletRequestUtils.getStringParameter(request, "parentId");
80
81         CsvExportCommand command = (CsvExportCommand)
82                 editorRepository.getListRepository().getCommand(commandId);
83
84         String JavaDoc fileEncoding = encoding;
85         if (command.getEncoding() != null) {
86             fileEncoding = command.getEncoding();
87         }
88
89         SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("yyyy-MM-dd-HH:mm:ss");
90
91         String JavaDoc fileName = listId + '-' + sdf.format(new Date JavaDoc()) + ".csv";
92
93         response.setContentType("text/csv; charset=" + fileEncoding);
94         response.setCharacterEncoding(encoding);
95         response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
96
97         PrintWriter JavaDoc out = response.getWriter();
98
99         for (int i = 0; i < command.getProperties().size(); i++) {
100             out.print(command.getProperties().get(i));
101             out.print(delimiter);
102         }
103         out.println();
104
105         ListDefinition listDefinition = editorRepository.getListDefinition(listId);
106         ListConfig listConfig = listDefinition.getListConfig();
107
108         Object JavaDoc parent = EditorDefinitionUtils.loadParent(listDefinition, parentId);
109
110         ListParams params = new ListParamsImpl();
111         Collection JavaDoc beans = listConfig.getDao().list(parent, params);
112         Iterator JavaDoc beanIterator = beans.iterator();
113         while (beanIterator.hasNext()) {
114             BeanWrapper bean = new BeanWrapperImpl(beanIterator.next());
115             for (int i = 0; i < command.getProperties().size(); i++) {
116                 String JavaDoc propertyName = (String JavaDoc) command.getProperties().get(i);
117                 Object JavaDoc value = bean.getPropertyValue(propertyName);
118                 if (value instanceof Collection JavaDoc) {
119                     Iterator JavaDoc it = ((Collection JavaDoc) value).iterator();
120                     while (it.hasNext()) {
121                         out.print(it.next());
122                         out.print(' ');
123                     }
124                 }
125                 else if (value != null) {
126                     out.print(value);
127                 }
128
129                 out.print(delimiter);
130             }
131             out.println();
132         }
133     }
134
135 }
136
Popular Tags