KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > control > CreateUserController


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.control;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.springframework.validation.BindException;
28 import org.springframework.web.bind.ServletRequestDataBinder;
29 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
30 import org.springframework.web.servlet.ModelAndView;
31 import org.springframework.web.servlet.mvc.SimpleFormController;
32
33 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
34 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl;
35 import com.jaspersoft.jasperserver.war.common.JasperServerConst;
36 import com.jaspersoft.jasperserver.war.common.JasperServerUtil;
37
38 import com.jaspersoft.jasperserver.api.metadata.user.domain.User;
39 import com.jaspersoft.jasperserver.api.metadata.user.domain.Role;
40 import com.jaspersoft.jasperserver.api.metadata.user.service.UserAuthorityService;
41 import com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl;
42
43 import java.util.*;
44
45
46 public class CreateUserController extends CRUDUserController {
47
48     //variable for userauth-service
49
protected UserAuthorityService userAuthService;
50
51     /*
52     * method to return authService object
53     * @param
54     * @return: Object
55     */

56     public UserAuthorityService getUserAuthService() {
57         return userAuthService;
58     }
59
60     /*
61     * method to return authService object
62     * @param
63     * @return: void
64     */

65     public void setUserAuthService(UserAuthorityService userAuthService) {
66         this.userAuthService = userAuthService;
67     }
68
69     /*
70     * overridden method for commandobject initialization
71     * during formView
72     * @param
73     * @return Object
74     */

75     public Object JavaDoc formBackingObject(HttpServletRequest JavaDoc request) {
76
77         User user = null;
78         ExecutionContext context = new ExecutionContextImpl();
79         String JavaDoc username = request.getParameter("username");
80
81         if(username == null || username.trim().length() == 0)
82             user = userAuthService.newUser(context);
83         else
84             user = userAuthService.getUser(context, username);
85
86         if(user == null)
87             user = userAuthService.newUser(context);
88
89         //temporary fix - problem with API
90
if(user.getRoles() != null) {
91             user.getRoles().clear();
92         }
93
94         //code added to get the roles from request scope
95
String JavaDoc roleNames = request.getParameter("curruserroles");
96         updateCurrentUserRoles(user, roleNames, context, false);
97
98         request.setAttribute("roleList", userAuthService.getRoles(context, null));
99         return user;
100     }
101
102     /*
103      * overridden lifecycle method on form submission
104      * arguments: HttpServletRequest, HttpServletResponse, Object, BindException
105      * returns: ModelAndView
106      */

107     public ModelAndView onSubmit(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
108         Object JavaDoc command, BindException errors) throws Exception JavaDoc {
109
110         String JavaDoc toPage = request.getParameter("frompage");
111         String JavaDoc enabled = request.getParameter("enabled");
112         User user = (User)command;
113
114         String JavaDoc roleNames = request.getParameter("curruserroles");
115         ExecutionContext context = new ExecutionContextImpl();
116
117         if(!updateCurrentUserRoles(user, roleNames, context, true))
118             throw new RuntimeException JavaDoc("Error in User-Role Update. Cannot continue.");
119
120         if(enabled != null && enabled.equals("true"))
121             user.setEnabled(true);
122         else
123             user.setEnabled(false);
124
125         JasperServerUtil.trimDTOFieldSpaces(user);
126         userAuthService.putUser(null, user);
127         request.setAttribute("userList", userAuthService.getUsers(context, null));
128         return new ModelAndView(toPage);
129
130     }
131
132     /*
133      * function to bind to a custom editor
134      * @param:
135      * @return: void
136      */

137     protected void initBinder(HttpServletRequest JavaDoc request, ServletRequestDataBinder binder)
138                                             throws ServletException JavaDoc {
139         super.initBinder(request, binder);
140     }
141
142     /*
143      * function to update the user with current roles
144      * @param:
145      * @return: boolean
146      */

147      protected boolean updateCurrentUserRoles(User user, String JavaDoc seldRoles, ExecutionContext context, boolean clear) {
148
149         Role role = null;
150         String JavaDoc roleName = null;
151
152         //always delete the existing roles to user
153
if(clear) {
154             user.getRoles().clear();
155         }
156
157         //always update with latest roles from GUI
158
if(seldRoles != null && seldRoles.trim().length() != 0) {
159             StringTokenizer roles = new StringTokenizer(seldRoles, ";");
160             while(roles.hasMoreTokens()) {
161                 roleName = (String JavaDoc)roles.nextToken();
162                 role = userAuthService.getRole(context, roleName);
163                 user.addRole(role);
164             }
165         }
166         return true;
167      }
168
169 }
170
Popular Tags