KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > web > controller > ManageRFunctionController


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
21
22 package org.openi.web.controller;
23
24 import java.io.*;
25 import java.util.*;
26
27 import javax.servlet.http.*;
28
29 import org.openi.stat.r.*;
30 import org.openi.xml.BeanStorage;
31 import org.openi.project.ProjectContext;
32
33 import org.apache.log4j.*;
34 import org.springframework.validation.*;
35 import org.springframework.web.servlet.*;
36 import org.springframework.web.servlet.mvc.*;
37
38 /**
39  * @author Uddhab Pant <br>
40  * @version $Revision: 1.8 $ $Date: 2006/04/12 00:39:12 $ <br>
41  *
42  * Controller for add, edit and delete R functions.
43  *
44  */

45
46
47 public class ManageRFunctionController extends SimpleFormController {
48
49     private static Logger logger = Logger.getLogger(ManageRFunctionController.class);
50
51     private static final String JavaDoc MAIN_VIEW = "manageRFunctionView";
52     private static final String JavaDoc EDIT_FUNCTION_VIEW = "editRFunctionView";
53     private static final String JavaDoc ADD_FUNCTION_VIEW = "addRFunctionView";
54
55     private static final String JavaDoc EDIT_PARAM_VIEW = "editRFunctionParamView";
56     private static final String JavaDoc ADD_PARAM_VIEW = "addRFunctionParamView";
57
58     private String JavaDoc mainView = MAIN_VIEW;
59     private String JavaDoc addTaskView = ADD_FUNCTION_VIEW;
60     private String JavaDoc editTaskView = EDIT_FUNCTION_VIEW;
61
62     private String JavaDoc addTaskParamView = ADD_PARAM_VIEW;
63     private String JavaDoc editTaskParamView = EDIT_PARAM_VIEW;
64
65
66     private RFunctionList rFunctionList;
67     private ProjectContext projectContext;
68
69
70     /**
71      * onBindAndValidate
72      *
73      * @param request HttpServletRequest
74      * @param command Object
75      * @param errors BindException
76      * @throws Exception
77      * @todo Implement this
78      * org.springframework.web.servlet.mvc.BaseCommandController method
79      */

80     protected void onBindAndValidate(HttpServletRequest request, Object JavaDoc command,
81                                      BindException errors) throws Exception JavaDoc {
82     }
83
84     /**
85      * Restores R function list xml file
86      *
87      * @param request HttpServletRequest
88      * @return Object
89      * @throws Exception
90      * @todo Implement this
91      * org.springframework.web.servlet.mvc.AbstractFormController method
92      */

93     protected Object JavaDoc formBackingObject(HttpServletRequest request) throws
94             Exception JavaDoc {
95
96         Map model = new HashMap();
97
98         projectContext = (ProjectContext) request.getSession().getAttribute("projectContext");
99
100
101         rFunctionList = projectContext.getRFunctionList();
102
103         model.put("rFunctionList", rFunctionList);
104         if ("GET".equals(request.getMethod())) {
105             String JavaDoc rFile = request.getParameter("rFile");
106             if(rFile != null ) {
107                 RFunction function = rFunctionList.getFunctionByFile(rFile);
108                 if(function == null) {
109                     model.put("message", "rFunctionIsNotDefined.message");
110                     model.put("messageClass", "errorMessage");
111                 } else {
112                     model.put("selectedRFunction", function.getName());
113                 }
114             }
115         }
116         return model;
117     }
118
119     /**
120      * Handles submit request
121      *
122      * @param request HttpServletRequest
123      * @param response HttpServletResponse
124      * @param command Object
125      * @param errors BindException
126      * @return ModelAndView
127      * @throws Exception
128      * @todo Implement this
129      * org.springframework.web.servlet.mvc.SimpleFormController method
130      */

131     protected ModelAndView onSubmit(HttpServletRequest request,
132                                     HttpServletResponse response,
133                                     Object JavaDoc command, BindException errors) throws
134             Exception JavaDoc {
135
136         String JavaDoc action = request.getParameter("action");
137
138         logger.debug("onSubmit action=" + action);
139
140         // action order is very important
141
if ("Add Function".equals(action)) {
142             return handleAddFunction(request, response, command, errors);
143         } else if ("Add Param".equals(action)) {
144             return handleAddFunctionParam(request, response, command, errors);
145         } else if ("Edit Function".equals(action)) {
146             return handleEditFunction(request, response, command, errors);
147         } else if ("Edit Param".equals(action)) {
148             return handleEditFunctionParam(request, response, command, errors);
149         } else if ("Delete Function".equals(action)) {
150             return handleDeleteFunction(request, response, command, errors);
151         } else if ("Delete Param".equals(action)) {
152             return handleDeleteFunctionParam(request, response, command, errors);
153         } else if ("Save Function".equals(action)) {
154             return handleSaveFunction(request, response, command, errors);
155         } else if ("Save Param".equals(action)) {
156             return handleSaveFunctionParam(request, response, command, errors);
157         } else if ("Cancel".equals(action)) {
158             return handleCancel(request, response, command, errors);
159         } else if (request.getParameter("functionName") != null) {
160             return handleSelection(request, response, command, errors);
161         } else {
162             return super.onSubmit(request, response, command, errors);
163         }
164
165     }
166
167     /**
168      * handleSaveFunctionParam
169      *
170      * @param request HttpServletRequest
171      * @param response HttpServletResponse
172      * @param command Object
173      * @param errors BindException
174      * @return ModelAndView
175      */

176     private ModelAndView handleSaveFunctionParam(HttpServletRequest request,
177                                              HttpServletResponse response,
178                                              Object JavaDoc command,
179                                              BindException errors) throws
180             IOException, Exception JavaDoc {
181
182         Map model = (Map) command;
183         if ("Add".equalsIgnoreCase(request.getParameter("saveType"))) {
184             if (!validateFunctionParam(request, model)) {
185                 model.put("functionName", request.getParameter("functionName"));
186                 return new ModelAndView(addTaskParamView, "model", command);
187             }
188             RFunctionParam param = new RFunctionParam();
189
190             RFunction rFunction = rFunctionList.getFunction(request.getParameter("functionName"));
191             param.setName(request.getParameter("paramName"));
192             param.setDisplayName(request.getParameter("paramDisplayName"));
193             param.setType(request.getParameter("paramType"));
194             param.setValue(request.getParameter("paramValue"));
195
196             rFunction.addParam(param);
197
198             rFunctionList.updateFunction(rFunction);
199
200         } else if ("Edit".equalsIgnoreCase(request.getParameter("saveType"))) {
201             if (!validateFunctionParam(request, model)) {
202                 model.put("functionName", request.getParameter("functionName"));
203                 model.put("paramName", request.getParameter("paramName"));
204                 model.put("paramDisplayName", request.getParameter("paramDisplayName"));
205                 model.put("paramType", request.getParameter("paramType"));
206                 return new ModelAndView(editTaskParamView, "model", command);
207             }
208
209             RFunction rFunction = rFunctionList.getFunction(request.getParameter("functionName"));
210             RFunctionParam param = rFunction.getFunctionParam(request.getParameter("paramName"));
211
212             param.setDisplayName(request.getParameter("paramDisplayName"));
213             param.setType(request.getParameter("paramType"));
214             param.setValue(request.getParameter("paramValue"));
215
216             rFunction.updateParam(param);
217
218             rFunctionList.updateFunction(rFunction);
219         }
220
221         projectContext.saveRFunctionListBean(rFunctionList);
222
223         return handleSelection(request, response, command, errors);
224
225     }
226
227     private boolean validateFunctionParam(HttpServletRequest request,
228                                           Map model) {
229         String JavaDoc name = request.getParameter("paramName");
230         String JavaDoc type = request.getParameter("paramType");
231         if (name == null || "".equals(name)) {
232             model.put("message", "missingParamName.error");
233             model.put("messageClass", "errorMessage");
234             return false;
235         } else if (type == null || "".equals(type)) {
236             model.put("message", "missingParamType.error");
237             model.put("messageClass", "errorMessage");
238             return false;
239         }
240         return true;
241     }
242
243
244     /**
245      * handleDeleteFunctionParam
246      *
247      * @param request HttpServletRequest
248      * @param response HttpServletResponse
249      * @param command Object
250      * @param errors BindException
251      * @return ModelAndView
252      */

253     private ModelAndView handleDeleteFunctionParam(HttpServletRequest request,
254                                                HttpServletResponse response,
255                                                Object JavaDoc command,
256                                                BindException errors) throws
257             IOException, Exception JavaDoc {
258
259         Map model = (Map)command;
260         if (request.getParameter("functionName") != null
261             && request.getParameter("functionParam") != null) {
262
263             RFunction rFunction = rFunctionList.getFunction(request.getParameter(
264                     "functionName"));
265             rFunction.removeParam(request.getParameter("functionParam"));
266             rFunctionList.updateFunction(rFunction);
267             projectContext.saveRFunctionListBean(rFunctionList);
268         } else {
269             model.put("message", "selectFunctionParamDelete.message");
270             model.put("messageClass", "infoMessage");
271         }
272         return handleSelection(request, response, command, errors);
273     }
274
275     /**
276      * handleEditFunctionParam
277      *
278      * @param request HttpServletRequest
279      * @param response HttpServletResponse
280      * @param command Object
281      * @param errors BindException
282      * @return ModelAndView
283      */

284     private ModelAndView handleEditFunctionParam(HttpServletRequest request,
285                                              HttpServletResponse response,
286                                              Object JavaDoc command,
287                                              BindException errors) throws
288            Exception JavaDoc {
289         Map model = (Map)command;
290         if (request.getParameter("functionName") != null
291             && request.getParameter("functionParam") != null) {
292
293             RFunctionParam param = rFunctionList.getFunction(request.getParameter("functionName")).getFunctionParam(
294                     request.getParameter("functionParam"));
295
296             model.put("functionName", request.getParameter("functionName"));
297             model.put("paramName", param.getName());
298             model.put("paramDisplayName", param.getDisplayName());
299             model.put("paramType", param.getType());
300             model.put("paramValue", param.getValue());
301             //model.put("param", param);
302
return new ModelAndView(editTaskParamView, "model", model);
303
304         } else{
305             model.put("message", "selectFunctionEditParam.message");
306             model.put("messageClass", "infoMessage");
307             return handleSelection(request, response, command, errors);
308         }
309
310     }
311
312     /**
313      * handleAddFunctionParam
314      *
315      * @param request HttpServletRequest
316      * @param response HttpServletResponse
317      * @param command Object
318      * @param errors BindException
319      * @return ModelAndView
320      */

321     private ModelAndView handleAddFunctionParam(HttpServletRequest request,
322                                                 HttpServletResponse response,
323                                                 Object JavaDoc command,
324                                                 BindException errors) throws
325             Exception JavaDoc {
326
327         Map model = (Map)command;
328         if (request.getParameter("functionName") != null) {
329
330             model.put("functionName", request.getParameter("functionName"));
331
332             return new ModelAndView(addTaskParamView, "model", model);
333         } else{
334             model.put("message", "selectFunctionAddParam.message");
335             model.put("messageClass", "infoMessage");
336         }
337
338         return super.onSubmit(request, response, command, errors);
339     }
340
341     /**
342      * handleCancel
343      *
344      * @param request HttpServletRequest
345      * @param response HttpServletResponse
346      * @param command Object
347      * @param errors BindException
348      * @return ModelAndView
349      */

350     private ModelAndView handleCancel(HttpServletRequest request,
351                                       HttpServletResponse response,
352                                       Object JavaDoc command, BindException errors) throws
353             Exception JavaDoc {
354
355         return handleSelection(request, response, command, errors);
356     }
357
358     private boolean validateFunction(HttpServletRequest request,
359                                           Map model) {
360         String JavaDoc name = request.getParameter("functionName") ;
361         String JavaDoc file = request.getParameter("functionFile");
362         if(name == null || "".equals(name)) {
363             model.put("message", "missingFunctionName.error");
364             model.put("messageClass", "errorMessage");
365             return false;
366         } else if(file == null || "".equals(file)) {
367             model.put("message", "missingRFileName.error");
368             model.put("messageClass", "errorMessage");
369             return false;
370         }
371         return true;
372     }
373
374     /**
375      * handleSave
376      *
377      * @param request HttpServletRequest
378      * @param response HttpServletResponse
379      * @param command Object
380      * @param errors BindException
381      * @return ModelAndView
382      */

383     private ModelAndView handleSaveFunction(HttpServletRequest request,
384                                         HttpServletResponse response,
385                                         Object JavaDoc command, BindException errors) throws
386             IOException, Exception JavaDoc {
387
388         Map model = (Map)command;
389
390         if ("Add".equalsIgnoreCase(request.getParameter("saveType"))) {
391
392             if (!validateFunction(request, (Map)command)) {
393                 model.put("rFiles", projectContext.getFilesByExtension(".r"));
394                 return new ModelAndView(addTaskView, "model", command);
395             }
396
397             RFunction rFunction = new RFunction();
398
399             if (request.getParameter("functionName") != null) {
400                 rFunction.setName(request.getParameter("functionName"));
401             }
402
403             if (request.getParameter("functionDisplayName") != null) {
404                 rFunction.setDisplayName(request.getParameter("functionDisplayName"));
405             }
406
407             if (request.getParameter("functionFile") != null) {
408                 rFunction.setFile(request.getParameter("functionFile"));
409             }
410
411             rFunctionList.addFunction(rFunction);
412
413         } else if ("Edit".equalsIgnoreCase(request.getParameter("saveType"))) {
414             if (!validateFunction(request, (Map)command)) {
415                 model.put("rFiles", projectContext.getFilesByExtension(".r"));
416                 model.put("functionName", request.getParameter("functionName"));
417                 model.put("functionFile", request.getParameter("functionFile"));
418                 model.put("functionDisplayName", request.getParameter("functionDisplayName"));
419                 return new ModelAndView(editTaskView, "model", command);
420             }
421             RFunction rFunction = rFunctionList.getFunction(request.getParameter("functionName"));
422             rFunction.setDisplayName(request.getParameter("functionDisplayName"));
423             rFunction.setFile(request.getParameter("functionFile"));
424
425             rFunctionList.updateFunction(rFunction);
426         }
427
428         projectContext.saveRFunctionListBean(rFunctionList);
429
430         return handleSelection(request, response, command, errors);
431     }
432
433     /**
434      * handleSelection
435      *
436      * @param request HttpServletRequest
437      * @param response HttpServletResponse
438      * @param command Object
439      * @param errors BindException
440      * @return ModelAndView
441      */

442     private ModelAndView handleSelection(HttpServletRequest request,
443                                          HttpServletResponse response,
444                                          Object JavaDoc command, BindException errors) throws
445             IOException {
446
447         Map model = (Map)command;
448
449         model.put("rFunctionList",rFunctionList);
450
451         if (request.getParameter("functionName") != null) {
452
453             logger.debug("Function Name:"+request.getParameter("functionName"));
454
455             RFunction rFunction = rFunctionList.getFunction(request.getParameter("functionName"));
456             logger.debug("rFunction:"+ rFunction);
457             model.put("rFunction", rFunction);
458             model.put("selectedRFunction", request.getParameter("functionName"));
459
460             if (request.getParameter("functionParam") != null && rFunction != null) {
461                 RFunctionParam param = rFunction.getFunctionParam(request.getParameter("functionParam"));
462                 model.put("param", param);
463                 model.put("selectedParam", request.getParameter("functionParam"));
464             }
465
466         }
467
468         return new ModelAndView(mainView, "model", model);
469
470     }
471
472     /**
473      * handleDelete
474      *
475      * @param request HttpServletRequest
476      * @param response HttpServletResponse
477      * @param command Object
478      * @param errors BindException
479      * @return ModelAndView
480      */

481     private ModelAndView handleDeleteFunction(HttpServletRequest request,
482                                               HttpServletResponse response,
483                                               Object JavaDoc command,
484                                               BindException errors) throws
485             IOException, Exception JavaDoc {
486         Map model = (Map) command;
487         if (request.getParameter("functionName") != null) {
488             rFunctionList.removeFunction(request.getParameter("functionName"));
489             projectContext.saveRFunctionListBean(rFunctionList);
490
491         } else {
492             model.put("message", "selectFunctionDelete.message");
493             model.put("messageClass", "infoMessage");
494         }
495
496         return handleSelection(request, response, command, errors);
497     }
498
499     /**
500      * handleEdit
501      *
502      * @param request HttpServletRequest
503      * @param response HttpServletResponse
504      * @param command Object
505      * @param errors BindException
506      * @return ModelAndView
507      */

508     private ModelAndView handleEditFunction(HttpServletRequest request,
509                                             HttpServletResponse response,
510                                             Object JavaDoc command,
511                                             BindException errors) throws
512             Exception JavaDoc {
513         Map model = (Map)command;
514
515         if (request.getParameter("functionName") != null) {
516
517
518             model.put("rFiles", projectContext.getFilesByExtension(".r"));
519
520             RFunction rFunction = rFunctionList.getFunction(request.getParameter("functionName"));
521             model.put("functionName", rFunction.getName());
522             model.put("functionDisplayName", rFunction.getDisplayName());
523             model.put("functionFile", rFunction.getFile());
524             //model.put("rFunction", rFunction);
525
return new ModelAndView(editTaskView, "model", model);
526         }
527
528         model.put("message", "selectFunctionEdit.message");
529         model.put("messageClass", "infoMessage");
530
531         return super.onSubmit(request, response, command, errors);
532     }
533
534     /**
535      * handleAdd
536      *
537      * @param request HttpServletRequest
538      * @param response HttpServletResponse
539      * @param command Object
540      * @param errors BindException
541      * @return ModelAndView
542      */

543     private ModelAndView handleAddFunction(HttpServletRequest request,
544                                        HttpServletResponse response,
545                                        Object JavaDoc command,
546                                        BindException errors) {
547         Map model = (Map) command;
548
549         model.put("rFiles", projectContext.getFilesByExtension(".r"));
550
551         return new ModelAndView(addTaskView, "model", model);
552
553     }
554
555 }
556
Popular Tags