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 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 super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal);
51 }
52
53 public SaveActionEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation) {
54 super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal, annotation);
55 }
56
57
58
59
60 @Override
61 public String validateActionRules() {
62 return validateActionRulesCustom(true);
63 }
64
65 private String validateActionRulesCustom(boolean checkIfActionIsValid) {
66 if (checkIfActionIsValid && (!getRouteHeader().isValidActionToTake(getActionPerformedCode()))) {
67 return "Document is not in a state to be saved";
68 }
69
70 if (! KEWServiceLocator.getDocumentTypePermissionService().canSave(getPrincipal().getPrincipalId(), getRouteHeader().getDocumentId(), getRouteHeader().getDocumentType(), getRouteHeader().getCurrentNodeNames(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) {
71 return "User is not authorized to Save document";
72 }
73 return "";
74 }
75
76
77
78
79
80
81 @Override
82 public String validateActionRules(List<ActionRequestValue> actionRequests) {
83 return validateActionRules();
84 }
85
86 public void recordAction() throws InvalidActionTakenException {
87 MDC.put("docId", getRouteHeader().getDocumentId());
88 LOG.debug("Checking to see if the action is legal");
89
90
91
92
93
94
95
96 boolean checkIfActionIsValid = true;
97 if (getRouteHeader().isEnroute() || getRouteHeader().isInException()) {
98
99 checkIfActionIsValid = false;
100 }
101 String errorMessage = validateActionRulesCustom(checkIfActionIsValid);
102 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
103 throw new InvalidActionTakenException(errorMessage);
104 }
105
106 updateSearchableAttributesIfPossible();
107
108
109 if (getRouteHeader().isStateInitiated()) {
110 LOG.debug("Record the save action");
111 ActionTakenValue actionTaken = saveActionTaken();
112
113 this.getActionRequestService().saveActionRequest(generateSaveRequest());
114 notifyActionTaken(actionTaken);
115 LOG.debug("Marking document saved");
116 try {
117 String oldStatus = getRouteHeader().getDocRouteStatus();
118 getRouteHeader().markDocumentSaved();
119 String newStatus = getRouteHeader().getDocRouteStatus();
120 notifyStatusChange(newStatus, oldStatus);
121 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
122 } catch (WorkflowException ex) {
123 LOG.warn(ex, ex);
124 throw new InvalidActionTakenException(ex.getMessage());
125 }
126 }
127 }
128
129 protected ActionRequestValue generateSaveRequest() {
130 RouteNodeInstance initialNode = null;
131 List initialNodes = KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getDocumentId());
132 if (!initialNodes.isEmpty()) {
133 initialNode = (RouteNodeInstance)initialNodes.get(0);
134 }
135
136 ActionRequestFactory arFactory = new ActionRequestFactory(getRouteHeader(), initialNode);
137 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 this.getActionRequestService().activateRequest(saveRequest);
142 return saveRequest;
143 }
144
145 }