Last active
April 24, 2018 14:39
-
-
Save danielezonca/43b3390fad0bb773c57ef835f9f0ae46 to your computer and use it in GitHub Desktop.
WorkItem instance reset (jBPM 6.x)
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 org.jbpm.kie.test; | |
import java.util.Collection; | |
import org.drools.core.command.impl.GenericCommand; | |
import org.drools.core.command.impl.KnowledgeCommandContext; | |
import org.jbpm.kie.services.impl.KModuleDeploymentUnit; | |
import org.jbpm.services.api.ProcessService; | |
import org.jbpm.services.api.model.DeploymentUnit; | |
import org.jbpm.workflow.instance.WorkflowProcessInstance; | |
import org.jbpm.workflow.instance.node.WorkItemNodeInstance; | |
import org.kie.api.runtime.KieSession; | |
import org.kie.api.runtime.process.NodeInstance; | |
import org.kie.internal.command.Context; | |
import org.kie.internal.runtime.manager.context.ProcessInstanceIdContext; | |
/** | |
* Use {@link #resetWorkItemId(long, ProcessService, String, String, String, boolean)} to retrigger all working items | |
* that are not valid. | |
* Change {@link #isBrokenWI(WorkItemNodeInstance, Long)} to identify broken WorkingItems | |
* You should already have a ProcessService instance (via CDI or remote EJB) | |
*/ | |
public class WorkItemNPE { | |
public static void resetWorkItemId(long processInstanceId, ProcessService processService, String groupId, String artifactId, String versionId, boolean cancelBefore) { | |
DeploymentUnit deploymentUnit = new KModuleDeploymentUnit(groupId, artifactId, versionId); | |
processService.execute(deploymentUnit.getIdentifier(), ProcessInstanceIdContext.get(processInstanceId), new RetriggerCommand(processInstanceId, cancelBefore)); | |
} | |
private static class RetriggerCommand implements GenericCommand<Void> { | |
private final long processInstanceId; | |
private final boolean cancelBefore; | |
RetriggerCommand(long processInstanceId, boolean cancelBefore) { | |
this.processInstanceId = processInstanceId; | |
this.cancelBefore = cancelBefore; | |
} | |
@Override | |
public Void execute(Context context) { | |
KieSession ksession = ((KnowledgeCommandContext) context).getKieSession(); | |
WorkflowProcessInstance pi = (WorkflowProcessInstance) ksession.getProcessInstance(processInstanceId, true); | |
Collection<NodeInstance> instances = pi.getNodeInstances(); | |
for (NodeInstance ni : instances) { | |
if (ni instanceof WorkItemNodeInstance) { | |
WorkItemNodeInstance wini = (WorkItemNodeInstance) ni; | |
if(isBrokenWI(wini, processInstanceId)) { | |
wini.retrigger(cancelBefore); | |
} | |
} | |
} | |
return null; | |
} | |
} | |
// TODO is this enough? | |
private static boolean isBrokenWI(WorkItemNodeInstance wi, Long processInstanceId) { | |
return wi.getId() == -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment