Search in sources :

Example 1 with PreconditionViolationException

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;
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) CaseType(com.evolveum.midpoint.xml.ns._public.common.common_3.CaseType) PreconditionViolationException(com.evolveum.midpoint.repo.api.PreconditionViolationException)

Example 2 with PreconditionViolationException

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");
            }
        }
    }
}
Also used : CaseTypeUtil(com.evolveum.midpoint.schema.util.cases.CaseTypeUtil) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) Trace(com.evolveum.midpoint.util.logging.Trace) TaskManager(com.evolveum.midpoint.task.api.TaskManager) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) WorkItemHelper(com.evolveum.midpoint.cases.impl.engine.helpers.WorkItemHelper) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) Duration(javax.xml.datatype.Duration) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) PrismContext(com.evolveum.midpoint.prism.PrismContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) AuthorizationHelper(com.evolveum.midpoint.cases.impl.helpers.AuthorizationHelper) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) TriggerHelper(com.evolveum.midpoint.cases.impl.engine.helpers.TriggerHelper) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) CaseMiscHelper(com.evolveum.midpoint.cases.impl.helpers.CaseMiscHelper) Collection(java.util.Collection) Set(java.util.Set) ExpressionEvaluationHelper(com.evolveum.midpoint.wf.impl.processes.common.ExpressionEvaluationHelper) PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) Collectors(java.util.stream.Collectors) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) PreconditionViolationException(com.evolveum.midpoint.repo.api.PreconditionViolationException) List(java.util.List) Component(org.springframework.stereotype.Component) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) PrimaryChangeProcessor(com.evolveum.midpoint.wf.impl.processors.primary.PrimaryChangeProcessor) SystemObjectCache(com.evolveum.midpoint.model.common.SystemObjectCache) StageComputeHelper(com.evolveum.midpoint.wf.impl.processes.common.StageComputeHelper) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) Clock(com.evolveum.midpoint.common.Clock) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) PrismObject(com.evolveum.midpoint.prism.PrismObject) PreconditionViolationException(com.evolveum.midpoint.repo.api.PreconditionViolationException)

Example 3 with PreconditionViolationException

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;
    }
}
Also used : Task(com.evolveum.midpoint.task.api.Task) TaskQuartzImpl(com.evolveum.midpoint.task.quartzimpl.TaskQuartzImpl) PreconditionViolationException(com.evolveum.midpoint.repo.api.PreconditionViolationException)

Example 4 with PreconditionViolationException

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);
}
Also used : DirtiesContext(org.springframework.test.annotation.DirtiesContext) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Test(org.testng.annotations.Test) VersionPrecondition(com.evolveum.midpoint.repo.api.VersionPrecondition) InternalsConfig(com.evolveum.midpoint.schema.internals.InternalsConfig) DOMUtil(com.evolveum.midpoint.util.DOMUtil) PrismTestUtil(com.evolveum.midpoint.prism.util.PrismTestUtil) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) XmlTypeConverter(com.evolveum.midpoint.prism.xml.XmlTypeConverter) PrismAsserts(com.evolveum.midpoint.prism.util.PrismAsserts) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Timestamp(java.sql.Timestamp) Collection(java.util.Collection) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) PreconditionViolationException(com.evolveum.midpoint.repo.api.PreconditionViolationException) List(java.util.List) PolyStringType(com.evolveum.prism.xml.ns._public.types_3.PolyStringType) com.evolveum.midpoint.prism.delta(com.evolveum.midpoint.prism.delta) QName(javax.xml.namespace.QName) RObject(com.evolveum.midpoint.repo.sql.data.common.RObject) NotNull(org.jetbrains.annotations.NotNull) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) COMPLETED(com.evolveum.midpoint.xml.ns._public.common.common_3.PendingOperationExecutionStatusType.COMPLETED) AssertJUnit(org.testng.AssertJUnit) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Session(org.hibernate.Session) DebugUtil(com.evolveum.midpoint.util.DebugUtil) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ArrayList(java.util.ArrayList) EquivalenceStrategy(com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy) ObjectTypeUtil(com.evolveum.midpoint.schema.util.ObjectTypeUtil) DeltaConvertor(com.evolveum.midpoint.schema.DeltaConvertor) ModificationPrecondition(com.evolveum.midpoint.repo.api.ModificationPrecondition) SqlRepoTestUtil(com.evolveum.midpoint.repo.sql.testing.SqlRepoTestUtil) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) GetOperationOptions.createRawCollection(com.evolveum.midpoint.schema.GetOperationOptions.createRawCollection) SynchronizationUtils(com.evolveum.midpoint.common.SynchronizationUtils) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) File(java.io.File) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) ItemName(com.evolveum.midpoint.prism.path.ItemName) RepoModifyOptions(com.evolveum.midpoint.repo.api.RepoModifyOptions) SerializationOptions.createSerializeForExport(com.evolveum.midpoint.prism.SerializationOptions.createSerializeForExport) SearchResultList(com.evolveum.midpoint.schema.SearchResultList) ObjectModificationType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectModificationType) MidPointConstants(com.evolveum.midpoint.schema.constants.MidPointConstants) SearchFilterType(com.evolveum.prism.xml.ns._public.query_3.SearchFilterType) ContextConfiguration(org.springframework.test.context.ContextConfiguration) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) PreconditionViolationException(com.evolveum.midpoint.repo.api.PreconditionViolationException) Test(org.testng.annotations.Test)

Example 5 with PreconditionViolationException

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);
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VersionPrecondition(com.evolveum.midpoint.repo.api.VersionPrecondition) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) PreconditionViolationException(com.evolveum.midpoint.repo.api.PreconditionViolationException) Test(org.testng.annotations.Test)

Aggregations

PreconditionViolationException (com.evolveum.midpoint.repo.api.PreconditionViolationException)8 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)6 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)3 XmlTypeConverter (com.evolveum.midpoint.prism.xml.XmlTypeConverter)3 VersionPrecondition (com.evolveum.midpoint.repo.api.VersionPrecondition)3 SearchResultList (com.evolveum.midpoint.schema.SearchResultList)3 SchemaConstants (com.evolveum.midpoint.schema.constants.SchemaConstants)3 ObjectTypeUtil (com.evolveum.midpoint.schema.util.ObjectTypeUtil)3 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)3 Test (org.testng.annotations.Test)3 SynchronizationUtils (com.evolveum.midpoint.common.SynchronizationUtils)2 com.evolveum.midpoint.prism (com.evolveum.midpoint.prism)2 SerializationOptions.createSerializeForExport (com.evolveum.midpoint.prism.SerializationOptions.createSerializeForExport)2 com.evolveum.midpoint.prism.delta (com.evolveum.midpoint.prism.delta)2 EquivalenceStrategy (com.evolveum.midpoint.prism.equivalence.EquivalenceStrategy)2 ItemName (com.evolveum.midpoint.prism.path.ItemName)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)2 PrismAsserts (com.evolveum.midpoint.prism.util.PrismAsserts)2