Search in sources :

Example 1 with PlanningItemList

use of org.kie.server.api.model.taskassigning.PlanningItemList in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeServiceBase method calculatePlanningCommands.

private Map<String, List<PlanningCommand>> calculatePlanningCommands(PlanningItemList planningItemList, String userId) {
    final Map<String, List<PlanningCommand>> commandsByContainer = new HashMap<>();
    final Map<Long, TaskData> taskDataById = prepareTaskDataForExecutePlanning();
    for (PlanningItem planningItem : planningItemList.getItems()) {
        final TaskData taskData = taskDataById.remove(planningItem.getTaskId());
        if (taskData == null) {
            // and a new plan will arrive soon.
            throw new PlanningException(String.format(TASK_MODIFIED_ERROR_MSG_3, planningItem.getPlanningTask().getTaskId(), Arrays.toString(new Status[] { Ready, Reserved, InProgress, Suspended })), planningItem.getContainerId(), PlanningExecutionResult.ErrorCode.TASK_MODIFIED_SINCE_PLAN_CALCULATION_ERROR);
        }
        final String actualOwner = taskData.getActualOwner();
        final PlanningTask actualPlanningTask = taskData.getPlanningTask();
        final Status taskStatus = convertFromString(taskData.getStatus());
        if (isNotEmpty(actualOwner) && actualPlanningTask != null && actualOwner.equals(actualPlanningTask.getAssignedUser()) && actualPlanningTask.equals(planningItem.getPlanningTask())) {
            continue;
        }
        switch(taskStatus) {
            case Ready:
                addCommand(commandsByContainer, planningItem.getContainerId(), new DelegateAndSaveCommand(planningItem, userId));
                break;
            case Reserved:
                if (actualPlanningTask != null && !actualOwner.equals(actualPlanningTask.getAssignedUser()) && !actualOwner.equals(planningItem.getPlanningTask().getAssignedUser())) {
                    // and a new plan will arrive soon.
                    throw new PlanningException(String.format(TASK_MODIFIED_ERROR_MSG_1, planningItem.getPlanningTask().getTaskId(), actualOwner, actualPlanningTask.getAssignedUser()), planningItem.getContainerId(), PlanningExecutionResult.ErrorCode.TASK_MODIFIED_SINCE_PLAN_CALCULATION_ERROR);
                } else {
                    addCommand(commandsByContainer, planningItem.getContainerId(), new DelegateAndSaveCommand(planningItem, userId));
                }
                break;
            case InProgress:
            case Suspended:
                if (actualOwner == null || !actualOwner.equals(planningItem.getPlanningTask().getAssignedUser())) {
                    // and a new plan will arrive soon.
                    throw new PlanningException(String.format(TASK_MODIFIED_ERROR_MSG_2, planningItem.getPlanningTask().getTaskId(), actualOwner, planningItem.getPlanningTask().getAssignedUser()), planningItem.getContainerId(), PlanningExecutionResult.ErrorCode.TASK_MODIFIED_SINCE_PLAN_CALCULATION_ERROR);
                } else {
                    // task might have been created, assigned and started/suspended completely out of the task
                    // or the planning data might have changed. Just update the planning data.
                    addCommand(commandsByContainer, planningItem.getContainerId(), new SavePlanningItemCommand(planningItem));
                }
                break;
            default:
                // sonar required, no more cases are expected for this switch by construction.
                throw new IndexOutOfBoundsException("Value: " + taskData.getStatus() + " is out of range in current switch");
        }
    }
    for (TaskData taskData : taskDataById.values()) {
        final Status status = convertFromString(taskData.getStatus());
        if ((status == Ready || status == Reserved || status == Suspended) && taskData.getPlanningTask() != null) {
            commandsByContainer.computeIfAbsent(taskData.getContainerId(), k -> new ArrayList<>()).add(new DeletePlanningItemCommand(taskData.getTaskId()));
        }
    }
    return commandsByContainer;
}
Also used : KieContainerStatus(org.kie.server.api.model.KieContainerStatus) Status(org.kie.api.task.model.Status) KieServicesException(org.kie.server.api.exception.KieServicesException) Arrays(java.util.Arrays) PlanningExecutionResult(org.kie.server.api.model.taskassigning.PlanningExecutionResult) CompositeCommand(org.jbpm.services.task.commands.CompositeCommand) QueryService(org.jbpm.services.api.query.QueryService) SavePlanningItemCommand(org.kie.server.services.taskassigning.runtime.command.SavePlanningItemCommand) LoggerFactory(org.slf4j.LoggerFactory) Reserved(org.kie.api.task.model.Status.Reserved) HashMap(java.util.HashMap) Function(java.util.function.Function) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) InProgress(org.kie.api.task.model.Status.InProgress) ArrayList(java.util.ArrayList) TaskData(org.kie.server.api.model.taskassigning.TaskData) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) DeletePlanningItemCommand(org.kie.server.services.taskassigning.runtime.command.DeletePlanningItemCommand) PlanningCommand(org.kie.server.services.taskassigning.runtime.command.PlanningCommand) Map(java.util.Map) PlanningItem(org.kie.server.api.model.taskassigning.PlanningItem) UserTaskService(org.jbpm.services.api.UserTaskService) KieContainerStatus(org.kie.server.api.model.KieContainerStatus) Suspended(org.kie.api.task.model.Status.Suspended) Ready(org.kie.api.task.model.Status.Ready) Logger(org.slf4j.Logger) PlanningItemList(org.kie.server.api.model.taskassigning.PlanningItemList) PlanningTask(org.kie.server.api.model.taskassigning.PlanningTask) StopWatch(org.apache.commons.lang3.time.StopWatch) StatusConverter.convertFromString(org.kie.server.api.model.taskassigning.util.StatusConverter.convertFromString) StatusConverter.convertToStringList(org.kie.server.api.model.taskassigning.util.StatusConverter.convertToStringList) Collectors(java.util.stream.Collectors) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) List(java.util.List) DelegateAndSaveCommand(org.kie.server.services.taskassigning.runtime.command.DelegateAndSaveCommand) PlanningException(org.kie.server.services.taskassigning.runtime.command.PlanningException) Context(org.kie.api.runtime.Context) TaskCommand(org.jbpm.services.task.commands.TaskCommand) KieServerImpl(org.kie.server.services.impl.KieServerImpl) Status(org.kie.api.task.model.Status) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StatusConverter.convertFromString(org.kie.server.api.model.taskassigning.util.StatusConverter.convertFromString) DelegateAndSaveCommand(org.kie.server.services.taskassigning.runtime.command.DelegateAndSaveCommand) TaskData(org.kie.server.api.model.taskassigning.TaskData) SavePlanningItemCommand(org.kie.server.services.taskassigning.runtime.command.SavePlanningItemCommand) PlanningTask(org.kie.server.api.model.taskassigning.PlanningTask) DeletePlanningItemCommand(org.kie.server.services.taskassigning.runtime.command.DeletePlanningItemCommand) PlanningException(org.kie.server.services.taskassigning.runtime.command.PlanningException) ArrayList(java.util.ArrayList) PlanningItemList(org.kie.server.api.model.taskassigning.PlanningItemList) StatusConverter.convertToStringList(org.kie.server.api.model.taskassigning.util.StatusConverter.convertToStringList) List(java.util.List) PlanningItem(org.kie.server.api.model.taskassigning.PlanningItem)

