View Javadoc

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