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.ActionRequestFactory; |
21 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
22 | |
import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient; |
23 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
24 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
25 | |
import org.kuali.rice.kew.exception.InvalidActionTakenException; |
26 | |
import org.kuali.rice.kew.exception.WorkflowException; |
27 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
28 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
29 | |
import org.kuali.rice.kew.util.KEWConstants; |
30 | |
import org.kuali.rice.kim.api.identity.principal.PrincipalContract; |
31 | |
|
32 | |
|
33 | |
import java.util.List; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class SaveActionEvent extends ActionTakenEvent { |
44 | |
|
45 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SaveActionEvent.class); |
46 | |
|
47 | |
private static final String RESPONSIBILITY_DESCRIPTION = "Initiator needs to complete document."; |
48 | |
|
49 | |
public SaveActionEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) { |
50 | 0 | super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal); |
51 | 0 | } |
52 | |
|
53 | |
public SaveActionEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation) { |
54 | 0 | super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal, annotation); |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
@Override |
61 | |
public String validateActionRules() { |
62 | 0 | return validateActionRulesCustom(true); |
63 | |
} |
64 | |
|
65 | |
private String validateActionRulesCustom(boolean checkIfActionIsValid) { |
66 | 0 | if (checkIfActionIsValid && (!getRouteHeader().isValidActionToTake(getActionPerformedCode()))) { |
67 | 0 | return "Document is not in a state to be saved"; |
68 | |
} |
69 | |
|
70 | 0 | if (! KEWServiceLocator.getDocumentTypePermissionService().canSave(getPrincipal().getPrincipalId(), getRouteHeader().getDocumentId(), getRouteHeader().getDocumentType(), getRouteHeader().getCurrentNodeNames(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) { |
71 | 0 | return "User is not authorized to Save document"; |
72 | |
} |
73 | 0 | return ""; |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
@Override |
82 | |
public String validateActionRules(List<ActionRequestValue> actionRequests) { |
83 | 0 | return validateActionRules(); |
84 | |
} |
85 | |
|
86 | |
public void recordAction() throws InvalidActionTakenException { |
87 | 0 | MDC.put("docId", getRouteHeader().getDocumentId()); |
88 | 0 | LOG.debug("Checking to see if the action is legal"); |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 0 | boolean checkIfActionIsValid = true; |
97 | 0 | if (getRouteHeader().isEnroute() || getRouteHeader().isInException()) { |
98 | |
|
99 | 0 | checkIfActionIsValid = false; |
100 | |
} |
101 | 0 | String errorMessage = validateActionRulesCustom(checkIfActionIsValid); |
102 | 0 | if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) { |
103 | 0 | throw new InvalidActionTakenException(errorMessage); |
104 | |
} |
105 | |
|
106 | 0 | updateSearchableAttributesIfPossible(); |
107 | |
|
108 | |
|
109 | 0 | if (getRouteHeader().isStateInitiated()) { |
110 | 0 | LOG.debug("Record the save action"); |
111 | 0 | ActionTakenValue actionTaken = saveActionTaken(); |
112 | |
|
113 | 0 | this.getActionRequestService().saveActionRequest(generateSaveRequest()); |
114 | 0 | notifyActionTaken(actionTaken); |
115 | 0 | LOG.debug("Marking document saved"); |
116 | |
try { |
117 | 0 | String oldStatus = getRouteHeader().getDocRouteStatus(); |
118 | 0 | getRouteHeader().markDocumentSaved(); |
119 | 0 | String newStatus = getRouteHeader().getDocRouteStatus(); |
120 | 0 | notifyStatusChange(newStatus, oldStatus); |
121 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader); |
122 | 0 | } catch (WorkflowException ex) { |
123 | 0 | LOG.warn(ex, ex); |
124 | 0 | throw new InvalidActionTakenException(ex.getMessage()); |
125 | 0 | } |
126 | |
} |
127 | 0 | } |
128 | |
|
129 | |
protected ActionRequestValue generateSaveRequest() { |
130 | 0 | RouteNodeInstance initialNode = null; |
131 | 0 | List initialNodes = KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getDocumentId()); |
132 | 0 | if (!initialNodes.isEmpty()) { |
133 | 0 | initialNode = (RouteNodeInstance)initialNodes.get(0); |
134 | |
} |
135 | |
|
136 | 0 | ActionRequestFactory arFactory = new ActionRequestFactory(getRouteHeader(), initialNode); |
137 | 0 | ActionRequestValue saveRequest = arFactory.createActionRequest(KEWConstants.ACTION_REQUEST_COMPLETE_REQ, |
138 | |
0, new KimPrincipalRecipient(getPrincipal()), RESPONSIBILITY_DESCRIPTION, KEWConstants.SAVED_REQUEST_RESPONSIBILITY_ID, |
139 | |
Boolean.TRUE, annotation); |
140 | |
|
141 | 0 | this.getActionRequestService().activateRequest(saveRequest); |
142 | 0 | return saveRequest; |
143 | |
} |
144 | |
|
145 | |
} |