<commit_before>/*
 * Copyright 2005-2008 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.kew.workgroup;

import org.kuali.rice.core.api.exception.RiceRuntimeException;
import org.kuali.rice.kew.actionitem.ActionItem;
import org.kuali.rice.kew.actionlist.service.ActionListService;
import org.kuali.rice.kew.actionrequest.ActionRequestValue;
import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
import org.kuali.rice.kew.exception.WorkflowException;
import org.kuali.rice.kew.messaging.ParameterTranslator;
import org.kuali.rice.kew.service.KEWServiceLocator;
import org.kuali.rice.kim.api.group.Group;
import org.kuali.rice.kim.api.identity.principal.Principal;
import org.kuali.rice.kim.api.services.KimApiServiceLocator;
import org.kuali.rice.ksb.messaging.service.KSBXMLService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;


/**
 * Executes the updating of {@link ActionItem}s for a {@link Workgroup} when
 * the membership of a Workgroup changes.  This keeps users' Action Lists
 * in-sync with their group membership.  Allowing their Action List to
 * be updated for requests routed to groups that they are either added to
 * or removed from.
 *
 * @see ActionItem
 * @see Workgroup
 *
 * @author <NAME> (<EMAIL>)
 */
public class WorkgroupMembershipChangeProcessor implements KSBXMLService {

	private static final String ADDED_OPERATION = "ADDED";
	private static final String REMOVED_OPERATION = "REMOVED";

    public ActionRequestService getActionRequestService() {
        return (ActionRequestService) KEWServiceLocator.getActionRequestService();
    }
    
    public ActionListService getActionListService() {
    	return (ActionListService) KEWServiceLocator.getActionListService();
    }
    
	public void invoke(String contents) throws Exception {
		ParameterTranslator translator = new ParameterTranslator(contents);
		String[] parameters = translator.getParameters();
		if (parameters.length != 3) {
			throw new IllegalArgumentException("The Workgroup Membership Change Processor requires four parameters.");
		}
		String operation = parameters[0];
		String principalId = parameters[1];
		String groupId = parameters[2];
		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
		if (principal == null) {
			throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
		}
		Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
		if (group == null) {
			throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
		}
		if (ADDED_OPERATION.equals(operation)) {
			updateActionListForUserAddedToGroup(principalId, groupId);
		} else if (REMOVED_OPERATION.equals(operation)) {
			updateActionListForUserRemovedFromGroup(principalId, groupId);
		} else {
			throw new WorkflowException("Did not understand requested group membership change operation '" + operation + "'");
		}
	}

	public static String getMemberAddedMessageContents(String principalId, String groupId) {
		return getMessageContents(principalId, groupId, ADDED_OPERATION);
    }

	public static String getMemberRemovedMessageContents(String principalId, String groupId) {
		return getMessageContents(principalId, groupId, REMOVED_OPERATION);
	}

	public static String getMessageContents(String principalId, String groupId, String operation) {
		ParameterTranslator translator = new ParameterTranslator();
		translator.addParameter(operation);
		translator.addParameter(principalId);
		translator.addParameter(groupId);
		return translator.getUntranslatedString();
	}

    	
    
    /**
     * Update the user's Action List to reflect their addition to the given Workgroup.
     */
    private void updateActionListForUserAddedToGroup(String principalId, String groupId) {
		List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
		List<String> allGroupsToCheck = KimApiServiceLocator.getIdentityManagementService().getParentGroupIds(groupId);
        allGroupsToCheck.add(0, groupId);
        for (String groupToCheckId : allGroupsToCheck) {
            actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
        }
        for (Iterator requestIt = actionRequests.iterator(); requestIt.hasNext();) {
            ActionRequestValue request = (ActionRequestValue) requestIt.next();
            ActionItem item = getActionListService().createActionItemForActionRequest(request);
            item.setPrincipalId(principalId);
            getActionListService().saveActionItem(item);
        }
    }
    
