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