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