View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.kuali.rice.kew.actions;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.kuali.rice.kew.actionitem.ActionItem;
25  import org.kuali.rice.kew.actionlist.service.ActionListService;
26  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
27  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
28  import org.kuali.rice.kew.exception.InvalidActionTakenException;
29  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  import org.kuali.rice.kew.util.KEWConstants;
32  import org.kuali.rice.kew.util.Utilities;
33  import org.kuali.rice.kim.bo.entity.KimPrincipal;
34  import org.kuali.rice.kim.service.KIMServiceLocator;
35  
36  
37  /**
38   * Removes all workgroup action items for a document from everyone's action list except the person
39   * who took the workgroup authority
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   *
43   */
44  public class TakeWorkgroupAuthority extends ActionTakenEvent {
45      
46      private String groupId;
47      
48      /**
49       * @param routeHeader
50       * @param principal
51       */
52      public TakeWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, KimPrincipal principal) {
53          super(KEWConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, routeHeader, principal);
54      }
55  
56      /**
57       * @param routeHeader
58       * @param principal
59       * @param annotation
60       * @param groupId
61       */
62      public TakeWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, KimPrincipal principal, String annotation, String groupId) {
63          super(KEWConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, routeHeader, principal, annotation);
64          this.groupId = groupId;
65      }
66  
67      /* (non-Javadoc)
68       * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
69       */
70      @Override
71      public String validateActionRules() {
72          if  ( (groupId != null) && (!KIMServiceLocator.getIdentityManagementService().isMemberOfGroup(getPrincipal().getPrincipalId(), groupId))) {
73              return (getPrincipal().getPrincipalName() + " not a member of workgroup " + groupId);
74          }
75          return "";
76      }
77  
78      @Override
79      public String validateActionRules(List<ActionRequestValue> actionRequests) {
80      	return validateActionRules();
81      }
82      
83      public void recordAction() throws InvalidActionTakenException {
84  
85          String errorMessage = validateActionRules();
86          if (!Utilities.isEmpty(errorMessage)) {
87              throw new InvalidActionTakenException(errorMessage);
88          }
89  //        if (! workgroup.hasMember(getUser())) {
90  //            throw new InvalidActionTakenException(getUser().getAuthenticationUserId() + " not a member of workgroup " + workgroup.getDisplayName());
91  //        }
92  
93          List<ActionRequestValue> documentRequests = getActionRequestService().findPendingByDoc(getRouteHeaderId());
94          List<ActionRequestValue> workgroupRequests = new ArrayList<ActionRequestValue>();
95          for (ActionRequestValue actionRequest : documentRequests)
96          {
97              if (actionRequest.isGroupRequest() && actionRequest.getGroup().getGroupId().equals(groupId))
98              {
99                  workgroupRequests.add(actionRequest);
100             }
101         }
102 
103         ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(workgroupRequests));
104         notifyActionTaken(actionTaken);
105 
106         ActionListService actionListService = KEWServiceLocator.getActionListService();
107         Collection<ActionItem> actionItems = actionListService.findByRouteHeaderId(getRouteHeaderId());
108         for (ActionItem actionItem : actionItems)
109         {
110             //delete all requests for this workgroup on this document not to this user
111             if (actionItem.isWorkgroupItem() && actionItem.getGroupId().equals(groupId) &&
112                     !actionItem.getPrincipalId().equals(getPrincipal().getPrincipalId()))
113             {
114                 actionListService.deleteActionItem(actionItem);
115             }
116         }
117     }
118 }