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  package org.kuali.rice.kew.actions;
17  
18  import org.kuali.rice.kew.actionitem.ActionItem;
19  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
20  import org.kuali.rice.kew.api.action.ActionRequestStatus;
21  import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
22  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  
27  import java.util.List;
28  
29  
30  
31  /**
32   * This is the inverse of the {@link TakeWorkgroupAuthority} action.  This puts the document back
33   * in all the peoples action lists that have the document routed to them.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class ReleaseWorkgroupAuthority extends ActionTakenEvent {
38  
39      private String groupId;
40      /**
41       * @param routeHeader
42       * @param principal
43       */
44      public ReleaseWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
45          super(KewApiConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, routeHeader, principal);
46      }
47  
48      /**
49       * @param routeHeader
50       * @param principal
51       * @param annotation
52       * @param groupId
53       */
54      public ReleaseWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, String groupId) {
55          super(KewApiConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, routeHeader, principal, annotation);
56          this.groupId = groupId;
57      }
58  
59      /* (non-Javadoc)
60       * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
61       */
62      @Override
63      public String validateActionRules() {
64          if (groupId == null) {
65              return "User cannot Release Workgroup Authority without a given workgroup";
66          } else {
67              return performReleaseWorkgroupAuthority(true);
68          }
69      }
70      
71      /**
72       * This overridden method ...
73       * 
74       * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules(java.util.List)
75       */
76      @Override
77      public String validateActionRules(List<ActionRequestValue> actionRequests) {
78      	return validateActionRules();
79      }
80  
81      public void recordAction() throws InvalidActionTakenException {
82          String error = performReleaseWorkgroupAuthority(false);
83          if (!org.apache.commons.lang.StringUtils.isEmpty(error)) {
84              throw new InvalidActionTakenException(error);
85          }
86  
87          queueDocumentProcessing();
88      }
89  
90      private String performReleaseWorkgroupAuthority(boolean forValidationOnly) {
91          if (!KimApiServiceLocator.getGroupService().isMemberOfGroup(getPrincipal().getPrincipalId(), groupId)){
92              return (getPrincipal().getPrincipalName() + " not a member of workgroup " + groupId);
93          }
94  
95          List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
96          //List groupRequestsToActivate = new ArrayList();//requests for this group that need action items
97          for (ActionRequestValue actionRequest : actionRequests)
98          {
99              //we left the group active from take authority action.  pending havent been normally activated yet
100             if (actionRequest.isGroupRequest() && actionRequest.isActive() && actionRequest.getGroupId().equals(groupId)) {
101             	List<ActionItem> actionItems = actionRequest.getActionItems();
102                 if (actionItems.size() == 1) {
103                     ActionItem actionItem = actionItems.get(0);
104                     if (! actionItem.getPrincipalId().equals(getPrincipal().getPrincipalId())) {
105                         return "User attempting to release workgroup authority did not take it.";
106                     } else if (!forValidationOnly)
107                     {
108                         actionRequest.setStatus(ActionRequestStatus.INITIALIZED.getCode());//to circumvent check in service during activation
109                         getActionRequestService().activateRequest(actionRequest);
110                     }
111                 }
112             }
113         }
114         return "";
115     }
116 }