Created
June 25, 2015 12:07
-
-
Save akoskm/5ce8fe830ebc8d2cac0c to your computer and use it in GitHub Desktop.
jBPM retrigger last WorkItem command
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.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