Search in sources :

Example 1 with ModelProjectionContext

use of com.evolveum.midpoint.model.api.context.ModelProjectionContext in project midpoint by Evolveum.

the class DefaultGuiProgressListener method updateStatusItemState.

private void updateStatusItemState(ProgressReportActivityDto si, ProgressInformation progressInformation, ModelContext modelContext) {
    si.setActivityType(progressInformation.getActivityType());
    si.setResourceShadowDiscriminator(progressInformation.getResourceShadowDiscriminator());
    if (progressInformation.getResourceShadowDiscriminator() != null) {
        String resourceOid = progressInformation.getResourceShadowDiscriminator().getResourceOid();
        String resourceName = resourceOid != null ? getResourceName(resourceOid) : "";
        si.setResourceName(resourceName);
    }
    if (progressInformation.getStateType() == null) {
        si.setStatus(null);
    } else if (progressInformation.getStateType() == ENTERING) {
        si.setStatus(OperationResultStatusType.IN_PROGRESS);
    } else {
        OperationResult result = progressInformation.getOperationResult();
        if (result != null) {
            OperationResultStatus status = result.getStatus();
            if (status == OperationResultStatus.UNKNOWN) {
                status = result.getComputeStatus();
            }
            si.setStatus(status.createStatusType());
        } else {
            si.setStatus(OperationResultStatusType.UNKNOWN);
        }
    }
    // information about modifications on a resource
    if (progressInformation.getActivityType() == RESOURCE_OBJECT_OPERATION && progressInformation.getStateType() == EXITING && progressInformation.getResourceShadowDiscriminator() != null && progressInformation.getResourceShadowDiscriminator().getResourceOid() != null) {
        ModelProjectionContext mpc = modelContext.findProjectionContext(progressInformation.getResourceShadowDiscriminator());
        if (mpc != null) {
            // it shouldn't be null!
            // operations performed (TODO aggregate them somehow?)
            List<ResourceOperationResult> resourceOperationResultList = new ArrayList<>();
            List<? extends ObjectDeltaOperation> executedDeltas = mpc.getExecutedDeltas();
            for (ObjectDeltaOperation executedDelta : executedDeltas) {
                ObjectDelta delta = executedDelta.getObjectDelta();
                if (delta != null) {
                    OperationResult r = executedDelta.getExecutionResult();
                    OperationResultStatus status = r.getStatus();
                    if (status == OperationResultStatus.UNKNOWN) {
                        status = r.getComputeStatus();
                    }
                    resourceOperationResultList.add(new ResourceOperationResult(delta.getChangeType(), status));
                }
            }
            si.setResourceOperationResultList(resourceOperationResultList);
            // object name
            PrismObject<ShadowType> object = mpc.getObjectNew();
            if (object == null) {
                object = mpc.getObjectOld();
            }
            String name = null;
            if (object != null) {
                if (object.asObjectable().getName() != null) {
                    name = PolyString.getOrig(object.asObjectable().getName());
                } else {
                    // determine from attributes
                    ResourceAttribute nameAttribute = ShadowUtil.getNamingAttribute(object);
                    if (nameAttribute != null) {
                        name = String.valueOf(nameAttribute.getAnyRealValue());
                    }
                }
            }
            if (name != null) {
                si.setResourceObjectName(name);
            }
        }
    }
}
Also used : ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResourceOperationResult(com.evolveum.midpoint.web.component.progress.ProgressReportActivityDto.ResourceOperationResult) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) OperationResultStatus(com.evolveum.midpoint.schema.result.OperationResultStatus) ObjectDeltaOperation(com.evolveum.midpoint.schema.ObjectDeltaOperation) ModelProjectionContext(com.evolveum.midpoint.model.api.context.ModelProjectionContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) ResourceAttribute(com.evolveum.midpoint.schema.processor.ResourceAttribute) ResourceOperationResult(com.evolveum.midpoint.web.component.progress.ProgressReportActivityDto.ResourceOperationResult)

Example 2 with ModelProjectionContext

use of com.evolveum.midpoint.model.api.context.ModelProjectionContext in project midpoint by Evolveum.

the class WfPrepareRootOperationTaskHandler method run.

