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