use of org.kie.server.services.taskassigning.runtime.command.PlanningException 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.services.taskassigning.runtime.command.PlanningException in project droolsjbpm-integration by kiegroup.
the class TaskAssigningRuntimeServiceBase method executePlanning.
public PlanningExecutionResult executePlanning(PlanningItemList planningItemList, String userId) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
checkServerStatus();
Map<String, List<PlanningCommand>> commandsByContainer;
try {
commandsByContainer = calculatePlanningCommands(planningItemList, userId);
} catch (PlanningException e) {
LOGGER.debug("An error was produced during plan calculation, containerId: {}, error code: {}, message: {}", e.getContainerId(), e.getCode(), e.getMessage());
return PlanningExecutionResult.builder().error(e.getCode()).errorMessage(e.getMessage()).containerId(e.getContainerId()).build();
} catch (Exception e) {
final String msg = String.format(UNEXPECTED_ERROR_DURING_PLAN_CALCULATION, e.getMessage());
LOGGER.error(msg, e);
return PlanningExecutionResult.builder().error(PlanningExecutionResult.ErrorCode.UNEXPECTED_ERROR).errorMessage(msg).build();
}
stopWatch.stop();
LOGGER.debug("Time to calculate the planning commands: {}", stopWatch);
stopWatch.reset();
stopWatch.start();
for (Map.Entry<String, List<PlanningCommand>> entry : commandsByContainer.entrySet()) {
try {
executeContainerCommands(entry.getKey(), entry.getValue());
} catch (PlanningException e) {
LOGGER.debug("An error was produced during plan execution on containerId: {}, error code: {}, message: {}", entry.getKey(), e.getCode(), e.getMessage());
return PlanningExecutionResult.builder().error(e.getCode()).errorMessage(e.getMessage()).containerId(e.getContainerId()).build();
} catch (Exception e) {
final String msg = String.format(UNEXPECTED_ERROR_DURING_PLAN_EXECUTION, entry.getKey(), e.getMessage());
LOGGER.error(msg, e);
return PlanningExecutionResult.builder().error(PlanningExecutionResult.ErrorCode.UNEXPECTED_ERROR).errorMessage(msg).containerId(entry.getKey()).build();
}
}
stopWatch.stop();
LOGGER.debug("Time for executing the planning with planning items: {} -> {}", planningItemList.getItems().size(), stopWatch);
return PlanningExecutionResult.builder().build();
}
Aggregations