Search in sources :

Example 1 with Policy

use of org.kie.kogito.process.workitem.Policy in project kogito-runtimes by kiegroup.

the class BaseHumanTaskLifeCycle method transitionTo.

@Override
public Map<String, Object> transitionTo(KogitoWorkItem workItem, KogitoWorkItemManager manager, Transition<Map<String, Object>> transition) {
    logger.debug("Transition method invoked for work item {} to transition to {}, currently in phase {} and status {}", workItem.getStringId(), transition.phase(), workItem.getPhaseId(), workItem.getPhaseStatus());
    HumanTaskWorkItemImpl humanTaskWorkItem = (HumanTaskWorkItemImpl) workItem;
    LifeCyclePhase targetPhase = phases.get(transition.phase());
    if (targetPhase == null) {
        logger.debug("Target life cycle phase '{}' does not exist in {}", transition.phase(), this.getClass().getSimpleName());
        throw new InvalidLifeCyclePhaseException(transition.phase());
    }
    LifeCyclePhase currentPhase = phases.get(humanTaskWorkItem.getPhaseId());
    if (!targetPhase.canTransition(currentPhase)) {
        logger.debug("Target life cycle phase '{}' cannot transition from current state '{}'", targetPhase.id(), currentPhase.id());
        throw new InvalidTransitionException("Cannot transition from " + humanTaskWorkItem.getPhaseId() + " to " + targetPhase.id());
    }
    if (!targetPhase.id().equals(Active.ID) && !targetPhase.id().equals(Abort.ID) && !humanTaskWorkItem.enforce(transition.policies().toArray(new Policy[transition.policies().size()]))) {
        throw new NotAuthorizedException("User is not authorized to access task instance with id " + humanTaskWorkItem.getStringId());
    }
    humanTaskWorkItem.setPhaseId(targetPhase.id());
    humanTaskWorkItem.setPhaseStatus(targetPhase.status());
    targetPhase.apply(humanTaskWorkItem, transition);
    if (transition.data() != null) {
        logger.debug("Updating data for phase {} and work item {}", targetPhase.id(), humanTaskWorkItem.getStringId());
        humanTaskWorkItem.getResults().putAll(transition.data());
    }
    logger.debug("Transition for work item {} to {} done, currently in phase {} and status {}", workItem.getStringId(), transition.phase(), workItem.getPhaseId(), workItem.getPhaseStatus());
    if (targetPhase.isTerminating()) {
        logger.debug("Target life cycle phase '{}' is terminiating, completing work item {}", targetPhase.id(), humanTaskWorkItem.getStringId());
        // since target life cycle phase is terminating completing work item
        ((InternalKogitoWorkItemManager) manager).internalCompleteWorkItem(humanTaskWorkItem);
    }
    return data(humanTaskWorkItem);
}
Also used : InvalidTransitionException(org.kie.kogito.process.workitem.InvalidTransitionException) Policy(org.kie.kogito.process.workitem.Policy) InvalidLifeCyclePhaseException(org.kie.kogito.process.workitem.InvalidLifeCyclePhaseException) InternalKogitoWorkItemManager(org.kie.kogito.process.workitems.InternalKogitoWorkItemManager) NotAuthorizedException(org.kie.kogito.process.workitem.NotAuthorizedException) LifeCyclePhase(org.kie.kogito.process.workitem.LifeCyclePhase)

Example 2 with Policy

use of org.kie.kogito.process.workitem.Policy in project kogito-runtimes by kiegroup.

the class TaskManagementService method updateTask.

