Created
November 4, 2014 09:46
-
-
Save akoskm/86ffe546640035eaeb1a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mycompany.test.jbpm; | |
import bitronix.tm.resource.jdbc.PoolingDataSource; | |
import org.jbpm.runtime.manager.impl.RuntimeEnvironmentBuilder; | |
import org.jbpm.services.task.identity.JBossUserGroupCallbackImpl; | |
import org.jbpm.services.task.utils.ContentMarshallerHelper; | |
import org.jbpm.test.JBPMHelper; | |
import org.junit.Test; | |
import org.kie.api.io.ResourceType; | |
import org.kie.api.runtime.KieSession; | |
import org.kie.api.runtime.manager.RuntimeEngine; | |
import org.kie.api.runtime.manager.RuntimeManager; | |
import org.kie.api.runtime.manager.RuntimeManagerFactory; | |
import org.kie.api.runtime.process.ProcessInstance; | |
import org.kie.api.task.TaskService; | |
import org.kie.api.task.model.Content; | |
import org.kie.api.task.model.Task; | |
import org.kie.api.task.model.TaskSummary; | |
import org.kie.internal.io.ResourceFactory; | |
import org.kie.internal.runtime.manager.RuntimeEnvironment; | |
import org.kie.internal.runtime.manager.context.ProcessInstanceIdContext; | |
import org.kie.internal.task.api.UserGroupCallback; | |
import javax.persistence.EntityManagerFactory; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Properties; | |
public class HumanTaskExampleOrig { | |
private static PoolingDataSource pds; | |
private EntityManagerFactory emf; | |
private Map<String, Object> results; | |
@Test | |
public void doFirst() { | |
RuntimeManager manager = getRuntimeManager("process/humantask/HumanTask.bpmn"); | |
RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get()); | |
KieSession ksession = runtime.getKieSession(); | |
// start a new process instance | |
Map<String, Object> params = new HashMap<String, Object>(); | |
params.put("userId", "krisv"); | |
params.put("description", "Need a new laptop computer"); | |
ProcessInstance pi = ksession.startProcess("com.sample.humantask", params); | |
System.out.println(pi.getId()); | |
// "sales-rep" reviews request | |
TaskService taskService = runtime.getTaskService(); | |
TaskSummary task1 = taskService.getTasksAssignedAsPotentialOwner("sales-rep", "en-UK").get(0); | |
System.out.println("Sales-rep executing task " + task1.getName() + "(" + task1.getId() + ": " + task1.getDescription() + ")"); | |
taskService.claim(task1.getId(), "sales-rep"); | |
taskService.start(task1.getId(), "sales-rep"); | |
results = new HashMap<String, Object>(); | |
results.put("comment", "Agreed, existing laptop needs replacing"); | |
results.put("outcome", "Accept"); | |
taskService.complete(task1.getId(), "sales-rep", results); | |
// "krisv" approves result | |
TaskSummary task2 = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK").get(0); | |
System.out.println("krisv executing task " + task2.getName() + "(" + task2.getId() + ": " + task2.getDescription() + ")"); | |
taskService.start(task2.getId(), "krisv"); | |
results = new HashMap<String, Object>(); | |
results.put("outcome", "Agree"); | |
taskService.complete(task2.getId(), "krisv", results); | |
manager.disposeRuntimeEngine(runtime); | |
} | |
@Test | |
public void doSecond() { | |
RuntimeManager manager = getRuntimeManager("process/humantask/HumanTask.bpmn"); | |
RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(Long.valueOf(111))); | |
KieSession ksession = runtime.getKieSession(); | |
TaskService taskService = runtime.getTaskService(); | |
// "john" as manager reviews request | |
TaskSummary task3 = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK").get(0); | |
System.out.println("john executing task " + task3.getName() + "(" + task3.getId() + ": " + task3.getDescription() + ")"); | |
taskService.claim(task3.getId(), "john"); | |
taskService.start(task3.getId(), "john"); | |
results = new HashMap<String, Object>(); | |
results.put("outcome", "Agree"); | |
taskService.complete(task3.getId(), "john", results); | |
// "sales-rep" gets notification | |
TaskSummary task4 = taskService.getTasksAssignedAsPotentialOwner("sales-rep", "en-UK").get(0); | |
System.out.println("sales-rep executing task " + task4.getName() + "(" + task4.getId() + ": " + task4.getDescription() + ")"); | |
taskService.start(task4.getId(), "sales-rep"); | |
Task task = taskService.getTaskById(task4.getId()); | |
Content content = taskService.getContentById(task.getTaskData().getDocumentContentId()); | |
Object result = ContentMarshallerHelper.unmarshall(content.getContent(), null); | |
Map<?, ?> map = (Map<?, ?>) result; | |
for (Map.Entry<?, ?> entry : map.entrySet()) { | |
System.out.println(entry.getKey() + " = " + entry.getValue()); | |
} | |
taskService.complete(task4.getId(), "sales-rep", null); | |
System.out.println("Process instance completed"); | |
manager.disposeRuntimeEngine(runtime); | |
} | |
private RuntimeManager getRuntimeManager(String process) { | |
// load up the knowledge base | |
JBPMHelper.startH2Server(); | |
JBPMHelper.setupDataSource(); | |
Properties properties= new Properties(); | |
properties.setProperty("krisv", ""); | |
properties.setProperty("sales-rep", "sales"); | |
properties.setProperty("john", "PM"); | |
UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties); | |
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault() | |
.userGroupCallback(userGroupCallback) | |
.addAsset(ResourceFactory.newClassPathResource(process), ResourceType.BPMN2) | |
.get(); | |
return RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment