1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.actions; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.log4j.MDC; |
21 | |
import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator; |
22 | |
import org.kuali.rice.kew.actionrequest.ActionRequestFactory; |
23 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
24 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
25 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
26 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
27 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
28 | |
import org.kuali.rice.kew.exception.WorkflowException; |
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.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 | 0 | 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 | 0 | super(KEWConstants.ACTION_TAKEN_DENIED_CD, rh, principal); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public DisapproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) { |
68 | 0 | super(KEWConstants.ACTION_TAKEN_DENIED_CD, rh, principal, annotation); |
69 | 0 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Override |
75 | |
public String validateActionRules() { |
76 | 0 | return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId())); |
77 | |
} |
78 | |
|
79 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
80 | 0 | if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { |
81 | 0 | return "Document is not in a state to be disapproved"; |
82 | |
} |
83 | 0 | List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
84 | 0 | if (!isActionCompatibleRequest(filteredActionRequests)) { |
85 | 0 | return "No request for the user is compatible " + "with the DISAPPROVE or DENY action"; |
86 | |
} |
87 | 0 | return ""; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@Override |
94 | |
public boolean isActionCompatibleRequest(List requests) { |
95 | |
|
96 | 0 | if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) { |
97 | 0 | return true; |
98 | |
} |
99 | |
|
100 | 0 | boolean actionCompatible = false; |
101 | 0 | Iterator ars = requests.iterator(); |
102 | 0 | ActionRequestValue actionRequest = null; |
103 | |
|
104 | 0 | while (ars.hasNext()) { |
105 | 0 | actionRequest = (ActionRequestValue) ars.next(); |
106 | 0 | String request = actionRequest.getActionRequested(); |
107 | |
|
108 | |
|
109 | 0 | if ( (KEWConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) || |
110 | |
(KEWConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) { |
111 | 0 | actionCompatible = true; |
112 | 0 | break; |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | 0 | return actionCompatible; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public void recordAction() throws InvalidActionTakenException { |
125 | 0 | MDC.put("docId", getRouteHeader().getDocumentId()); |
126 | 0 | updateSearchableAttributesIfPossible(); |
127 | |
|
128 | 0 | LOG.debug("Disapproving document : " + annotation); |
129 | |
|
130 | 0 | List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
131 | 0 | LOG.debug("Checking to see if the action is legal"); |
132 | 0 | String errorMessage = validateActionRules(actionRequests); |
133 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
134 | 0 | throw new InvalidActionTakenException(errorMessage); |
135 | |
} |
136 | |
|
137 | 0 | LOG.debug("Record the disapproval action"); |
138 | 0 | Recipient delegator = findDelegatorForActionRequests(actionRequests); |
139 | 0 | ActionTakenValue actionTaken = saveActionTaken(delegator); |
140 | |
|
141 | 0 | LOG.debug("Deactivate all pending action requests"); |
142 | 0 | actionRequests = getActionRequestService().findPendingByDoc(getDocumentId()); |
143 | 0 | getActionRequestService().deactivateRequests(actionTaken, actionRequests); |
144 | 0 | notifyActionTaken(actionTaken); |
145 | |
|
146 | 0 | LOG.debug("Sending Acknowledgements to all previous approvers/completers"); |
147 | |
|
148 | 0 | RouteNodeInstance notificationNodeInstance = null; |
149 | |
|
150 | 0 | notificationNodeInstance = ((ActionRequestValue)actionRequests.get(0)).getNodeInstance(); |
151 | |
|
152 | 0 | generateNotifications(notificationNodeInstance); |
153 | |
|
154 | 0 | LOG.debug("Disapproving document"); |
155 | |
try { |
156 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
157 | 0 | routeHeader.markDocumentDisapproved(); |
158 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
159 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader); |
160 | 0 | notifyStatusChange(newStatus, oldStatus); |
161 | 0 | } catch (WorkflowException ex) { |
162 | 0 | LOG.warn(ex, ex); |
163 | 0 | throw new InvalidActionTakenException(ex.getMessage()); |
164 | 0 | } |
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
private void generateNotifications(RouteNodeInstance notificationNodeInstance) |
169 | |
{ |
170 | 0 | String groupName = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString( |
171 | |
KEWConstants.KEW_NAMESPACE, |
172 | |
KRADConstants.DetailTypes.WORKGROUP_DETAIL_TYPE, |
173 | |
KEWConstants.NOTIFICATION_EXCLUDED_USERS_WORKGROUP_NAME_IND); |
174 | |
|
175 | 0 | Set<String> systemPrincipalIds = new HashSet<String>(); |
176 | |
|
177 | 0 | if( !StringUtils.isBlank(groupName)) |
178 | |
{ |
179 | 0 | Group systemUserWorkgroup = KimApiServiceLocator.getGroupService(). |
180 | |
getGroupByName(Utilities.parseGroupNamespaceCode(groupName), Utilities.parseGroupName(groupName)); |
181 | |
|
182 | 0 | List<String> principalIds = KimApiServiceLocator. |
183 | |
getGroupService().getMemberPrincipalIds( systemUserWorkgroup.getId()); |
184 | |
|
185 | 0 | if (systemUserWorkgroup != null) |
186 | |
{ |
187 | 0 | for( String id : principalIds) |
188 | |
{ |
189 | 0 | systemPrincipalIds.add(id); |
190 | |
} |
191 | |
} |
192 | |
} |
193 | 0 | ActionRequestFactory arFactory = new ActionRequestFactory(getRouteHeader(), notificationNodeInstance); |
194 | 0 | Collection<ActionTakenValue> actions = KEWServiceLocator.getActionTakenService().findByDocumentId(getDocumentId()); |
195 | |
|
196 | 0 | Set<String> usersNotified = new HashSet<String>(); |
197 | 0 | for (ActionTakenValue action : actions) |
198 | |
{ |
199 | 0 | if ((action.isApproval() || action.isCompletion()) && !usersNotified.contains(action.getPrincipalId())) |
200 | |
{ |
201 | 0 | if (!systemPrincipalIds.contains(action.getPrincipalId())) |
202 | |
{ |
203 | 0 | ActionRequestValue request = arFactory.createNotificationRequest(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, action.getPrincipal(), getActionTakenCode(), getPrincipal(), getActionTakenCode()); |
204 | 0 | KEWServiceLocator.getActionRequestService().activateRequest(request); |
205 | 0 | usersNotified.add(request.getPrincipalId()); |
206 | 0 | } |
207 | |
} |
208 | |
} |
209 | |
|
210 | 0 | } |
211 | |
} |