//endregion
//region run method
@Override
public TaskRunResult run(Task task) {
    TaskRunResultStatus status = TaskRunResultStatus.FINISHED;
    try {
        OperationResult result = task.getResult();
        WfTask rootWfTask = wfTaskController.recreateRootWfTask(task);
        List<WfTask> children = rootWfTask.listChildren(result);
        LensContext rootContext = (LensContext) rootWfTask.retrieveModelContext(result);
        boolean changed = false;
        for (WfTask child : children) {
            if (child.getTaskExecutionStatus() != TaskExecutionStatus.CLOSED) {
                throw new IllegalStateException("Child task " + child + " is not in CLOSED state; its state is " + child.getTaskExecutionStatus());
            }
            if (child.hasModelContext()) {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Child job {} has model context present - skipping fetching deltas from it.", child);
                }
            } else {
                ObjectTreeDeltas deltas = child.retrieveResultingDeltas();
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Child job {} returned {} deltas", child, deltas != null ? deltas.getDeltaList().size() : 0);
                }
                if (deltas != null) {
                    LensFocusContext focusContext = rootContext.getFocusContext();
                    ObjectDelta focusDelta = deltas.getFocusChange();
                    if (focusDelta != null) {
                        if (LOGGER.isTraceEnabled()) {
                            LOGGER.trace("Adding delta from job {} to root model context; delta = {}", child, focusDelta.debugDump(0));
                        }
                        if (focusContext.getPrimaryDelta() != null && !focusContext.getPrimaryDelta().isEmpty()) {
                            focusContext.addPrimaryDelta(focusDelta);
                        } else {
                            focusContext.setPrimaryDelta(focusDelta);
                        }
                        changed = true;
                    }
                    Set<Map.Entry<ResourceShadowDiscriminator, ObjectDelta<ShadowType>>> entries = deltas.getProjectionChangeMapEntries();
                    for (Map.Entry<ResourceShadowDiscriminator, ObjectDelta<ShadowType>> entry : entries) {
                        if (LOGGER.isTraceEnabled()) {
                            LOGGER.trace("Adding projection delta from job {} to root model context; rsd = {}, delta = {}", child, entry.getKey(), entry.getValue().debugDump());
                        }
                        ModelProjectionContext projectionContext = rootContext.findProjectionContext(entry.getKey());
                        if (projectionContext == null) {
                            // TODO more liberal treatment?
                            throw new IllegalStateException("No projection context for " + entry.getKey());
                        }
                        if (projectionContext.getPrimaryDelta() != null && !projectionContext.getPrimaryDelta().isEmpty()) {
                            projectionContext.addPrimaryDelta(entry.getValue());
                        } else {
                            projectionContext.setPrimaryDelta(entry.getValue());
                        }
                        changed = true;
                    }
                }
            }
        }
        if (!rootContext.hasAnyPrimaryChange()) {
            // deletes the model context
            rootContext = null;
            // regardless of whether rootContext was changed or not
            changed = true;
        }
        if (changed) {
            rootWfTask.storeModelContext(rootContext);
            rootWfTask.commitChanges(result);
        }
    } catch (SchemaException | ObjectNotFoundException | ObjectAlreadyExistsException | ConfigurationException | ExpressionEvaluationException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't aggregate resulting deltas from child workflow-monitoring tasks due to schema exception", e);
        status = TaskRunResultStatus.PERMANENT_ERROR;
    } catch (CommunicationException e) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't aggregate resulting deltas from child workflow-monitoring tasks", e);
        status = TaskRunResultStatus.TEMPORARY_ERROR;
    }
    TaskRunResult runResult = new TaskRunResult();
    runResult.setRunResultStatus(status);
    return runResult;
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ModelProjectionContext(com.evolveum.midpoint.model.api.context.ModelProjectionContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) ObjectTreeDeltas(com.evolveum.midpoint.schema.ObjectTreeDeltas) TaskRunResultStatus(com.evolveum.midpoint.task.api.TaskRunResult.TaskRunResultStatus) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) WfTask(com.evolveum.midpoint.wf.impl.tasks.WfTask) ResourceShadowDiscriminator(com.evolveum.midpoint.schema.ResourceShadowDiscriminator) LensFocusContext(com.evolveum.midpoint.model.impl.lens.LensFocusContext) Map(java.util.Map)

Example 3 with ModelProjectionContext

use of com.evolveum.midpoint.model.api.context.ModelProjectionContext in project midpoint by Evolveum.

the class PrimaryChangeProcessor method contextCopyWithDeltasReplaced.

