Search in sources :

Example 1 with TaskData

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

the class TaskDataReader method readTasks.

/**
 * Executes the {@link TaskAssigningRuntimeClient#findTasks(Long, List, LocalDateTime, Integer, Integer, TaskInputVariablesReadMode)}
 * method and return all the results. The paging reading is managed internally by this method.
 * <p>
 * @param fromTaskId filters the tasks with taskId >= fromTaskId. If null no filtering is applied.
 * @param status filters the tasks that are in one of the following status. If null or the empty list no filtering
 * is applied.
 * @param fromLastModificationDate filters the tasks with lastModificationDate >= fromLastModificationDate. If null
 * no filtering is applied.
 * @param pageSize sets the pageSize for the paged reading.
 * @param taskInputVariablesReadMode establishes the tasks input variables reading mode.
 * @return a list of TaskData with the jBPM tasks that met the filtering conditions. The potential owners of the task
 * is properly loaded with all the elements. The task inputs data is loaded accordingly with the selected taskInputVariablesReadMode.
 * @see TaskInputVariablesReadMode
 */
public Result readTasks(long fromTaskId, List<String> status, LocalDateTime fromLastModificationDate, int pageSize, TaskInputVariablesReadMode taskInputVariablesReadMode) {
    final List<TaskData> result = new ArrayList<>();
    boolean finished = false;
    List<TaskData> partialResult;
    TaskDataList taskDataList;
    TaskData lastItem = null;
    LocalDateTime queryTime = null;
    long taskId = fromTaskId;
    int nextPageSize = pageSize;
    while (!finished) {
        taskDataList = runtimeClient.findTasks(taskId, status, fromLastModificationDate, 0, nextPageSize, taskInputVariablesReadMode);
        partialResult = new ArrayList<>(taskDataList.getItems());
        if (queryTime == null) {
            queryTime = taskDataList.getQueryTime();
        }
        if (partialResult.isEmpty()) {
            finished = true;
        } else {
            if (lastItem == null || partialResult.size() > 1) {
                lastItem = partialResult.remove(partialResult.size() - 1);
                taskId = lastItem.getTaskId();
                nextPageSize = pageSize;
                result.addAll(partialResult);
            } else {
                // or it might be the case when some of them fall out of the pageSize.
                if (lastItem.getTaskId().equals(partialResult.get(0).getTaskId())) {
                    if (partialResult.get(0).getPotentialOwners().isEmpty()) {
                        // no potential owners, check if a taskId+1 exists prior to exit.
                        result.add(partialResult.get(0));
                        lastItem = null;
                        taskId++;
                        nextPageSize = pageSize;
                    } else if (partialResult.get(0).getPotentialOwners().size() < nextPageSize) {
                        // the potential owners fits the page margins, we can exit.
                        result.add(partialResult.get(0));
                        finished = true;
                    } else {
                        // there might exists more potential owners and in the worst case we loaded only one element
                        // in last page. increase the page size to ensure we can fetch all.
                        nextPageSize = nextPageSize * 2;
                    }
                } else {
                    // last item might have been disappeared from result since last query, retry.
                    lastItem = partialResult.get(0);
                    taskId = lastItem.getTaskId();
                    nextPageSize = pageSize;
                }
            }
        }
    }
    return new Result(queryTime, result);
}
Also used : LocalDateTime(java.time.LocalDateTime) ArrayList(java.util.ArrayList) TaskDataList(org.kie.server.api.model.taskassigning.TaskDataList) TaskData(org.kie.server.api.model.taskassigning.TaskData)

Example 2 with TaskData

use of org.kie.server.api.model.taskassigning.TaskData 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 3 with TaskData

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

the class TaskAssigningRuntimeServiceQueryHelper method executeOptimizedFindTasksDataSummaryQuery.

private List<TaskData> executeOptimizedFindTasksDataSummaryQuery(Long fromTaskId, List<String> status, int page, int pageSize) {
    QueryContext queryContext = new QueryContext(page * pageSize, pageSize, AbstractTaskAssigningQueryMapper.TASK_QUERY_COLUMN.TASK_ID.columnName(), true);
    Map<String, Object> params = new HashMap<>();
    params.put(FROM_TASK_ID, fromTaskId);
    params.put(STATUS, status);
    List<QueryParam> queryParams = buildQueryParams(params);
    AbstractTaskAssigningQueryMapper<TaskData> resultMapper = (AbstractTaskAssigningQueryMapper<TaskData>) QueryMapperRegistry.get().mapperFor(TaskAssigningTaskDataSummaryQueryMapper.NAME, null);
    return executeQuery(queryService, TASK_ASSIGNING_TASKS_WITH_PLANNING_TASK_OPTIMIZED, resultMapper, queryContext, queryParams.toArray(new QueryParam[0]));
}
Also used : AbstractTaskAssigningQueryMapper(org.kie.server.services.taskassigning.runtime.query.AbstractTaskAssigningQueryMapper) HashMap(java.util.HashMap) QueryParam(org.jbpm.services.api.query.model.QueryParam) QueryContext(org.kie.api.runtime.query.QueryContext) TaskData(org.kie.server.api.model.taskassigning.TaskData)

Example 4 with TaskData

use of org.kie.server.api.model.taskassigning.TaskData 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 5 with TaskData

use of org.kie.server.api.model.taskassigning.TaskData 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)

Aggregations

TaskData (org.kie.server.api.model.taskassigning.TaskData)55 Test (org.junit.Test)30 PlanningTask (org.kie.server.api.model.taskassigning.PlanningTask)26 TaskAssigningSolution (org.kie.server.services.taskassigning.core.model.TaskAssigningSolution)14 TaskUtil.fromTaskData (org.kie.server.services.taskassigning.planning.util.TaskUtil.fromTaskData)13 PlanningExecutionResult (org.kie.server.api.model.taskassigning.PlanningExecutionResult)10 PlanningItemList (org.kie.server.api.model.taskassigning.PlanningItemList)10 Task (org.kie.server.services.taskassigning.core.model.Task)10 AddTaskProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange)10 AddUserProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.AddUserProblemFactChange)10 AssignTaskProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange)10 DisableUserProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange)10 ReleaseTaskProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange)10 RemoveTaskProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange)10 RemoveUserProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange)10 TaskPropertyChangeProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.TaskPropertyChangeProblemFactChange)10 UserPropertyChangeProblemFactChange (org.kie.server.services.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange)10 ProblemFactChange (org.optaplanner.core.impl.solver.ProblemFactChange)10 PlanningItem (org.kie.server.api.model.taskassigning.PlanningItem)9 ArrayList (java.util.ArrayList)8