| 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.log4j.MDC; |
| 20 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
| 21 | |
import org.kuali.rice.kew.actionrequest.Recipient; |
| 22 | |
import org.kuali.rice.kew.actions.asyncservices.BlanketApproveProcessorService; |
| 23 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
| 24 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
| 25 | |
import org.kuali.rice.kew.engine.BlanketApproveEngine; |
| 26 | |
import org.kuali.rice.kew.engine.CompatUtils; |
| 27 | |
import org.kuali.rice.kew.engine.OrchestrationConfig; |
| 28 | |
import org.kuali.rice.kew.engine.RouteContext; |
| 29 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
| 30 | |
import org.kuali.rice.kew.engine.node.service.RouteNodeService; |
| 31 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
| 32 | |
import org.kuali.rice.kew.exception.WorkflowRuntimeException; |
| 33 | |
import org.kuali.rice.kew.messaging.MessageServiceNames; |
| 34 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
| 35 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 36 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 37 | |
import org.kuali.rice.kim.bo.entity.KimPrincipal; |
| 38 | |
|
| 39 | |
import java.util.*; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public class BlanketApproveAction extends ActionTakenEvent { |
| 48 | |
|
| 49 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BlanketApproveAction.class); |
| 50 | |
private Set<String> nodeNames; |
| 51 | |
|
| 52 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, KimPrincipal principal) { |
| 53 | 0 | super(KEWConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, rh, principal); |
| 54 | |
|
| 55 | 0 | setQueueDocumentAfterAction(false); |
| 56 | |
|
| 57 | 0 | } |
| 58 | |
|
| 59 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, KimPrincipal principal, String annotation, Integer routeLevel) { |
| 60 | 0 | this(rh, principal, annotation, convertRouteLevel(rh.getDocumentType(), routeLevel)); |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, KimPrincipal principal, String annotation, String nodeName) { |
| 64 | 0 | this(rh, principal, annotation, Collections.singleton(nodeName)); |
| 65 | |
|
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public BlanketApproveAction(DocumentRouteHeaderValue rh, KimPrincipal principal, String annotation, Set<String> nodeNames) { |
| 69 | 0 | super(KEWConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, rh, principal, annotation); |
| 70 | 0 | this.nodeNames = (nodeNames == null ? new HashSet<String>() : nodeNames); |
| 71 | 0 | setQueueDocumentAfterAction(false); |
| 72 | 0 | } |
| 73 | |
|
| 74 | |
private static Set<String> convertRouteLevel(DocumentType documentType, Integer routeLevel) { |
| 75 | 0 | Set<String> nodeNames = new HashSet<String>(); |
| 76 | 0 | if (routeLevel == null) { |
| 77 | 0 | return nodeNames; |
| 78 | |
} |
| 79 | 0 | RouteNode node = CompatUtils.getNodeForLevel(documentType, routeLevel); |
| 80 | 0 | if (node == null) { |
| 81 | 0 | throw new WorkflowRuntimeException("Could not locate a valid node for the given route level: " + routeLevel); |
| 82 | |
} |
| 83 | 0 | nodeNames.add(node.getRouteNodeName()); |
| 84 | 0 | return nodeNames; |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public String validateActionRules() { |
| 92 | 0 | return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getRouteHeaderId())); |
| 93 | |
} |
| 94 | |
|
| 95 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
| 96 | 0 | if ( (nodeNames != null) && (!nodeNames.isEmpty()) ) { |
| 97 | 0 | String nodeName = isGivenNodeListValid(); |
| 98 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(nodeName)) { |
| 99 | 0 | return "Document already at or beyond route node " + nodeName; |
| 100 | |
} |
| 101 | |
} |
| 102 | 0 | if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) { |
| 103 | 0 | return "Document is not in a state to be approved"; |
| 104 | |
} |
| 105 | 0 | List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
| 106 | 0 | if (!isActionCompatibleRequest(filteredActionRequests)) { |
| 107 | 0 | return "No request for the user is compatible with the BlanketApprove Action"; |
| 108 | |
} |
| 109 | |
|
| 110 | 0 | if (! KEWServiceLocator.getDocumentTypePermissionService().canBlanketApprove(getPrincipal().getPrincipalId(), getRouteHeader().getDocumentType(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) { |
| 111 | 0 | return "User is not authorized to BlanketApprove document"; |
| 112 | |
} |
| 113 | 0 | return ""; |
| 114 | |
} |
| 115 | |
|
| 116 | |
private String isGivenNodeListValid() { |
| 117 | 0 | for (Iterator<String> iterator = nodeNames.iterator(); iterator.hasNext();) { |
| 118 | 0 | String nodeName = (String) iterator.next(); |
| 119 | 0 | if (nodeName == null) { |
| 120 | 0 | iterator.remove(); |
| 121 | 0 | continue; |
| 122 | |
} |
| 123 | 0 | if (!getRouteNodeService().isNodeInPath(getRouteHeader(), nodeName)) { |
| 124 | 0 | return nodeName; |
| 125 | |
} |
| 126 | 0 | } |
| 127 | 0 | return ""; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public void recordAction() throws InvalidActionTakenException { |
| 131 | 0 | MDC.put("docId", getRouteHeader().getRouteHeaderId()); |
| 132 | 0 | updateSearchableAttributesIfPossible(); |
| 133 | |
|
| 134 | 0 | List<ActionRequestValue> actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getRouteHeaderId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ); |
| 135 | 0 | String errorMessage = validateActionRules(actionRequests); |
| 136 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
| 137 | 0 | throw new InvalidActionTakenException(errorMessage); |
| 138 | |
} |
| 139 | |
|
| 140 | 0 | LOG.debug("Checking to see if the action is legal"); |
| 141 | |
|
| 142 | 0 | LOG.debug("Blanket approving document : " + annotation); |
| 143 | |
|
| 144 | 0 | if (getRouteHeader().isStateInitiated() || getRouteHeader().isStateSaved()) { |
| 145 | 0 | markDocumentEnroute(getRouteHeader()); |
| 146 | 0 | getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId()); |
| 147 | |
} |
| 148 | |
|
| 149 | 0 | LOG.debug("Record the blanket approval action"); |
| 150 | 0 | Recipient delegator = findDelegatorForActionRequests(actionRequests); |
| 151 | 0 | ActionTakenValue actionTaken = saveActionTaken(delegator); |
| 152 | |
|
| 153 | 0 | LOG.debug("Deactivate pending action requests for user"); |
| 154 | 0 | getActionRequestService().deactivateRequests(actionTaken, actionRequests); |
| 155 | 0 | notifyActionTaken(actionTaken); |
| 156 | |
|
| 157 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader()); |
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | 0 | queueDeferredWork(actionTaken); |
| 165 | 0 | } |
| 166 | |
|
| 167 | |
protected void queueDeferredWork(ActionTakenValue actionTaken) { |
| 168 | |
try { |
| 169 | 0 | final boolean shouldIndex = getRouteHeader().getDocumentType().hasSearchableAttributes() && RouteContext.getCurrentRouteContext().isSearchIndexingRequestedForContext(); |
| 170 | |
|
| 171 | 0 | BlanketApproveProcessorService blanketApprove = MessageServiceNames.getBlanketApproveProcessorService(routeHeader); |
| 172 | 0 | blanketApprove.doBlanketApproveWork(routeHeader.getRouteHeaderId(), getPrincipal().getPrincipalId(), actionTaken.getActionTakenId(), nodeNames, shouldIndex); |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | 0 | } catch (Exception e) { |
| 182 | 0 | LOG.error(e); |
| 183 | 0 | throw new WorkflowRuntimeException(e); |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
|
| 187 | 0 | } |
| 188 | |
|
| 189 | |
public void performDeferredBlanketApproveWork(ActionTakenValue actionTaken) throws Exception { |
| 190 | |
|
| 191 | 0 | if (getRouteHeader().isInException()) { |
| 192 | 0 | LOG.debug("Moving document back to Enroute from Exception"); |
| 193 | |
|
| 194 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
| 195 | 0 | getRouteHeader().markDocumentEnroute(); |
| 196 | |
|
| 197 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
| 198 | 0 | notifyStatusChange(newStatus, oldStatus); |
| 199 | |
} |
| 200 | 0 | OrchestrationConfig config = new OrchestrationConfig(); |
| 201 | 0 | config.setDestinationNodeNames(nodeNames); |
| 202 | 0 | config.setCause(actionTaken); |
| 203 | 0 | BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getBlanketApproveEngineFactory().newEngine(config); |
| 204 | 0 | blanketApproveEngine.process(getRouteHeader().getRouteHeaderId(), null); |
| 205 | |
|
| 206 | 0 | queueDocumentProcessing(); |
| 207 | 0 | } |
| 208 | |
|
| 209 | |
protected void markDocumentEnroute(DocumentRouteHeaderValue routeHeader) throws InvalidActionTakenException { |
| 210 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
| 211 | 0 | getRouteHeader().markDocumentEnroute(); |
| 212 | |
|
| 213 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
| 214 | 0 | notifyStatusChange(newStatus, oldStatus); |
| 215 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader()); |
| 216 | 0 | } |
| 217 | |
|
| 218 | |
private RouteNodeService getRouteNodeService() { |
| 219 | 0 | return KEWServiceLocator.getRouteNodeService(); |
| 220 | |
} |
| 221 | |
} |