001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.group;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Set;
023
024import org.apache.commons.collections.ListUtils;
025import org.apache.commons.lang.StringUtils;
026import org.kuali.rice.kew.api.KewApiConstants;
027import org.kuali.rice.kew.api.KewApiServiceLocator;
028import org.kuali.rice.kim.api.group.GroupService;
029import org.kuali.rice.kim.api.services.KimApiServiceLocator;
030import org.kuali.rice.krad.data.DataObjectService;
031import org.kuali.rice.krad.data.PersistenceOption;
032import org.kuali.rice.krad.service.KRADServiceLocator;
033
034/**
035 * Concrete Implementation of {@link GroupInternalService}
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 *
039 */
040public class GroupInternalServiceImpl implements GroupInternalService {
041
042    protected DataObjectService getDataObjectService() {
043        return KRADServiceLocator.getDataObjectService();
044    }
045
046    protected GroupService getGroupService(){
047        return KimApiServiceLocator.getGroupService();
048    }
049
050    @Override
051    public GroupBo saveWorkgroup(GroupBo group) {
052        GroupService ims = getGroupService();
053        List<String> oldIds = Collections.emptyList();
054        if (StringUtils.isNotEmpty(group.getId())) {
055            oldIds = ims.getMemberPrincipalIds(group.getId());
056        }
057        group = getDataObjectService().save(group,PersistenceOption.FLUSH);
058        List<String> newIds = ims.getMemberPrincipalIds(group.getId());
059        updateForWorkgroupChange(group.getId(), oldIds, newIds);
060        return group;
061    }
062
063    @Override
064    public void updateForWorkgroupChange(String groupId,
065                List<String> oldPrincipalIds, List<String> newPrincipalIds) {
066        MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds);
067        for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) {
068                updateForUserRemovedFromGroup(removedPrincipalId, groupId);
069        }
070        for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) {
071                updateForUserAddedToGroup(addedPrincipalId, groupId);
072        }
073    }
074
075    @Override
076    public void updateForUserAddedToGroup(String principalId, String groupId) {
077        // first verify that the user is still a member of the workgroup
078        if(getGroupService().isMemberOfGroup(principalId, groupId))
079        {
080            KewApiServiceLocator.getGroupMembershipChangeQueue()
081                .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.ADDED, groupId, principalId);
082        }
083    }
084
085    @Override
086    public void updateForUserRemovedFromGroup(String principalId, String groupId) {
087        // first verify that the user is no longer a member of the workgroup
088        if(!getGroupService().isMemberOfGroup(principalId, groupId))
089        {
090            KewApiServiceLocator.getGroupMembershipChangeQueue()
091                .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.REMOVED, groupId, principalId);
092        }
093
094    }
095
096    private MembersDiff getMembersDiff(List<String> oldMemberPrincipalIds, List<String> newMemberPrincipalIds) {
097
098        // ListUtils does not check the null case.  Which can happen when adding a new group
099        // 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}