Search in sources :

Example 11 with ItemDeltaItem

use of com.evolveum.midpoint.prism.util.ItemDeltaItem in project midpoint by Evolveum.

the class PathExpressionResolver method resolve.

/**
 * Main entry point.
 */
TypedValue<?> resolve(OperationResult result) throws SchemaException, ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
    TypedValue<?> root;
    String topVarDesc;
    if (variableName != null) {
        topVarDesc = "variable " + variableName;
        if (variables.containsKey(variableName)) {
            root = variables.get(variableName);
        } else {
            throw new SchemaException("No variable with name " + variableName + " in " + shortDesc);
        }
    } else {
        topVarDesc = "default context";
        root = defaultContext;
    }
    if (root == null) {
        return null;
    }
    if (relativePath.isEmpty()) {
        return root;
    }
    // TODO reconsider this (see MID-7057).
    if (normalizeValuesToDelete) {
        root = normalizeValuesToDelete(root);
    }
    if (root.getValue() instanceof ObjectReferenceType) {
        root = resolveReference(root, objectResolver, null, topVarDesc, shortDesc, task, result);
    }
    String lastPathSegmentName = relativePath.lastName().getLocalPart();
    Object rootValue = root.getValue();
    if (rootValue == null) {
        return determineNullTypedValue(root);
    } else if (rootValue instanceof Objectable) {
        return determineTypedValue(((Objectable) rootValue).asPrismObject(), false, result);
    } else if (rootValue instanceof PrismObject<?>) {
        return determineTypedValue((PrismObject<?>) rootValue, false, result);
    } else if (rootValue instanceof PrismContainer<?>) {
        return determineTypedValue((PrismContainer<?>) rootValue, false, result);
    } else if (rootValue instanceof PrismContainerValue<?>) {
        return determineTypedValue((PrismContainerValue<?>) rootValue);
    } else if (rootValue instanceof Item<?, ?>) {
        // Except for container (which is handled above)
        throw new SchemaException("Cannot apply path " + relativePath + " to " + root + " in " + shortDesc);
    } else if (rootValue instanceof ObjectDeltaObject<?>) {
        return determineTypedValueOdo(lastPathSegmentName, root);
    } else if (rootValue instanceof ItemDeltaItem<?, ?>) {
        return determineTypedValue((ItemDeltaItem<?, ?>) rootValue);
    } else {
        throw new IllegalArgumentException("Unexpected root " + rootValue + " (relative path:" + relativePath + ") in " + shortDesc);
    }
}
Also used : ObjectDeltaObject(com.evolveum.midpoint.prism.util.ObjectDeltaObject) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) ItemDeltaItem(com.evolveum.midpoint.prism.util.ItemDeltaItem) ObjectDeltaObject(com.evolveum.midpoint.prism.util.ObjectDeltaObject)

Example 12 with ItemDeltaItem

use of com.evolveum.midpoint.prism.util.ItemDeltaItem in project midpoint by Evolveum.

the class LensUtil method formatIterationToken.

public static <F extends ObjectType> String formatIterationToken(LensContext<F> context, LensElementContext<?> accountContext, IterationSpecificationType iterationType, int iteration, ExpressionFactory expressionFactory, VariablesMap variables, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
    if (iterationType == null) {
        return formatIterationTokenDefault(iteration);
    }
    ExpressionType tokenExpressionType = iterationType.getTokenExpression();
    if (tokenExpressionType == null) {
        return formatIterationTokenDefault(iteration);
    }
    PrismContext prismContext = PrismContext.get();
    PrismPropertyDefinition<String> outputDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.VAR_ITERATION_TOKEN_QNAME, DOMUtil.XSD_STRING);
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(tokenExpressionType, outputDefinition, MiscSchemaUtil.getExpressionProfile(), "iteration token expression in " + accountContext.getHumanReadableName(), task, result);
    Collection<Source<?, ?>> sources = new ArrayList<>();
    MutablePrismPropertyDefinition<Integer> inputDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.VAR_ITERATION_QNAME, DOMUtil.XSD_INT);
    inputDefinition.setMaxOccurs(1);
    PrismProperty<Integer> input = inputDefinition.instantiate();
    input.addRealValue(iteration);
    ItemDeltaItem<PrismPropertyValue<Integer>, PrismPropertyDefinition<Integer>> idi = new ItemDeltaItem<>(input);
    Source<PrismPropertyValue<Integer>, PrismPropertyDefinition<Integer>> iterationSource = new Source<>(idi, ExpressionConstants.VAR_ITERATION_QNAME);
    sources.add(iterationSource);
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(sources, variables, "iteration token expression in " + accountContext.getHumanReadableName(), task);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, expressionContext, task, result);
    Collection<PrismPropertyValue<String>> outputValues = outputTriple.getNonNegativeValues();
    if (outputValues.isEmpty()) {
        return "";
    }
    if (outputValues.size() > 1) {
        throw new ExpressionEvaluationException("Iteration token expression in " + accountContext.getHumanReadableName() + " returned more than one value (" + outputValues.size() + " values)");
    }
    String realValue = outputValues.iterator().next().getValue();
    if (realValue == null) {
        return "";
    }
    return realValue;
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) Source(com.evolveum.midpoint.repo.common.expression.Source) ItemDeltaItem(com.evolveum.midpoint.prism.util.ItemDeltaItem)