private LensContext contextCopyWithDeltasReplaced(ModelContext context, ObjectTreeDeltas changes) throws SchemaException {
    Validate.notNull(changes, "changes");
    LensContext contextCopy = ((LensContext) context).clone();
    contextCopy.replacePrimaryFocusDelta(changes.getFocusChange());
    Map<ResourceShadowDiscriminator, ObjectDelta<ShadowType>> changeMap = changes.getProjectionChangeMap();
    Collection<ModelProjectionContext> projectionContexts = contextCopy.getProjectionContexts();
    for (ModelProjectionContext projectionContext : projectionContexts) {
        ObjectDelta<ShadowType> projectionDelta = changeMap.get(projectionContext.getResourceShadowDiscriminator());
        projectionContext.setPrimaryDelta(projectionDelta);
    }
    return contextCopy;
}
Also used : ResourceShadowDiscriminator(com.evolveum.midpoint.schema.ResourceShadowDiscriminator) ModelProjectionContext(com.evolveum.midpoint.model.api.context.ModelProjectionContext) LensContext(com.evolveum.midpoint.model.impl.lens.LensContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta)

Example 4 with ModelProjectionContext

use of com.evolveum.midpoint.model.api.context.ModelProjectionContext in project midpoint by Evolveum.

the class TestPreviewChanges method test212ModifyUserAddAccountRef.

