[jbpm-commits] JBoss JBPM SVN: r5306 - in jbpm4/trunk/modules: pvm/src/main/java/org/jbpm/pvm/internal/cmd and 2 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Thu Jul 16 03:00:38 EDT 2009


Author: jbarrez
Date: 2009-07-16 03:00:37 -0400 (Thu, 16 Jul 2009)
New Revision: 5306

Added:
   jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/taskactivity/TaskCompletionTest.java
Modified:
   jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskService.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/CompleteTaskCmd.java
   jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/svc/TaskServiceImpl.java
Log:
JBPM-2343: add convenience method complete task + set variables in one transaction. Also added test case for various usages of task completion.

Modified: jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskService.java
===================================================================
--- jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskService.java	2009-07-16 06:58:08 UTC (rev 5305)
+++ jbpm4/trunk/modules/api/src/main/java/org/jbpm/api/TaskService.java	2009-07-16 07:00:37 UTC (rev 5306)
@@ -78,6 +78,13 @@
    * of a process execution, this operation may result in a process instance 
    * being triggered. */
   void completeTask(String taskId);
+
+  /** Deletes this task and marks the related history task as completed. The
+   * given variables are created (or they overwrite existing values) as task
+   * variables. If the task was created in the context of a process execution,
+   * this operation may result in a process instance being triggered to
+   * continue.*/
+  void completeTask(String taskId, Map<String, Object> variables);
   
   /** Deletes this task, marks the related history task as completed 
    * with the specified outcome.  If the task was created in the context 
@@ -85,6 +92,14 @@
    * being triggered. The outcome in that case corresponds to an outgoing 
    * transition in the process. */
   void completeTask(String taskId, String outcome);
+  
+  /** Deletes this task, marks the related history task as completed with the
+   * specified outcome. The given variables are created (or they overwrite
+   * existing values) as task variables. If the task was created in the context
+   * of a process execution, this operation may result in a process instance
+   * being triggered. The outcome in that case corresponds to an outgoing
+   * transition in the process. */
+  void completeTask(String taskId, String outcome, Map<String, Object> variables);
 
   /** Deletes the task without completing it.
    * The history information is kept in the DB.

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/CompleteTaskCmd.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/CompleteTaskCmd.java	2009-07-16 06:58:08 UTC (rev 5305)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cmd/CompleteTaskCmd.java	2009-07-16 07:00:37 UTC (rev 5306)
@@ -21,6 +21,7 @@
  */
 package org.jbpm.pvm.internal.cmd;
 
+import org.jbpm.api.JbpmException;
 import org.jbpm.pvm.internal.env.Environment;
 import org.jbpm.pvm.internal.session.DbSession;
 import org.jbpm.pvm.internal.task.TaskImpl;
