KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > group > AssignGroupRolesAction


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.group;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.Role;
20 import com.blandware.atleap.model.core.Group;
21 import com.blandware.atleap.model.core.User;
22 import com.blandware.atleap.service.core.RoleManager;
23 import com.blandware.atleap.service.core.GroupManager;
24 import com.blandware.atleap.service.core.UserManager;
25 import com.blandware.atleap.webapp.action.core.BaseAction;
26 import com.blandware.atleap.webapp.form.core.SelectRolesForm;
27 import com.blandware.atleap.webapp.util.core.WebappConstants;
28 import com.blandware.atleap.webapp.util.core.CacheUtil;
29 import com.blandware.atleap.webapp.acegi.UserManagerDaoImpl;
30 import org.apache.commons.validator.GenericValidator;
31 import org.apache.struts.action.*;
32 import org.springframework.orm.ObjectOptimisticLockingFailureException;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import java.util.*;
37
38 /**
39  * <p>Assigns Roles to Group
40  * </p>
41  * <p><a HREF="AssignGroupRolesAction.java.htm"><i>View Source</i></a></p>
42  * <p/>
43  *
44  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
45  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
46  * @version $Revision: 1.3 $ $Date: 2006/03/25 11:27:52 $
47  * @struts.action path="/core/group/assignRoles"
48  * name="selectRolesForm"
49  * scope="request"
50  * input="inputForward"
51  * validate="true"
52  * roles="core-group-assignRoles"
53  * @struts.action-forward name="inputForward"
54  * path=".core.group.assignRoles"
55  * @struts.action-forward name="viewGroup"
56  * path="/core/group/view.do"
57  * redirect="true"
58  * @struts.action-forward name="listGroups"
59  * path="/core/group/list.do"
60  * redirect="true"
61  * @struts.action-forward name="callAssignRoles"
62  * path="/core/group/callAssignRoles.do"
63  * redirect="false"
64  * @struts.action-forward name="unsatisfiable"
65  * path="/core/group/list.do"
66  */

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

77     public ActionForward execute(ActionMapping mapping, ActionForm form,
78                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
79
80         if ( !isCancelled(request) ) {
81
82             String JavaDoc groupName = (String JavaDoc) request.getSession().getAttribute(WebappConstants.GROUP_NAME_KEY);
83             if ( GenericValidator.isBlankOrNull(groupName) ) {
84                 if ( log.isWarnEnabled() ) {
85                     log.warn("Missing group name. Returning to list...");
86                 }
87                 return mapping.findForward("listGroups");
88             }
89
90             // acquire POJOs for all selected roles
91
RoleManager roleManager = (RoleManager) getBean(Constants.ROLE_MANAGER_BEAN);
92             SelectRolesForm selectRolesForm = (SelectRolesForm) form;
93             String JavaDoc[] selectedRoleNames = selectRolesForm.getSelectedRoles();
94
95             List roles = new ArrayList();
96             for ( int i = 0; i < selectedRoleNames.length; i++ ) {
97                 String JavaDoc roleName = selectedRoleNames[i];
98                 Role role = roleManager.retrieveRole(roleName);
99                 if ( role != null ) {
100                     roles.add(role);
101                 }
102             }
103
104             // update group roles
105
GroupManager groupManager = (GroupManager) getBean(Constants.GROUP_MANAGER_BEAN);
106             Group group = groupManager.retrieveGroup(groupName);
107
108             if ( group == null ) {
109                 // group not found. it might be deleted by someone else
110
ActionMessages errors = new ActionMessages();
111                 errors.add("groupNotFound", new ActionMessage("core.group.errors.notFound"));
112                 saveErrors(request, errors);
113                 return mapping.findForward("listGroups");
114             }
115
116             group.setVersion(Long.valueOf(selectRolesForm.getVersion()));
117
118             UserManager userManager = (UserManager) getBean(Constants.USER_MANAGER_BEAN);
119             Set touchedUsers = new HashSet();
120
121             // remove roles which were unselected
122
// list of roles is copied in order to prevent ConcurrentModificationException to be thrown
123
List groupRoles = new ArrayList(group.getRoles());
124             for ( int i = 0; i < groupRoles.size(); i++ ) {
125                 Role role = (Role) groupRoles.get(i);
126                 if ( !roles.contains(role) ) {
127                     group.removeRole(role);
128                     List groupUsers = group.getUsers();
129                     for (int j = 0; j < groupUsers.size(); j++) {
130                         User user = (User) groupUsers.get(j);
131                         user.removeRole(role, group);
132                         touchedUsers.add(user);
133                     }
134                 }
135             }
136             // add new roles
137
for ( Iterator i = roles.iterator(); i.hasNext(); ) {
138                 Role role = (Role) i.next();
139                 group.addRole(role);
140                 List groupUsers = group.getUsers();
141                 for (int j = 0; j < groupUsers.size(); j++) {
142                     User user = (User) groupUsers.get(j);
143                     user.addRole(role, group);
144                     touchedUsers.add(user);
145                 }
146             }
147
148             UserManagerDaoImpl userManagerDaoImpl = (UserManagerDaoImpl) getBean(Constants.USER_DETAILS_SERVICE_BEAN);
149
150             try {
151                 groupManager.updateGroup(group);
152                 for (Iterator i = touchedUsers.iterator(); i.hasNext();) {
153                     User user = (User) i.next();
154                     userManager.updateUser(user);
155
156                     if (user.getName().equals(request.getRemoteUser())) {
157                         userManagerDaoImpl.updateUser(user);
158                     }
159                 }
160             } catch ( ObjectOptimisticLockingFailureException e ) {
161                 // group was updated or deleted by another transaction
162
ActionMessages errors = new ActionMessages();
163                 errors.add("updateFailed", new ActionMessage("core.group.errors.updateFailed"));
164                 saveErrors(request, errors);
165                 request.setAttribute(WebappConstants.GROUP_NAME_KEY, groupName);
166                 return mapping.findForward("callAssignRoles");
167             }
168
169             // flush CP cache to avoid a situation when user permissions for
170
// CP menu items are changed but he/she still obtains cached version
171
// of page
172
CacheUtil cacheUtil = CacheUtil.getInstance(request);
173             cacheUtil.flushContentPageCache();
174         }
175         return mapping.findForward("listGroups");
176     }
177 }
Popular Tags