use of org.jbpm.workflow.core.node.HumanTaskNode in project jbpm by kiegroup.
the class DynamicProcessTest method testDynamicProcess.
@Test
public void testDynamicProcess() throws Exception {
RuleFlowProcessFactory factory = RuleFlowProcessFactory.createProcess("org.jbpm.HelloWorld");
factory.name("HelloWorldProcess").version("1.0").packageName("org.jbpm").startNode(1).name("Start").done().humanTaskNode(2).name("Task1").actorId("krisv").taskName("MyTask").done().endNode(3).name("End").done().connection(1, 2).connection(2, 3);
final RuleFlowProcess process = factory.validate().getProcess();
Resource resource = ResourceFactory.newByteArrayResource(XmlRuleFlowProcessDumper.INSTANCE.dump(process).getBytes());
// source path or target path must be set to be added into kbase
resource.setSourcePath("/tmp/dynamicProcess.bpmn2");
KieBase kbase = createKnowledgeBaseFromResources(resource);
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler testHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", testHandler);
ksession.addEventListener(new ProcessEventListener() {
public void beforeVariableChanged(ProcessVariableChangedEvent arg0) {
}
public void beforeProcessStarted(ProcessStartedEvent arg0) {
logger.info("{}", arg0);
}
public void beforeProcessCompleted(ProcessCompletedEvent arg0) {
logger.info("{}", arg0);
}
public void beforeNodeTriggered(ProcessNodeTriggeredEvent arg0) {
logger.info("{}", arg0);
}
public void beforeNodeLeft(ProcessNodeLeftEvent arg0) {
logger.info("{}", arg0);
}
public void afterVariableChanged(ProcessVariableChangedEvent arg0) {
}
public void afterProcessStarted(ProcessStartedEvent arg0) {
}
public void afterProcessCompleted(ProcessCompletedEvent arg0) {
}
public void afterNodeTriggered(ProcessNodeTriggeredEvent arg0) {
}
public void afterNodeLeft(ProcessNodeLeftEvent arg0) {
}
});
final ProcessInstanceImpl processInstance = (ProcessInstanceImpl) ksession.startProcess("org.jbpm.HelloWorld");
HumanTaskNode node = new HumanTaskNode();
node.setName("Task2");
node.setId(4);
insertNodeInBetween(process, 2, 3, node);
((CommandBasedStatefulKnowledgeSession) ksession).getRunner().execute(new ExecutableCommand<Void>() {
public Void execute(Context context) {
StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) ((RegistryContext) context).lookup(KieSession.class);
((ProcessInstanceImpl) ksession.getProcessInstance(processInstance.getId())).updateProcess(process);
return null;
}
});
assertProcessInstanceActive(processInstance);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceActive(processInstance);
ksession.getWorkItemManager().completeWorkItem(testHandler.getWorkItem().getId(), null);
assertProcessInstanceFinished(processInstance, ksession);
ksession.dispose();
}
use of org.jbpm.workflow.core.node.HumanTaskNode in project jbpm by kiegroup.
the class ServicesProcessDataEventListener method onNodeAdded.
@SuppressWarnings("unchecked")
@Override
public void onNodeAdded(Node node) {
logger.debug("Added node " + node);
if (node instanceof HumanTaskNode) {
HumanTaskNode humanTaskNode = (HumanTaskNode) node;
String name = humanTaskNode.getName();
UserTaskDefinitionImpl task = (UserTaskDefinitionImpl) processDescriptor.getTasks().get(name);
if (task == null) {
task = new UserTaskDefinitionImpl();
task.setId(humanTaskNode.getUniqueId());
task.setName(name);
processDescriptor.getTasks().put(task.getName(), task);
}
Map<String, Object> parameters = humanTaskNode.getWork().getParameters();
Collection<String> currentAssignment = processDescriptor.getTaskAssignments().get(humanTaskNode.getName());
for (String parameter : parameters.keySet()) {
if (parameter.equals("GroupId") || parameter.equals("ActorId")) {
if (currentAssignment == null) {
currentAssignment = new ArrayList<String>();
processDescriptor.getTaskAssignments().put(humanTaskNode.getName(), currentAssignment);
}
currentAssignment.add(humanTaskNode.getWork().getParameter(parameter).toString());
}
}
((UserTaskDefinitionImpl) processDescriptor.getTasks().get(humanTaskNode.getName())).setAssociatedEntities(currentAssignment);
Map<String, String> inputParams = new HashMap<String, String>();
for (Map.Entry<String, String> in : ((Map<String, String>) humanTaskNode.getMetaData("DataInputs")).entrySet()) {
inputParams.put(in.getKey(), in.getValue());
}
Map<String, String> outputParams = new HashMap<String, String>();
for (Map.Entry<String, String> out : ((Map<String, String>) humanTaskNode.getMetaData("DataOutputs")).entrySet()) {
outputParams.put(out.getKey(), out.getValue());
}
task.setTaskInputMappings(inputParams);
task.setTaskOutputMappings(outputParams);
task.setComment(asString(humanTaskNode.getWork().getParameter("Comment")));
task.setCreatedBy(asString(humanTaskNode.getWork().getParameter("CreatedBy")));
task.setPriority(asInt(humanTaskNode.getWork().getParameter("Priority")));
task.setSkippable(asBoolean(humanTaskNode.getWork().getParameter("Skippable")));
task.setFormName(asString(humanTaskNode.getWork().getParameter("TaskName")));
processDescriptor.getTaskInputMappings().put(task.getName(), inputParams);
processDescriptor.getTaskOutputMappings().put(task.getName(), outputParams);
} else if (node instanceof RuleSetNode) {
RuleSetNode ruleSetNode = (RuleSetNode) node;
String ruleFlowGroup = ruleSetNode.getRuleFlowGroup();
if (ruleFlowGroup != null) {
processDescriptor.getReferencedRules().add(ruleFlowGroup);
}
} else if (node instanceof WorkItemNode) {
processDescriptor.getServiceTasks().put(node.getName(), ((WorkItemNode) node).getWork().getName());
} else if (node instanceof SubProcessNode) {
SubProcessNode subProcess = (SubProcessNode) node;
String processId = subProcess.getProcessId();
if (subProcess.getProcessName() != null) {
processDescriptor.addReusableSubProcessName(subProcess.getProcessName());
} else {
processDescriptor.getReusableSubProcesses().add(processId);
}
}
}
use of org.jbpm.workflow.core.node.HumanTaskNode in project jbpm by kiegroup.
the class MigrationManager method updateNodeInstances.
@SuppressWarnings("unchecked")
private void updateNodeInstances(NodeInstanceContainer nodeInstanceContainer, Map<String, String> nodeMapping, NodeContainer nodeContainer, EntityManager em) {
for (NodeInstance nodeInstance : nodeInstanceContainer.getNodeInstances()) {
Long upgradedNodeId = null;
String oldNodeId = (String) ((NodeImpl) ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).getNode()).getMetaData().get("UniqueId");
String newNodeId = nodeMapping.get(oldNodeId);
if (newNodeId == null) {
newNodeId = oldNodeId;
}
Node upgradedNode = findNodeByUniqueId(newNodeId, nodeContainer);
if (upgradedNode == null) {
try {
upgradedNodeId = Long.parseLong(newNodeId);
} catch (NumberFormatException e) {
continue;
}
} else {
upgradedNodeId = upgradedNode.getId();
}
((NodeInstanceImpl) nodeInstance).setNodeId(upgradedNodeId);
if (upgradedNode != null) {
// update log information for new node information
Query nodeInstanceIdQuery = em.createQuery("select nodeInstanceId from NodeInstanceLog nil" + " where nil.nodeId = :oldNodeId and processInstanceId = :processInstanceId " + " GROUP BY nil.nodeInstanceId" + " HAVING sum(nil.type) = 0");
nodeInstanceIdQuery.setParameter("oldNodeId", oldNodeId).setParameter("processInstanceId", nodeInstance.getProcessInstance().getId());
List<Long> nodeInstanceIds = nodeInstanceIdQuery.getResultList();
report.addEntry(Type.INFO, "Mapping: Node instance logs to be updated = " + nodeInstanceIds);
Query nodeLogQuery = em.createQuery("update NodeInstanceLog set nodeId = :nodeId, nodeName = :nodeName, nodeType = :nodeType " + "where nodeInstanceId in (:ids) and processInstanceId = :processInstanceId");
nodeLogQuery.setParameter("nodeId", (String) upgradedNode.getMetaData().get("UniqueId")).setParameter("nodeName", upgradedNode.getName()).setParameter("nodeType", upgradedNode.getClass().getSimpleName()).setParameter("ids", nodeInstanceIds).setParameter("processInstanceId", nodeInstance.getProcessInstance().getId());
int nodesUpdated = nodeLogQuery.executeUpdate();
report.addEntry(Type.INFO, "Mapping: Node instance logs updated = " + nodesUpdated + " for node instance id " + nodeInstance.getId());
if (upgradedNode instanceof HumanTaskNode && nodeInstance instanceof HumanTaskNodeInstance) {
Long taskId = (Long) em.createQuery("select id from TaskImpl where workItemId = :workItemId").setParameter("workItemId", ((HumanTaskNodeInstance) nodeInstance).getWorkItemId()).getSingleResult();
String name = ((HumanTaskNode) upgradedNode).getName();
String description = (String) ((HumanTaskNode) upgradedNode).getWork().getParameter("Description");
// update task audit instance log with new deployment and process id
Query auditTaskLogQuery = em.createQuery("update AuditTaskImpl set name = :name, description = :description where taskId = :taskId");
auditTaskLogQuery.setParameter("name", name).setParameter("description", description).setParameter("taskId", taskId);
int auditTaskUpdated = auditTaskLogQuery.executeUpdate();
report.addEntry(Type.INFO, "Mapping: Task audit updated = " + auditTaskUpdated + " for task id " + taskId);
// update task instance log with new deployment and process id
Query taskLogQuery = em.createQuery("update TaskImpl set name = :name, description = :description where id = :taskId");
taskLogQuery.setParameter("name", name).setParameter("description", description).setParameter("taskId", taskId);
int taskUpdated = taskLogQuery.executeUpdate();
report.addEntry(Type.INFO, "Mapping: Task updated = " + taskUpdated + " for task id " + taskId);
}
}
if (nodeInstance instanceof NodeInstanceContainer) {
updateNodeInstances((NodeInstanceContainer) nodeInstance, nodeMapping, nodeContainer, em);
}
}
}
Aggregations