1 24 package org.riotfamily.riot.list.export; 25 26 import java.io.PrintWriter ; 27 import java.text.SimpleDateFormat ; 28 import java.util.Collection ; 29 import java.util.Date ; 30 import java.util.Iterator ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 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 encoding = "UTF-8"; 52 53 private String delimiter = ";"; 54 55 public CsvExportController(EditorRepository editorRepository) { 56 this.editorRepository = editorRepository; 57 } 58 59 public void setDelimiter(String delimiter) { 60 this.delimiter = delimiter; 61 } 62 63 public void setEncoding(String encoding) { 64 this.encoding = encoding; 65 } 66 67 public ModelAndView handleRequest(HttpServletRequest request, 68 HttpServletResponse response) throws Exception { 69 70 generateCsv(request, response); 71 return null; 72 } 73 74 public void generateCsv(HttpServletRequest request, 75 HttpServletResponse response) throws Exception { 76 77 String listId = ServletRequestUtils.getStringParameter(request, "listId"); 78 String commandId = ServletRequestUtils.getStringParameter(request, "commandId"); 79 String parentId = ServletRequestUtils.getStringParameter(request, "parentId"); 80 81 CsvExportCommand command = (CsvExportCommand) 82 editorRepository.getListRepository().getCommand(commandId); 83 84 String fileEncoding = encoding; 85 if (command.getEncoding() != null) { 86 fileEncoding = command.getEncoding(); 87 } 88 89 SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd-HH:mm:ss"); 90 91 String fileName = listId + '-' + sdf.format(new Date ()) + ".csv"; 92 93 response.setContentType("text/csv; charset=" + fileEncoding); 94 response.setCharacterEncoding(encoding); 95 response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 96 97 PrintWriter 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 parent = EditorDefinitionUtils.loadParent(listDefinition, parentId); 109 110 ListParams params = new ListParamsImpl(); 111 Collection beans = listConfig.getDao().list(parent, params); 112 Iterator 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 propertyName = (String ) command.getProperties().get(i); 117 Object value = bean.getPropertyValue(propertyName); 118 if (value instanceof Collection ) { 119 Iterator it = ((Collection ) 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 |