KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > web > controller > admin > MondrianDatasourceFormController


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

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 JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import javax.servlet.http.*;
31
32
33 /**
34  * @author Uddhab Pant <br>
35  * @author Dipendra Pokhrel <br>
36  * @version $Revision: 1.7 $ $Date: 2006/04/12 00:39:12 $ <br>
37  *
38  * Mondrian datasources.xml form controller.
39  *
40  */

41 public class MondrianDatasourceFormController extends SimpleFormController {
42     public static final String JavaDoc ADD_EDIT_VIEW = "editMondrianDatasourceForm";
43     public static final String JavaDoc MAIN_VIEW = "mondrianDatasourceForm";
44     private String JavaDoc mondrianXslFile;
45     private String JavaDoc datasourceXmlFile;
46     DataSourceConfig datasourceConfig = null;
47
48     /**
49      *
50      * @param request HttpServletRequest
51      * @return Object
52      * @throws Exception
53      */

54     protected Object JavaDoc formBackingObject(HttpServletRequest request)
55         throws Exception JavaDoc {
56         Map JavaDoc model = new HashMap JavaDoc();
57
58         String JavaDoc xslFilePath = this.getServletContext()
59                                  .getRealPath(mondrianXslFile);
60         String JavaDoc xmlFilePath = this.getServletContext()
61                                  .getRealPath(datasourceXmlFile);
62
63         datasourceConfig = new DataSourceConfig(xslFilePath, xmlFilePath);
64
65         List JavaDoc datasources = datasourceConfig.getDataSources();
66
67         model.put("datasourceList", datasources);
68
69         return model;
70     }
71
72     /**
73      *
74      * @param request HttpServletRequest
75      * @param response HttpServletResponse
76      * @param command Object
77      * @param errors BindException
78      * @return ModelAndView
79      * @throws Exception
80      */

81     protected ModelAndView onSubmit(HttpServletRequest request,
82         HttpServletResponse response, Object JavaDoc command, BindException errors)
83         throws Exception JavaDoc {
84         String JavaDoc 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 JavaDoc command, BindException errors)
105         throws Exception JavaDoc {
106         Map JavaDoc map = (Map JavaDoc) command;
107
108         if (request.getParameter("dataSource") != null) {
109             List JavaDoc ds = (List JavaDoc) ((Map JavaDoc) command).get("datasourceList");
110             DataSource datasource = getDatasourceFromList(ds,
111                     request.getParameter("dataSource"));
112             map.put("selectedDatasource", datasource);
113             // A hack to wrap datasource info in view.
114
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 JavaDoc command, BindException errors)
123         throws Exception JavaDoc {
124         Map JavaDoc map = (Map JavaDoc) 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 JavaDoc command, BindException errors)
132         throws Exception JavaDoc {
133         Map JavaDoc map = (Map JavaDoc) command;
134         String JavaDoc type = request.getParameter("formType");
135         String JavaDoc dataSource = request.getParameter("dataSource");
136         String JavaDoc datasourceDescription = request.getParameter(
137                 "datasourceDescription");
138         String JavaDoc url = request.getParameter("url");
139         String JavaDoc datasourceInfo = request.getParameter("datasourceInfo");
140         String JavaDoc providerName = request.getParameter("providerName");
141         String JavaDoc providerType = request.getParameter("providerType");
142         String JavaDoc authMode = request.getParameter("authMode");
143         map.put("formType", type);
144
145         List JavaDoc dsList = (List JavaDoc) ((Map JavaDoc) 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 JavaDoc command, BindException errors)
182         throws Exception JavaDoc {
183         String JavaDoc selectedDatasource = request.getParameter("dataSource");
184         List JavaDoc dsList = (List JavaDoc) ((Map JavaDoc) 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 JavaDoc command, BindException errors)
202         throws Exception JavaDoc {
203         ModelAndView modelview;
204
205         String JavaDoc dssource = request.getParameter("dataSource");
206
207         Map JavaDoc map = (Map JavaDoc) command;
208         map.put("dataSource", dssource);
209
210         DataSource dataSource = null;
211
212         if (dssource != null) {
213             List JavaDoc dsList = (List JavaDoc) 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 JavaDoc list, String JavaDoc datasourceName) {
235         DataSource dataSource = null;
236         Iterator JavaDoc 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     /**
250      *
251      * @param datasourceXmlFile String
252      */

253     public void setDatasourceXmlFile(String JavaDoc datasourceXmlFile) {
254         this.datasourceXmlFile = datasourceXmlFile;
255     }
256
257     /**
258      *
259      * @param mondrianXslFile String
260      */

261     public void setMondrianXslFile(String JavaDoc mondrianXslFile) {
262         this.mondrianXslFile = mondrianXslFile;
263     }
264 }
265
Popular Tags