use of com.evolveum.midpoint.repo.api.PreconditionViolationException in project midpoint by Evolveum.
the class CaseOperationExecutionTaskHandler method run.
@Override
public TaskRunResult run(@NotNull RunningTask task) {
OperationResult result = task.getResult().createSubresult(DOT_CLASS + "run");
TaskRunResult runResult = new TaskRunResult();
try {
PrismObject<CaseType> caseObject = task.getObject(CaseType.class, result);
if (caseObject == null) {
throw new IllegalStateException("No case reference in task " + task);
}
CaseType aCase = caseObject.asObjectable();
if (aCase.getParentRef() == null) {
executeAllChanges(aCase, task, result);
} else {
executeLocalChanges(aCase, task, result);
}
result.computeStatus();
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.FINISHED);
} catch (RuntimeException | ObjectNotFoundException | SchemaException | CommunicationException | ConfigurationException | ExpressionEvaluationException | PolicyViolationException | PreconditionViolationException | ObjectAlreadyExistsException | SecurityViolationException e) {
String message = "An exception occurred when trying to execute model operation for a case in " + task;
LoggingUtils.logUnexpectedException(LOGGER, message, e);
result.recordFatalError(message, e);
// let's assume it's temporary
runResult.setRunResultStatus(TaskRunResult.TaskRunResultStatus.TEMPORARY_ERROR);
}
task.getResult().recomputeStatus();
runResult.setOperationResultStatus(task.getResult().getStatus());
return runResult;
}
use of com.evolveum.midpoint.repo.api.PreconditionViolationException in project midpoint by Evolveum.
the class ExecutionHelper method checkDependentCases.
/**
* We need to check
* 1) if there are any executable cases that depend on this one
* 2) if we can close the parent (root)
*/
public void checkDependentCases(String rootOid, OperationResult result) throws SchemaException, ObjectNotFoundException, ObjectAlreadyExistsException {
CaseType rootCase = repositoryService.getObject(CaseType.class, rootOid, null, result).asObjectable();
if (CaseTypeUtil.isClosed(rootCase)) {
return;
}
List<CaseType> subcases = miscHelper.getSubcases(rootOid, result);
LOGGER.debug("Subcases:");
for (CaseType subcase : subcases) {
LOGGER.debug(" - {}: state={}, closeTS={}", subcase, subcase.getState(), subcase.getCloseTimestamp());
}
List<String> openOids = subcases.stream().filter(c -> !CaseTypeUtil.isClosed(c)).map(ObjectType::getOid).collect(Collectors.toList());
LOGGER.debug("open cases OIDs: {}", openOids);
if (openOids.isEmpty()) {
closeCaseInRepository(rootCase, result);
} else {
ObjectQuery query = prismContext.queryFor(TaskType.class).item(TaskType.F_OBJECT_REF).ref(openOids.toArray(new String[0])).and().item(TaskType.F_EXECUTION_STATE).eq(TaskExecutionStateType.WAITING).build();
SearchResultList<PrismObject<TaskType>> waitingTasks = repositoryService.searchObjects(TaskType.class, query, null, result);
LOGGER.debug("Waiting tasks: {}", waitingTasks);
for (PrismObject<TaskType> waitingTask : waitingTasks) {
String waitingCaseOid = waitingTask.asObjectable().getObjectRef().getOid();
assert waitingCaseOid != null;
List<CaseType> waitingCaseList = subcases.stream().filter(c -> waitingCaseOid.equals(c.getOid())).collect(Collectors.toList());
assert waitingCaseList.size() == 1;
Set<String> prerequisiteOids = waitingCaseList.get(0).getPrerequisiteRef().stream().map(ObjectReferenceType::getOid).collect(Collectors.toSet());
Collection<String> openPrerequisites = CollectionUtils.intersection(prerequisiteOids, openOids);
LOGGER.trace("prerequisite OIDs = {}; intersection with open OIDs = {}", prerequisiteOids, openPrerequisites);
if (openPrerequisites.isEmpty()) {
LOGGER.trace("All prerequisites are fulfilled, going to release the task {}", waitingTask);
try {
taskManager.unpauseTask(taskManager.createTaskInstance(waitingTask, result), result);
} catch (PreconditionViolationException e) {
LoggingUtils.logException(LOGGER, "Race condition when unpausing the task. " + "It may or may not be unpaused now. Continuing.", e);
}
} else {
LOGGER.trace("...task is not released and continues waiting for those cases");
}
}
}
}
use of com.evolveum.midpoint.repo.api.PreconditionViolationException in project midpoint by Evolveum.
the class UnpauseHelper method unpauseTaskIfPossible.
/**
* @return true if unpaused
*/
boolean unpauseTaskIfPossible(TaskQuartzImpl task, OperationResult result) throws SchemaException, ObjectNotFoundException {
if (task.getSchedulingState() != TaskSchedulingStateType.WAITING || task.getWaitingReason() != TaskWaitingReasonType.OTHER_TASKS) {
LOGGER.debug("Not considering task for unpausing {} because the state does not match: {}/{}", task, task.getSchedulingState(), task.getWaitingReason());
return false;
}
List<TaskQuartzImpl> allPrerequisites = task.listSubtasks(result);
allPrerequisites.addAll(taskRetriever.listPrerequisiteTasks(task, result));
LOGGER.trace("Checking {} prerequisites for waiting task {}", allPrerequisites.size(), task);
for (Task prerequisite : allPrerequisites) {
if (!prerequisite.isClosed()) {
LOGGER.debug("Prerequisite {} of {} is not closed (scheduling state = {})", prerequisite, task, prerequisite.getSchedulingState());
return false;
}
}
LOGGER.debug("All prerequisites of {} are closed, unpausing the task", task);
try {
return unpauseTask(task, result);
} catch (PreconditionViolationException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Task cannot be unpaused because it is no longer in WAITING state -- ignoring", e, this);
return false;
}
}
use of com.evolveum.midpoint.repo.api.PreconditionViolationException in project midpoint by Evolveum.
the class ModifyTest method test160ModifyWithPrecondition.
@Test
public void test160ModifyWithPrecondition() throws Exception {
OperationResult result = createOperationResult();
// GIVEN
String versionBefore = repositoryService.getVersion(RoleType.class, roleOid, result);
ModificationPrecondition<RoleType> precondition = o -> {
throw new PreconditionViolationException("hello");
};
// WHEN
List<ItemDelta<?, ?>> itemDeltas = prismContext.deltaFor(RoleType.class).item(RoleType.F_DESCRIPTION).replace("123456").asItemDeltas();
try {
repositoryService.modifyObject(RoleType.class, roleOid, itemDeltas, precondition, getModifyOptions(), result);
// THEN
fail("unexpected success");
} catch (PreconditionViolationException e) {
assertEquals("Wrong exception message", "hello", e.getMessage());
}
String versionAfter = repositoryService.getVersion(RoleType.class, roleOid, result);
assertEquals("unexpected version change", versionBefore, versionAfter);
}
use of com.evolveum.midpoint.repo.api.PreconditionViolationException in project midpoint by Evolveum.
the class ModifyTest method test164ModifyWithVersionPreconditionFalse.
@Test
public void test164ModifyWithVersionPreconditionFalse() throws Exception {
OperationResult result = createOperationResult();
// GIVEN
String versionBefore = repositoryService.getVersion(RoleType.class, roleOid, result);
ModificationPrecondition<RoleType> precondition = new VersionPrecondition<>("9999");
// WHEN
List<ItemDelta<?, ?>> itemDeltas = prismContext.deltaFor(RoleType.class).item(RoleType.F_DESCRIPTION).replace("123456").asItemDeltas();
try {
repositoryService.modifyObject(RoleType.class, roleOid, itemDeltas, precondition, getModifyOptions(), result);
// THEN
fail("unexpected success");
} catch (PreconditionViolationException e) {
// ok
System.out.println("got expected exception: " + e.getMessage());
}
String versionAfter = repositoryService.getVersion(RoleType.class, roleOid, result);
assertEquals("unexpected version change", versionBefore, versionAfter);
}
Aggregations