001/**
002 * Copyright 2005-2014 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.kew.routemodule;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
025import org.kuali.rice.kew.actionrequest.ActionRequestValue;
026import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
027import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
028import org.kuali.rice.kew.actionrequest.Recipient;
029import org.kuali.rice.kew.api.action.RecipientType;
030import org.kuali.rice.kew.api.exception.ResourceUnavailableException;
031import org.kuali.rice.kew.api.exception.WorkflowException;
032import org.kuali.rice.kew.engine.RouteContext;
033import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
034import org.kuali.rice.kew.service.KEWServiceLocator;
035import org.kuali.rice.kew.util.ResponsibleParty;
036
037
038/**
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public class TestRouteModule implements RouteModule {
042
043    private static Map responsibilityMap = new HashMap();
044
045    public List findActionRequests(RouteContext context) throws ResourceUnavailableException, WorkflowException {
046        return findActionRequests(context.getDocument());
047    }
048
049    public List findActionRequests(DocumentRouteHeaderValue routeHeader) throws ResourceUnavailableException, WorkflowException {
050        TestRouteLevel routeLevel = TestRouteModuleXMLHelper.parseCurrentRouteLevel(routeHeader);
051        List actionRequests = new ArrayList();
052        if (routeLevel == null) {
053            return actionRequests;
054        }
055        for (Iterator iterator = routeLevel.getResponsibilities().iterator(); iterator.hasNext();) {
056            TestResponsibility responsibility = (TestResponsibility) iterator.next();
057            TestRecipient recipient = responsibility.getRecipient();
058            Recipient realRecipient = getRealRecipient(recipient);
059            ActionRequestFactory arFactory = new ActionRequestFactory(routeHeader);
060            String responsibilityId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
061            ActionRequestValue request = arFactory.addRootActionRequest(responsibility.getActionRequested(), new Integer(responsibility.getPriority()), realRecipient, "", responsibilityId, Boolean.FALSE, null, null);
062            responsibilityMap.put(request.getResponsibilityId(), recipient);
063            for (Iterator delIt = responsibility.getDelegations().iterator(); delIt.hasNext();) {
064                TestDelegation delegation = (TestDelegation) delIt.next();
065                TestRecipient delegationRecipient = delegation.getResponsibility().getRecipient();
066                Recipient realDelegationRecipient = getRealRecipient(delegationRecipient);
067                responsibilityId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
068                ActionRequestValue delegationRequest = arFactory.addDelegationRequest(request, realDelegationRecipient, responsibilityId, Boolean.FALSE, delegation.getType(), "", null);
069                responsibilityMap.put(delegationRequest.getResponsibilityId(), delegationRecipient);
070            }
071            actionRequests.add(request);
072        }
073        return actionRequests;
074    }
075
076    public Recipient getRealRecipient(TestRecipient recipient) throws WorkflowException {
077        Recipient realRecipient = null;
078        if (recipient.getType().equals(RecipientType.PRINCIPAL.getCode())) {
079                realRecipient = new KimPrincipalRecipient(recipient.getId());
080        } else if (recipient.getType().equals(RecipientType.GROUP.getCode())) {
081                realRecipient = new KimGroupRecipient(recipient.getId());
082        } else {
083                throw new WorkflowException("Could not resolve recipient with type " + recipient.getType());
084        }
085        return realRecipient;
086    }
087
088    public ResponsibleParty resolveResponsibilityId(String responsibilityId) throws ResourceUnavailableException, WorkflowException {
089        TestRecipient recipient = (TestRecipient)responsibilityMap.get(responsibilityId);
090        if (recipient == null) {
091            return null;
092        }
093        ResponsibleParty responsibleParty = new ResponsibleParty();
094        if (recipient.getType().equals(RecipientType.PRINCIPAL.getCode())) {
095            responsibleParty.setPrincipalId(recipient.getId());
096        } else if (recipient.getType().equals(RecipientType.GROUP.getCode())) {
097                responsibleParty.setGroupId(recipient.getId());
098        } else if (recipient.getType().equals(RecipientType.ROLE.getCode())) {
099            responsibleParty.setRoleName(recipient.getId());
100        } else {
101            throw new WorkflowException("Invalid recipient type code of '"+recipient.getType()+"' for responsibility id "+responsibilityId);
102        }
103        return responsibleParty;
104    }
105
106    @Override
107    public boolean isMoreRequestsAvailable(RouteContext context) {
108        return false;
109    }
110}