@@ -42,7 +43,17 @@
 
   public Void execute(Environment environment) throws Exception {
     DbSession dbSession = environment.get(DbSession.class);
+    
+    if (taskId == null || "".equals(taskId)) {
+      throw new JbpmException("Cannot complete a task with a null or empty taskId");
+    }
+    
     TaskImpl task = dbSession.get(TaskImpl.class, Long.parseLong(taskId));
+    
+    if (task == null) {
+      throw new JbpmException("No task with id " + taskId + " was found");
+    }
+    
     if (outcome==null) {
       task.complete();
     } else {

Modified: jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/svc/TaskServiceImpl.java
===================================================================
--- jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/svc/TaskServiceImpl.java	2009-07-16 06:58:08 UTC (rev 5305)
+++ jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/svc/TaskServiceImpl.java	2009-07-16 07:00:37 UTC (rev 5306)
@@ -35,6 +35,7 @@
 import org.jbpm.pvm.internal.cmd.AddReplyCommentCmd;
 import org.jbpm.pvm.internal.cmd.AddTaskCommentCmd;
 import org.jbpm.pvm.internal.cmd.AssignTaskCmd;
+import org.jbpm.pvm.internal.cmd.CompositeCmd;
 import org.jbpm.pvm.internal.cmd.DeleteTaskCmd;
 import org.jbpm.pvm.internal.cmd.CompleteTaskCmd;
 import org.jbpm.pvm.internal.cmd.CreateTaskQueryCmd;
@@ -84,12 +85,25 @@
   }
 
   public void completeTask(String taskId) {
-    completeTask(taskId, null);
+    commandService.execute(new CompleteTaskCmd(taskId, null));
   }
+  
+  public void completeTask(String taskId, Map<String, Object> variables) {
+    completeTask(taskId, null, variables);
+  }
 
   public void completeTask(String taskId, String outcome) {
     commandService.execute(new CompleteTaskCmd(taskId, outcome));
   }
+  
+  public void completeTask(String taskId, String outcome, Map<String, Object> variables) {
+    SetTaskVariablesCmd setTaskVariablesCmd = new SetTaskVariablesCmd(taskId);
+    setTaskVariablesCmd.setVariables(variables);
+    CompositeCmd compositeCmd = new CompositeCmd();
+    compositeCmd.addCommand(setTaskVariablesCmd);
+    compositeCmd.addCommand(new CompleteTaskCmd(taskId, outcome));
+    commandService.execute(compositeCmd);
+  }
 
   public void addTaskParticipatingUser(String taskId, String userId, String participation) {
     commandService.execute(new AddParticipationCmd(taskId, null, userId, null, participation));

Added: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/taskactivity/TaskCompletionTest.java
===================================================================
--- jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/taskactivity/TaskCompletionTest.java	                        (rev 0)
+++ jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/taskactivity/TaskCompletionTest.java	2009-07-16 07:00:37 UTC (rev 5306)
@@ -0,0 +1,144 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jbpm.test.taskactivity;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jbpm.api.JbpmException;
+import org.jbpm.api.history.HistoryTask;
+import org.jbpm.test.JbpmTestCase;
+
+/**
+ * Testcase for the several ways of completing a task.
+ * 
+ * @author jbarrez
+ */
+public class TaskCompletionTest extends JbpmTestCase {
+
+  private static final String PROCESS = 
+    "<process name='taskCompletion'>" +
+    "  <start>" +
+    "    <transition to='theTask' />" +
+    "  </start>" +
+    "  <task name='theTask' assignee='johndoe'>" +
+    "    <transition name='one' to='stateOne' />" +
+    "    <transition name='two' to='stateTwo' />" +
+    "  </task>" +
+    "  <state name='stateOne'>" + 
+    "    <transition to='theEnd' />" +
+    "  </state>" +
+    "  <state name='stateTwo'>" + 
+    "    <transition to='theEnd' />" +
+    "  </state>" +
+    "  <end name='theEnd' />" +
+    "</process>";
+  
+  public void testCompletionWithNoOutcome() {
+    Ids ids = deployAndStartProcessInstance();
+    taskService.completeTask(ids.taskId);
+    assertExecutionInOneOrMoreActivitiesActive(ids.processInstanceId, "stateOne", "stateTwo");
+    assertHistoryTaskCreated(ids.processInstanceId, null);
+  }
+  
+  public void testCompletionWithNullOrEmptyId() {
+    try {
+      taskService.completeTask(null);
+      fail();
+    } catch (JbpmException e) {
+      // exception should be thrown
+    }
+    try {
+      taskService.completeTask("");
+      fail();
+    } catch (JbpmException e) {
+      // exception should be thrown
+    }
+  }
+  
+  public void testCompletionWithInvalidId() {
+    try {
+      taskService.completeTask(Long.toString(-123456789L));
+      fail();
+    } catch (JbpmException e) {
+      // exception should be thrown
+    }
+  }
+  
+  public void testCompletionWithOutcome() {
+    Ids ids = deployAndStartProcessInstance();
+    taskService.completeTask(ids.taskId, "one");
+    assertActivityActive(ids.processInstanceId, "stateOne");
+    assertNotActivityActive(ids.processInstanceId, "stateTwo");
+    assertHistoryTaskCreated(ids.processInstanceId, "one");
+  }
+  
+  public void testCompletionWithVariables() {
+    Ids ids = deployAndStartProcessInstance();
+    Map<String, Object> vars = new HashMap<String, Object>();
+    vars.put("testVar", "testValue");
+    taskService.completeTask(ids.taskId, vars);
+    assertEquals("testValue", executionService.getVariable(ids.processInstanceId, "testVar"));
+    assertHistoryTaskCreated(ids.processInstanceId, null);
+  }
+  
+  public void testCompletionWithOutcomeAndVariables() {
+    Ids ids = deployAndStartProcessInstance();
+    Map<String, Object> vars = new HashMap<String, Object>();
+    vars.put("testVar", "testValue");
+    taskService.completeTask(ids.taskId, "two",vars);
+    assertEquals("testValue", executionService.getVariable(ids.processInstanceId, "testVar"));
+    assertActivityActive(ids.processInstanceId, "stateTwo");
+    assertHistoryTaskCreated(ids.processInstanceId, "two");
+  }
+  
+  /** 
+   * Returns the process instance id and the taskId of the single task 
+   * that is open after process start 
+   */
+  private Ids deployAndStartProcessInstance() {
+    deployJpdlXmlString(PROCESS);
+    Ids result = new Ids();
+    result.processInstanceId = executionService.startProcessInstanceByKey("taskCompletion").getId();
+    result.taskId = taskService.createTaskQuery().processInstanceId(result.processInstanceId).uniqueResult().getId();
+    return result;
+  }
+  
+  private void assertHistoryTaskCreated(String executionId, String outcome) {
+    HistoryTask historyTask = historyService.createHistoryTaskQuery()
+                                            .executionId(executionId)
+                                            .uniqueResult();
+    assertNotNull(historyTask);
+    if (outcome != null) {
+      assertEquals(outcome, historyTask.getOutcome());
+    }
+  }
+  
+  /* Just a wrapper for two ids, since Java doesnt allow to return multiple values */
+  private class Ids {
+    
+    private String processInstanceId;
+    private String taskId;
+    
+  }
+  
+}


Property changes on: jbpm4/trunk/modules/test-db/src/test/java/org/jbpm/test/taskactivity/TaskCompletionTest.java
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + LF



More information about the jbpm-commits mailing list