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);
}
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;
}
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]));
}
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);
}
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);
}
Aggregations