Example 2 with PlanningItemList

use of org.kie.server.api.model.taskassigning.PlanningItemList in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeServiceBaseTest method unexpectedErrorDuringPlanCalculation.

@Test
public void unexpectedErrorDuringPlanCalculation() {
    when(queryHelper.readTasksDataSummary(anyLong(), any(), anyInt())).thenThrow(new RuntimeException(ERROR_MESSAGE));
    PlanningExecutionResult result = serviceBase.executePlanning(new PlanningItemList(Collections.emptyList()), USER_ID);
    assertHasError(result, PlanningExecutionResult.ErrorCode.UNEXPECTED_ERROR, String.format(UNEXPECTED_ERROR_DURING_PLAN_CALCULATION, ERROR_MESSAGE), null);
}
Also used : PlanningItemList(org.kie.server.api.model.taskassigning.PlanningItemList) PlanningExecutionResult(org.kie.server.api.model.taskassigning.PlanningExecutionResult) Test(org.junit.Test)

Example 3 with PlanningItemList

use of org.kie.server.api.model.taskassigning.PlanningItemList in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeServiceBaseTest method executePlanningWithTaskInInProgressOrSuspendedStatusWithActualOwnerUnChanged.

private void executePlanningWithTaskInInProgressOrSuspendedStatusWithActualOwnerUnChanged(Status status) {
    TaskData taskData = mockTaskData(TASK_ID, status, ASSIGNED_USER_ID, null);
    List<TaskData> taskDataList = Collections.singletonList(taskData);
    PlanningItem planningItem = mockPlanningItem(TASK_ID, CONTAINER_ID, ASSIGNED_USER_ID);
    PlanningItemList planningItemList = new PlanningItemList(Collections.singletonList(planningItem));
    prepareExecution(taskDataList, CONTAINER_ID);
    PlanningExecutionResult result = serviceBase.executePlanning(planningItemList, USER_ID);
    verify(userTaskService).execute(eq(CONTAINER_ID), planningCommandCaptor.capture());
    CompositeCommand compositeCommand = (CompositeCommand) planningCommandCaptor.getValue();
    assertSavePlanningItemCommand(compositeCommand.getCommands(), 0, planningItem);
    assertNoError(result);
}
Also used : PlanningItemList(org.kie.server.api.model.taskassigning.PlanningItemList) PlanningExecutionResult(org.kie.server.api.model.taskassigning.PlanningExecutionResult) PlanningItem(org.kie.server.api.model.taskassigning.PlanningItem) TaskData(org.kie.server.api.model.taskassigning.TaskData) CompositeCommand(org.jbpm.services.task.commands.CompositeCommand)

