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