001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.actions;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.log4j.MDC;
023:
024: import edu.iu.uis.eden.EdenConstants;
025: import edu.iu.uis.eden.actionrequests.ActionRequestValue;
026: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
027: import edu.iu.uis.eden.exception.InvalidActionTakenException;
028: import edu.iu.uis.eden.exception.WorkflowException;
029: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
030: import edu.iu.uis.eden.user.WorkflowUser;
031: import edu.iu.uis.eden.util.Utilities;
032:
033: /**
034: * Cancels a document at the request of a client app.
035: *
036: * @author rkirkend
037: * @author ewestfal
038: * @author seiffert
039: */
040: public class CancelAction extends ActionTakenEvent {
041:
042: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
043: .getLogger(CancelAction.class);
044:
045: public CancelAction(DocumentRouteHeaderValue rh, WorkflowUser user) {
046: super (rh, user);
047: setActionTakenCode(EdenConstants.ACTION_TAKEN_CANCELED_CD);
048: }
049:
050: public CancelAction(DocumentRouteHeaderValue rh, WorkflowUser user,
051: String annotation) {
052: super (rh, user, annotation);
053: setActionTakenCode(EdenConstants.ACTION_TAKEN_CANCELED_CD);
054: }
055:
056: @Override
057: protected boolean requireInitiatorCheck() {
058: return routeHeader.getDocumentType()
059: .getInitiatorMustCancelPolicy().getPolicyValue()
060: .booleanValue();
061: }
062:
063: /* (non-Javadoc)
064: * @see edu.iu.uis.eden.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
065: */
066: @Override
067: public String validateActionRules()
068: throws EdenUserNotFoundException {
069: return validateActionRules(getActionRequestService()
070: .findAllValidRequests(getUser(),
071: routeHeader.getRouteHeaderId(),
072: EdenConstants.ACTION_REQUEST_COMPLETE_REQ));
073: }
074:
075: private String validateActionRules(List actionRequests)
076: throws EdenUserNotFoundException {
077: String super Error = super .validateActionTakenRules();
078: if (!Utilities.isEmpty(super Error)) {
079: return super Error;
080: }
081: // FYI delyea: This is new validation check... was not being checked previously
082: if (!getRouteHeader().isValidActionToTake(
083: getActionPerformedCode())) {
084: return "Document is not in a state to be cancelled";
085: }
086: if (!isActionCompatibleRequest(actionRequests)) {
087: return "No request for the user is compatible with the Cancel Action";
088: }
089: return "";
090: }
091:
092: /* (non-Javadoc)
093: * @see edu.iu.uis.eden.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
094: */
095: @Override
096: public boolean isActionCompatibleRequest(List requests)
097: throws EdenUserNotFoundException {
098:
099: // can always cancel saved or initiated document
100: if (routeHeader.isStateInitiated()
101: || routeHeader.isStateSaved()) {
102: return true;
103: }
104:
105: boolean actionCompatible = false;
106: Iterator ars = requests.iterator();
107: ActionRequestValue actionRequest = null;
108:
109: while (ars.hasNext()) {
110: actionRequest = (ActionRequestValue) ars.next();
111: String request = actionRequest.getActionRequested();
112:
113: // APPROVE and COMPLETE request matches CANCEL Taken code
114: if ((EdenConstants.ACTION_REQUEST_APPROVE_REQ
115: .equals(request))
116: || (EdenConstants.ACTION_REQUEST_COMPLETE_REQ
117: .equals(request))) {
118: actionCompatible = true;
119: break;
120: }
121: }
122:
123: return actionCompatible;
124: }
125:
126: public void recordAction() throws InvalidActionTakenException,
127: EdenUserNotFoundException {
128: MDC.put("docId", getRouteHeader().getRouteHeaderId());
129: checkLocking();
130: updateSearchableAttributesIfPossible();
131:
132: LOG.debug("Canceling document : " + annotation);
133:
134: List actionRequests = getActionRequestService()
135: .findAllValidRequests(getUser(), getRouteHeaderId(),
136: EdenConstants.ACTION_REQUEST_COMPLETE_REQ);
137: LOG.debug("Checking to see if the action is legal");
138: String errorMessage = validateActionRules(actionRequests);
139: if (!Utilities.isEmpty(errorMessage)) {
140: throw new InvalidActionTakenException(errorMessage);
141: }
142:
143: // List actionRequests = getActionRequestService().findAllValidRequests(getUser(), getRouteHeaderId(), EdenConstants.ACTION_REQUEST_COMPLETE_REQ);
144: //
145: // LOG.debug("Checking to see if the action is legal");
146: // if (!isActionCompatibleRequest(actionRequests, getActionTakenCode())) {
147: // throw new InvalidActionTakenException("No request for the user is compatible with the DISAPPROVE or DENY action");
148: // }
149:
150: LOG.debug("Record the cancel action");
151: saveActionTaken(findDelegatorForActionRequests(actionRequests));
152:
153: LOG.debug("Deactivate all pending action requests");
154: actionRequests = getActionRequestService().findPendingByDoc(
155: getRouteHeaderId());
156:
157: getActionRequestService().deactivateRequests(actionTaken,
158: actionRequests);
159: notifyActionTaken(this .actionTaken);
160:
161: LOG.debug("Canceling document");
162: try {
163: String oldStatus = getRouteHeader().getDocRouteStatus();
164: getRouteHeader().markDocumentCanceled();
165: String newStatus = getRouteHeader().getDocRouteStatus();
166: getRouteHeaderService().saveRouteHeader(getRouteHeader());
167: notifyStatusChange(newStatus, oldStatus);
168: } catch (WorkflowException ex) {
169: LOG.warn(ex, ex);
170: throw new InvalidActionTakenException(ex.getMessage());
171: }
172: }
173: }
|