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