@Override
public TaskInfo updateTask(String processId, String processInstanceId, String taskId, TaskInfo taskInfo, boolean shouldReplace, Policy<?>... policies) {
    ProcessInstance<?> pi = getProcessInstance(processId, processInstanceId, taskId);
    KogitoWorkItem workItem = UnitOfWorkExecutor.executeInUnitOfWork(processConfig.unitOfWorkManager(), () -> pi.updateWorkItem(taskId, wi -> {
        HumanTaskWorkItemImpl humanTask = HumanTaskHelper.asHumanTask(wi);
        setField(humanTask::setAdminGroups, taskInfo::getAdminGroups, shouldReplace);
        setField(humanTask::setAdminUsers, taskInfo::getAdminUsers, shouldReplace);
        setField(humanTask::setExcludedUsers, taskInfo::getExcludedUsers, shouldReplace);
        setField(humanTask::setPotentialUsers, taskInfo::getPotentialUsers, shouldReplace);
        setField(humanTask::setPotentialGroups, taskInfo::getPotentialGroups, shouldReplace);
        setField(humanTask::setTaskPriority, taskInfo::getPriority, shouldReplace);
        setField(humanTask::setTaskDescription, taskInfo::getDescription, shouldReplace);
        setMap(humanTask::setParameters, humanTask::setParameter, taskInfo.getInputParams(), shouldReplace);
        return wi;
    }, policies));
    return convert((HumanTaskWorkItem) workItem);
}
Also used : Policy(org.kie.kogito.process.workitem.Policy) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) ProcessConfig(org.kie.kogito.process.ProcessConfig) HumanTaskHelper(org.jbpm.process.instance.impl.humantask.HumanTaskHelper) UnitOfWorkExecutor(org.kie.kogito.services.uow.UnitOfWorkExecutor) Supplier(java.util.function.Supplier) Consumer(java.util.function.Consumer) HumanTaskWorkItemImpl(org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemImpl) ProcessInstance(org.kie.kogito.process.ProcessInstance) Map(java.util.Map) Entry(java.util.Map.Entry) BiConsumer(java.util.function.BiConsumer) Process(org.kie.kogito.process.Process) HumanTaskWorkItem(org.kie.kogito.process.workitem.HumanTaskWorkItem) Processes(org.kie.kogito.process.Processes) ProcessInstanceNotFoundException(org.kie.kogito.process.ProcessInstanceNotFoundException) KogitoWorkItem(org.kie.kogito.internal.process.runtime.KogitoWorkItem) HumanTaskWorkItemImpl(org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemImpl)

Example 3 with Policy

use of org.kie.kogito.process.workitem.Policy in project kogito-runtimes by kiegroup.

the class JsonSchemaUtilTest method testJsonSchemaPhases.

@Test
<T> void testJsonSchemaPhases() throws IOException {
    InputStream in = new ByteArrayInputStream(example.getBytes());
    Policy<T>[] policies = new Policy[0];
    Map<String, Object> schemaMap = JsonSchemaUtil.load(in);
    in.close();
    Process<T> process = mock(Process.class);
    ProcessInstances<T> processInstances = mock(ProcessInstances.class);
    when(process.instances()).thenReturn(processInstances);
    ProcessInstance<T> processInstance = mock(ProcessInstance.class);
    when(processInstances.findById("pepe", ProcessInstanceReadMode.READ_ONLY)).thenReturn((Optional) Optional.of(processInstance));
    WorkItem task = mock(WorkItem.class);
    when(processInstance.workItem("task", policies)).thenReturn(task);
    when(task.getPhase()).thenReturn("active");
    Config config = mock(Config.class);
    ProcessConfig processConfig = mock(ProcessConfig.class);
    when(config.get(any())).thenReturn(processConfig);
    WorkItemHandlerConfig workItemHandlerConfig = mock(WorkItemHandlerConfig.class);
    when(processConfig.workItemHandlers()).thenReturn(workItemHandlerConfig);
    KogitoWorkItemHandler workItemHandler = new HumanTaskWorkItemHandler();
    when(workItemHandlerConfig.forName("Human Task")).thenReturn(workItemHandler);
    schemaMap = JsonSchemaUtil.addPhases(process, workItemHandler, "pepe", "task", policies, schemaMap);
    assertFalse(((Collection) schemaMap.get("phases")).isEmpty());
}
Also used : Policy(org.kie.kogito.process.workitem.Policy) HumanTaskWorkItemHandler(org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemHandler) ProcessConfig(org.kie.kogito.process.ProcessConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Config(org.kie.kogito.Config) ProcessConfig(org.kie.kogito.process.ProcessConfig) WorkItemHandlerConfig(org.kie.kogito.process.WorkItemHandlerConfig) KogitoWorkItemHandler(org.kie.kogito.internal.process.runtime.KogitoWorkItemHandler) WorkItem(org.kie.kogito.process.WorkItem) WorkItemHandlerConfig(org.kie.kogito.process.WorkItemHandlerConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

Policy (org.kie.kogito.process.workitem.Policy)3 ProcessConfig (org.kie.kogito.process.ProcessConfig)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Supplier (java.util.function.Supplier)1 HumanTaskHelper (org.jbpm.process.instance.impl.humantask.HumanTaskHelper)1 HumanTaskWorkItemHandler (org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemHandler)1 HumanTaskWorkItemImpl (org.jbpm.process.instance.impl.humantask.HumanTaskWorkItemImpl)1 Test (org.junit.jupiter.api.Test)1 Config (org.kie.kogito.Config)1 KogitoWorkItem (org.kie.kogito.internal.process.runtime.KogitoWorkItem)1 KogitoWorkItemHandler (org.kie.kogito.internal.process.runtime.KogitoWorkItemHandler)1 Process (org.kie.kogito.process.Process)1 ProcessInstance (org.kie.kogito.process.ProcessInstance)1 ProcessInstanceNotFoundException (org.kie.kogito.process.ProcessInstanceNotFoundException)1 Processes (org.kie.kogito.process.Processes)1