1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.actions;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.MDC;
20 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
21 import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
22 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
23 import org.kuali.rice.kew.api.doctype.DocumentTypePolicy;
24 import org.kuali.rice.kew.actionrequest.Recipient;
25 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
26 import org.kuali.rice.kew.api.exception.WorkflowException;
27 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
28 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
29 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
30 import org.kuali.rice.kew.service.KEWServiceLocator;
31 import org.kuali.rice.kew.api.KewApiConstants;
32 import org.kuali.rice.kew.util.Utilities;
33 import org.kuali.rice.kim.api.group.Group;
34 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
35 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36 import org.kuali.rice.krad.util.KRADConstants;
37
38 import java.util.Collection;
39 import java.util.HashSet;
40 import java.util.Iterator;
41 import java.util.List;
42 import java.util.Set;
43
44
45
46
47
48
49
50
51 public class DisapproveAction extends ActionTakenEvent {
52 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisapproveAction.class);
53
54
55
56
57
58 public DisapproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
59 super(KewApiConstants.ACTION_TAKEN_DENIED_CD, rh, principal);
60 }
61
62
63
64
65
66
67 public DisapproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
68 super(KewApiConstants.ACTION_TAKEN_DENIED_CD, rh, principal, annotation);
69 }
70
71
72
73
74 @Override
75 public String validateActionRules() {
76 return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
77 }
78
79 public String validateActionRules(List<ActionRequestValue> actionRequests) {
80 if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
81 return "Document is not in a state to be disapproved";
82 }
83 List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
84 if (!isActionCompatibleRequest(filteredActionRequests)) {
85 return "No request for the user is compatible " + "with the DISAPPROVE or DENY action";
86 }
87 return "";
88 }
89
90
91
92
93 @Override
94 public boolean isActionCompatibleRequest(List requests) {
95
96 if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) {
97 return true;
98 }
99
100 boolean actionCompatible = false;
101 Iterator ars = requests.iterator();
102 ActionRequestValue actionRequest = null;
103
104 while (ars.hasNext()) {
105 actionRequest = (ActionRequestValue) ars.next();
106 String request = actionRequest.getActionRequested();
107
108
109 if ( (KewApiConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) ||
110 (KewApiConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) {
111 actionCompatible = true;
112 break;
113 }
114 }
115
116 return actionCompatible;
117 }
118
119
120
121
122
123
124 public void recordAction() throws InvalidActionTakenException {
125 MDC.put("docId", getRouteHeader().getDocumentId());
126 updateSearchableAttributesIfPossible();
127
128 LOG.debug("Disapproving document : " + annotation);
129
130 List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
131 LOG.debug("Checking to see if the action is legal");
132 String errorMessage = validateActionRules(actionRequests);
133 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
134 throw new InvalidActionTakenException(errorMessage);
135 }
136
137 LOG.debug("Record the disapproval action");
138 Recipient delegator = findDelegatorForActionRequests(actionRequests);
139 ActionTakenValue actionTaken = saveActionTaken(delegator);
140
141 LOG.debug("Deactivate all pending action requests");
142 actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
143 getActionRequestService().deactivateRequests(actionTaken, actionRequests);
144 notifyActionTaken(actionTaken);
145
146 if(!isPolicySet(getRouteHeader().getDocumentType(), DocumentTypePolicy.SUPPRESS_ACKNOWLEDGEMENTS_ON_DISAPPROVE)){
147 LOG.debug("Sending Acknowledgements to all previous approvers/completers");
148
149 RouteNodeInstance notificationNodeInstance = null;
150
151 notificationNodeInstance = ((ActionRequestValue)actionRequests.get(0)).getNodeInstance();
152
153 generateAcknowledgementsToPreviousActionTakers(notificationNodeInstance);
154 }
155
156 LOG.debug("Disapproving document");
157 try {
158 String oldStatus = getRouteHeader().getDocRouteStatus();
159 routeHeader.markDocumentDisapproved();
160 String newStatus = getRouteHeader().getDocRouteStatus();
161 DocumentRouteHeaderValue routeHeaderValue = KEWServiceLocator.getRouteHeaderService().
162 saveRouteHeader(routeHeader);
163 setRouteHeader(routeHeaderValue);
164 notifyStatusChange(newStatus, oldStatus);
165 } catch (WorkflowException ex) {
166 LOG.warn(ex, ex);
167 throw new InvalidActionTakenException(ex.getMessage());
168 }
169 }
170 }