KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > role > UpdateRoleAction


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.role;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.Role;
20 import com.blandware.atleap.service.core.RoleManager;
21 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
22 import com.blandware.atleap.webapp.action.core.BaseAction;
23 import com.blandware.atleap.webapp.form.RoleForm;
24 import com.blandware.atleap.webapp.util.core.WebappConstants;
25 import com.blandware.atleap.webapp.util.core.WebappUtil;
26 import org.apache.commons.validator.GenericValidator;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32 import org.springframework.orm.ObjectOptimisticLockingFailureException;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 /**
38  * <p>Updates role
39  * </p>
40  * <p><a HREF="UpdateRoleAction.java.htm"><i>View Source</i></a></p>
41  * <p/>
42  *
43  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
44  * @version $Revision: 1.18 $ $Date: 2006/03/11 10:41:29 $
45  * @struts.action path="/core/role/update"
46  * name="roleForm"
47  * scope="request"
48  * input="inputForward"
49  * validate="true"
50  * roles="core-role-update"
51  * @struts.action-forward name="inputForward"
52  * path=".core.role.update"
53  * @struts.action-forward name="viewRole"
54  * path="/core/role/view.do"
55  * redirect="true"
56  * @struts.action-forward name="listRoles"
57  * path="/core/role/list.do"
58  * redirect="true"
59  * @struts.action-forward name="callUpdateRole"
60  * path="/core/role/callUpdate.do"
61  * redirect="false"
62  * @struts.action-forward name="unsatisfiable"
63  * path="/core/role/list.do"
64  */

65 public final class UpdateRoleAction extends BaseAction {
66     /**
67      * @param mapping The ActionMapping used to select this instance
68      * @param form The optional ActionForm bean for this request (if any)
69      * @param request The HTTP request we are proceeding
70      * @param response The HTTP response we are creating
71      * @return an ActionForward instance describing where and how
72      * control should be forwarded, or null if response
73      * has already been completed
74      */

75     public ActionForward execute(ActionMapping mapping, ActionForm form,
76                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
77
78         if ( !isCancelled(request) ) {
79
80             RoleForm roleForm = (RoleForm) form;
81
82             String JavaDoc roleName = roleForm.getName();
83             if ( GenericValidator.isBlankOrNull(roleName) ) {
84                 if ( log.isWarnEnabled() ) {
85                     log.warn("Missing role name. Returning to list...");
86                 }
87                 return mapping.findForward("listRoles");
88             }
89
90             request.getSession().setAttribute(WebappConstants.ROLE_NAME_KEY, roleName);
91
92             RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN);
93
94             Role role = roleManager.retrieveRole(roleName);
95
96             if ( role == null ) {
97                 // role not found. it might be deleted by someone else
98
ActionMessages errors = new ActionMessages();
99                 errors.add("roleNotFound", new ActionMessage("core.role.errors.notFound"));
100                 saveErrors(request, errors);
101                 return mapping.findForward("listRoles");
102             }
103
104             WebappUtil.copyProperties(role, roleForm, request);
105
106             try {
107                 roleManager.updateRole(role);
108             } catch ( BeanAlreadyExistsException e ) {
109                 // role already exists
110
ActionMessages errors = new ActionMessages();
111                 errors.add("roleAlreadyExists", new ActionMessage("core.role.errors.alreadyExists"));
112                 saveErrors(request, errors);
113                 saveToken(request);
114                 return mapping.getInputForward();
115             } catch ( ObjectOptimisticLockingFailureException e ) {
116                 // role was updated or deleted by another transaction
117
ActionMessages errors = new ActionMessages();
118                 errors.add("updateFailed", new ActionMessage("core.role.errors.updateFailed"));
119                 saveErrors(request, errors);
120                 return mapping.findForward("callUpdateRole");
121             }
122         }
123         return mapping.findForward("listRoles");
124     }
125 }
Popular Tags