Example 13 with ItemDeltaItem

use of com.evolveum.midpoint.prism.util.ItemDeltaItem in project midpoint by Evolveum.

the class AssignmentCollector method evaluateAssignments.

private <AH extends AssignmentHolderType> Collection<EvaluatedAssignment<AH>> evaluateAssignments(AH focus, Collection<AssignmentType> assignments, AssignmentOrigin origin, AssignmentEvaluator<AH> assignmentEvaluator, Task task, OperationResult result) {
    List<EvaluatedAssignment<AH>> evaluatedAssignments = new ArrayList<>();
    RepositoryCache.enterLocalCaches(cacheConfigurationManager);
    try {
        PrismContainerDefinition<AssignmentType> standardAssignmentDefinition = prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(AssignmentHolderType.class).findContainerDefinition(AssignmentHolderType.F_ASSIGNMENT);
        for (AssignmentType assignmentType : emptyIfNull(assignments)) {
            try {
                // noinspection unchecked
                PrismContainerDefinition<AssignmentType> definition = defaultIfNull(assignmentType.asPrismContainerValue().getDefinition(), standardAssignmentDefinition);
                ItemDeltaItem<PrismContainerValue<AssignmentType>, PrismContainerDefinition<AssignmentType>> assignmentIdi = new ItemDeltaItem<>(LensUtil.createAssignmentSingleValueContainer(assignmentType), definition);
                EvaluatedAssignment<AH> assignment = assignmentEvaluator.evaluate(assignmentIdi, PlusMinusZero.ZERO, false, focus, focus.toString(), origin, task, result);
                evaluatedAssignments.add(assignment);
            } catch (SchemaException | ObjectNotFoundException | ExpressionEvaluationException | PolicyViolationException | SecurityViolationException | ConfigurationException | CommunicationException e) {
                LOGGER.error("Error while processing assignment of {}: {}; assignment: {}", focus, e.getMessage(), assignmentType, e);
            }
        }
    } finally {
        RepositoryCache.exitLocalCaches();
    }
    return evaluatedAssignments;
}
Also used : PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) ArrayList(java.util.ArrayList) EvaluatedAssignment(com.evolveum.midpoint.model.api.context.EvaluatedAssignment) ItemDeltaItem(com.evolveum.midpoint.prism.util.ItemDeltaItem) AssignmentType(com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) AssignmentHolderType(com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentHolderType)

Example 14 with ItemDeltaItem

use of com.evolveum.midpoint.prism.util.ItemDeltaItem in project midpoint by Evolveum.

the class TestAssignmentProcessor2 method test062JackDeputyOfGuybrushDeputyOfBarbossaInLoginMode.

