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.log4j.MDC; |
19 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
20 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
21 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
22 | |
import org.kuali.rice.kew.api.exception.InvalidActionTakenException; |
23 | |
import org.kuali.rice.kew.doctype.DocumentTypePolicy; |
24 | |
import org.kuali.rice.kew.api.exception.ResourceUnavailableException; |
25 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
26 | |
import org.kuali.rice.kew.api.KewApiConstants; |
27 | |
import org.kuali.rice.kim.api.identity.principal.PrincipalContract; |
28 | |
|
29 | |
|
30 | |
import java.util.Iterator; |
31 | |
import java.util.List; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class AcknowledgeAction extends ActionTakenEvent { |
46 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AcknowledgeAction.class); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public AcknowledgeAction(DocumentRouteHeaderValue rh, PrincipalContract principal) { |
55 | 0 | super(KewApiConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, rh, principal); |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public AcknowledgeAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) { |
67 | 0 | super(KewApiConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, rh, principal, annotation); |
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public String validateActionRules() { |
75 | 0 | return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId())); |
76 | |
} |
77 | |
|
78 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
79 | 0 | if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { |
80 | 0 | return "Document is not in a state to be acknowledged"; |
81 | |
} |
82 | 0 | List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ); |
83 | 0 | if (!isActionCompatibleRequest(filteredActionRequests)) { |
84 | 0 | return "No request for the user is compatible " + "with the ACKNOWLEDGE action"; |
85 | |
} |
86 | 0 | return ""; |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
@Override |
93 | |
public boolean isActionCompatibleRequest(List requests) { |
94 | |
|
95 | |
|
96 | 0 | if (requests.isEmpty()) { |
97 | 0 | return true; |
98 | |
} |
99 | |
|
100 | |
|
101 | 0 | if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) { |
102 | 0 | return true; |
103 | |
} |
104 | |
|
105 | 0 | boolean actionCompatible = false; |
106 | 0 | Iterator ars = requests.iterator(); |
107 | 0 | ActionRequestValue actionRequest = null; |
108 | |
|
109 | 0 | while (ars.hasNext()) { |
110 | 0 | actionRequest = (ActionRequestValue) ars.next(); |
111 | 0 | String request = actionRequest.getActionRequested(); |
112 | |
|
113 | |
|
114 | 0 | if ( (KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ.equals(request)) || (KewApiConstants.ACTION_REQUEST_FYI_REQ.equals(request)) ) { |
115 | 0 | actionCompatible = true; |
116 | 0 | break; |
117 | |
} |
118 | 0 | } |
119 | |
|
120 | 0 | return actionCompatible; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public void recordAction() throws InvalidActionTakenException { |
130 | 0 | MDC.put("docId", getRouteHeader().getDocumentId()); |
131 | 0 | updateSearchableAttributesIfPossible(); |
132 | |
|
133 | 0 | LOG.debug("Acknowledging document : " + annotation); |
134 | |
|
135 | 0 | LOG.debug("Checking to see if the action is legal"); |
136 | 0 | List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), routeHeader.getDocumentId(), KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ); |
137 | 0 | if (actionRequests == null || actionRequests.isEmpty()) { |
138 | 0 | DocumentTypePolicy allowUnrequested = getRouteHeader().getDocumentType().getAllowUnrequestedActionPolicy(); |
139 | 0 | if (allowUnrequested != null) { |
140 | 0 | if (!allowUnrequested.getPolicyValue()) { |
141 | 0 | throw new InvalidActionTakenException("No request for the user is compatible " + "with the ACKNOWLEDGE action. " + "Doctype policy ALLOW_UNREQUESTED_ACTION is set to false and someone else likely just took action on the document."); |
142 | |
} |
143 | |
} |
144 | |
} |
145 | |
|
146 | 0 | String errorMessage = validateActionRules(actionRequests); |
147 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
148 | 0 | throw new InvalidActionTakenException(errorMessage); |
149 | |
} |
150 | |
|
151 | 0 | LOG.debug("Record the acknowledge action"); |
152 | 0 | Recipient delegator = findDelegatorForActionRequests(actionRequests); |
153 | 0 | ActionTakenValue actionTaken = saveActionTaken(delegator); |
154 | 0 | LOG.debug("Deactivate all pending action requests"); |
155 | 0 | getActionRequestService().deactivateRequests(actionTaken, actionRequests); |
156 | 0 | notifyActionTaken(actionTaken); |
157 | 0 | } |
158 | |
} |