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.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import edu.iu.uis.eden.EdenConstants;
024: import edu.iu.uis.eden.KEWServiceLocator;
025: import edu.iu.uis.eden.actionrequests.ActionRequestFactory;
026: import edu.iu.uis.eden.actionrequests.ActionRequestValue;
027: import edu.iu.uis.eden.engine.node.RouteNodeInstance;
028: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
029: import edu.iu.uis.eden.exception.InvalidActionTakenException;
030: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
031: import edu.iu.uis.eden.user.Recipient;
032: import edu.iu.uis.eden.user.WorkflowUser;
033: import edu.iu.uis.eden.util.Utilities;
034:
035: /**
036: * Responsible for creating adhoc requests that are requested from the client.
037: *
038: * @author rkirkend
039: */
040: public class AdHocAction extends ActionTakenEvent {
041:
042: private String actionRequested;
043: private String nodeName;
044: private String responsibilityDesc;
045: private Boolean ignorePrevious;
046: private Recipient recipient;
047: private String annotation;
048:
049: public AdHocAction(DocumentRouteHeaderValue routeHeader,
050: WorkflowUser user) {
051: super (routeHeader, user);
052: }
053:
054: public AdHocAction(DocumentRouteHeaderValue routeHeader,
055: WorkflowUser user, String annotation,
056: String actionRequested, String nodeName,
057: Recipient recipient, String responsibilityDesc,
058: Boolean ignorePrevActions) {
059: super (routeHeader, user, annotation);
060: this .actionRequested = actionRequested;
061: this .nodeName = nodeName;
062: this .responsibilityDesc = responsibilityDesc;
063: this .ignorePrevious = ignorePrevActions;
064: this .recipient = recipient;
065: this .annotation = annotation;
066: }
067:
068: public void recordAction() throws InvalidActionTakenException,
069: EdenUserNotFoundException {
070: List targetNodes = KEWServiceLocator.getRouteNodeService()
071: .getCurrentNodeInstances(getRouteHeaderId());
072: String error = adhocRouteAction(targetNodes, false);
073: if (!Utilities.isEmpty(error)) {
074: throw new InvalidActionTakenException(error);
075: }
076: }
077:
078: /* (non-Javadoc)
079: * @see edu.iu.uis.eden.actions.ActionTakenEvent#validateActionRules()
080: */
081: @Override
082: public String validateActionRules()
083: throws EdenUserNotFoundException {
084: List targetNodes = KEWServiceLocator.getRouteNodeService()
085: .getCurrentNodeInstances(getRouteHeaderId());
086: return validateActionRules(targetNodes);
087: }
088:
089: private String validateActionRules(List targetNodes)
090: throws EdenUserNotFoundException {
091: return adhocRouteAction(targetNodes, true);
092: }
093:
094: private String adhocRouteAction(List targetNodes,
095: boolean forValidationOnly) throws EdenUserNotFoundException {
096: if (targetNodes.isEmpty()) {
097: return "Could not locate an node instance on the document with the name '"
098: + nodeName + "'";
099: }
100: boolean requestCreated = false;
101: for (Iterator iter = targetNodes.iterator(); iter.hasNext();) {
102: RouteNodeInstance routeNode = (RouteNodeInstance) iter
103: .next();
104: // if the node name is null, then adhoc it to the first available node
105: if (nodeName == null
106: || routeNode.getName().equals(nodeName)) {
107: ActionRequestValue adhocRequest = new ActionRequestValue();
108: if (!forValidationOnly) {
109: ActionRequestFactory arFactory = new ActionRequestFactory(
110: routeHeader, routeNode);
111: adhocRequest = arFactory.createActionRequest(
112: actionRequested, recipient,
113: responsibilityDesc, ignorePrevious,
114: annotation);
115: adhocRequest
116: .setResponsibilityId(EdenConstants.ADHOC_REQUEST_RESPONSIBILITY_ID);
117: } else {
118: adhocRequest.setActionRequested(actionRequested);
119: }
120: if (adhocRequest.isApproveOrCompleteRequest()
121: && !(routeHeader.isEnroute()
122: || routeHeader.isStateInitiated() || routeHeader
123: .isStateSaved())) {
124: return "Cannot AdHoc a Complete or Approve request when document is in state '"
125: + routeHeader.getDocRouteStatusLabel()
126: + "'.";
127: }
128: if (!forValidationOnly) {
129: if (routeHeader.isDisaproved()
130: || routeHeader.isFinal()
131: || routeHeader.isProcessed()) {
132: getActionRequestService().activateRequest(
133: adhocRequest);
134: } else {
135: KEWServiceLocator.getActionRequestService()
136: .saveActionRequest(adhocRequest);
137: }
138: }
139: requestCreated = true;
140: if (nodeName == null) {
141: break;
142: }
143: }
144: }
145: if (!requestCreated) {
146: return "Didn't create request. The node name " + nodeName
147: + " given is probably invalid ";
148: }
149: return "";
150: }
151: }
|