View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 org.kuali.rice.kim.impl.group;
17  
18  import org.apache.commons.collections.ListUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.kew.api.KewApiServiceLocator;
21  import org.kuali.rice.kew.api.KewApiConstants;
22  import org.kuali.rice.kim.api.group.GroupService;
23  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24  import org.kuali.rice.krad.service.BusinessObjectService;
25  import org.kuali.rice.krad.service.KRADServiceLocator;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.HashSet;
30  import java.util.List;
31  import java.util.Set;
32  
33  /**
34   * Concrete Implementation of {@link GroupInternalService}
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class GroupInternalServiceImpl implements GroupInternalService {
40      protected BusinessObjectService getBusinessObjectService() {
41      	return KRADServiceLocator.getBusinessObjectService();
42      }
43  
44  
45      public GroupService getGroupService(){
46      	return KimApiServiceLocator.getGroupService();
47      }
48  
49      @Override
50      public GroupBo saveWorkgroup(GroupBo group) {
51      	GroupService ims = getGroupService();
52          List<String> oldIds = Collections.emptyList();
53      	if (StringUtils.isNotEmpty(group.getId())) {
54              oldIds = ims.getMemberPrincipalIds(group.getId());
55          }
56          group = (GroupBo)getBusinessObjectService().save( group );
57          List<String> newIds = ims.getMemberPrincipalIds(group.getId());
58          updateForWorkgroupChange(group.getId(), oldIds, newIds);
59          return group;
60      }
61  
62      @Override
63      public void updateForWorkgroupChange(String groupId,
64      		List<String> oldPrincipalIds, List<String> newPrincipalIds) {
65          MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds);
66          for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) {
67          	updateForUserRemovedFromGroup(removedPrincipalId, groupId);
68          }
69          for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) {
70          	updateForUserAddedToGroup(addedPrincipalId, groupId);
71          }
72      }
73  
74      @Override
75      public void updateForUserAddedToGroup(String principalId, String groupId) {
76          // first verify that the user is still a member of the workgroup
77      	if(getGroupService().isMemberOfGroup(principalId, groupId))
78      	{
79      	    KewApiServiceLocator.getGroupMembershipChangeQueue()
80      	        .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.ADDED, groupId, principalId);
81      	}
82      }
83  
84      @Override
85      public void updateForUserRemovedFromGroup(String principalId, String groupId) {
86          // first verify that the user is no longer a member of the workgroup
87      	if(!getGroupService().isMemberOfGroup(principalId, groupId))
88      	{
89              KewApiServiceLocator.getGroupMembershipChangeQueue()
90                  .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.REMOVED, groupId, principalId);
91      	}
92  
93      }
94  
95      private MembersDiff getMembersDiff(List<String> oldMemberPrincipalIds, List<String> newMemberPrincipalIds) {
96  
97      	// ListUtils does not check the null case.  Which can happen when adding a new group
98      	// so, if they're null make them empty lists.
99      	if(oldMemberPrincipalIds == null) oldMemberPrincipalIds = new ArrayList<String>();
100     	if(newMemberPrincipalIds == null) newMemberPrincipalIds = new ArrayList<String>();
101 
102         Set<String> addedPrincipalIds = new HashSet<String>(ListUtils.subtract(newMemberPrincipalIds, oldMemberPrincipalIds));
103         Set<String> removedPrincipalIds = new HashSet<String>(ListUtils.subtract(oldMemberPrincipalIds, newMemberPrincipalIds));
104         return new MembersDiff(addedPrincipalIds, removedPrincipalIds);
105     }
106 
107     private class MembersDiff {
108         private final Set<String> addedPrincipalIds;
109 
110         private final Set<String> removedPrincipalIds;
111 
112         public MembersDiff(Set<String> addedPrincipalIds, Set<String>removedPrincipalIds) {
113             this.addedPrincipalIds = addedPrincipalIds;
114             this.removedPrincipalIds = removedPrincipalIds;
115         }
116 
117         public Set<String> getAddedPrincipalIds() {
118             return addedPrincipalIds;
119         }
120 
121         public Set<String> getRemovedPrincipalIds() {
122             return removedPrincipalIds;
123         }
124     }
125 
126 }