View Javadoc
1   /**
2    * Copyright 2005-2014 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.kew.routemodule;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
25  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
26  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
27  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
28  import org.kuali.rice.kew.actionrequest.Recipient;
29  import org.kuali.rice.kew.api.action.RecipientType;
30  import org.kuali.rice.kew.api.exception.ResourceUnavailableException;
31  import org.kuali.rice.kew.api.exception.WorkflowException;
32  import org.kuali.rice.kew.engine.RouteContext;
33  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
34  import org.kuali.rice.kew.service.KEWServiceLocator;
35  import org.kuali.rice.kew.util.ResponsibleParty;
36  
37  
38  /**
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class TestRouteModule implements RouteModule {
42  
43      private static Map responsibilityMap = new HashMap();
44  
45      public List findActionRequests(RouteContext context) throws ResourceUnavailableException, WorkflowException {
46      	return findActionRequests(context.getDocument());
47      }
48  
49      public List findActionRequests(DocumentRouteHeaderValue routeHeader) throws ResourceUnavailableException, WorkflowException {
50          TestRouteLevel routeLevel = TestRouteModuleXMLHelper.parseCurrentRouteLevel(routeHeader);
51          List actionRequests = new ArrayList();
52          if (routeLevel == null) {
53              return actionRequests;
54          }
55          for (Iterator iterator = routeLevel.getResponsibilities().iterator(); iterator.hasNext();) {
56              TestResponsibility responsibility = (TestResponsibility) iterator.next();
57              TestRecipient recipient = responsibility.getRecipient();
58              Recipient realRecipient = getRealRecipient(recipient);
59              ActionRequestFactory arFactory = new ActionRequestFactory(routeHeader);
60              String responsibilityId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
61              ActionRequestValue request = arFactory.addRootActionRequest(responsibility.getActionRequested(), new Integer(responsibility.getPriority()), realRecipient, "", responsibilityId, Boolean.FALSE, null, null);
62              responsibilityMap.put(request.getResponsibilityId(), recipient);
63              for (Iterator delIt = responsibility.getDelegations().iterator(); delIt.hasNext();) {
64                  TestDelegation delegation = (TestDelegation) delIt.next();
65                  TestRecipient delegationRecipient = delegation.getResponsibility().getRecipient();
66                  Recipient realDelegationRecipient = getRealRecipient(delegationRecipient);
67                  responsibilityId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
68                  ActionRequestValue delegationRequest = arFactory.addDelegationRequest(request, realDelegationRecipient, responsibilityId, Boolean.FALSE, delegation.getType(), "", null);
69                  responsibilityMap.put(delegationRequest.getResponsibilityId(), delegationRecipient);
70              }
71              actionRequests.add(request);
72          }
73          return actionRequests;
74      }
75  
76      public Recipient getRealRecipient(TestRecipient recipient) throws WorkflowException {
77          Recipient realRecipient = null;
78          if (recipient.getType().equals(RecipientType.PRINCIPAL.getCode())) {
79          	realRecipient = new KimPrincipalRecipient(recipient.getId());
80          } else if (recipient.getType().equals(RecipientType.GROUP.getCode())) {
81          	realRecipient = new KimGroupRecipient(recipient.getId());
82          } else {
83          	throw new WorkflowException("Could not resolve recipient with type " + recipient.getType());
84          }
85          return realRecipient;
86      }
87  
88      public ResponsibleParty resolveResponsibilityId(String responsibilityId) throws ResourceUnavailableException, WorkflowException {
89          TestRecipient recipient = (TestRecipient)responsibilityMap.get(responsibilityId);
90          if (recipient == null) {
91              return null;
92          }
93          ResponsibleParty responsibleParty = new ResponsibleParty();
94          if (recipient.getType().equals(RecipientType.PRINCIPAL.getCode())) {
95              responsibleParty.setPrincipalId(recipient.getId());
96          } else if (recipient.getType().equals(RecipientType.GROUP.getCode())) {
97          	responsibleParty.setGroupId(recipient.getId());
98          } else if (recipient.getType().equals(RecipientType.ROLE.getCode())) {
99              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 }