KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > user > ChangeUserStatusAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 package com.blandware.atleap.webapp.action.core.user;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.User;
20 import com.blandware.atleap.service.core.UserManager;
21 import com.blandware.atleap.webapp.action.core.BaseAction;
22 import com.blandware.atleap.webapp.form.UserForm;
23 import org.apache.commons.validator.GenericValidator;
24 import org.apache.struts.action.ActionForm;
25 import org.apache.struts.action.ActionForward;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionMessage;
28 import org.apache.struts.action.ActionMessages;
29 import org.springframework.orm.ObjectOptimisticLockingFailureException;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 /**
35  * <p>Enables/disables user
36  * </p>
37  * <p><a HREF="ChangeUserStatusAction.java.htm"><i>View Source</i></a></p>
38  * <p/>
39  *
40  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
41  * @version $Revision: 1.5 $ $Date: 2006/03/10 17:10:30 $
42  * @struts.action path="/core/user/changeStatus"
43  * name="userForm"
44  * scope="request"
45  * validate="false"
46  * roles="core-user-changeStatus"
47  * @struts.action-forward name="listUsers"
48  * path="/core/user/list.do"
49  * redirect="true"
50  * @struts.action-forward name="unsatisfiable"
51  * path="/core/user/list.do"
52  */

53 public final class ChangeUserStatusAction extends BaseAction {
54     /**
55      * @param mapping The ActionMapping used to select this instance
56      * @param form The optional ActionForm bean for this request (if any)
57      * @param request The HTTP request we are proceeding
58      * @param response The HTTP response we are creating
59      * @return an ActionForward instance describing where and how
60      * control should be forwarded, or null if response
61      * has already been completed
62      */

63     public ActionForward execute(ActionMapping mapping, ActionForm form,
64                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
65
66         UserForm userForm = (UserForm) form;
67
68         String JavaDoc userName = userForm.getName();
69         if ( GenericValidator.isBlankOrNull(userName) ) {
70             if ( log.isWarnEnabled() ) {
71                 log.warn("Missing user name. Returning to list.");
72             }
73             return mapping.findForward("listUsers");
74         }
75
76         if ( userName.equals(request.getRemoteUser()) ) {
77             // currently logged in user cannot be deleted
78
saveToken(request);
79             ActionMessages errors = new ActionMessages();
80             errors.add("loggedUserCannotDisable", new ActionMessage("core.user.form.changeStatusDisabled"));
81             saveErrors(request, errors);
82             return mapping.findForward("listUsers");
83         }
84
85         UserManager userManager = (UserManager) getBean(Constants.USER_MANAGER_BEAN);
86         User user = userManager.retrieveUser(userName);
87
88         if ( user == null ) {
89             // content page not found. it might be deleted by someone else
90
ActionMessages errors = new ActionMessages();
91             errors.add("userNotFound", new ActionMessage("core.user.errors.notFound"));
92             saveErrors(request, errors);
93             return mapping.findForward("listUsers");
94         }
95
96         String JavaDoc status = request.getParameter("status");
97         boolean alreadyEnabled = user.getEnabled() != null ? user.getEnabled().booleanValue() : false;
98         boolean enabled = true;
99         if ( !"enabled".equalsIgnoreCase(status) ) {
100             enabled = false;
101         }
102
103         try {
104
105             if ( (alreadyEnabled && !enabled) || (!alreadyEnabled && enabled) ) {
106                 user.setEnabled(Boolean.valueOf(enabled));
107                 userManager.updateUser(user);
108             }
109         } catch ( ObjectOptimisticLockingFailureException e ) {
110             // content page was updated or deleted by another transaction
111
ActionMessages errors = new ActionMessages();
112             errors.add("updateFailed", new ActionMessage("core.user.errors.updateFailed"));
113             saveErrors(request, errors);
114             return mapping.findForward("listUsers");
115         }
116         return mapping.findForward("listUsers");
117     }
118 }
Popular Tags