在jbpm4中如何配置sql活动使用动态参数呢?
我们使用JBPM4的例子为例,查看修改的方法:
流程定义文件:
代码
<?xml version="1.0" encoding="UTF-8"?>
<process name="Sql" xmlns="http://jbpm.org/4.4/jpdl">
<start g="16,20,48,48">
<transition to="get task names" />
</start>
<sql name="get task names"
var="tasknames with i"
g="96,16,126,52">
<query>
select NAME_
from JBPM4_TASK
where NAME_ like :name
</query>
<parameters>
<object name="name" expr="#{name}" />
<!--
<string name="name" value="%i%" />
-->
</parameters>
<transition to="count tasks" />
</sql>
<sql name="count tasks"
var="tasks"
unique="true"
g="254,16,92,52">
<query>
select count(*)
from JBPM4_TASK
</query>
<transition to="wait" />
</sql>
<state name="wait" g="378,18,94,48"/>
</process>
<process name="Sql" xmlns="http://jbpm.org/4.4/jpdl">
<start g="16,20,48,48">
<transition to="get task names" />
</start>
<sql name="get task names"
var="tasknames with i"
g="96,16,126,52">
<query>
select NAME_
from JBPM4_TASK
where NAME_ like :name
</query>
<parameters>
<object name="name" expr="#{name}" />
<!--
<string name="name" value="%i%" />
-->
</parameters>
<transition to="count tasks" />
</sql>
<sql name="count tasks"
var="tasks"
unique="true"
g="254,16,92,52">
<query>
select count(*)
from JBPM4_TASK
</query>
<transition to="wait" />
</sql>
<state name="wait" g="378,18,94,48"/>
</process>
把<string name="name" value="%i%" />
修改为 <object name="name" expr="#{name}" />
这样我们就可以从外部传入参数。
下面为JAVA调用的示例。
代码
/*
* 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.examples.sql;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jbpm.api.Execution;
import org.jbpm.api.task.Task;
import org.jbpm.examples.java.Hand;
import org.jbpm.test.JbpmTestCase;
/**
* @author Tom Baeyens
*/
public class SqlTest extends JbpmTestCase {
String deploymentId;
String taskLaundryId;
String taskDishesId;
String taskIronId;
protected void setUp() throws Exception {
super.setUp();
deploymentId = repositoryService.createDeployment()
.addResourceFromClasspath("org/jbpm/examples/sql/process.jpdl.xml")
.deploy();
// add task laundry
Task task = taskService.newTask();
task.setName("laundry");
taskLaundryId = taskService.saveTask(task);
// add task dishes
task = taskService.newTask();
task.setName("dishes");
taskDishesId = taskService.saveTask(task);
// add task iron
task = taskService.newTask();
task.setName("iron");
taskIronId = taskService.saveTask(task);
}
protected void tearDown() throws Exception {
repositoryService.deleteDeploymentCascade(deploymentId);
taskService.deleteTaskCascade(taskLaundryId);
taskService.deleteTaskCascade(taskDishesId);
taskService.deleteTaskCascade(taskIronId);
super.tearDown();
}
public void testSql() {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("name", "%i%");
Execution execution = executionService.startProcessInstanceByKey("Sql",variables);
String executionId = execution.getId();
Set<String> expectedTaskNames = new HashSet<String>();
expectedTaskNames.add("dishes");
expectedTaskNames.add("iron");
Collection<String> taskNames = (Collection<String>) executionService.getVariable(executionId, "tasknames with i");
taskNames = new HashSet<String>(taskNames);
assertEquals(expectedTaskNames, taskNames);
Object activities = executionService.getVariable(executionId, "tasks");
assertEquals("3", activities.toString());
}
}
* 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.examples.sql;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jbpm.api.Execution;
import org.jbpm.api.task.Task;
import org.jbpm.examples.java.Hand;
import org.jbpm.test.JbpmTestCase;
/**
* @author Tom Baeyens
*/
public class SqlTest extends JbpmTestCase {
String deploymentId;
String taskLaundryId;
String taskDishesId;
String taskIronId;
protected void setUp() throws Exception {
super.setUp();
deploymentId = repositoryService.createDeployment()
.addResourceFromClasspath("org/jbpm/examples/sql/process.jpdl.xml")
.deploy();
// add task laundry
Task task = taskService.newTask();
task.setName("laundry");
taskLaundryId = taskService.saveTask(task);
// add task dishes
task = taskService.newTask();
task.setName("dishes");
taskDishesId = taskService.saveTask(task);
// add task iron
task = taskService.newTask();
task.setName("iron");
taskIronId = taskService.saveTask(task);
}
protected void tearDown() throws Exception {
repositoryService.deleteDeploymentCascade(deploymentId);
taskService.deleteTaskCascade(taskLaundryId);
taskService.deleteTaskCascade(taskDishesId);
taskService.deleteTaskCascade(taskIronId);
super.tearDown();
}
public void testSql() {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("name", "%i%");
Execution execution = executionService.startProcessInstanceByKey("Sql",variables);
String executionId = execution.getId();
Set<String> expectedTaskNames = new HashSet<String>();
expectedTaskNames.add("dishes");
expectedTaskNames.add("iron");
Collection<String> taskNames = (Collection<String>) executionService.getVariable(executionId, "tasknames with i");
taskNames = new HashSet<String>(taskNames);
assertEquals(expectedTaskNames, taskNames);
Object activities = executionService.getVariable(executionId, "tasks");
assertEquals("3", activities.toString());
}
}
这里是修改过后的代码:
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("name", "%i%");
Execution execution = executionService.startProcessInstanceByKey("Sql",variables);
我们传入name变量,进行计算了。