View Javadoc

1   /**
2    * Copyright 2005-2013 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  /*
17   * Copyright 2005-2007 The Kuali Foundation
18   *
19   *
20   * Licensed under the Educational Community License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   * http://www.opensource.org/licenses/ecl2.php
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.kuali.rice.kew.routemodule;
33  
34  import java.util.ArrayList;
35  import java.util.HashMap;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Map;
39  
40  import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
41  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
42  import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
43  import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
44  import org.kuali.rice.kew.actionrequest.Recipient;
45  import org.kuali.rice.kew.api.action.RecipientType;
46  import org.kuali.rice.kew.api.exception.ResourceUnavailableException;
47  import org.kuali.rice.kew.api.exception.WorkflowException;
48  import org.kuali.rice.kew.engine.RouteContext;
49  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
50  import org.kuali.rice.kew.service.KEWServiceLocator;
51  import org.kuali.rice.kew.util.ResponsibleParty;
52  
53  
54  /**
55   * @author Kuali Rice Team (rice.collab@kuali.org)
56   */
57  public class TestRouteModule implements RouteModule {
58  
59      private static Map responsibilityMap = new HashMap();
60  
61      public List findActionRequests(RouteContext context) throws ResourceUnavailableException, WorkflowException {
62      	return findActionRequests(context.getDocument());
63      }
64  
65      public List findActionRequests(DocumentRouteHeaderValue routeHeader) throws ResourceUnavailableException, WorkflowException {
66          TestRouteLevel routeLevel = TestRouteModuleXMLHelper.parseCurrentRouteLevel(routeHeader);
67          List actionRequests = new ArrayList();
68          if (routeLevel == null) {
69              return actionRequests;
70          }
71          for (Iterator iterator = routeLevel.getResponsibilities().iterator(); iterator.hasNext();) {
72              TestResponsibility responsibility = (TestResponsibility) iterator.next();
73              TestRecipient recipient = responsibility.getRecipient();
74              Recipient realRecipient = getRealRecipient(recipient);
75              ActionRequestFactory arFactory = new ActionRequestFactory(routeHeader);
76              String responsibilityId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
77              ActionRequestValue request = arFactory.addRootActionRequest(responsibility.getActionRequested(), new Integer(responsibility.getPriority()), realRecipient, "", responsibilityId, Boolean.FALSE, null, null);
78              responsibilityMap.put(request.getResponsibilityId(), recipient);
79              for (Iterator delIt = responsibility.getDelegations().iterator(); delIt.hasNext();) {
80                  TestDelegation delegation = (TestDelegation) delIt.next();
81                  TestRecipient delegationRecipient = delegation.getResponsibility().getRecipient();
82                  Recipient realDelegationRecipient = getRealRecipient(delegationRecipient);
83                  responsibilityId = KEWServiceLocator.getResponsibilityIdService().getNewResponsibilityId();
84                  ActionRequestValue delegationRequest = arFactory.addDelegationRequest(request, realDelegationRecipient, responsibilityId, Boolean.FALSE, delegation.getType(), "", null);
85                  responsibilityMap.put(delegationRequest.getResponsibilityId(), delegationRecipient);
86              }
87              actionRequests.add(request);
88          }
89          return actionRequests;
90      }
91  
92      public Recipient getRealRecipient(TestRecipient recipient) throws WorkflowException {
93          Recipient realRecipient = null;
94          if (recipient.getType().equals(RecipientType.PRINCIPAL.getCode())) {
95          	realRecipient = new KimPrincipalRecipient(recipient.getId());
96          } else if (recipient.getType().equals(RecipientType.GROUP.getCode())) {
97          	realRecipient = new KimGroupRecipient(recipient.getId());
98          } else {
99          	throw new WorkflowException("Could not resolve recipient with type " + recipient.getType());
100         }
101         return realRecipient;
102     }
103 
104     public ResponsibleParty resolveResponsibilityId(String responsibilityId) throws ResourceUnavailableException, WorkflowException {
105         TestRecipient recipient = (TestRecipient)responsibilityMap.get(responsibilityId);
106         if (recipient == null) {
107             return null;
108         }
109         ResponsibleParty responsibleParty = new ResponsibleParty();
110         if (recipient.getType().equals(RecipientType.PRINCIPAL.getCode())) {
111             responsibleParty.setPrincipalId(recipient.getId());
112         } else if (recipient.getType().equals(RecipientType.GROUP.getCode())) {
113         	responsibleParty.setGroupId(recipient.getId());
114         } else if (recipient.getType().equals(RecipientType.ROLE.getCode())) {
115             responsibleParty.setRoleName(recipient.getId());
116         } else {
117             throw new WorkflowException("Invalid recipient type code of '"+recipient.getType()+"' for responsibility id "+responsibilityId);
118         }
119         return responsibleParty;
120     }
121 
122     @Override
123     public boolean isMoreRequestsAvailable(RouteContext context) {
124         return false;
125     }
126 }