Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
mtitek-process-core-workitems-api
|
WorkItemHandlerDefault.java
WorkItemHandlerDefault.java
WorkItemHandlerDefault.java
package mtitek.process.workItems; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import org.kie.api.runtime.process.WorkItem; import org.kie.api.runtime.process.WorkItemHandler; import org.kie.api.runtime.process.WorkItemManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import mtitek.process.runtime.ProcessRuntimePayload; import mtitek.process.runtime.utils.ProcessRuntimeConstants; import mtitek.process.workItems.exception.WorkItemHandlerRuntimeException; public class WorkItemHandlerDefault implements WorkItemHandler { private static final Logger LOG = LoggerFactory.getLogger(WorkItemHandlerDefault.class); public static final String PROCESS_RESULTS = "process_results"; public static final String WORKITEM_HANDLER_RESULTS = "workitem_handler_results"; public static final String PROCESS_SUSPEND = "suspend"; @Override public void executeWorkItem(final WorkItem workItem, final WorkItemManager workItemManager) { final ProcessRuntimePayload processRuntimePayload; final Object object = workItem.getParameters().get(ProcessRuntimeConstants.PROCESS_RUNTIME_PAYLOAD_PARAM_IN); if (object == null) { processRuntimePayload = null; } else if (object instanceof ProcessRuntimePayload) { processRuntimePayload = (ProcessRuntimePayload) object; } else { throw new WorkItemHandlerRuntimeException("The process runtime payload is invalid!"); } final Map<String, Map<String, Object>> results = execute(processRuntimePayload, workItem.getParameters()); final Map<String, Object> processResults = results.get(WorkItemHandlerDefault.PROCESS_RESULTS); final Map<String, Object> workItemHandlerResults = results.get(WorkItemHandlerDefault.WORKITEM_HANDLER_RESULTS); if (completeWorkItem(workItemHandlerResults)) { workItemManager.completeWorkItem(workItem.getId(), processResults); } } @Override public void abortWorkItem(final WorkItem workItem, final WorkItemManager workItemManager) { workItemManager.abortWorkItem(workItem.getId()); } protected Map<String, Map<String, Object>> execute(final ProcessRuntimePayload processRuntimePayload, final Map<String, Object> workItemParameters) { Objects.requireNonNull(processRuntimePayload, "The processRuntimePayload cannot be null!"); WorkItemHandlerDefault.LOG.trace("executeWorkItem: workItemParameters= {}", workItemParameters); Map<String, Object> workitemHandlerResults = execute(processRuntimePayload.getPayloadId(), processRuntimePayload.getPayload(), processRuntimePayload.getParameters(), workItemParameters); Map<String, Map<String, Object>> results = new ConcurrentHashMap<>(); results.put(WorkItemHandlerDefault.WORKITEM_HANDLER_RESULTS, workitemHandlerResults); results.put(WorkItemHandlerDefault.PROCESS_RESULTS, workItemParameters); return results; } protected Map<String, Object> execute(final String payloadId, final Object payload, final Map<String, String> payloadParameters, final Map<String, Object> workItemParameters) { Map<String, Object> workitemHandlerResults = new ConcurrentHashMap<>(); if (execute(payloadId, payload, payloadParameters)) { workitemHandlerResults.put(WorkItemHandlerDefault.PROCESS_SUSPEND, Boolean.TRUE); } return workitemHandlerResults; } protected boolean execute(final String payloadId, final Object payload, final Map<String, String> payloadParameters) { return false; } private boolean completeWorkItem(final Map<String, Object> workItemHandlerResults) { if (workItemHandlerResults == null) { return true; } final Object object = workItemHandlerResults.get(WorkItemHandlerDefault.PROCESS_SUSPEND); if (!(object instanceof Boolean) || Boolean.FALSE.equals(object)) { return true; } return false; } }
© 2010-2022
mti
tek