Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
mtitek-process-core-runtime-api
|
ProcessRuntimeUtils.java
References
ProcessRuntimeUtils.java
References
See this java class for examples how to select jBPM entities from the persistent store:
https://github.com/droolsjbpm/jbpm/blob/master/jbpm-runtime-manager/src/main/java/org/jbpm/runtime/manager/impl/mapper/JPAMapper.java
ProcessRuntimeUtils.java
package mtitek.process.runtime.utils; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import org.jbpm.persistence.correlation.CorrelationKeyInfo; import org.jbpm.persistence.correlation.CorrelationPropertyInfo; import org.kie.internal.process.CorrelationKey; import org.kie.internal.process.CorrelationProperty; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import mtitek.process.runtime.exception.ProcessRuntimeException; public class ProcessRuntimeUtils { private static final Logger LOG = LoggerFactory.getLogger(ProcessRuntimeUtils.class); public static Long getProcessInstanceId(final EntityManager entityManager, final String payloadId) throws ProcessRuntimeException { Objects.requireNonNull(payloadId, "The payloadId cannot be null!"); ProcessRuntimeUtils.LOG.trace("getProcessInstanceId: payloadId='{}'", payloadId); final CorrelationKey correlationKey = ProcessRuntimeUtils.getCorrelationKey(payloadId); Optional<Long> optionalProcessInstanceId = ProcessRuntimeUtils.getProcessInstanceId(entityManager, correlationKey); if (!optionalProcessInstanceId.isPresent()) { throw new ProcessRuntimeException("ProcessInstance ID not found for the correlationKey: " + correlationKey); } final Long processInstanceId = optionalProcessInstanceId.get(); ProcessRuntimeUtils.LOG.trace("ProcessInstance ID: '{}'", processInstanceId); return processInstanceId; } public static Optional<Long> getProcessInstanceId(final EntityManager entityManager, final CorrelationKey correlationKey) throws ProcessRuntimeException { Objects.requireNonNull(correlationKey, "The correlationKey cannot be null!"); ProcessRuntimeUtils.LOG.trace("getProcessInstanceId: correlationKey= {}", correlationKey); final List<Object> correlationProperties = new ArrayList<>(); for (final CorrelationProperty<?> property : correlationKey.getProperties()) { correlationProperties.add(property.getValue()); } final TypedQuery<Long> query = entityManager.createNamedQuery("GetProcessInstanceIdByCorrelation", Long.class); query.setParameter("properties", correlationProperties); query.setParameter("elem_count", new Long(correlationProperties.size())); try { final List<Long> resultList = query.getResultList(); if (resultList == null || resultList.isEmpty()) { return Optional.empty(); } else if (resultList.size() > 1) { throw new ProcessRuntimeException( "More than one ProcessInstance ID found for the correlationKey: " + correlationKey); } else { Long processInstanceId = resultList.get(0); if (processInstanceId == null) { return Optional.empty(); } return Optional.of(processInstanceId); } } catch (final RuntimeException e) { throw new ProcessRuntimeException(e); } } public static Optional<Long> getWorkItemId(final EntityManager entityManager, final Long processInstanceId) throws ProcessRuntimeException { Objects.requireNonNull(processInstanceId, "The processInstanceId cannot be null!"); ProcessRuntimeUtils.LOG.trace("getWorkItemId: ProcessInstance ID='{}'", processInstanceId); final TypedQuery<Long> query = entityManager.createNamedQuery("GetWorkItemIdByProcessInstanceId", Long.class); query.setParameter("processInstanceId", processInstanceId); try { final List<Long> resultList = query.getResultList(); if (resultList == null || resultList.isEmpty()) { return Optional.empty(); } else if (resultList.size() > 1) { throw new ProcessRuntimeException( "More than one WorkItem ID found for the ProcessInstance ID: '" + processInstanceId + "'"); } else { return Optional.of(resultList.get(0)); } } catch (final RuntimeException e) { throw new ProcessRuntimeException(e); } } public static CorrelationKey getCorrelationKey(final String payloadId) { Objects.requireNonNull(payloadId, "The payloadId cannot be null!"); ProcessRuntimeUtils.LOG.trace("getCorrelationKey: payloadId='{}'", payloadId); CorrelationKeyInfo correlationKeyInfo = new CorrelationKeyInfo(); correlationKeyInfo.setName(ProcessRuntimeConstants.PROCESS_RUNTIME_PAYLOAD); correlationKeyInfo.addProperty( new CorrelationPropertyInfo(ProcessRuntimeConstants.PROCESS_RUNTIME_PAYLOAD_ID, payloadId)); return correlationKeyInfo; } }
© 2010-2022
mti
tek