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.ResourceUnavailableException;
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: * CompleteAction records and process a complete action
035: *
036: * The routeheader is first checked to make sure the action is valid for the document.
037: * Next the user is checked to make sure he/she has not taken a previous action on this
038: * document at the actions responsibility or below. The action is recorded. Any requests
039: * related to this user are deactivated.
040: *
041: * @author rkirkend
042: * @author ewestfal
043: * @author seiffert
044: */
045: public class CompleteAction extends ActionTakenEvent {
046: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(CompleteAction.class);
048:
049: /**
050: * @param rh
051: * RouteHeader for the document upon which the action is taken.
052: * @param user
053: * User taking the action.
054: */
055: public CompleteAction(DocumentRouteHeaderValue rh, WorkflowUser user) {
056: super (rh, user);
057: setActionTakenCode(EdenConstants.ACTION_TAKEN_COMPLETED_CD);
058: }
059:
060: /**
061: * @param rh
062: * RouteHeader for the document upon which the action is taken.
063: * @param user
064: * User taking the action.
065: * @param annotation
066: * User comment on the action taken
067: */
068: public CompleteAction(DocumentRouteHeaderValue rh,
069: WorkflowUser user, String annotation) {
070: super (rh, user, annotation);
071: setActionTakenCode(EdenConstants.ACTION_TAKEN_COMPLETED_CD);
072: }
073:
074: /* (non-Javadoc)
075: * @see edu.iu.uis.eden.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
076: */
077: @Override
078: public String validateActionRules()
079: throws EdenUserNotFoundException {
080: return validateActionRules(getActionRequestService()
081: .findAllValidRequests(getUser(),
082: routeHeader.getRouteHeaderId(),
083: EdenConstants.ACTION_REQUEST_COMPLETE_REQ));
084: }
085:
086: private String validateActionRules(List actionRequests)
087: throws EdenUserNotFoundException {
088: String super Error = super .validateActionTakenRules();
089: if (!Utilities.isEmpty(super Error)) {
090: return super Error;
091: }
092: if (!getRouteHeader().isValidActionToTake(
093: getActionPerformedCode())) {
094: return "Document is not in a state to be completed";
095: }
096: if (!isActionCompatibleRequest(actionRequests)) {
097: return "No request for the user is compatible "
098: + "with the COMPLETE action";
099: }
100: return "";
101: }
102:
103: /* (non-Javadoc)
104: * @see edu.iu.uis.eden.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
105: */
106: @Override
107: public boolean isActionCompatibleRequest(List requests)
108: throws EdenUserNotFoundException {
109: // we allow pre-approval
110: if (requests.isEmpty()) {
111: return true;
112: }
113:
114: // can always cancel saved or initiated document
115: if (routeHeader.isStateInitiated()
116: || routeHeader.isStateSaved()) {
117: return true;
118: }
119:
120: boolean actionCompatible = false;
121: Iterator ars = requests.iterator();
122: ActionRequestValue actionRequest = null;
123:
124: while (ars.hasNext()) {
125: actionRequest = (ActionRequestValue) ars.next();
126: String request = actionRequest.getActionRequested();
127:
128: // Complete action matches Complete, Approve, FYI, and ACK requests
129: if ((EdenConstants.ACTION_REQUEST_FYI_REQ.equals(request))
130: || (EdenConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ
131: .equals(request))
132: || (EdenConstants.ACTION_REQUEST_APPROVE_REQ
133: .equals(request))
134: || (EdenConstants.ACTION_REQUEST_COMPLETE_REQ
135: .equals(request))) {
136: actionCompatible = true;
137: break;
138: }
139: }
140: return actionCompatible;
141: }
142:
143: /**
144: * Records the complete action. - Checks to make sure the document status allows the action. - Checks that the user has not taken a previous action. - Deactivates the pending requests for this user - Records the action
145: *
146: * @throws InvalidActionTakenException
147: * @throws ResourceUnavailableException
148: */
149: public void recordAction()
150: throws edu.iu.uis.eden.exception.InvalidActionTakenException,
151: EdenUserNotFoundException {
152: MDC.put("docId", getRouteHeader().getRouteHeaderId());
153: checkLocking();
154: updateSearchableAttributesIfPossible();
155: LOG.debug("Completing document : " + annotation);
156:
157: List actionRequests = getActionRequestService()
158: .findAllValidRequests(getUser(), getRouteHeaderId(),
159: EdenConstants.ACTION_REQUEST_COMPLETE_REQ);
160: LOG.debug("Checking to see if the action is legal");
161: String errorMessage = validateActionRules(actionRequests);
162: if (!Utilities.isEmpty(errorMessage)) {
163: throw new InvalidActionTakenException(errorMessage);
164: }
165:
166: // LOG.debug("Checking to see if the action is legal");
167: //
168: // if (! getRouteHeader().isValidActionToTake(getActionTakenCode())) {
169: // LOG.warn("Document not in state to be completed.");
170: // throw new InvalidActionTakenException("Document is not in a state to be completed");
171: // }
172: //
173: // List actionRequests = getActionRequestService().findAllValidRequests(getUser(), getRouteHeaderId(), EdenConstants.ACTION_REQUEST_COMPLETE_REQ);
174: // //getActionRequestService().findAllPendingByUserAndDoc(getDelegator(), getRouteHeaderId());
175:
176: LOG.debug("Record the complete action");
177: saveActionTaken(findDelegatorForActionRequests(actionRequests));
178:
179: // if (!isActionCompatibleRequest(actionRequests, getActionTakenCode())) {
180: // throw new InvalidActionTakenException("No request for the user is compatible " + "with the DISAPPROVE or DENY action");
181: // }
182:
183: LOG.debug("Deactivate all pending action requests");
184: getActionRequestService().deactivateRequests(actionTaken,
185: actionRequests);
186: notifyActionTaken(this .actionTaken);
187:
188: boolean isException = getRouteHeader().isInException();
189: boolean isSaved = getRouteHeader().isStateSaved();
190: if (isException || isSaved) {
191: String oldStatus = getRouteHeader().getDocRouteStatus();
192: LOG.debug("Moving document back to Enroute from "
193: + EdenConstants.DOCUMENT_STATUSES.get(oldStatus));
194: getRouteHeader().markDocumentEnroute();
195: String newStatus = getRouteHeader().getDocRouteStatus();
196: notifyStatusChange(newStatus, oldStatus);
197: this.getRouteHeaderService().saveRouteHeader(
198: getRouteHeader());
199: }
200: }
201:
202: }
|