Example 4 with PlanningItemList

use of org.kie.server.api.model.taskassigning.PlanningItemList in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeServiceBaseTest method executePlanningWithTaskInReservedStatusWithNoPlanningTask.

@Test
public void executePlanningWithTaskInReservedStatusWithNoPlanningTask() {
    TaskData taskData = mockTaskData(TASK_ID, Reserved);
    List<TaskData> taskDataList = Collections.singletonList(taskData);
    PlanningItem planningItem = mockPlanningItem(TASK_ID, CONTAINER_ID, ASSIGNED_USER_ID);
    PlanningItemList planningItemList = new PlanningItemList(Collections.singletonList(planningItem));
    prepareExecution(taskDataList, CONTAINER_ID);
    PlanningExecutionResult result = serviceBase.executePlanning(planningItemList, USER_ID);
    verify(userTaskService).execute(eq(CONTAINER_ID), planningCommandCaptor.capture());
    assertDelegateAndSaveCommand(planningCommandCaptor.getAllValues(), 0, USER_ID, planningItem);
    assertNoError(result);
}
Also used : PlanningItemList(org.kie.server.api.model.taskassigning.PlanningItemList) PlanningExecutionResult(org.kie.server.api.model.taskassigning.PlanningExecutionResult) PlanningItem(org.kie.server.api.model.taskassigning.PlanningItem) TaskData(org.kie.server.api.model.taskassigning.TaskData) Test(org.junit.Test)

Example 5 with PlanningItemList

use of org.kie.server.api.model.taskassigning.PlanningItemList in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeResource method executePlanning.

@ApiOperation(value = "Executes a planning into the processes runtime.", notes = "This operation is intended for the task assigning integration implementation, third parties should avoid using it.", response = PlanningExecutionResult.class)
@POST
@Path(TASK_ASSIGNING_EXECUTE_PLANNING_URI)
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response executePlanning(@javax.ws.rs.core.Context HttpHeaders headers, @ApiParam(value = "identifier of the user to execute the planning on behalf of", required = true) @QueryParam("user") String userId, @ApiParam(value = "planning definition represented as PlanningItemList", required = true) String payload) {
    final Variant v = getVariant(headers);
    // no container id available so only used to transfer conversation id if given by client
    final Header conversationIdHeader = buildConversationIdHeader("", context, headers);
    try {
        final String contentType = getContentType(headers);
        final PlanningItemList planningItemList = marshallerHelper.unmarshal(payload, contentType, PlanningItemList.class);
        final PlanningExecutionResult result = runtimeServiceBase.executePlanning(planningItemList, userId);
        return createCorrectVariant(result, headers, Response.Status.OK, conversationIdHeader);
    } catch (Exception e) {
        LOGGER.error("Unexpected error executing planning {}", e.getMessage(), e);
        return internalServerError(errorMessage(e), v);
    }
}
Also used : RestUtils.createCorrectVariant(org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant) RestUtils.getVariant(org.kie.server.remote.rest.common.util.RestUtils.getVariant) Variant(javax.ws.rs.core.Variant) PlanningItemList(org.kie.server.api.model.taskassigning.PlanningItemList) Header(org.kie.server.remote.rest.common.Header) RestUtils.buildConversationIdHeader(org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader) PlanningExecutionResult(org.kie.server.api.model.taskassigning.PlanningExecutionResult) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

PlanningItemList (org.kie.server.api.model.taskassigning.PlanningItemList)14 PlanningExecutionResult (org.kie.server.api.model.taskassigning.PlanningExecutionResult)13 TaskData (org.kie.server.api.model.taskassigning.TaskData)10 PlanningItem (org.kie.server.api.model.taskassigning.PlanningItem)9 Test (org.junit.Test)8 CompositeCommand (org.jbpm.services.task.commands.CompositeCommand)3 PlanningTask (org.kie.server.api.model.taskassigning.PlanningTask)3 StatusConverter.convertToString (org.kie.server.api.model.taskassigning.util.StatusConverter.convertToString)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 Status (org.kie.api.task.model.Status)2 KieServicesException (org.kie.server.api.exception.KieServicesException)2 KieContainerStatus (org.kie.server.api.model.KieContainerStatus)2 StatusConverter.convertFromString (org.kie.server.api.model.taskassigning.util.StatusConverter.convertFromString)2 StatusConverter.convertToStringList (org.kie.server.api.model.taskassigning.util.StatusConverter.convertToStringList)2 PlanningException (org.kie.server.services.taskassigning.runtime.command.PlanningException)2 ApiOperation (io.swagger.annotations.ApiOperation)1