use of org.kie.server.api.model.taskassigning.PlanningItem 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.PlanningItem 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.PlanningItem 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);
}
use of org.kie.server.api.model.taskassigning.PlanningItem in project droolsjbpm-integration by kiegroup.
the class PlanningBuilder method buildPlanningItems.
private List<PlanningItem> buildPlanningItems(User user) {
int index = 0;
int publishedCount = 0;
PlanningItem planningItem;
// dummy tasks has nothing to with the jBPM runtime, don't process them
final List<Task> userTasks = extractTasks(user, IS_NOT_DUMMY);
final List<PlanningItem> userPlanningItems = new ArrayList<>();
for (Task task : userTasks) {
planningItem = PlanningItem.builder().containerId(task.getContainerId()).taskId(task.getId()).processInstanceId(task.getProcessInstanceId()).planningTask(PlanningTask.builder().taskId(task.getId()).published(task.isPinned()).assignedUser(user.getUser().getEntityId()).index(index++).build()).build();
userPlanningItems.add(planningItem);
publishedCount += planningItem.getPlanningTask().isPublished() ? 1 : 0;
}
Iterator<PlanningItem> userPlanningItemsIt = userPlanningItems.iterator();
while (userPlanningItemsIt.hasNext() && (publishedCount < publishWindowSize || IS_PLANNING_USER.test(user.getEntityId()))) {
planningItem = userPlanningItemsIt.next();
if (!planningItem.getPlanningTask().isPublished()) {
planningItem.getPlanningTask().setPublished(true);
publishedCount++;
}
}
return userPlanningItems;
}
use of org.kie.server.api.model.taskassigning.PlanningItem in project droolsjbpm-integration by kiegroup.
the class PlanningBuilderTest method assertTasksArePlanned.
private void assertTasksArePlanned(List<Task> tasks, List<PlanningItem> planningItems) {
for (Task task : tasks) {
PlanningItem peerItem = planningItems.stream().filter(item -> item.getTaskId().equals(task.getId())).findFirst().orElse(null);
if (peerItem == null) {
fail("Task: " + task.getId() + " for user: " + task.getUser().getId() + " must be part of the generated planning");
}
assertEquals("PlanningItem containerId for task: " + task.getId() + " and user: " + task.getUser().getId() + " don't have the expected value.", task.getContainerId(), peerItem.getContainerId());
assertEquals("PlanningItem processInstanceId for task: " + task.getId() + " and user: " + task.getUser().getId() + " don't have the expected value.", task.getProcessInstanceId(), task.getProcessInstanceId());
assertEquals("PlanningItem assignedUser for task: " + task.getId() + " and user: " + task.getUser().getId() + " don't have the expected value.", task.getUser().getEntityId(), peerItem.getPlanningTask().getAssignedUser());
}
}
Aggregations