<commit_before>/*
 * Copyright 2007-2009 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kim.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.kuali.rice.kew.exception.WorkflowRuntimeException;
import org.kuali.rice.kew.messaging.MessageServiceNames;
import org.kuali.rice.kew.util.KEWConstants;
import org.kuali.rice.kew.workgroup.WorkgroupMembershipChangeProcessor;
import org.kuali.rice.kim.api.group.Group;
import org.kuali.rice.kim.api.group.GroupMember;
import org.kuali.rice.kim.api.services.KimApiServiceLocator;
import org.kuali.rice.kim.impl.group.GroupBo;
import org.kuali.rice.kim.impl.group.GroupMemberBo;
import org.kuali.rice.kim.impl.group.GroupBo;
import org.kuali.rice.kim.api.group.GroupService;
import org.kuali.rice.kim.service.GroupInternalService;
import org.kuali.rice.kns.service.BusinessObjectService;
import org.kuali.rice.kns.service.KNSServiceLocator;
import org.kuali.rice.ksb.messaging.service.KSBXMLService;
import org.kuali.rice.ksb.service.KSBServiceLocator;

/**
 * Concrete Implementation of {@link GroupInternalService}
 *
 * @author K<NAME> (<EMAIL>)
 *
 */
public class GroupInternalServiceImpl implements GroupInternalService {
    protected BusinessObjectService getBusinessObjectService() {
    	return KNSServiceLocator.getBusinessObjectService();
    }


    public GroupService getGroupService(){
    	return KimApiServiceLocator.getGroupService();
    }

    public GroupBo saveWorkgroup(GroupBo group) {
    	GroupService ims = getGroupService();
    	List<String> oldIds = ims.getMemberPrincipalIds(group.getId());
        group = (GroupBo)getBusinessObjectService().save( group );
        List<String> newIds = ims.getMemberPrincipalIds(group.getId());
        updateForWorkgroupChange(group.getId(), oldIds, newIds);
        return group;
    }

    public Group saveWorkgroup(Group group) {
    	GroupService ims = getGroupService();
    	List<String> oldIds = ims.getMemberPrincipalIds(group.getId());
        GroupBo groupBo = GroupBo.from(group);
        //add members
        Collection<GroupMember> members = KimApiServiceLocator.getGroupService().getMembersOfGroup(group.getId());
        if (!CollectionUtils.isEmpty(members)) {
            for (GroupMember member : members) {
                groupBo.getMembers().add(GroupMemberBo.from(member));
            }
        }
        getBusinessObjectService().save(groupBo);
        List<String> newIds = ims.getMemberPrincipalIds(group.getId());
        updateForWorkgroupChange(group.getId(), oldIds, newIds);
        return group;
    }

    public void updateForWorkgroupChange(String groupId,
    		List<String> oldPrincipalIds, List<String> newPrincipalIds) {
        MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds);
        for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) {
        	updateForUserRemovedFromGroup(removedPrincipalId, groupId);
        }
        for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) {
        	updateForUserAddedToGroup(addedPrincipalId, groupId);
        }
    }

    public void updateForUserAddedToGroup(String principalId, String groupId) {
        // first verify that the user is still a member of the workgroup
    	if(getGroupService().isMemberOfGroup(principalId, groupId))
    	{
            KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KSBServiceLocator.getMessageHelper()
            .getServiceAsynchronously(new QName(KEWConstants.KEW_MODULE_NAMESPACE, MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE));
            try {
                workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor
                        .getMemberAddedMessageContents(principalId, groupId));
            } catch (Exception e) {
                throw new WorkflowRuntimeException(e);
            }
    	}
    }

    public void updateForUserRemovedFromGroup(String principalId, String groupId) {
        // first verify that the user is no longer a member of the workgroup
    	if(!getGroupService().isMemberOfGroup(principalId, groupId))
    	{
            KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KSBServiceLocator.getMessageHelper()
            .getServiceAsynchronously(new QName(KEWConstants.KEW_MODULE_NAMESPACE, MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE));
            try {
                workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor
                        .getMemberRemovedMessageContents(principalId, groupId));
            } catch (Exception e) {
                throw new WorkflowRuntimeException(e);
            }
    	}<commit_msg>KULRICE-5049 - change for GroupInternalServiceImpl<commit_after>/*
 * Copyright 2007-2009 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kim.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.namespace.QName;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.kuali.rice.kew.exception.WorkflowRuntimeException;
import org.kuali.rice.kew.messaging.MessageServiceNames;
import org.kuali.rice.kew.workgroup.WorkgroupMembershipChangeProcessor;
import org.kuali.rice.kim.api.group.Group;
import org.kuali.rice.kim.api.group.GroupMember;
import org.kuali.rice.kim.api.services.KimApiServiceLocator;
import org.kuali.rice.kim.impl.group.GroupBo;
import org.kuali.rice.kim.impl.group.GroupMemberBo;
import org.kuali.rice.kim.impl.group.GroupBo;
import org.kuali.rice.kim.api.group.GroupService;
import org.kuali.rice.kim.service.GroupInternalService;
import org.kuali.rice.kns.service.BusinessObjectService;
import org.kuali.rice.kns.service.KNSServiceLocator;
import org.kuali.rice.ksb.messaging.service.KSBXMLService;
import org.kuali.rice.ksb.service.KSBServiceLocator;

/**
 * Concrete Implementation of {@link GroupInternalService}
 *
 * @author Kuali Rice Team (<EMAIL>)
 *
 */
