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.edl.components;
018:
019: import org.apache.log4j.Logger;
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022:
023: import edu.iu.uis.eden.clientapp.WorkflowDocument;
024: import edu.iu.uis.eden.clientapp.vo.NetworkIdVO;
025: import edu.iu.uis.eden.edl.EDLContext;
026: import edu.iu.uis.eden.edl.EDLModelComponent;
027: import edu.iu.uis.eden.edl.EDLXmlUtils;
028: import edu.iu.uis.eden.edl.RequestParser;
029: import edu.iu.uis.eden.exception.WorkflowException;
030: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
031: import edu.iu.uis.eden.util.XmlHelper;
032:
033: /**
034: * Used as a pre processor and post processor.
035: * As a pre processor this creates/fetches the workflow document and sets it on request.
036: * As a post processor this takes appropriate user action on the document if the document is not in error.
037: *
038: * @author rkirkend
039: * @author ahamid
040: *
041: */
042: public class WorkflowDocumentActions implements EDLModelComponent {
043:
044: private static final Logger LOG = Logger
045: .getLogger(WorkflowDocumentActions.class);
046:
047: public static final String USER_ACTION_REQUEST_KEY = "userAction";
048: public static final String ACTION_CREATE = "initiate";
049: public static final String RETRIEVE = "retrieve";
050: public static final String ACTION_ROUTE = "route";
051: public static final String ACTION_APPROVE = "approve";
052: public static final String ACTION_DISAPPROVE = "disapprove";
053: public static final String ACTION_CANCEL = "cancel";
054: public static final String ACTION_BLANKETAPPROVE = "blanketApprove";
055: public static final String ACTION_FYI = "fyi";
056: public static final String ACTION_ACKNOWLEDGE = "acknowledge";
057: public static final String ACTION_SAVE = "save";
058: public static final String ACTION_COMPLETE = "complete";
059: public static final String ACTION_DELETE = "delete";
060: public static final String ACTION_RETURN_TO_PREVIOUS = "returnToPrevious";
061:
062: boolean isPreProcessor;
063:
064: public void updateDOM(Document dom, Element configElement,
065: EDLContext edlContext) {
066:
067: try {
068: isPreProcessor = configElement.getTagName().equals(
069: "preProcessor");
070: if (isPreProcessor) {
071: doPreProcessWork(edlContext);
072: } else {
073: doPostProcessWork(dom, edlContext);
074: }
075: } catch (Exception e) {
076: throw new WorkflowRuntimeException(e);
077: }
078:
079: }
080:
081: private void doPreProcessWork(EDLContext edlContext)
082: throws Exception {
083: RequestParser requestParser = edlContext.getRequestParser();
084: String userAction = requestParser
085: .getParameterValue(USER_ACTION_REQUEST_KEY);
086: if (userAction == null) {
087: userAction = requestParser.getParameterValue("command");//'WorkflowQuicklinks'
088: }
089:
090: WorkflowDocument document = null;
091: if (ACTION_CREATE.equals(userAction)) {
092: document = new WorkflowDocument(new NetworkIdVO(edlContext
093: .getUserSession().getNetworkId()), edlContext
094: .getEdocLiteAssociation().getEdlName());
095: document.setTitle("Routing Document Type '"
096: + document.getDocumentType() + "'");
097: document.getRouteHeaderId();
098: LOG.info("Created document " + document.getRouteHeaderId());
099: } else {
100: document = (WorkflowDocument) requestParser
101: .getAttribute(RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
102: if (document == null) {
103: String docId = requestParser.getParameterValue("docId");
104: if (docId == null) {
105: LOG.info("no document found for edl "
106: + edlContext.getEdocLiteAssociation()
107: .getEdlName());
108: return;
109: } else {
110: document = new WorkflowDocument(
111: new NetworkIdVO(edlContext.getUserSession()
112: .getNetworkId()), new Long(docId));
113: }
114: }
115: }
116:
117: requestParser.setAttribute(
118: RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY, document);
119: }
120:
121: private void doPostProcessWork(Document dom, EDLContext edlContext)
122: throws Exception {
123: RequestParser requestParser = edlContext.getRequestParser();
124: // if the document is in error then we don't want to execute the action!
125: if (edlContext.isInError()) {
126: return;
127: }
128: WorkflowDocument document = (WorkflowDocument) edlContext
129: .getRequestParser().getAttribute(
130: RequestParser.WORKFLOW_DOCUMENT_SESSION_KEY);
131: if (document == null) {
132: return;
133: }
134: //strip out the data element
135: Element dataElement = (Element) dom.getElementsByTagName(
136: EDLXmlUtils.DATA_E).item(0);
137: String docContent = XmlHelper.writeNode(dataElement);//use the transformer on edlcontext
138: document.setApplicationContent(docContent);
139: takeAction(document, requestParser);
140: }
141:
142: public static void takeAction(WorkflowDocument document,
143: RequestParser requestParser) throws WorkflowException {
144:
145: String annotation = requestParser
146: .getParameterValue("annotation");
147: String action = requestParser
148: .getParameterValue(USER_ACTION_REQUEST_KEY);
149: String nodeName = requestParser
150: .getParameterValue("previousNode");
151:
152: if (ACTION_ROUTE.equals(action)) {
153: document.routeDocument(annotation);
154: } else if (ACTION_APPROVE.equals(action)) {
155: document.approve(annotation);
156: } else if (ACTION_DISAPPROVE.equals(action)) {
157: document.disapprove(annotation);
158: } else if (ACTION_CANCEL.equals(action)) {
159: document.cancel(annotation);
160: } else if (ACTION_BLANKETAPPROVE.equals(action)) {
161: document.blanketApprove(annotation);
162: } else if (ACTION_FYI.equals(action)) {
163: document.fyi();
164: } else if (ACTION_ACKNOWLEDGE.equals(action)) {
165: document.acknowledge(annotation);
166: } else if (ACTION_SAVE.equals(action)) {
167: document.saveDocument(annotation);
168: } else if (ACTION_COMPLETE.equals(action)) {
169: document.complete(annotation);
170: } else if (ACTION_DELETE.equals(action)) {
171: document.delete();
172: } else if (ACTION_RETURN_TO_PREVIOUS.equals(action)) {
173: document.returnToPreviousNode(annotation, nodeName);
174: }
175: }
176:
177: }
|