use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.
the class InputMarshaller method readWorkItem.
public static WorkItem readWorkItem(MarshallerReaderContext context) throws IOException {
ObjectInputStream stream = context.stream;
WorkItemImpl workItem = new WorkItemImpl();
workItem.setId(stream.readLong());
workItem.setProcessInstanceId(stream.readLong());
workItem.setName(stream.readUTF());
workItem.setState(stream.readInt());
// WorkItem Paramaters
int nbVariables = stream.readInt();
if (nbVariables > 0) {
for (int i = 0; i < nbVariables; i++) {
String name = stream.readUTF();
try {
int index = stream.readInt();
ObjectMarshallingStrategy strategy = null;
// Old way of retrieving strategy objects
if (index >= 0) {
strategy = context.resolverStrategyFactory.getStrategy(index);
if (strategy == null) {
throw new IllegalStateException("No strategy of with index " + index + " available.");
}
} else // New way
if (index == -2) {
String strategyClassName = stream.readUTF();
// fix for backwards compatibility (5.x -> 6.x)
if ("org.drools.marshalling.impl.SerializablePlaceholderResolverStrategy".equals(strategyClassName)) {
strategyClassName = "org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy";
}
strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
if (strategy == null) {
throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
}
}
Object value = strategy.read(stream);
workItem.setParameter(name, value);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not reload variable " + name);
}
}
}
return workItem;
}
use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.
the class RegisterWorkItemHandlerTest method testRegisterWorkItemHandlerWithStatelessSession.
@Test
public void testRegisterWorkItemHandlerWithStatelessSession() {
String str = "package org.kie.workitem.test \n" + "import " + DefaultWorkItemManager.class.getCanonicalName() + "\n" + "import " + WorkItem.class.getCanonicalName() + "\n" + "import " + WorkItemImpl.class.getCanonicalName() + "\n" + "rule r1 when \n" + "then \n" + " WorkItem wi = new WorkItemImpl(); \n" + " wi.setName( \"wihandler\" ); \n" + " DefaultWorkItemManager wim = ( DefaultWorkItemManager ) kcontext.getKieRuntime().getWorkItemManager(); \n" + " wim.internalExecuteWorkItem(wi); \n" + "end \n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final boolean[] answer = new boolean[] { false };
StatelessKieSession ks = kbase.newStatelessKieSession();
ks.execute(CommandFactory.newRegisterWorkItemHandlerCommand(new WorkItemHandler() {
public void executeWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
answer[0] = true;
}
public void abortWorkItem(org.kie.api.runtime.process.WorkItem workItem, WorkItemManager manager) {
// TODO Auto-generated method stub
}
}, "wihandler"));
assertTrue(answer[0]);
}
use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.
the class JPAWorkItemManager method abortWorkItem.
public void abortWorkItem(long id) {
PersistenceContext context = getPersistenceContext();
WorkItemInfo workItemInfo = null;
if (this.workItems != null) {
workItemInfo = this.workItems.get(id);
if (workItemInfo != null) {
workItemInfo = (WorkItemInfo) context.merge(workItemInfo);
}
}
if (workItemInfo == null) {
workItemInfo = (WorkItemInfo) context.findWorkItem(id);
}
// work item may have been aborted
if (workItemInfo != null) {
WorkItem workItem = (WorkItemImpl) internalGetWorkItem(workItemInfo);
ProcessInstance processInstance = kruntime.getProcessInstance(workItem.getProcessInstanceId());
workItem.setState(WorkItem.ABORTED);
// process instance may have finished already
if (processInstance != null) {
processInstance.signalEvent("workItemAborted", workItem);
}
context.remove(workItemInfo);
if (workItems != null) {
workItems.remove(workItem.getId());
}
}
}
use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.
the class JPAWorkItemManager method internalExecuteWorkItem.
public void internalExecuteWorkItem(WorkItem workItem) {
Environment env = this.kruntime.getEnvironment();
WorkItemInfo workItemInfo = new WorkItemInfo(workItem, env);
PersistenceContext context = getPersistenceContext();
workItemInfo = (WorkItemInfo) context.persist(workItemInfo);
((WorkItemImpl) workItem).setId(workItemInfo.getId());
if (this.workItems == null) {
this.workItems = new HashMap<Long, WorkItemInfo>();
}
workItems.put(workItem.getId(), workItemInfo);
WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
if (handler != null) {
handler.executeWorkItem(workItem, this);
} else {
throwWorkItemNotFoundException(workItem);
}
}
use of org.drools.core.process.instance.impl.WorkItemImpl in project drools by kiegroup.
the class JPAWorkItemManager method internalAbortWorkItem.
public void internalAbortWorkItem(long id) {
PersistenceContext context = getPersistenceContext();
WorkItemInfo workItemInfo = (WorkItemInfo) context.findWorkItem(id);
// work item may have been aborted
if (workItemInfo != null) {
WorkItemImpl workItem = (WorkItemImpl) internalGetWorkItem(workItemInfo);
WorkItemHandler handler = (WorkItemHandler) this.workItemHandlers.get(workItem.getName());
if (handler != null) {
handler.abortWorkItem(workItem, this);
} else {
if (workItems != null) {
workItems.remove(id);
throwWorkItemNotFoundException(workItem);
}
}
if (workItems != null) {
workItems.remove(id);
}
context.remove(workItemInfo);
}
}
Aggregations