public class GroupInternalServiceImpl implements GroupInternalService {
    protected BusinessObjectService getBusinessObjectService() {
    	return KNSServiceLocator.getBusinessObjectService();
    }


    public GroupService getGroupService(){
    	return KimApiServiceLocator.getGroupService();
    }

    public GroupBo saveWorkgroup(GroupBo group) {
    	GroupService ims = getGroupService();
    	List<String> oldIds = ims.getMemberPrincipalIds(group.getId());
        group = (GroupBo)getBusinessObjectService().save( group );
        List<String> newIds = ims.getMemberPrincipalIds(group.getId());
        updateForWorkgroupChange(group.getId(), oldIds, newIds);
        return group;
    }

    public Group saveWorkgroup(Group group) {
    	GroupService ims = getGroupService();
    	List<String> oldIds = ims.getMemberPrincipalIds(group.getId());
        GroupBo groupBo = GroupBo.from(group);
        //add members
        Collection<GroupMember> members = KimApiServiceLocator.getGroupService().getMembersOfGroup(group.getId());
        if (!CollectionUtils.isEmpty(members)) {
            for (GroupMember member : members) {
                groupBo.getMembers().add(GroupMemberBo.from(member));
            }
        }
        getBusinessObjectService().save(groupBo);
        List<String> newIds = ims.getMemberPrincipalIds(group.getId());
        updateForWorkgroupChange(group.getId(), oldIds, newIds);
        return group;
    }

    public void updateForWorkgroupChange(String groupId,
    		List<String> oldPrincipalIds, List<String> newPrincipalIds) {
        MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds);
        for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) {
        	updateForUserRemovedFromGroup(removedPrincipalId, groupId);
        }
        for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) {
        	updateForUserAddedToGroup(addedPrincipalId, groupId);
        }
    }

    public void updateForUserAddedToGroup(String principalId, String groupId) {
        // first verify that the user is still a member of the workgroup
    	if(getGroupService().isMemberOfGroup(principalId, groupId))
    	{
            KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KSBServiceLocator.getMessageHelper()
            .getServiceAsynchronously(new QName(MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE));
            try {
                workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor
                        .getMemberAddedMessageContents(principalId, groupId));
            } catch (Exception e) {
                throw new WorkflowRuntimeException(e);
            }
    	}
    }

    public void updateForUserRemovedFromGroup(String principalId, String groupId) {
        // first verify that the user is no longer a member of the workgroup
    	if(!getGroupService().isMemberOfGroup(principalId, groupId))
    	{
            KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KSBServiceLocator.getMessageHelper()
            .getServiceAsynchronously(new QName(MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE));
            try {
                workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor
                        .getMemberRemovedMessageContents(principalId, groupId));
            } catch (Exception e) {
                throw new WorkflowRuntimeException(e);
            }
    	}
{'fraction_non_alphanumeric': 0.08862385321100917, 'fraction_numerical': 0.0023853211009174312, 'mean_word_length': 3.7272333044232435, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2896237', 'n_tokens_mistral': 3100, 'n_tokens_neox': 3028, 'n_words': 625}