@Test
public void test212ModifyUserAddAccountRef() throws Exception {
    final String TEST_NAME = "test212ModifyUserAddAccountRef";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(TestPreviewChanges.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    assumeAssignmentPolicy(AssignmentPolicyEnforcementType.NONE);
    ObjectDelta<UserType> userDelta = ObjectDelta.createEmptyModifyDelta(UserType.class, USER_GUYBRUSH_OID, prismContext);
    ReferenceDelta accountDelta = ReferenceDelta.createModificationAdd(UserType.F_LINK_REF, getUserDefinition(), ACCOUNT_SHADOW_GUYBRUSH_OID);
    userDelta.addModification(accountDelta);
    Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(userDelta);
    display("Input deltas: ", userDelta);
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    ModelContext<UserType> modelContext = modelInteractionService.previewChanges(deltas, new ModelExecuteOptions(), task, result);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    display("Preview context", modelContext);
    assertNotNull("Null model context", modelContext);
    result.computeStatus();
    TestUtil.assertSuccess(result);
    ModelElementContext<UserType> focusContext = modelContext.getFocusContext();
    assertNotNull("Null model focus context", focusContext);
    assertNull("Unexpected focus primary delta: " + focusContext.getPrimaryDelta(), focusContext.getPrimaryDelta());
    ObjectDelta<UserType> userSecondaryDelta = focusContext.getSecondaryDelta();
    assertSideEffectiveDeltasOnly("focus secondary delta", focusContext.getSecondaryDelta());
    Collection<? extends ModelProjectionContext> projectionContexts = modelContext.getProjectionContexts();
    assertNotNull("Null model projection context list", projectionContexts);
    assertEquals("Unexpected number of projection contexts", 1, projectionContexts.size());
    ModelProjectionContext accContext = projectionContexts.iterator().next();
    assertNotNull("Null model projection context", accContext);
    assertEquals("Wrong policy decision", SynchronizationPolicyDecision.KEEP, accContext.getSynchronizationPolicyDecision());
    ObjectDelta<ShadowType> accountPrimaryDelta = accContext.getPrimaryDelta();
    assertNull("Unexpected account primary delta", accountPrimaryDelta);
    ObjectDelta<ShadowType> accountSecondaryDelta = accContext.getSecondaryDelta();
    assertEquals("Unexpected size of account secondary delta: " + accountSecondaryDelta, 2, accountSecondaryDelta.getModifications().size());
    PrismAsserts.assertPropertyAdd(accountSecondaryDelta, dummyResourceCtl.getAttributePath(DummyResourceContoller.DUMMY_ACCOUNT_ATTRIBUTE_DRINK_NAME), "rum");
    PrismAsserts.assertPropertyAdd(accountSecondaryDelta, dummyResourceCtl.getAttributePath(DummyResourceContoller.DUMMY_ACCOUNT_ATTRIBUTE_QUOTE_NAME), "Arr!");
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) ReferenceDelta(com.evolveum.midpoint.prism.delta.ReferenceDelta) ModelExecuteOptions(com.evolveum.midpoint.model.api.ModelExecuteOptions) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ModelProjectionContext(com.evolveum.midpoint.model.api.context.ModelProjectionContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test)

Example 5 with ModelProjectionContext

use of com.evolveum.midpoint.model.api.context.ModelProjectionContext in project midpoint by Evolveum.

the class TestPreviewChanges method test210GuybrushAddAccount.

@Test
public void test210GuybrushAddAccount() throws Exception {
    final String TEST_NAME = "test210GuybrushAddAccount";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance(TestPreviewChanges.class.getName() + "." + TEST_NAME);
    OperationResult result = task.getResult();
    assumeAssignmentPolicy(AssignmentPolicyEnforcementType.NONE);
    PrismObject<ShadowType> account = PrismTestUtil.parseObject(ACCOUNT_JACK_DUMMY_FILE);
    ObjectDelta<ShadowType> accountDelta = ObjectDelta.createAddDelta(account);
    Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(accountDelta);
    display("Input deltas: ", deltas);
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    ModelContext<UserType> modelContext = modelInteractionService.previewChanges(deltas, new ModelExecuteOptions(), task, result);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    display("Preview context", modelContext);
    assertNotNull("Null model context", modelContext);
    result.computeStatus();
    TestUtil.assertSuccess(result);
    ModelElementContext<UserType> focusContext = modelContext.getFocusContext();
    assertNull("Unexpected model focus context", focusContext);
    Collection<? extends ModelProjectionContext> projectionContexts = modelContext.getProjectionContexts();
    assertNotNull("Null model projection context list", projectionContexts);
    assertEquals("Unexpected number of projection contexts", 1, projectionContexts.size());
    ModelProjectionContext accContext = projectionContexts.iterator().next();
    assertNotNull("Null model projection context", accContext);
    // Decision does not matter now
    //		assertEquals("Wrong policy decision", SynchronizationPolicyDecision.ADD, accContext.getSynchronizationPolicyDecision());
    ObjectDelta<ShadowType> accountPrimaryDelta = accContext.getPrimaryDelta();
    assertEquals(ChangeType.ADD, accountPrimaryDelta.getChangeType());
    PrismObject<ShadowType> accountToAddPrimary = accountPrimaryDelta.getObjectToAdd();
    assertNotNull("No object in account primary add delta", accountToAddPrimary);
    assertEquals(getDummyResourceController().getAccountObjectClass(), accountToAddPrimary.findProperty(ShadowType.F_OBJECT_CLASS).getRealValue());
    PrismReference resourceRef = accountToAddPrimary.findReference(ShadowType.F_RESOURCE_REF);
    assertEquals(getDummyResourceObject().getOid(), resourceRef.getOid());
    ObjectDelta<ShadowType> accountSecondaryDelta = accContext.getSecondaryDelta();
    PrismAsserts.assertModifications(accountSecondaryDelta, 1);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) ModelExecuteOptions(com.evolveum.midpoint.model.api.ModelExecuteOptions) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) PrismReference(com.evolveum.midpoint.prism.PrismReference) ModelProjectionContext(com.evolveum.midpoint.model.api.context.ModelProjectionContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test)

Aggregations

ModelProjectionContext (com.evolveum.midpoint.model.api.context.ModelProjectionContext)26 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)21 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)20 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)19 Task (com.evolveum.midpoint.task.api.Task)18 Test (org.testng.annotations.Test)18 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)15 ModelExecuteOptions (com.evolveum.midpoint.model.api.ModelExecuteOptions)13 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)12 ResourceShadowDiscriminator (com.evolveum.midpoint.schema.ResourceShadowDiscriminator)11 LensContext (com.evolveum.midpoint.model.impl.lens.LensContext)3 PrismReference (com.evolveum.midpoint.prism.PrismReference)2 ReferenceDelta (com.evolveum.midpoint.prism.delta.ReferenceDelta)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 ObjectDeltaOperation (com.evolveum.midpoint.schema.ObjectDeltaOperation)2 ArrayList (java.util.ArrayList)2 LensFocusContext (com.evolveum.midpoint.model.impl.lens.LensFocusContext)1 LensProjectionContext (com.evolveum.midpoint.model.impl.lens.LensProjectionContext)1 PrismReferenceValue (com.evolveum.midpoint.prism.PrismReferenceValue)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1