Search in sources :

Example 1 with WorkItemInfo

use of org.drools.persistence.info.WorkItemInfo 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());
        }
    }
}
Also used : WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) PersistenceContext(org.drools.persistence.api.PersistenceContext) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItem(org.drools.core.process.instance.WorkItem) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Example 2 with WorkItemInfo

use of org.drools.persistence.info.WorkItemInfo in project drools by kiegroup.

the class JPAWorkItemManager method getWorkItem.

public WorkItem getWorkItem(long id) {
    PersistenceContext context = getPersistenceContext();
    WorkItemInfo workItemInfo = null;
    if (this.workItems != null) {
        workItemInfo = this.workItems.get(id);
    }
    if (this.pessimisticLocking && workItemInfo != null) {
        context.lock(workItemInfo);
    }
    if (workItemInfo == null && context != null) {
        workItemInfo = (WorkItemInfo) context.findWorkItem(id);
    }
    if (workItemInfo == null) {
        return null;
    }
    return internalGetWorkItem(workItemInfo);
}
Also used : PersistenceContext(org.drools.persistence.api.PersistenceContext) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Example 3 with WorkItemInfo

use of org.drools.persistence.info.WorkItemInfo 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);
    }
}
Also used : WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) Environment(org.kie.api.runtime.Environment) PersistenceContext(org.drools.persistence.api.PersistenceContext) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Example 4 with WorkItemInfo

use of org.drools.persistence.info.WorkItemInfo in project drools by kiegroup.

the class JpaPersistenceContext method merge.

public PersistentWorkItem merge(PersistentWorkItem workItem) {
    if (this.pessimisticLocking) {
        if (em.contains(workItem)) {
            em.lock(workItem, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
        } else {
            // Yes, this is a hack, but for detached entities, it's the only way to lock before merging
            WorkItemInfo dbWorkItemInfo = em.find(WorkItemInfo.class, workItem.getId(), LockModeType.PESSIMISTIC_FORCE_INCREMENT);
            for (Field field : WorkItemInfo.class.getDeclaredFields()) {
                boolean access = field.isAccessible();
                field.setAccessible(true);
                try {
                    field.set(dbWorkItemInfo, field.get(workItem));
                } catch (Exception e) {
                    logger.error("Unable to set field " + field.getName() + " of unmerged WorkItemInfo instance!", e);
                }
                field.setAccessible(access);
            }
        }
    }
    TransactionManagerHelper.addToUpdatableSet(txm, workItem);
    return em.merge(workItem);
}
Also used : Field(java.lang.reflect.Field) WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Example 5 with WorkItemInfo

use of org.drools.persistence.info.WorkItemInfo in project drools by kiegroup.

the class JpaPersistenceContext method findWorkItem.

public PersistentWorkItem findWorkItem(Long id) {
    WorkItemInfo workItemInfo = null;
    if (this.pessimisticLocking) {
        workItemInfo = this.em.find(WorkItemInfo.class, id, LockModeType.PESSIMISTIC_FORCE_INCREMENT);
        TransactionManagerHelper.addToUpdatableSet(txm, workItemInfo);
        return workItemInfo;
    }
    workItemInfo = em.find(WorkItemInfo.class, id);
    TransactionManagerHelper.addToUpdatableSet(txm, workItemInfo);
    return workItemInfo;
}
Also used : WorkItemInfo(org.drools.persistence.info.WorkItemInfo)

Aggregations

WorkItemInfo (org.drools.persistence.info.WorkItemInfo)8 PersistenceContext (org.drools.persistence.api.PersistenceContext)6 WorkItem (org.drools.core.process.instance.WorkItem)3 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)3 Environment (org.kie.api.runtime.Environment)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 WorkItemHandler (org.kie.api.runtime.process.WorkItemHandler)2 Field (java.lang.reflect.Field)1