0001: /**
0002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003: *
0004: * Permission is hereby granted, free of charge, to any person obtaining a copy
0005: * of this software and associated documentation files (the "Software"), to deal
0006: * in the Software without restriction, including without limitation the rights
0007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008: * copies of the Software, and to permit persons to whom the Software is
0009: * furnished to do so, subject to the following conditions:
0010: *
0011: * The above copyright notice and this permission notice shall be included in
0012: * all copies or substantial portions of the Software.
0013: *
0014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020: * SOFTWARE.
0021: */package com.liferay.jbpm;
0022:
0023: import com.liferay.jbpm.db.GraphSession;
0024: import com.liferay.jbpm.util.TaskFormElement;
0025: import com.liferay.jbpm.util.WorkflowUtil;
0026: import com.liferay.portal.kernel.jbi.WorkflowComponent;
0027: import com.liferay.portal.kernel.jbi.WorkflowComponentException;
0028: import com.liferay.portal.kernel.log.Log;
0029: import com.liferay.portal.kernel.log.LogFactoryUtil;
0030: import com.liferay.portal.kernel.util.GetterUtil;
0031: import com.liferay.portal.kernel.util.ParamUtil;
0032: import com.liferay.portal.kernel.util.StringMaker;
0033: import com.liferay.portal.kernel.util.StringPool;
0034: import com.liferay.portal.kernel.util.StringUtil;
0035: import com.liferay.portal.kernel.util.Validator;
0036: import com.liferay.util.JS;
0037: import com.liferay.util.xml.DocUtil;
0038:
0039: import java.text.ParseException;
0040: import java.text.SimpleDateFormat;
0041:
0042: import java.util.ArrayList;
0043: import java.util.Date;
0044: import java.util.HashMap;
0045: import java.util.HashSet;
0046: import java.util.Iterator;
0047: import java.util.List;
0048: import java.util.Map;
0049: import java.util.Set;
0050: import java.util.TimeZone;
0051:
0052: import javax.servlet.http.HttpServletRequest;
0053:
0054: import org.dom4j.Document;
0055: import org.dom4j.DocumentHelper;
0056: import org.dom4j.Element;
0057:
0058: import org.jbpm.JbpmConfiguration;
0059: import org.jbpm.JbpmContext;
0060: import org.jbpm.context.def.VariableAccess;
0061: import org.jbpm.db.TaskMgmtSession;
0062: import org.jbpm.graph.def.ProcessDefinition;
0063: import org.jbpm.graph.def.Transition;
0064: import org.jbpm.graph.exe.ProcessInstance;
0065: import org.jbpm.graph.exe.Token;
0066: import org.jbpm.taskmgmt.exe.TaskInstance;
0067:
0068: /**
0069: * <a href="WorkflowComponentImpl.java.html"><b><i>View Source</i></b></a>
0070: *
0071: * @author Charles May
0072: *
0073: */
0074: public class WorkflowComponentImpl implements WorkflowComponent {
0075:
0076: public String process(HttpServletRequest req) {
0077: String result = null;
0078:
0079: try {
0080: userId = ParamUtil.getString(req, "userId");
0081: timeZoneId = ParamUtil.getString(req, "timeZoneId");
0082: timeZone = TimeZone.getTimeZone(timeZoneId);
0083: jbpmContext = JbpmConfiguration.getInstance()
0084: .createJbpmContext();
0085: graphSession = new GraphSession(userId, timeZoneId,
0086: jbpmContext);
0087: taskMgmtSession = jbpmContext.getTaskMgmtSession();
0088:
0089: String cmd = ParamUtil.getString(req, "cmd");
0090:
0091: if (cmd.equals("deploy")) {
0092: String xml = ParamUtil.getString(req, "xml");
0093:
0094: result = deploy(xml);
0095: } else if (cmd.equals("getCurrentTasksXml")) {
0096: long instanceId = ParamUtil.getLong(req, "instanceId");
0097: long tokenId = ParamUtil.getLong(req, "tokenId");
0098:
0099: result = getCurrentTasksXml(instanceId, tokenId);
0100: } else if (cmd.equals("getDefinitionsCountXml")) {
0101: long definitionId = ParamUtil.getLong(req,
0102: "definitionId");
0103: String name = ParamUtil.getString(req, "name");
0104:
0105: result = getDefinitionsCountXml(definitionId, name);
0106: } else if (cmd.equals("getDefinitionsXml")) {
0107: long definitionId = ParamUtil.getLong(req,
0108: "definitionId");
0109: String name = ParamUtil.getString(req, "name");
0110: int begin = ParamUtil.getInteger(req, "begin");
0111: int end = ParamUtil.getInteger(req, "end");
0112:
0113: result = getDefinitionsXml(definitionId, name, begin,
0114: end);
0115: } else if (cmd.equals("getDefinitionXml")) {
0116: long definitionId = ParamUtil.getLong(req,
0117: "definitionId");
0118:
0119: result = getDefinitionXml(definitionId);
0120: } else if (cmd.equals("getInstancesCountXml")) {
0121: long definitionId = ParamUtil.getLong(req,
0122: "definitionId");
0123: long instanceId = ParamUtil.getLong(req, "instanceId");
0124: String definitionName = ParamUtil.getString(req,
0125: "definitionName");
0126: String definitionVersion = ParamUtil.getString(req,
0127: "definitionVersion");
0128: String startDateGT = ParamUtil.getString(req,
0129: "startDateGT");
0130: String startDateLT = ParamUtil.getString(req,
0131: "startDateLT");
0132: String endDateGT = ParamUtil
0133: .getString(req, "endDateGT");
0134: String endDateLT = ParamUtil
0135: .getString(req, "endDateLT");
0136: boolean hideEndedTasks = ParamUtil.getBoolean(req,
0137: "hideEndedTasks");
0138: boolean retrieveUserInstances = ParamUtil.getBoolean(
0139: req, "retrieveUserInstances");
0140: boolean andOperator = ParamUtil.getBoolean(req,
0141: "andOperator");
0142:
0143: result = getInstancesCountXml(definitionId, instanceId,
0144: definitionName, definitionVersion, startDateGT,
0145: startDateLT, endDateGT, endDateLT,
0146: hideEndedTasks, retrieveUserInstances,
0147: andOperator);
0148: } else if (cmd.equals("getInstancesXml")) {
0149: long definitionId = ParamUtil.getLong(req,
0150: "definitionId");
0151: long instanceId = ParamUtil.getLong(req, "instanceId");
0152: String definitionName = ParamUtil.getString(req,
0153: "definitionName");
0154: String definitionVersion = ParamUtil.getString(req,
0155: "definitionVersion");
0156: String startDateGT = ParamUtil.getString(req,
0157: "startDateGT");
0158: String startDateLT = ParamUtil.getString(req,
0159: "startDateLT");
0160: String endDateGT = ParamUtil
0161: .getString(req, "endDateGT");
0162: String endDateLT = ParamUtil
0163: .getString(req, "endDateLT");
0164: boolean hideEndedTasks = ParamUtil.getBoolean(req,
0165: "hideEndedTasks");
0166: boolean retrieveUserInstances = ParamUtil.getBoolean(
0167: req, "retrieveUserInstances");
0168: boolean andOperator = ParamUtil.getBoolean(req,
0169: "andOperator");
0170: int begin = ParamUtil.getInteger(req, "begin");
0171: int end = ParamUtil.getInteger(req, "end");
0172:
0173: result = getInstancesXml(definitionId, instanceId,
0174: definitionName, definitionVersion, startDateGT,
0175: startDateLT, endDateGT, endDateLT,
0176: hideEndedTasks, retrieveUserInstances,
0177: andOperator, begin, end);
0178: } else if (cmd.equals("getTaskXml")) {
0179: long taskId = ParamUtil.getLong(req, "taskId");
0180:
0181: result = getTaskXml(taskId);
0182: } else if (cmd.equals("getTaskFormElementsXml")) {
0183: long taskId = ParamUtil.getLong(req, "taskId");
0184:
0185: result = getTaskFormElementsXml(taskId);
0186: } else if (cmd.equals("getTaskTransitionsXml")) {
0187: long taskId = ParamUtil.getLong(req, "taskId");
0188:
0189: result = getTaskTransitionsXml(taskId);
0190: } else if (cmd.equals("getUserTasksCountXml")) {
0191: long instanceId = ParamUtil.getLong(req, "instanceId");
0192: String taskName = ParamUtil.getString(req, "taskName");
0193: String definitionName = ParamUtil.getString(req,
0194: "definitionName");
0195: String assignedTo = ParamUtil.getString(req,
0196: "assignedTo");
0197: String createDateGT = ParamUtil.getString(req,
0198: "createDateGT");
0199: String createDateLT = ParamUtil.getString(req,
0200: "createDateLT");
0201: String startDateGT = ParamUtil.getString(req,
0202: "startDateGT");
0203: String startDateLT = ParamUtil.getString(req,
0204: "startDateLT");
0205: String endDateGT = ParamUtil
0206: .getString(req, "endDateGT");
0207: String endDateLT = ParamUtil
0208: .getString(req, "endDateLT");
0209: boolean hideEndedTasks = ParamUtil.getBoolean(req,
0210: "hideEndedTasks");
0211: boolean andOperator = ParamUtil.getBoolean(req,
0212: "andOperator");
0213:
0214: result = getUserTasksCountXml(instanceId, taskName,
0215: definitionName, assignedTo, createDateGT,
0216: createDateLT, startDateGT, startDateLT,
0217: endDateGT, endDateLT, hideEndedTasks,
0218: andOperator);
0219: } else if (cmd.equals("getUserTasksXml")) {
0220: long instanceId = ParamUtil.getLong(req, "instanceId");
0221: String taskName = ParamUtil.getString(req, "taskName");
0222: String definitionName = ParamUtil.getString(req,
0223: "definitionName");
0224: String assignedTo = ParamUtil.getString(req,
0225: "assignedTo");
0226: String createDateGT = ParamUtil.getString(req,
0227: "createDateGT");
0228: String createDateLT = ParamUtil.getString(req,
0229: "createDateLT");
0230: String startDateGT = ParamUtil.getString(req,
0231: "startDateGT");
0232: String startDateLT = ParamUtil.getString(req,
0233: "startDateLT");
0234: String endDateGT = ParamUtil
0235: .getString(req, "endDateGT");
0236: String endDateLT = ParamUtil
0237: .getString(req, "endDateLT");
0238: boolean hideEndedTasks = ParamUtil.getBoolean(req,
0239: "hideEndedTasks");
0240: boolean andOperator = ParamUtil.getBoolean(req,
0241: "andOperator");
0242: int begin = ParamUtil.getInteger(req, "begin");
0243: int end = ParamUtil.getInteger(req, "end");
0244:
0245: result = getUserTasksXml(instanceId, taskName,
0246: definitionName, assignedTo, createDateGT,
0247: createDateLT, startDateGT, startDateLT,
0248: endDateGT, endDateLT, hideEndedTasks,
0249: andOperator, begin, end);
0250: } else if (cmd.equals("signalInstance")) {
0251: long instanceId = ParamUtil.getLong(req, "instanceId");
0252:
0253: signalInstance(instanceId);
0254: } else if (cmd.equals("signalToken")) {
0255: long instanceId = ParamUtil.getLong(req, "instanceId");
0256: long tokenId = ParamUtil.getLong(req, "tokenId");
0257:
0258: signalToken(instanceId, tokenId);
0259: } else if (cmd.equals("startWorkflow")) {
0260: long definitionId = ParamUtil.getLong(req,
0261: "definitionId");
0262:
0263: result = startWorkflow(definitionId);
0264: } else if (cmd.equals("updateTaskXml")) {
0265: long taskId = ParamUtil.getLong(req, "taskId");
0266: String transition = ParamUtil.getString(req,
0267: "transition");
0268:
0269: result = updateTaskXml(taskId, transition, req
0270: .getParameterMap());
0271: }
0272: } catch (Exception e) {
0273: _log.error(e, e);
0274: } finally {
0275: try {
0276: jbpmContext.close();
0277: } catch (Exception e) {
0278: }
0279:
0280: try {
0281: graphSession.close();
0282: } catch (Exception e) {
0283: }
0284: }
0285:
0286: if (Validator.isNull(result)) {
0287: result = "<result />";
0288: }
0289:
0290: return result;
0291: }
0292:
0293: public String deploy(String xml) throws WorkflowComponentException {
0294: ProcessDefinition definition = ProcessDefinition
0295: .parseXmlString(xml);
0296:
0297: jbpmContext.deployProcessDefinition(definition);
0298:
0299: StringMaker sm = new StringMaker();
0300:
0301: sm.append("<result>");
0302: sm.append("<definitionId>");
0303: sm.append(definition.getId());
0304: sm.append("</definitionId>");
0305: sm.append("</result>");
0306:
0307: return sm.toString();
0308: }
0309:
0310: public List getCurrentTasks(long instanceId, long tokenId)
0311: throws WorkflowComponentException {
0312:
0313: List userTasks = getUserTasks(instanceId, null, null, null,
0314: null, null, null, null, null, null, false, false, 0, 0);
0315:
0316: List currentTasks = new ArrayList();
0317:
0318: List tokenTasks = taskMgmtSession
0319: .findTaskInstancesByToken(tokenId);
0320:
0321: if (tokenTasks.size() == 0) {
0322: currentTasks = null;
0323: } else {
0324: Iterator itr = tokenTasks.iterator();
0325:
0326: Set tokenTaskIds = new HashSet();
0327:
0328: while (itr.hasNext()) {
0329: TaskInstance task = (TaskInstance) itr.next();
0330:
0331: tokenTaskIds.add(new Long(task.getId()));
0332: }
0333:
0334: itr = userTasks.iterator();
0335:
0336: while (itr.hasNext()) {
0337: TaskInstance task = (TaskInstance) itr.next();
0338:
0339: if (tokenTaskIds.contains(new Long(task.getId()))) {
0340: currentTasks.add(task);
0341: }
0342: }
0343: }
0344:
0345: // At this point, if currentTasks is null, then that means no tasks
0346: // exist for this token, and the instance must be signaled to continue.
0347: // If currentTasks is just empty, then that means the current token
0348: // has tasks, but the current user cannot manage them.
0349:
0350: return currentTasks;
0351: }
0352:
0353: public String getCurrentTasksXml(long instanceId, long tokenId)
0354: throws WorkflowComponentException {
0355:
0356: List tasks = getCurrentTasks(instanceId, tokenId);
0357:
0358: Document doc = DocumentHelper.createDocument();
0359:
0360: Element root = doc.addElement("result");
0361:
0362: if (tasks != null) {
0363: for (int i = 0; i < tasks.size(); i++) {
0364: TaskInstance task = (TaskInstance) tasks.get(i);
0365:
0366: createElement(task, root);
0367: }
0368: }
0369:
0370: return doc.asXML();
0371: }
0372:
0373: public Object getDefinition(long definitionId)
0374: throws WorkflowComponentException {
0375:
0376: ProcessDefinition definition = graphSession
0377: .loadProcessDefinition(definitionId);
0378:
0379: WorkflowUtil.initDefinition(definition);
0380:
0381: return definition;
0382: }
0383:
0384: public List getDefinitions(long definitionId, String name,
0385: int begin, int end) throws WorkflowComponentException {
0386:
0387: List definitions = new ArrayList();
0388:
0389: if (definitionId > 0) {
0390: ProcessDefinition definition = (ProcessDefinition) getDefinition(definitionId);
0391:
0392: definitions.add(definition);
0393: } else {
0394: definitions = graphSession.findProcessDefinitionsByName(
0395: name, begin, end);
0396: }
0397:
0398: return definitions;
0399: }
0400:
0401: public String getDefinitionsXml(long definitionId, String name,
0402: int begin, int end) throws WorkflowComponentException {
0403:
0404: List definitions = getDefinitions(definitionId, name, begin,
0405: end);
0406:
0407: Document doc = DocumentHelper.createDocument();
0408:
0409: Element root = doc.addElement("result");
0410:
0411: for (int i = 0; i < definitions.size(); i++) {
0412: ProcessDefinition definition = (ProcessDefinition) definitions
0413: .get(i);
0414:
0415: createElement(definition, root);
0416: }
0417:
0418: return doc.asXML();
0419: }
0420:
0421: public int getDefinitionsCount(long definitionId, String name)
0422: throws WorkflowComponentException {
0423:
0424: int count = 0;
0425:
0426: if (definitionId > 0) {
0427: count = 1;
0428: } else {
0429: count = graphSession.countProcessDefinitionsByName(name);
0430: }
0431:
0432: return count;
0433: }
0434:
0435: public String getDefinitionsCountXml(long definitionId, String name)
0436: throws WorkflowComponentException {
0437:
0438: int count = getDefinitionsCount(definitionId, name);
0439:
0440: return getCountXml(count);
0441: }
0442:
0443: public String getDefinitionXml(long definitionId)
0444: throws WorkflowComponentException {
0445:
0446: ProcessDefinition definition = (ProcessDefinition) getDefinition(definitionId);
0447:
0448: Document doc = DocumentHelper.createDocument();
0449:
0450: Element root = doc.addElement("result");
0451:
0452: createElement(definition, root);
0453:
0454: return doc.asXML();
0455: }
0456:
0457: public List getInstances(long definitionId, long instanceId,
0458: String definitionName, String definitionVersion,
0459: String startDateGT, String startDateLT, String endDateGT,
0460: String endDateLT, boolean hideEndedTasks,
0461: boolean retrieveUserInstances, boolean andOperator,
0462: int begin, int end) throws WorkflowComponentException {
0463:
0464: List instances = new ArrayList();
0465:
0466: if (definitionId > 0) {
0467: ProcessDefinition definition = graphSession
0468: .loadProcessDefinition(definitionId);
0469:
0470: instances = graphSession.findProcessInstances(definition
0471: .getId());
0472:
0473: WorkflowUtil.initInstances(instances);
0474: } else if (instanceId > 0) {
0475: ProcessInstance instance = graphSession
0476: .loadProcessInstance(instanceId);
0477:
0478: WorkflowUtil.initInstance(instance);
0479:
0480: instances.add(instance);
0481: } else {
0482: String assignedUserId = null;
0483:
0484: if (retrieveUserInstances) {
0485: assignedUserId = userId;
0486: }
0487:
0488: instances = graphSession.findProcessInstancesBySearchTerms(
0489: definitionName, definitionVersion, startDateGT,
0490: startDateLT, endDateGT, endDateLT, hideEndedTasks,
0491: assignedUserId, andOperator, begin, end);
0492: }
0493:
0494: return instances;
0495: }
0496:
0497: public int getInstancesCount(long definitionId, long instanceId,
0498: String definitionName, String definitionVersion,
0499: String startDateGT, String startDateLT, String endDateGT,
0500: String endDateLT, boolean hideEndedTasks,
0501: boolean retrieveUserInstances, boolean andOperator)
0502: throws WorkflowComponentException {
0503:
0504: if (definitionId > 0) {
0505: return 1;
0506: } else if (instanceId > 0) {
0507: return 1;
0508: } else {
0509: String assignedUserId = null;
0510:
0511: if (retrieveUserInstances) {
0512: assignedUserId = userId;
0513: }
0514:
0515: return graphSession.countProcessInstancesBySearchTerms(
0516: definitionName, definitionVersion, startDateGT,
0517: startDateLT, endDateGT, endDateLT, hideEndedTasks,
0518: assignedUserId, andOperator);
0519: }
0520: }
0521:
0522: public String getInstancesCountXml(long definitionId,
0523: long instanceId, String definitionName,
0524: String definitionVersion, String startDateGT,
0525: String startDateLT, String endDateGT, String endDateLT,
0526: boolean hideEndedTasks, boolean retrieveUserInstances,
0527: boolean andOperator) throws WorkflowComponentException {
0528:
0529: int count = getInstancesCount(definitionId, instanceId,
0530: definitionName, definitionVersion, startDateGT,
0531: startDateLT, endDateGT, endDateLT, hideEndedTasks,
0532: retrieveUserInstances, andOperator);
0533:
0534: return getCountXml(count);
0535: }
0536:
0537: public String getInstancesXml(long definitionId, long instanceId,
0538: String definitionName, String definitionVersion,
0539: String startDateGT, String startDateLT, String endDateGT,
0540: String endDateLT, boolean hideEndedTasks,
0541: boolean retrieveUserInstances, boolean andOperator,
0542: int begin, int end) throws WorkflowComponentException {
0543:
0544: List instances = getInstances(definitionId, instanceId,
0545: definitionName, definitionVersion, startDateGT,
0546: startDateLT, endDateGT, endDateLT, hideEndedTasks,
0547: retrieveUserInstances, andOperator, begin, end);
0548:
0549: Document doc = DocumentHelper.createDocument();
0550:
0551: Element root = doc.addElement("result");
0552:
0553: for (int i = 0; i < instances.size(); i++) {
0554: ProcessInstance instance = (ProcessInstance) instances
0555: .get(i);
0556:
0557: createElement(instance, root, true);
0558: }
0559:
0560: return doc.asXML();
0561: }
0562:
0563: public Object getTask(long taskId)
0564: throws WorkflowComponentException {
0565:
0566: TaskInstance task = taskMgmtSession.loadTaskInstance(taskId);
0567:
0568: WorkflowUtil.initTask(task);
0569:
0570: return task;
0571: }
0572:
0573: public String getTaskXml(long taskId)
0574: throws WorkflowComponentException {
0575:
0576: TaskInstance task = (TaskInstance) getTask(taskId);
0577:
0578: Document doc = DocumentHelper.createDocument();
0579:
0580: Element root = doc.addElement("result");
0581:
0582: createElement(task, root);
0583:
0584: return doc.asXML();
0585: }
0586:
0587: public List getTaskFormElements(long taskId)
0588: throws WorkflowComponentException {
0589:
0590: List taskFormElements = new ArrayList();
0591:
0592: TaskInstance task = taskMgmtSession.loadTaskInstance(taskId);
0593:
0594: Map variableInstances = task.getVariables();
0595:
0596: List variableAccesses = task.getTask().getTaskController()
0597: .getVariableAccesses();
0598:
0599: Iterator itr = variableAccesses.iterator();
0600:
0601: while (itr.hasNext()) {
0602: VariableAccess variableAccess = (VariableAccess) itr.next();
0603:
0604: String value = (String) variableInstances
0605: .get(variableAccess.getVariableName());
0606:
0607: TaskFormElement taskFormElement = new TaskFormElement(
0608: variableAccess, value);
0609:
0610: taskFormElements.add(taskFormElement);
0611: }
0612:
0613: return taskFormElements;
0614: }
0615:
0616: public String getTaskFormElementsXml(long taskId)
0617: throws WorkflowComponentException {
0618:
0619: List taskFormElements = getTaskFormElements(taskId);
0620:
0621: Document doc = DocumentHelper.createDocument();
0622:
0623: Element root = doc.addElement("result");
0624:
0625: for (int i = 0; i < taskFormElements.size(); i++) {
0626: TaskFormElement taskFormElement = (TaskFormElement) taskFormElements
0627: .get(i);
0628:
0629: createElement(taskFormElement, root);
0630: }
0631:
0632: return doc.asXML();
0633: }
0634:
0635: public List getTaskTransitions(long taskId)
0636: throws WorkflowComponentException {
0637:
0638: TaskInstance task = taskMgmtSession.loadTaskInstance(taskId);
0639:
0640: return task.getAvailableTransitions();
0641: }
0642:
0643: public String getTaskTransitionsXml(long taskId)
0644: throws WorkflowComponentException {
0645:
0646: List transitions = getTaskTransitions(taskId);
0647:
0648: Document doc = DocumentHelper.createDocument();
0649:
0650: Element root = doc.addElement("result");
0651:
0652: for (int i = 0; i < transitions.size(); i++) {
0653: Transition transition = (Transition) transitions.get(i);
0654:
0655: DocUtil.add(root, "transition", transition.getName());
0656: }
0657:
0658: return doc.asXML();
0659: }
0660:
0661: public List getUserTasks(long instanceId, String taskName,
0662: String definitionName, String assignedTo,
0663: String createDateGT, String createDateLT,
0664: String startDateGT, String startDateLT, String endDateGT,
0665: String endDateLT, boolean hideEndedTasks,
0666: boolean andOperator, int begin, int end)
0667: throws WorkflowComponentException {
0668:
0669: List tasks = new ArrayList();
0670:
0671: if (Validator.isNull(taskName)
0672: && Validator.isNull(definitionName)
0673: && Validator.isNull(assignedTo)
0674: && Validator.isNull(createDateGT)
0675: && Validator.isNull(createDateLT)
0676: && Validator.isNull(startDateGT)
0677: && Validator.isNull(startDateLT)
0678: && Validator.isNull(endDateGT)
0679: && Validator.isNull(endDateLT)) {
0680:
0681: List taskInstances = taskMgmtSession
0682: .findTaskInstances(userId);
0683:
0684: taskInstances.addAll(taskMgmtSession
0685: .findPooledTaskInstances(userId));
0686:
0687: Iterator itr = taskInstances.iterator();
0688:
0689: while (itr.hasNext()) {
0690: TaskInstance task = (TaskInstance) itr.next();
0691:
0692: ProcessInstance instance = task.getToken()
0693: .getProcessInstance();
0694:
0695: if (instance.getId() == instanceId) {
0696: WorkflowUtil.initTask(task);
0697:
0698: tasks.add(task);
0699: }
0700: }
0701: } else {
0702: tasks = graphSession.findTaskInstancesBySearchTerms(
0703: taskName, definitionName, assignedTo, createDateGT,
0704: createDateLT, startDateGT, startDateLT, endDateGT,
0705: endDateLT, hideEndedTasks, andOperator, begin, end);
0706:
0707: WorkflowUtil.initTasks(tasks);
0708: }
0709:
0710: return tasks;
0711: }
0712:
0713: public int getUserTasksCount(long instanceId, String taskName,
0714: String definitionName, String assignedTo,
0715: String createDateGT, String createDateLT,
0716: String startDateGT, String startDateLT, String endDateGT,
0717: String endDateLT, boolean hideEndedTasks,
0718: boolean andOperator) throws WorkflowComponentException {
0719:
0720: return graphSession.countTaskInstancesBySearchTerms(taskName,
0721: definitionName, assignedTo, createDateGT, createDateLT,
0722: startDateGT, startDateLT, endDateGT, endDateLT,
0723: hideEndedTasks, andOperator);
0724: }
0725:
0726: public String getUserTasksCountXml(long instanceId,
0727: String taskName, String definitionName, String assignedTo,
0728: String createDateGT, String createDateLT,
0729: String startDateGT, String startDateLT, String endDateGT,
0730: String endDateLT, boolean hideEndedTasks,
0731: boolean andOperator) throws WorkflowComponentException {
0732:
0733: int count = getUserTasksCount(instanceId, taskName,
0734: definitionName, assignedTo, createDateGT, createDateLT,
0735: startDateGT, startDateLT, endDateGT, endDateLT,
0736: hideEndedTasks, andOperator);
0737:
0738: return getCountXml(count);
0739: }
0740:
0741: public String getUserTasksXml(long instanceId, String taskName,
0742: String definitionName, String assignedTo,
0743: String createDateGT, String createDateLT,
0744: String startDateGT, String startDateLT, String endDateGT,
0745: String endDateLT, boolean hideEndedTasks,
0746: boolean andOperator, int begin, int end)
0747: throws WorkflowComponentException {
0748:
0749: List tasks = getUserTasks(instanceId, taskName, definitionName,
0750: assignedTo, createDateGT, createDateLT, startDateGT,
0751: startDateLT, endDateGT, endDateLT, hideEndedTasks,
0752: andOperator, begin, end);
0753:
0754: Document doc = DocumentHelper.createDocument();
0755:
0756: Element root = doc.addElement("result");
0757:
0758: for (int i = 0; i < tasks.size(); i++) {
0759: TaskInstance task = (TaskInstance) tasks.get(i);
0760:
0761: createElement(task, root);
0762: }
0763:
0764: return doc.asXML();
0765: }
0766:
0767: public void signalInstance(long instanceId)
0768: throws WorkflowComponentException {
0769:
0770: ProcessInstance instance = jbpmContext
0771: .loadProcessInstance(instanceId);
0772:
0773: instance.signal();
0774:
0775: jbpmContext.save(instance);
0776: }
0777:
0778: public void signalToken(long instanceId, long tokenId)
0779: throws WorkflowComponentException {
0780:
0781: ProcessInstance instance = jbpmContext
0782: .loadProcessInstance(instanceId);
0783:
0784: Token token = instance.getRootToken();
0785:
0786: if (token.getId() == tokenId) {
0787: token.signal();
0788: } else {
0789: Map activeChildren = instance.getRootToken()
0790: .getActiveChildren();
0791:
0792: Iterator itr = activeChildren.values().iterator();
0793:
0794: while (itr.hasNext()) {
0795: token = (Token) itr.next();
0796:
0797: if (token.getId() == tokenId) {
0798: token.signal();
0799:
0800: break;
0801: }
0802: }
0803: }
0804: }
0805:
0806: public String startWorkflow(long definitionId)
0807: throws WorkflowComponentException {
0808:
0809: ProcessDefinition definition = graphSession
0810: .loadProcessDefinition(definitionId);
0811:
0812: ProcessInstance instance = new ProcessInstance(definition);
0813:
0814: // After creating process instance, assign the currently authenticated
0815: // user to the first task if it exists
0816:
0817: if (instance.getTaskMgmtInstance().getTaskMgmtDefinition()
0818: .getStartTask() != null) {
0819:
0820: jbpmContext.setActorId(userId);
0821:
0822: instance.getTaskMgmtInstance().createStartTaskInstance();
0823: }
0824:
0825: jbpmContext.save(instance);
0826:
0827: WorkflowUtil.initInstance(instance);
0828:
0829: Document doc = DocumentHelper.createDocument();
0830:
0831: Element root = doc.addElement("result");
0832:
0833: createElement(instance, root, false);
0834:
0835: return doc.asXML();
0836: }
0837:
0838: public Map updateTask(long taskId, String transition,
0839: Map parameterMap) throws WorkflowComponentException {
0840:
0841: List taskFormElements = getTaskFormElements(taskId);
0842:
0843: TaskInstance task = taskMgmtSession.loadTaskInstance(taskId);
0844:
0845: String actorId = task.getActorId();
0846:
0847: try {
0848: task.start();
0849:
0850: if (actorId == null) {
0851: task.setActorId(userId);
0852: }
0853: } catch (Exception e) {
0854: _log.error("Task has already started");
0855: }
0856:
0857: Map variables = new HashMap();
0858: Map errors = new HashMap();
0859:
0860: Iterator itr = taskFormElements.iterator();
0861:
0862: while (itr.hasNext()) {
0863: TaskFormElement taskFormElement = (TaskFormElement) itr
0864: .next();
0865:
0866: String name = taskFormElement.getDisplayName();
0867:
0868: if (taskFormElement.isWritable()) {
0869: String value = getParamValue(parameterMap, name);
0870:
0871: if (taskFormElement.getType().equals(
0872: TaskFormElement.TYPE_DATE)) {
0873:
0874: value = getParamValue(parameterMap, JS
0875: .getSafeName(name));
0876: }
0877:
0878: variables.put(taskFormElement.getVariableName(), value);
0879:
0880: String error = validate(taskFormElement, parameterMap);
0881:
0882: if (error != null) {
0883: errors.put(name, error);
0884: }
0885: }
0886: }
0887:
0888: task.addVariables(variables);
0889:
0890: if (errors.size() == 0) {
0891: try {
0892: task.end(transition);
0893: } catch (Exception e) {
0894: _log.error("Task has already ended");
0895: }
0896: }
0897:
0898: ProcessInstance instance = task.getToken().getProcessInstance();
0899:
0900: WorkflowUtil.initInstance(instance);
0901:
0902: jbpmContext.save(instance);
0903:
0904: return errors;
0905: }
0906:
0907: public String updateTaskXml(long taskId, String transition,
0908: Map parameterMap) throws WorkflowComponentException {
0909:
0910: Map errors = updateTask(taskId, transition, parameterMap);
0911:
0912: Document doc = DocumentHelper.createDocument();
0913:
0914: Element root = doc.addElement("results");
0915:
0916: Iterator itr = errors.entrySet().iterator();
0917:
0918: while (itr.hasNext()) {
0919: Map.Entry entry = (Map.Entry) itr.next();
0920:
0921: String name = (String) entry.getKey();
0922: String code = (String) entry.getValue();
0923:
0924: Element el = root.addElement("error");
0925:
0926: DocUtil.add(el, "name", name);
0927: DocUtil.add(el, "code", code);
0928: }
0929:
0930: return doc.asXML();
0931: }
0932:
0933: protected void createElement(ProcessDefinition definition,
0934: Element root) {
0935: Element el = root.addElement("definition");
0936:
0937: DocUtil.add(el, "definitionId", definition.getId());
0938: DocUtil.add(el, "name", definition.getName());
0939: DocUtil.add(el, "version", definition.getVersion());
0940: }
0941:
0942: protected void createElement(ProcessInstance instance,
0943: Element root, boolean includeToken)
0944: throws WorkflowComponentException {
0945:
0946: Element el = root.addElement("instance");
0947:
0948: DocUtil.add(el, "instanceId", instance.getId());
0949: DocUtil.add(el, "startDate",
0950: formatDateTime(instance.getStart()));
0951: DocUtil.add(el, "endDate", formatDateTime(instance.getEnd()));
0952:
0953: if (instance.hasEnded()) {
0954: DocUtil.add(el, "ended", "true");
0955: } else {
0956: DocUtil.add(el, "ended", "false");
0957: }
0958:
0959: createElement(instance.getProcessDefinition(), el);
0960:
0961: if (includeToken) {
0962: createElement(instance.getRootToken(), el, true);
0963: }
0964: }
0965:
0966: protected void createElement(TaskInstance task, Element root)
0967: throws WorkflowComponentException {
0968:
0969: Element el = root.addElement("task");
0970:
0971: DocUtil.add(el, "taskId", task.getId());
0972: DocUtil.add(el, "name", task.getName());
0973: DocUtil.add(el, "assignedUserId", task.getActorId());
0974: DocUtil.add(el, "createDate", formatDateTime(task.getCreate()));
0975: DocUtil.add(el, "startDate", formatDateTime(task.getStart()));
0976: DocUtil.add(el, "endDate", formatDateTime(task.getEnd()));
0977:
0978: createElement(task.getToken().getProcessInstance(), el, false);
0979: }
0980:
0981: protected void createElement(TaskFormElement taskFormElement,
0982: Element root) {
0983:
0984: Element el = root.addElement("taskFormElement");
0985:
0986: DocUtil.add(el, "type", taskFormElement.getType());
0987: DocUtil
0988: .add(el, "displayName", taskFormElement
0989: .getDisplayName());
0990: DocUtil.add(el, "variableName", taskFormElement
0991: .getVariableName());
0992: DocUtil.add(el, "value", taskFormElement.getValue());
0993: DocUtil.add(el, "readable", taskFormElement.isReadable());
0994: DocUtil.add(el, "writable", taskFormElement.isWritable());
0995: DocUtil.add(el, "required", taskFormElement.isRequired());
0996:
0997: List values = taskFormElement.getValueList();
0998:
0999: Element valuesEl = el.addElement("values");
1000:
1001: for (int i = 0; i < values.size(); i++) {
1002: String value = (String) values.get(i);
1003:
1004: DocUtil.add(valuesEl, "value", value);
1005: }
1006: }
1007:
1008: protected void createElement(Token token, Element root,
1009: boolean checkChildren) throws WorkflowComponentException {
1010:
1011: Element tokenEl = root.addElement("token");
1012:
1013: DocUtil.add(tokenEl, "tokenId", token.getId());
1014: DocUtil.add(tokenEl, "name", token.getNode().getName());
1015:
1016: if (token.getNode().toString().startsWith("Join")) {
1017: DocUtil.add(tokenEl, "type", "join");
1018: } else {
1019: DocUtil.add(tokenEl, "type", "default");
1020: }
1021:
1022: List tasks = getCurrentTasks(
1023: token.getProcessInstance().getId(), token.getId());
1024:
1025: if (tasks == null) {
1026: Element task = tokenEl.addElement("task");
1027:
1028: task.addElement("taskId").addText("null");
1029: } else {
1030: for (int i = 0; i < tasks.size(); i++) {
1031: TaskInstance task = (TaskInstance) tasks.get(i);
1032:
1033: createElement(task, tokenEl);
1034: }
1035: }
1036:
1037: if (checkChildren) {
1038: Map activeChildren = getActiveChildren(token
1039: .getProcessInstance().getId());
1040:
1041: if (hasActiveChildren(activeChildren)) {
1042: Iterator itr = activeChildren.values().iterator();
1043:
1044: while (itr.hasNext()) {
1045: Token child = (Token) itr.next();
1046:
1047: createElement(child, tokenEl, false);
1048: }
1049: }
1050: }
1051: }
1052:
1053: protected String formatDate(Date date) {
1054: if (date == null) {
1055: return StringPool.BLANK;
1056: }
1057:
1058: SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
1059:
1060: return sdf.format(date);
1061: }
1062:
1063: protected Date formatDate(String date) throws ParseException {
1064: if (date == null) {
1065: return null;
1066: }
1067:
1068: SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
1069:
1070: return sdf.parse(date);
1071: }
1072:
1073: protected String formatDateTime(Date date) {
1074: if (date == null) {
1075: return StringPool.BLANK;
1076: }
1077:
1078: SimpleDateFormat sdf = new SimpleDateFormat(
1079: "MM/dd/yyyy hh:mm:ss aaa");
1080:
1081: return sdf.format(date);
1082: }
1083:
1084: protected Date formatDateTime(String date) throws ParseException {
1085: if (date == null) {
1086: return null;
1087: }
1088:
1089: SimpleDateFormat sdf = new SimpleDateFormat(
1090: "MM/dd/yyyy hh:mm:ss aaa");
1091:
1092: return sdf.parse(date);
1093: }
1094:
1095: protected Map getActiveChildren(long instanceId) {
1096: ProcessInstance processInstance = jbpmContext
1097: .loadProcessInstance(instanceId);
1098:
1099: Map activeChildren = processInstance.getRootToken()
1100: .getActiveChildren();
1101:
1102: WorkflowUtil.initTokens(new ArrayList(activeChildren.values()));
1103:
1104: return activeChildren;
1105: }
1106:
1107: protected String getCountXml(int count) {
1108: StringMaker sm = new StringMaker();
1109:
1110: sm.append("<result>");
1111: sm.append("<count>");
1112: sm.append(count);
1113: sm.append("</count>");
1114: sm.append("</result>");
1115:
1116: return sm.toString();
1117: }
1118:
1119: protected String getParamValue(Map parameterMap, String name) {
1120: String value = StringPool.BLANK;
1121:
1122: String[] values = (String[]) parameterMap.get(name);
1123:
1124: if ((values != null) && (values.length > 0)) {
1125: value = values[0];
1126: }
1127:
1128: return value;
1129: }
1130:
1131: protected boolean hasActiveChildren(Map activeChildren) {
1132: Iterator itr = activeChildren.values().iterator();
1133:
1134: while (itr.hasNext()) {
1135: Token token = (Token) itr.next();
1136:
1137: if (!token.getNode().toString().startsWith("Join")) {
1138: return true;
1139: }
1140: }
1141:
1142: return false;
1143: }
1144:
1145: protected String validate(TaskFormElement taskFormElement,
1146: Map parameterMap) {
1147:
1148: String error = null;
1149:
1150: String type = taskFormElement.getType();
1151: String name = taskFormElement.getDisplayName();
1152: String value = getParamValue(parameterMap, name);
1153:
1154: if (type.equals(TaskFormElement.TYPE_CHECKBOX)) {
1155: if (taskFormElement.isRequired() && value.equals("false")) {
1156: error = "required-value";
1157: }
1158: } else if (type.equals(TaskFormElement.TYPE_DATE)) {
1159: value = getParamValue(parameterMap, JS.getSafeName(name));
1160:
1161: if (taskFormElement.isRequired()) {
1162: try {
1163: formatDate(value);
1164:
1165: String[] dateValues = StringUtil.split(value, "/");
1166:
1167: int month = GetterUtil.getInteger(dateValues[0]) - 1;
1168: int day = GetterUtil.getInteger(dateValues[1]);
1169: int year = GetterUtil.getInteger(dateValues[2]);
1170:
1171: if (!Validator.isDate(month, day, year)) {
1172: error = "invalid-date";
1173: }
1174: } catch (Exception e) {
1175: error = "invalid-date";
1176: }
1177: }
1178: } else if (type.equals(TaskFormElement.TYPE_EMAIL)) {
1179: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1180: error = "required-value";
1181: } else if (!Validator.isNull(value)
1182: && !Validator.isEmailAddress(value)) {
1183:
1184: error = "invalid-email";
1185: }
1186: } else if (type.equals(TaskFormElement.TYPE_NUMBER)) {
1187: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1188: error = "required-value";
1189: } else if (!Validator.isNull(value)
1190: && !Validator.isNumber(value)) {
1191: error = "invalid-number";
1192: }
1193: } else if (type.equals(TaskFormElement.TYPE_PASSWORD)) {
1194: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1195: error = "required-value";
1196: }
1197: } else if (type.equals(TaskFormElement.TYPE_PHONE)) {
1198: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1199: error = "required-value";
1200: } else if (!Validator.isNull(value)
1201: && !Validator.isPhoneNumber(value)) {
1202:
1203: error = "invalid-phone";
1204: }
1205: } else if (type.equals(TaskFormElement.TYPE_RADIO)) {
1206: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1207: error = "required-value";
1208: }
1209: } else if (type.equals(TaskFormElement.TYPE_SELECT)) {
1210: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1211: error = "required-value";
1212: }
1213: } else if (type.equals(TaskFormElement.TYPE_TEXT)) {
1214: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1215: error = "required-value";
1216: }
1217: } else if (type.equals(TaskFormElement.TYPE_TEXTAREA)) {
1218: if (taskFormElement.isRequired() && Validator.isNull(value)) {
1219: error = "required-value";
1220: }
1221: }
1222:
1223: return error;
1224: }
1225:
1226: protected String userId;
1227: protected String timeZoneId;
1228: protected TimeZone timeZone;
1229: protected JbpmContext jbpmContext;
1230: protected GraphSession graphSession;
1231: protected TaskMgmtSession taskMgmtSession;
1232:
1233: private static Log _log = LogFactoryUtil
1234: .getLog(WorkflowComponentImpl.class);
1235:
1236: }
|