Search in sources :

Example 1 with Reserved

use of org.kie.api.task.model.Status.Reserved 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 Reserved

use of org.kie.api.task.model.Status.Reserved in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeServiceQueryHelper method executeFindTasksQuery.

public List<TaskData> executeFindTasksQuery(Map<String, Object> params) {
    List<QueryParam> queryParams = buildQueryParams(params);
    Integer page = params.containsKey(PAGE) ? (Integer) params.get(PAGE) : 0;
    Integer pageSize = params.containsKey(PAGE_SIZE) ? (Integer) params.get(PAGE_SIZE) : 10;
    String loadVariablesMode = (String) params.get(TASK_INPUT_VARIABLES_MODE);
    QueryContext queryContext = new QueryContext(page * pageSize, pageSize, AbstractTaskAssigningQueryMapper.TASK_QUERY_COLUMN.TASK_ID.columnName(), true);
    AbstractTaskAssigningQueryMapper<TaskData> resultMapper = (AbstractTaskAssigningQueryMapper<TaskData>) QueryMapperRegistry.get().mapperFor(TaskAssigningTaskDataWithPotentialOwnersQueryMapper.NAME, null);
    List<TaskData> result = executeQuery(queryService, TASK_ASSIGNING_TASKS_WITH_POTENTIAL_OWNERS_AND_PLANNING_TASK, resultMapper, queryContext, queryParams.toArray(new QueryParam[0]));
    Optional<Predicate<TaskData>> loadInputVariables = Optional.empty();
    if (TaskInputVariablesReadMode.READ_FOR_ALL.name().equals(loadVariablesMode)) {
        loadInputVariables = Optional.of(taskData -> true);
    } else if (TaskInputVariablesReadMode.READ_FOR_ACTIVE_TASKS_WITH_NO_PLANNING_ENTITY.name().equals(loadVariablesMode)) {
        final Predicate<TaskData> isActive = taskData -> {
            if (taskData.getPlanningTask() != null) {
                return false;
            }
            Status taskStatus = StatusConverter.convertFromString(taskData.getStatus());
            return taskStatus == Created || taskStatus == Ready || taskStatus == Reserved || taskStatus == InProgress || taskStatus == Suspended;
        };
        loadInputVariables = Optional.of(isActive);
    }
    loadInputVariables.ifPresent(taskDataPredicate -> result.stream().filter(taskDataPredicate).forEach(taskData -> taskData.setInputData(readTaskVariables(taskData))));
    return result;
}
Also used : KieServicesException(org.kie.server.api.exception.KieServicesException) Created(org.kie.api.task.model.Status.Created) QueryService(org.jbpm.services.api.query.QueryService) Date(java.util.Date) QueryResultMapper(org.jbpm.services.api.query.QueryResultMapper) LocalDateTime(java.time.LocalDateTime) AbstractTaskAssigningQueryMapper(org.kie.server.services.taskassigning.runtime.query.AbstractTaskAssigningQueryMapper) Reserved(org.kie.api.task.model.Status.Reserved) HashMap(java.util.HashMap) STATUS(org.kie.server.api.model.taskassigning.QueryParamName.STATUS) TaskAssigningTaskDataWithPotentialOwnersQueryMapper(org.kie.server.services.taskassigning.runtime.query.TaskAssigningTaskDataWithPotentialOwnersQueryMapper) ClassUtils.isPrimitiveWrapper(org.apache.commons.lang3.ClassUtils.isPrimitiveWrapper) QueryParam(org.jbpm.services.api.query.model.QueryParam) 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) PAGE(org.kie.server.api.model.taskassigning.QueryParamName.PAGE) Map(java.util.Map) PAGE_SIZE(org.kie.server.api.model.taskassigning.QueryParamName.PAGE_SIZE) UserTaskService(org.jbpm.services.api.UserTaskService) KieContainerStatus(org.kie.server.api.model.KieContainerStatus) Suspended(org.kie.api.task.model.Status.Suspended) FROM_LAST_MODIFICATION_DATE(org.kie.server.api.model.taskassigning.QueryParamName.FROM_LAST_MODIFICATION_DATE) StatusConverter(org.kie.server.api.model.taskassigning.util.StatusConverter) Ready(org.kie.api.task.model.Status.Ready) TASK_INPUT_VARIABLES_MODE(org.kie.server.api.model.taskassigning.QueryParamName.TASK_INPUT_VARIABLES_MODE) TaskInputVariablesReadMode(org.kie.server.api.model.taskassigning.TaskInputVariablesReadMode) Predicate(java.util.function.Predicate) QueryContext(org.kie.api.runtime.query.QueryContext) QueryMapperRegistry(org.jbpm.services.api.query.QueryMapperRegistry) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) TO_TASK_ID(org.kie.server.api.model.taskassigning.QueryParamName.TO_TASK_ID) List(java.util.List) TaskAssigningTaskDataSummaryQueryMapper(org.kie.server.services.taskassigning.runtime.query.TaskAssigningTaskDataSummaryQueryMapper) FROM_TASK_ID(org.kie.server.api.model.taskassigning.QueryParamName.FROM_TASK_ID) Optional(java.util.Optional) Status(org.kie.api.task.model.Status) LocalDateTimeValue(org.kie.server.api.model.taskassigning.LocalDateTimeValue) KieContainerStatus(org.kie.server.api.model.KieContainerStatus) Status(org.kie.api.task.model.Status) QueryContext(org.kie.api.runtime.query.QueryContext) TaskData(org.kie.server.api.model.taskassigning.TaskData) Predicate(java.util.function.Predicate) AbstractTaskAssigningQueryMapper(org.kie.server.services.taskassigning.runtime.query.AbstractTaskAssigningQueryMapper) QueryParam(org.jbpm.services.api.query.model.QueryParam)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 UserTaskService (org.jbpm.services.api.UserTaskService)2 QueryService (org.jbpm.services.api.query.QueryService)2 Status (org.kie.api.task.model.Status)2 InProgress (org.kie.api.task.model.Status.InProgress)2 Ready (org.kie.api.task.model.Status.Ready)2 Reserved (org.kie.api.task.model.Status.Reserved)2 Suspended (org.kie.api.task.model.Status.Suspended)2 KieServicesException (org.kie.server.api.exception.KieServicesException)2 KieContainerStatus (org.kie.server.api.model.KieContainerStatus)2 TaskData (org.kie.server.api.model.taskassigning.TaskData)2 KieServerRegistry (org.kie.server.services.api.KieServerRegistry)2 KieContainerInstanceImpl (org.kie.server.services.impl.KieContainerInstanceImpl)2 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 Arrays (java.util.Arrays)1