Search in sources :

Example 1 with PopulateItemType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.PopulateItemType in project midpoint by Evolveum.

the class AbstractSearchExpressionEvaluator method evaluatePopulateExpression.

private <IV extends PrismValue, ID extends ItemDefinition, C extends Containerable> ItemDelta<IV, ID> evaluatePopulateExpression(PopulateItemType populateItem, ExpressionVariables variables, ExpressionEvaluationContext params, PrismContainerDefinition<C> objectDefinition, String contextDescription, boolean evaluateMinus, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    ExpressionType expressionType = populateItem.getExpression();
    if (expressionType == null) {
        LOGGER.warn("No expression in populateObject in assignment expression in {}, " + "skipping. Subsequent operations will most likely fail", contextDescription);
        return null;
    }
    VariableBindingDefinitionType targetType = populateItem.getTarget();
    if (targetType == null) {
        LOGGER.warn("No target in populateObject in assignment expression in {}, " + "skipping. Subsequent operations will most likely fail", contextDescription);
        return null;
    }
    ItemPathType itemPathType = targetType.getPath();
    if (itemPathType == null) {
        throw new SchemaException("No path in target definition in " + contextDescription);
    }
    ItemPath targetPath = itemPathType.getItemPath();
    ID propOutputDefinition = ExpressionUtil.resolveDefinitionPath(targetPath, variables, objectDefinition, "target definition in " + contextDescription);
    if (propOutputDefinition == null) {
        throw new SchemaException("No target item that would conform to the path " + targetPath + " in " + contextDescription);
    }
    String expressionDesc = "expression in assignment expression in " + contextDescription;
    ExpressionFactory expressionFactory = params.getExpressionFactory();
    Expression<IV, ID> expression = expressionFactory.makeExpression(expressionType, propOutputDefinition, expressionDesc, task, result);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, expressionDesc, task, result);
    context.setExpressionFactory(expressionFactory);
    context.setStringPolicyResolver(params.getStringPolicyResolver());
    context.setDefaultTargetContext(params.getDefaultTargetContext());
    context.setSkipEvaluationMinus(true);
    context.setSkipEvaluationPlus(false);
    PrismValueDeltaSetTriple<IV> outputTriple = expression.evaluate(context);
    LOGGER.trace("output triple: {}", outputTriple.debugDump());
    Collection<IV> pvalues = outputTriple.getNonNegativeValues();
    // Maybe not really clean but it works. TODO: refactor later
    NameItemPathSegment first = (NameItemPathSegment) targetPath.first();
    if (first.isVariable()) {
        targetPath = targetPath.rest();
    }
    ItemDelta<IV, ID> itemDelta = propOutputDefinition.createEmptyDelta(targetPath);
    itemDelta.addValuesToAdd(PrismValue.cloneCollection(pvalues));
    LOGGER.trace("Item delta:\n{}", itemDelta.debugDump());
    return itemDelta;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) VariableBindingDefinitionType(com.evolveum.midpoint.xml.ns._public.common.common_3.VariableBindingDefinitionType) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 2 with PopulateItemType

use of com.evolveum.midpoint.xml.ns._public.common.common_3.PopulateItemType in project midpoint by Evolveum.

the class AbstractSearchExpressionEvaluator method createOnDemand.

private <O extends ObjectType> String createOnDemand(Class<O> targetTypeClass, ExpressionVariables variables, ExpressionEvaluationContext params, String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Going to create assignment targets on demand, variables:\n{}", variables.formatVariables());
    }
    PrismObjectDefinition<O> objectDefinition = prismContext.getSchemaRegistry().findObjectDefinitionByCompileTimeClass(targetTypeClass);
    PrismObject<O> newObject = objectDefinition.instantiate();
    PopulateType populateObject = getExpressionEvaluatorType().getPopulateObject();
    if (populateObject == null) {
        LOGGER.warn("No populateObject in assignment expression in {}, " + "object created on demand will be empty. Subsequent operations will most likely fail", contextDescription);
    } else {
        for (PopulateItemType populateItem : populateObject.getPopulateItem()) {
            ItemDelta<?, ?> itemDelta = evaluatePopulateExpression(populateItem, variables, params, objectDefinition, contextDescription, true, task, result);
            if (itemDelta != null) {
                itemDelta.applyTo(newObject);
            }
        }
    }
    LOGGER.debug("Creating object on demand from {}: {}", contextDescription, newObject);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Creating object on demand:\n{}", newObject.debugDump());
    }
    ObjectDelta<O> addDelta = newObject.createAddDelta();
    Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(addDelta);
    try {
        modelService.executeChanges(deltas, null, task, result);
    } catch (ObjectAlreadyExistsException | CommunicationException | ConfigurationException | PolicyViolationException | SecurityViolationException e) {
        throw new ExpressionEvaluationException(e.getMessage(), e);
    }
    return addDelta.getOid();
}
Also used : ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) PopulateItemType(com.evolveum.midpoint.xml.ns._public.common.common_3.PopulateItemType) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) PolicyViolationException(com.evolveum.midpoint.util.exception.PolicyViolationException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException) PopulateType(com.evolveum.midpoint.xml.ns._public.common.common_3.PopulateType)

Aggregations

ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 NameItemPathSegment (com.evolveum.midpoint.prism.path.NameItemPathSegment)1 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)1 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)1 CommunicationException (com.evolveum.midpoint.util.exception.CommunicationException)1 ConfigurationException (com.evolveum.midpoint.util.exception.ConfigurationException)1 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)1 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)1 PolicyViolationException (com.evolveum.midpoint.util.exception.PolicyViolationException)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SecurityViolationException (com.evolveum.midpoint.util.exception.SecurityViolationException)1 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)1 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)1 PopulateItemType (com.evolveum.midpoint.xml.ns._public.common.common_3.PopulateItemType)1 PopulateType (com.evolveum.midpoint.xml.ns._public.common.common_3.PopulateType)1 VariableBindingDefinitionType (com.evolveum.midpoint.xml.ns._public.common.common_3.VariableBindingDefinitionType)1 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)1