KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > webflow > UserFormAction


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.webflow;
18
19 import info.jtrac.domain.Space;
20 import info.jtrac.domain.User;
21 import info.jtrac.util.ValidationUtils;
22
23 import java.io.Serializable JavaDoc;
24
25 import static info.jtrac.Constants.*;
26 import info.jtrac.domain.UserSpaceRole;
27 import info.jtrac.util.SecurityUtils;
28 import info.jtrac.util.UserUtils;
29 import java.util.LinkedHashMap JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32 import org.springframework.beans.propertyeditors.StringTrimmerEditor;
33 import org.springframework.util.StringUtils;
34 import org.springframework.validation.DataBinder;
35
36 import org.springframework.validation.Errors;
37 import org.springframework.validation.Validator;
38 import org.springframework.webflow.execution.Event;
39 import org.springframework.webflow.execution.RequestContext;
40 import org.springframework.webflow.execution.ScopeType;
41
42 /**
43  * Multiaction that backs the "User Create / Edit" flow
44  */

45 public class UserFormAction extends AbstractFormAction {
46     
47     public UserFormAction() {
48         setFormObjectClass(UserForm.class);
49         setFormObjectName("userForm");
50         setFormObjectScope(ScopeType.REQUEST);
51         setValidator(new UserFormValidator());
52     }
53     
54     @Override JavaDoc
55     protected void initBinder(RequestContext request, DataBinder binder) {
56         binder.registerCustomEditor(String JavaDoc.class, new StringTrimmerEditor(true));
57     }
58     
59     @Override JavaDoc
60     public Object JavaDoc createFormObject(RequestContext context) {
61         context.getRequestScope().put("locales", jtrac.getLocales());
62         UserForm userForm = new UserForm();
63         // note that form has a hidden field 'userId' to handle re-show form on errors
64
String JavaDoc userId = ValidationUtils.getParameter(context, "userId");
65         // if called as subflow, userId may be unintended request parameter from space allocate form
66
// hence extra check for space in Flow scope
67
if (userId != null && context.getFlowScope().get("space") == null) {
68             User user = jtrac.loadUser(Integer.parseInt(userId));
69             userForm.setUser(user);
70             return userForm;
71         }
72         return userForm;
73     }
74     
75     /**
76      * Form backing object
77      */

78     public static class UserForm implements Serializable JavaDoc {
79         
80         private transient User user;
81         private String JavaDoc password;
82         private String JavaDoc passwordConfirm;
83         private boolean admin;
84         
85         public User getUser() {
86             if (user == null) {
87                 user = new User();
88             }
89             return user;
90         }
91         
92         public void setUser(User user) {
93             this.user = user;
94         }
95         
96         public String JavaDoc getPassword() {
97             return password;
98         }
99         
100         public void setPassword(String JavaDoc password) {
101             this.password = password;
102         }
103         
104         public String JavaDoc getPasswordConfirm() {
105             return passwordConfirm;
106         }
107         
108         public void setPasswordConfirm(String JavaDoc passwordConfirm) {
109             this.passwordConfirm = passwordConfirm;
110         }
111                
112     }
113     
114     /**
115      * A validator for our form backing object.
116      */

117     public static class UserFormValidator implements Validator {
118         
119         public boolean supports(Class JavaDoc clazz) {
120             return UserForm.class.isAssignableFrom(clazz);
121         }
122         
123         public void validate(Object JavaDoc o, Errors errors) {
124             UserForm userForm = (UserForm) o;
125             ValidationUtils.rejectIfEmpty(errors, "user.loginName", "user.name", "user.email");
126             if (!ValidationUtils.isAllLowerCase(userForm.getUser().getLoginName())) {
127                 errors.rejectValue("user.loginName", "user_form.loginId.error.invalid");
128             }
129             String JavaDoc password = userForm.getPassword();
130             String JavaDoc passwordConfirm = userForm.getPasswordConfirm();
131             if ((password != null && !password.equals(passwordConfirm)) ||
132                     (passwordConfirm != null && !passwordConfirm.equals(password))) {
133                 errors.rejectValue("passwordConfirm", "user_form.passwordConfirm.error");
134             }
135         }
136     }
137     
138     public Event userFormHandler(RequestContext context) throws Exception JavaDoc {
139         UserForm userForm = (UserForm) getFormObject(context);
140         User user = userForm.getUser();
141         User temp = jtrac.loadUser(user.getLoginName());
142         if (temp != null && temp.getId() != user.getId()) {
143             Errors errors = getFormErrors(context);
144             errors.rejectValue("user.loginName", "user_form.loginId.error.exists");
145             return error();
146         }
147         if (userForm.getPassword() != null || user.getId() == 0) {
148             jtrac.storeUser(user, userForm.getPassword());
149         } else {
150             jtrac.storeUser(user);
151         }
152         temp = SecurityUtils.getPrincipal();
153         if (temp.getId() == user.getId()) {
154             SecurityUtils.refreshSecurityContext();
155             UserUtils.refreshLocale(context, user.getLocale());
156         }
157         return success();
158     }
159     
160     public Event userAllocateSpaceSetup(RequestContext context) throws Exception JavaDoc {
161         User user = (User) context.getFlowScope().get("user");
162         if (user == null) {
163             String JavaDoc userId = ValidationUtils.getParameter(context, "userId");
164             int id = Integer.parseInt(userId);
165             user = jtrac.loadUser(id);
166         }
167         context.getFlowScope().put("user", user);
168         context.getRequestScope().put("unallocatedSpaces", jtrac.findUnallocatedSpacesForUser(user.getId()));
169         return success();
170     }
171     
172     public Event userAllocateSpaceRoleSetup(RequestContext context) {
173         String JavaDoc spaceId = ValidationUtils.getParameter(context, "spaceId");
174         if (spaceId == null) {
175             // no spaces left, no navigation
176
return error();
177         }
178         int id = Integer.parseInt(spaceId);
179         context.getFlowScope().put("space", jtrac.loadSpace(id));
180         return success();
181     }
182     
183     public Event userAllocateHandler(RequestContext context) {
184         User user = (User) context.getFlowScope().get("user");
185         Space space = (Space) context.getFlowScope().get("space");
186         String JavaDoc roleKey = ValidationUtils.getParameter(context, "roleKey");
187         jtrac.storeUserSpaceRole(user, space, roleKey);
188         String JavaDoc admin = ValidationUtils.getParameter(context, "admin");
189         if (admin != null) {
190             jtrac.storeUserSpaceRole(user, space, "ROLE_ADMIN");
191         }
192         SecurityUtils.refreshSecurityContextIfPrincipal(user);
193         return success();
194     }
195
196     public Event userDeallocateHandler(RequestContext context) {
197         String JavaDoc userSpaceRoleId = ValidationUtils.getParameter(context, "deallocate");
198         int id = Integer.parseInt(userSpaceRoleId);
199         UserSpaceRole userSpaceRole = jtrac.loadUserSpaceRole(id);
200         jtrac.removeUserSpaceRole(userSpaceRole);
201         User user = jtrac.loadUser(userSpaceRole.getUser().getId());
202         SecurityUtils.refreshSecurityContextIfPrincipal(user);
203         context.getFlowScope().put("user", user);
204         return success();
205     }
206     
207     public Event userMakeAdminHandler(RequestContext context) {
208         User user = (User) context.getFlowScope().get("user");
209         jtrac.storeUserSpaceRole(user, null, "ROLE_ADMIN");
210         SecurityUtils.refreshSecurityContextIfPrincipal(user);
211         return success();
212     }
213     
214 }
215
Popular Tags