// goes through assignmentEvaluator in order to employ login mode
// MID-4176
@Test(enabled = FIRST_PART)
public void test062JackDeputyOfGuybrushDeputyOfBarbossaInLoginMode() throws Exception {
    // GIVEN
    Task task = getTestTask();
    OperationResult result = getTestOperationResult();
    PrismObject<UserType> jack = getUser(USER_JACK_OID);
    AssignmentType jackGuybrushAssignment = new AssignmentType(prismContext).targetRef(USER_GUYBRUSH_OID, UserType.COMPLEX_TYPE, SchemaConstants.ORG_DEPUTY);
    jackGuybrushAssignment.beginLimitTargetContent().allowTransitive(true);
    jack.asObjectable().getAssignment().add(jackGuybrushAssignment);
    LensContext<UserType> context = new LensContextPlaceholder<>(jack);
    AssignmentEvaluator<UserType> assignmentEvaluator = new AssignmentEvaluator.Builder<UserType>().referenceResolver(referenceResolver).focusOdo(new ObjectDeltaObject<>(jack, null, jack, jack.getDefinition())).lensContext(context).channel(context.getChannel()).modelBeans(modelBeans).objectResolver(objectResolver).systemObjectCache(systemObjectCache).relationRegistry(relationRegistry).prismContext(prismContext).mappingFactory(mappingFactory).mappingEvaluator(mappingEvaluator).contextLoader(contextLoader).activationComputer(activationComputer).now(clock.currentTimeXMLGregorianCalendar()).systemConfiguration(context.getSystemConfiguration()).loginMode(true).build();
    // noinspection unchecked,rawtypes
    ItemDeltaItem<PrismContainerValue<AssignmentType>, PrismContainerDefinition<AssignmentType>> assignmentIdi = new ItemDeltaItem<>(LensUtil.createAssignmentSingleValueContainer(jackGuybrushAssignment), jackGuybrushAssignment.asPrismContainerValue().getDefinition());
    // WHEN
    when();
    EvaluatedAssignmentImpl<UserType> evaluatedAssignment = assignmentEvaluator.evaluate(assignmentIdi, PlusMinusZero.ZERO, false, jack.asObjectable(), jack.toString(), AssignmentOrigin.createInObject(), task, result);
    // THEN
    then();
    displayDumpable("Output context", context);
    displayDumpable("Evaluated assignment", evaluatedAssignment);
    assertSuccess("Assignment evaluator failed (result)", result);
    assertEquals("Wrong evaluatedAssignment.isValid", true, evaluatedAssignment.isValid());
    assertTargets(evaluatedAssignment, true, "R1 R2 O3 R4 R5 R6", null, null, null, null, null);
    assertTargets(evaluatedAssignment, false, "guybrush barbossa MR1 MR2 MR3 MR4 MMR1", null, null, null, null, null);
    assertMembershipRef(evaluatedAssignment, "");
    assertOrgRef(evaluatedAssignment, "O3");
    assertDelegation(evaluatedAssignment, "guybrush barbossa R1 R2 O3 R4 R5 R6");
    PrismReferenceValue guybrushRef = evaluatedAssignment.getDelegationRefVals().stream().filter(v -> USER_GUYBRUSH_OID.equals(v.getOid())).findFirst().orElseThrow(() -> new AssertionError("No guybrush ref in delegation ref vals"));
    assertEquals("Wrong relation for guybrush delegation", SchemaConstants.ORG_DEPUTY, guybrushRef.getRelation());
    // the following entities are not evaluated in login mode
    assertConstructions(evaluatedAssignment, (String) null, null, null, null, null, null);
    assertFocusMappings(evaluatedAssignment, (String) null);
    assertFocusPolicyRules(evaluatedAssignment, (String) null);
    assertTargetPolicyRules(evaluatedAssignment, (String) null, null);
    assertAuthorizations(evaluatedAssignment, "R1 R2 O3 R4 R5 R6");
    assertGuiConfig(evaluatedAssignment, "R1 R2 O3 R4 R5 R6");
    assertSerializable(context);
}
Also used : Task(com.evolveum.midpoint.task.api.Task) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectDeltaObject(com.evolveum.midpoint.prism.util.ObjectDeltaObject) PrismReferenceValue(com.evolveum.midpoint.prism.PrismReferenceValue) ItemDeltaItem(com.evolveum.midpoint.prism.util.ItemDeltaItem) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) Test(org.testng.annotations.Test)

Example 15 with ItemDeltaItem

use of com.evolveum.midpoint.prism.util.ItemDeltaItem in project midpoint by Evolveum.

the class TestExpressionSpec method prepareBasicVariablesWithDelta.

private VariablesMap prepareBasicVariablesWithDelta() throws SchemaException {
    VariablesMap variables = new VariablesMap();
    variables.put(VAR_FOO, "fooValue", String.class);
    PrismProperty<String> barProperty = createProperty(VAR_BAR, singleton("barValueOld"));
    PropertyDelta<String> barDelta = barProperty.createDelta();
    barDelta.setRealValuesToReplace("barValueNew");
    ItemDeltaItem<PrismPropertyValue<String>, PrismPropertyDefinition<String>> barIdi = new ItemDeltaItem<>(barProperty, barDelta, null, barProperty.getDefinition());
    barIdi.recompute();
    variables.put(VAR_BAR, barIdi, String.class);
    return variables;
}
Also used : ItemDeltaItem(com.evolveum.midpoint.prism.util.ItemDeltaItem) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

ItemDeltaItem (com.evolveum.midpoint.prism.util.ItemDeltaItem)15 ObjectDeltaObject (com.evolveum.midpoint.prism.util.ObjectDeltaObject)8 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)3 Source (com.evolveum.midpoint.repo.common.expression.Source)3 PrismContainerDefinition (com.evolveum.midpoint.prism.PrismContainerDefinition)2 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)2 TypedValue (com.evolveum.midpoint.schema.expression.TypedValue)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 Task (com.evolveum.midpoint.task.api.Task)2 EvaluatedAssignment (com.evolveum.midpoint.model.api.context.EvaluatedAssignment)1 EvaluatedAssignmentTarget (com.evolveum.midpoint.model.api.context.EvaluatedAssignmentTarget)1 SynchronizationPolicyDecision (com.evolveum.midpoint.model.api.context.SynchronizationPolicyDecision)1 ObjectValuePolicyEvaluator (com.evolveum.midpoint.model.common.stringpolicy.ObjectValuePolicyEvaluator)1 ShadowValuePolicyOriginResolver (com.evolveum.midpoint.model.common.stringpolicy.ShadowValuePolicyOriginResolver)1 ValuePolicyProcessor (com.evolveum.midpoint.model.common.stringpolicy.ValuePolicyProcessor)1 ModelObjectResolver (com.evolveum.midpoint.model.impl.ModelObjectResolver)1 com.evolveum.midpoint.model.impl.lens (com.evolveum.midpoint.model.impl.lens)1 AssignmentEvaluator (com.evolveum.midpoint.model.impl.lens.assignments.AssignmentEvaluator)1 ContextLoader (com.evolveum.midpoint.model.impl.lens.projector.ContextLoader)1