    private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) {
		List<String> allGroupsToCheck = KimApiServiceLocator.getIdentityManagementService().getParentGroupIds(groupId);
        allGroupsToCheck.add(0, groupId);
        Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId);
		for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) {
        	ActionItem item = itemIt.next();<commit_msg>KULRICE-5318: removing calls to IdentityManagementService<commit_after>/*
 * Copyright 2005-2008 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.kew.workgroup;

import org.kuali.rice.core.api.exception.RiceRuntimeException;
import org.kuali.rice.kew.actionitem.ActionItem;
import org.kuali.rice.kew.actionlist.service.ActionListService;
import org.kuali.rice.kew.actionrequest.ActionRequestValue;
import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
import org.kuali.rice.kew.exception.WorkflowException;
import org.kuali.rice.kew.messaging.ParameterTranslator;
import org.kuali.rice.kew.service.KEWServiceLocator;
import org.kuali.rice.kim.api.group.Group;
import org.kuali.rice.kim.api.identity.principal.Principal;
import org.kuali.rice.kim.api.services.KimApiServiceLocator;
import org.kuali.rice.ksb.messaging.service.KSBXMLService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;


/**
 * Executes the updating of {@link ActionItem}s for a {@link Workgroup} when
 * the membership of a Workgroup changes.  This keeps users' Action Lists
 * in-sync with their group membership.  Allowing their Action List to
 * be updated for requests routed to groups that they are either added to
 * or removed from.
 *
 * @see ActionItem
 * @see Workgroup
 *
 * @author Kuali Rice Team (<EMAIL>)
 */
public class WorkgroupMembershipChangeProcessor implements KSBXMLService {

	private static final String ADDED_OPERATION = "ADDED";
	private static final String REMOVED_OPERATION = "REMOVED";

    public ActionRequestService getActionRequestService() {
        return (ActionRequestService) KEWServiceLocator.getActionRequestService();
    }
    
    public ActionListService getActionListService() {
    	return (ActionListService) KEWServiceLocator.getActionListService();
    }
    
	public void invoke(String contents) throws Exception {
		ParameterTranslator translator = new ParameterTranslator(contents);
		String[] parameters = translator.getParameters();
		if (parameters.length != 3) {
			throw new IllegalArgumentException("The Workgroup Membership Change Processor requires four parameters.");
		}
		String operation = parameters[0];
		String principalId = parameters[1];
		String groupId = parameters[2];
		Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
		if (principal == null) {
			throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
		}
		Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
		if (group == null) {
			throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
		}
		if (ADDED_OPERATION.equals(operation)) {
			updateActionListForUserAddedToGroup(principalId, groupId);
		} else if (REMOVED_OPERATION.equals(operation)) {
			updateActionListForUserRemovedFromGroup(principalId, groupId);
		} else {
			throw new WorkflowException("Did not understand requested group membership change operation '" + operation + "'");
		}
	}

	public static String getMemberAddedMessageContents(String principalId, String groupId) {
		return getMessageContents(principalId, groupId, ADDED_OPERATION);
    }

	public static String getMemberRemovedMessageContents(String principalId, String groupId) {
		return getMessageContents(principalId, groupId, REMOVED_OPERATION);
	}

	public static String getMessageContents(String principalId, String groupId, String operation) {
		ParameterTranslator translator = new ParameterTranslator();
		translator.addParameter(operation);
		translator.addParameter(principalId);
		translator.addParameter(groupId);
		return translator.getUntranslatedString();
	}

    	
    
    /**
     * Update the user's Action List to reflect their addition to the given Workgroup.
     */
    private void updateActionListForUserAddedToGroup(String principalId, String groupId) {
		List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
		List<String> allGroupsToCheck = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
        allGroupsToCheck.add(0, groupId);
        for (String groupToCheckId : allGroupsToCheck) {
            actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
        }
        for (Iterator requestIt = actionRequests.iterator(); requestIt.hasNext();) {
            ActionRequestValue request = (ActionRequestValue) requestIt.next();
            ActionItem item = getActionListService().createActionItemForActionRequest(request);
            item.setPrincipalId(principalId);
            getActionListService().saveActionItem(item);
        }
    }
    
    private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) {
		List<String> allGroupsToCheck = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
        allGroupsToCheck.add(0, groupId);
        Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId);
		for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) {
        	ActionItem item = itemIt.next();
{'fraction_non_alphanumeric': 0.08903214565728722, 'fraction_numerical': 0.0033103928913668437, 'mean_word_length': 4.899280575539568, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4336925', 'n_tokens_mistral': 3270, 'n_tokens_neox': 3113, 'n_words': 815}