KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.log4j.Logger;
23 import org.openi.project.Module;
24 import org.openi.project.Project;
25 import org.openi.project.ProjectContext;
26 import org.openi.xml.BeanStorage;
27 import org.springframework.validation.BindException;
28 import org.springframework.web.servlet.ModelAndView;
29 import org.springframework.web.servlet.mvc.SimpleFormController;
30 import java.util.HashMap JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37
38 /**
39  * @author Uddhab Pant
40  * @version $Revision: 1.7 $ $Date: 2006/04/12 00:39:12 $
41  *
42  * Controller for handling <strong>edit project config</strong> request.
43  * This class is responsible for
44  * <ul>
45  * <li> display edit project form
46  * <li> form validation
47  * <li> form submit
48  * </ul>
49  *
50  */

51 public class ProjectFormController extends SimpleFormController {
52     private static Logger logger = Logger.getLogger(ProjectFormController.class);
53
54     /**
55      * Retrieve a backing object for the current form from the given request
56      * @param request HttpServletRequest
57      * @return Object as application config object
58      * @throws Exception
59      */

60     protected Object JavaDoc formBackingObject(HttpServletRequest JavaDoc request)
61         throws Exception JavaDoc {
62         try {
63             //get project context from session
64
ProjectContext projectContext = (ProjectContext) request.getSession()
65                                                                     .getAttribute("projectContext");
66
67             //get project.
68
Project project = projectContext.getProject();
69
70             return project;
71         } catch (Exception JavaDoc e) {
72             logger.error("Exception:", e);
73             throw e;
74         }
75     }
76
77     /**
78      * Callback for custom post-processing in terms of binding and validation.
79      * Called on each submit, after standard binding and validation, but before error evaluation.
80      * @param request HttpServletRequest
81      * @param command Object
82      * @param errors BindException
83      * @throws Exception
84      */

85     protected void onBindAndValidate(HttpServletRequest JavaDoc request,
86         Object JavaDoc command, BindException errors) throws Exception JavaDoc {
87         Project project = (Project) command;
88
89         String JavaDoc[] folders = request.getParameterValues("modules.folderName");
90         String JavaDoc[] allowedUsers = request.getParameterValues(
91                 "modules.allowedUsers");
92
93         List JavaDoc modules = new LinkedList JavaDoc();
94
95         for (int i = 0; i < folders.length; i++) {
96             if ((folders[i] != null) && !folders[i].equals("")) {
97                 Module module = new Module();
98                 module.setFolderName(folders[i]);
99                 module.setAllowedUsers(allowedUsers[i]);
100                 modules.add(module);
101             }
102         }
103
104         project.setModules(modules);
105
106     }
107
108     /**
109      * Submit callback with all parameters. Called in case of submit
110      * without errors reported by the registered validator, or on every submit if no validator.
111      * @param request HttpServletRequest
112      * @param response HttpServletResponse
113      * @param command Object
114      * @param errors BindException
115      * @return ModelAndView
116      * @throws Exception
117      */

118     protected ModelAndView onSubmit(HttpServletRequest JavaDoc request,
119         HttpServletResponse JavaDoc response, Object JavaDoc command, BindException errors)
120         throws Exception JavaDoc {
121         try {
122             Project project = (Project) command;
123
124             //get project context from session
125
ProjectContext projectContext = (ProjectContext) request.getSession()
126                                                                     .getAttribute("projectContext");
127             projectContext.setProject(project);
128
129             //save project
130
projectContext.saveProject();
131
132             return super.onSubmit(request, response, command, errors);
133         } catch (Exception JavaDoc e) {
134             logger.error("Exception:", e);
135             throw e;
136         }
137     }
138 }
139
Popular Tags