Skip to content

Instantly share code, notes, and snippets.

@akoskm
Created June 25, 2015 12:07
Show Gist options
  • Save akoskm/5ce8fe830ebc8d2cac0c to your computer and use it in GitHub Desktop.
Save akoskm/5ce8fe830ebc8d2cac0c to your computer and use it in GitHub Desktop.
jBPM retrigger last WorkItem command
package com.mycompany.sample.jbpm.command;
import org.drools.core.command.impl.GenericCommand;
import org.drools.core.command.impl.KnowledgeCommandContext;
import org.drools.core.process.instance.WorkItem;
import org.jbpm.workflow.instance.node.WorkItemNodeInstance;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.process.NodeInstance;
import org.kie.api.runtime.process.WorkflowProcessInstance;
import org.kie.internal.command.Context;
import com.mycompany.sample.jbpm.dto.AppSession;
import java.util.Collection;
import java.util.NoSuchElementException;
public class ReTriggerWorkItemCommand implements GenericCommand<WorkItem> {
private final Long processInstanceId;
public ReTriggerWorkItemCommand(Long processInstanceId) {
this.processInstanceId = processInstanceId;
}
public ReTriggerWorkItemCommand(AppSession appSession) {
if (appSession == null) {
throw new IllegalArgumentException("AppSession can not be null");
}
this.processInstanceId = appSession.getProcessInstanceId();
}
@Override
public WorkItem execute(Context context) {
WorkItem workItem = null;
KieSession ksession = ((KnowledgeCommandContext) context).getKieSession();
WorkflowProcessInstance pi = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId);
if (pi != null) {
Collection<NodeInstance> nodes = pi.getNodeInstances();
try {
NodeInstance nodeInstance = nodes.iterator().next();
WorkItemNodeInstance workItemNodeInstance = (WorkItemNodeInstance) nodeInstance;
workItem = workItemNodeInstance.getWorkItem();
switch(workItem.getState()) {
case WorkItem.COMPLETED:
workItemNodeInstance.workItemCompleted(workItem);
break;
case WorkItem.ABORTED:
workItemNodeInstance.workItemAborted(workItem);
break;
default:
workItemNodeInstance.retrigger(true);
}
} catch (NoSuchElementException nsee) {
/**
* this is about retrieving the currently active
* node. if it fails the process is probably
* isn't working but the execution status
* remained active for some reason. This makes
* the client to check for the currently active
* node constantly.
*/
}
}
return workItem;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment