| 1 20 package org.openi.web.controller.admin; 21 22 import org.openi.mondrian.datasource.*; 23 import org.springframework.validation.*; 24 import org.springframework.web.servlet.*; 25 import org.springframework.web.servlet.mvc.*; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import javax.servlet.http.*; 31 32 33 41 public class MondrianDatasourceFormController extends SimpleFormController { 42 public static final String ADD_EDIT_VIEW = "editMondrianDatasourceForm"; 43 public static final String MAIN_VIEW = "mondrianDatasourceForm"; 44 private String mondrianXslFile; 45 private String datasourceXmlFile; 46 DataSourceConfig datasourceConfig = null; 47 48 54 protected Object formBackingObject(HttpServletRequest request) 55 throws Exception { 56 Map model = new HashMap (); 57 58 String xslFilePath = this.getServletContext() 59 .getRealPath(mondrianXslFile); 60 String xmlFilePath = this.getServletContext() 61 .getRealPath(datasourceXmlFile); 62 63 datasourceConfig = new DataSourceConfig(xslFilePath, xmlFilePath); 64 65 List datasources = datasourceConfig.getDataSources(); 66 67 model.put("datasourceList", datasources); 68 69 return model; 70 } 71 72 81 protected ModelAndView onSubmit(HttpServletRequest request, 82 HttpServletResponse response, Object command, BindException errors) 83 throws Exception { 84 String action = request.getParameter("action"); 85 86 if ("Add".equalsIgnoreCase(action)) { 87 return handleAdd(request, response, command, errors); 88 } else if ("Edit".equalsIgnoreCase(action)) { 89 return handleEdit(request, response, command, errors); 90 } else if ("Delete".equalsIgnoreCase(action)) { 91 return handleDelete(request, response, command, errors); 92 } else if ("Save".equalsIgnoreCase(action)) { 93 return handleSave(request, response, command, errors); 94 } else if ("Cancel".equalsIgnoreCase(action)) { 95 return new ModelAndView(MAIN_VIEW, "model", command); 96 } else if (request.getParameter("dataSource") != null) { 97 return handleSelection(request, response, command, errors); 98 } 99 100 return super.onSubmit(request, response, command, errors); 101 } 102 103 private ModelAndView handleSelection(HttpServletRequest request, 104 HttpServletResponse response, Object command, BindException errors) 105 throws Exception { 106 Map map = (Map ) command; 107 108 if (request.getParameter("dataSource") != null) { 109 List ds = (List ) ((Map ) command).get("datasourceList"); 110 DataSource datasource = getDatasourceFromList(ds, 111 request.getParameter("dataSource")); 112 map.put("selectedDatasource", datasource); 113 map.put("datasourceInfo", 115 datasource.getDataSourceInfo().replaceAll(";", "; ")); 116 } 117 118 return new ModelAndView(MAIN_VIEW, "model", map); 119 } 120 121 private ModelAndView handleAdd(HttpServletRequest request, 122 HttpServletResponse response, Object command, BindException errors) 123 throws Exception { 124 Map map = (Map ) command; 125 map.put("formType", "Add"); 126 127 return new ModelAndView(ADD_EDIT_VIEW, "model", command); 128 } 129 130 private ModelAndView handleSave(HttpServletRequest request, 131 HttpServletResponse response, Object command, BindException errors) 132 throws Exception { 133 Map map = (Map ) command; 134 String type = request.getParameter("formType"); 135 String dataSource = request.getParameter("dataSource"); 136 String datasourceDescription = request.getParameter( 137 "datasourceDescription"); 138 String url = request.getParameter("url"); 139 String datasourceInfo = request.getParameter("datasourceInfo"); 140 String providerName = request.getParameter("providerName"); 141 String providerType = request.getParameter("providerType"); 142 String authMode = request.getParameter("authMode"); 143 map.put("formType", type); 144 145 List dsList = (List ) ((Map ) command).get("datasourceList"); 146 147 if ((dataSource == null) || dataSource.equalsIgnoreCase("") 148 || ("Add".equals(type) 149 && (getDatasourceFromList(dsList, dataSource) != null))) { 150 if ((dataSource == null) || dataSource.equalsIgnoreCase("")) { 151 map.put("errorMessage", "mondrianDatasourceMissing.message"); 152 } else { 153 map.put("errorMessage", "mondrianDatasourceExists.message"); 154 } 155 156 return new ModelAndView(ADD_EDIT_VIEW, "model", command); 157 } 158 159 DataSource newDataSource; 160 161 if ("Add".equals(type)) { 162 newDataSource = new DataSource(); 163 dsList.add(newDataSource); 164 } else { 165 newDataSource = getDatasourceFromList(dsList, dataSource); 166 } 167 168 newDataSource.setDataSourceName(dataSource); 169 newDataSource.setDataSourceDescription(datasourceDescription); 170 newDataSource.setUrl(url); 171 newDataSource.setDataSourceInfo(datasourceInfo); 172 newDataSource.setProviderName(providerName); 173 newDataSource.setProviderType(providerType); 174 newDataSource.setAuthenticationMode(authMode); 175 datasourceConfig.setDataSources(dsList); 176 177 return new ModelAndView(MAIN_VIEW, "model", command); 178 } 179 180 private ModelAndView handleDelete(HttpServletRequest request, 181 HttpServletResponse response, Object command, BindException errors) 182 throws Exception { 183 String selectedDatasource = request.getParameter("dataSource"); 184 List dsList = (List ) ((Map ) command).get("datasourceList"); 185 186 if (selectedDatasource != null) { 187 DataSource datasource = getDatasourceFromList(dsList, 188 selectedDatasource); 189 190 if (datasource != null) { 191 dsList.remove(datasource); 192 193 datasourceConfig.setDataSources(dsList); 194 } 195 } 196 197 return new ModelAndView(MAIN_VIEW, "model", command); 198 } 199 200 private ModelAndView handleEdit(HttpServletRequest request, 201 HttpServletResponse response, Object command, BindException errors) 202 throws Exception { 203 ModelAndView modelview; 204 205 String dssource = request.getParameter("dataSource"); 206 207 Map map = (Map ) command; 208 map.put("dataSource", dssource); 209 210 DataSource dataSource = null; 211 212 if (dssource != null) { 213 List dsList = (List ) map.get("datasourceList"); 214 dataSource = getDatasourceFromList(dsList, dssource); 215 map.put("selectedDatasource", dataSource); 216 map.put("dataSource", dataSource.getDataSourceName()); 217 map.put("datasourceDescription", 218 dataSource.getDataSourceDescription()); 219 map.put("url", dataSource.getUrl()); 220 map.put("datasourceInfo", dataSource.getDataSourceInfo()); 221 map.put("providerName", dataSource.getProviderName()); 222 map.put("providerType", dataSource.getProviderType()); 223 map.put("authMode", dataSource.getAuthenticationMode()); 224 225 map.put("formType", "Edit"); 226 modelview = new ModelAndView(ADD_EDIT_VIEW, "model", map); 227 } else { 228 modelview = super.onSubmit(request, response, command, errors); 229 } 230 231 return modelview; 232 } 233 234 private DataSource getDatasourceFromList(List list, String datasourceName) { 235 DataSource dataSource = null; 236 Iterator iterator = list.iterator(); 237 238 while (iterator.hasNext()) { 239 dataSource = (DataSource) iterator.next(); 240 241 if (dataSource.getDataSourceName().equals(datasourceName)) { 242 return dataSource; 243 } 244 } 245 246 return null; 247 } 248 249 253 public void setDatasourceXmlFile(String datasourceXmlFile) { 254 this.datasourceXmlFile = datasourceXmlFile; 255 } 256 257 261 public void setMondrianXslFile(String mondrianXslFile) { 262 this.mondrianXslFile = mondrianXslFile; 263 } 264 } 265 | Popular Tags |