Search in sources :

Example 96 with ItemPath

use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.

the class PathExpressionEvaluator method evaluate.

/* (non-Javadoc)
	 * @see com.evolveum.midpoint.common.expression.ExpressionEvaluator#evaluate(java.util.Collection, java.util.Map, boolean, java.lang.String, com.evolveum.midpoint.schema.result.OperationResult)
	 */
@Override
public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    ItemDeltaItem<?, ?> resolveContext = null;
    if (context.getSources() != null && context.getSources().size() == 1) {
        Source<?, ?> source = context.getSources().iterator().next();
        if (path.isEmpty()) {
            PrismValueDeltaSetTriple<V> outputTriple = (PrismValueDeltaSetTriple<V>) source.toDeltaSetTriple();
            return outputTriple.clone();
        }
        resolveContext = source;
    }
    Map<QName, Object> variablesAndSources = ExpressionUtil.compileVariablesAndSources(context);
    ItemPath resolvePath = path;
    ItemPathSegment first = path.first();
    if (first instanceof NameItemPathSegment && first.isVariable()) {
        QName variableName = ((NameItemPathSegment) first).getName();
        Object variableValue;
        if (variablesAndSources.containsKey(variableName)) {
            variableValue = variablesAndSources.get(variableName);
        } else if (QNameUtil.matchAny(variableName, variablesAndSources.keySet())) {
            QName fullVariableName = QNameUtil.resolveNs(variableName, variablesAndSources.keySet());
            variableValue = variablesAndSources.get(fullVariableName);
        } else {
            throw new ExpressionEvaluationException("No variable with name " + variableName + " in " + context.getContextDescription());
        }
        if (variableValue == null) {
            return null;
        }
        if (variableValue instanceof Item || variableValue instanceof ItemDeltaItem<?, ?>) {
            resolveContext = ExpressionUtil.toItemDeltaItem(variableValue, objectResolver, "path expression in " + context.getContextDescription(), context.getResult());
        } else if (variableValue instanceof PrismPropertyValue<?>) {
            PrismValueDeltaSetTriple<V> outputTriple = new PrismValueDeltaSetTriple<>();
            outputTriple.addToZeroSet((V) variableValue);
            return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
        } else {
            throw new ExpressionEvaluationException("Unexpected variable value " + variableValue + " (" + variableValue.getClass() + ")");
        }
        resolvePath = path.rest();
    }
    if (resolveContext == null) {
        return null;
    }
    while (!resolvePath.isEmpty()) {
        if (resolveContext.isContainer()) {
            resolveContext = resolveContext.findIdi(resolvePath.head());
            resolvePath = resolvePath.tail();
            if (resolveContext == null) {
                throw new ExpressionEvaluationException("Cannot find item using path " + path + " in " + context.getContextDescription());
            }
        } else if (resolveContext.isStructuredProperty()) {
            // The output path does not really matter. The delta will be converted to triple anyway
            // But the path cannot be null, oherwise the code will die
            resolveContext = resolveContext.resolveStructuredProperty(resolvePath, (PrismPropertyDefinition) outputDefinition, new ItemPath());
            break;
        } else if (resolveContext.isNull()) {
            break;
        } else {
            throw new ExpressionEvaluationException("Cannot resolve path " + resolvePath + " on " + resolveContext + " in " + context.getContextDescription());
        }
    }
    PrismValueDeltaSetTriple<V> outputTriple = ItemDelta.toDeltaSetTriple((Item<V, D>) resolveContext.getItemOld(), (ItemDelta<V, D>) resolveContext.getDelta());
    if (outputTriple == null) {
        return null;
    }
    return ExpressionUtil.toOutputTriple(outputTriple, outputDefinition, context.getAdditionalConvertor(), null, protector, prismContext);
}
Also used : ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) QName(javax.xml.namespace.QName) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemPathSegment(com.evolveum.midpoint.prism.path.ItemPathSegment) NameItemPathSegment(com.evolveum.midpoint.prism.path.NameItemPathSegment) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) Item(com.evolveum.midpoint.prism.Item) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 97 with ItemPath

use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.

the class PathExpressionEvaluatorFactory method createEvaluator.

/* (non-Javadoc)
	 * @see com.evolveum.midpoint.common.expression.ExpressionEvaluatorFactory#createEvaluator(javax.xml.bind.JAXBElement, com.evolveum.midpoint.prism.ItemDefinition, com.evolveum.midpoint.prism.PrismContext)
	 */
@Override
public <V extends PrismValue, D extends ItemDefinition> ExpressionEvaluator<V, D> createEvaluator(Collection<JAXBElement<?>> evaluatorElements, D outputDefinition, String contextDescription, Task task, OperationResult result) throws SchemaException {
    Validate.notNull(outputDefinition, "output definition must be specified for path expression evaluator");
    if (evaluatorElements.size() > 1) {
        throw new SchemaException("More than one evaluator specified in " + contextDescription);
    }
    JAXBElement<?> evaluatorElement = evaluatorElements.iterator().next();
    Object evaluatorElementObject = evaluatorElement.getValue();
    if (!(evaluatorElementObject instanceof ItemPathType)) {
        throw new IllegalArgumentException("Path expression cannot handle elements of type " + evaluatorElementObject.getClass().getName() + " in " + contextDescription);
    }
    ItemPath path = ((ItemPathType) evaluatorElementObject).getItemPath();
    return new PathExpressionEvaluator<>(path, objectResolver, outputDefinition, protector, prismContext);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 98 with ItemPath

use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.

the class Mapping method parseSource.

private <IV extends PrismValue, ID extends ItemDefinition> Source<IV, ID> parseSource(VariableBindingDefinitionType sourceType, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException {
    ItemPathType itemPathType = sourceType.getPath();
    if (itemPathType == null) {
        throw new SchemaException("No path in source definition in " + getMappingContextDescription());
    }
    ItemPath path = itemPathType.getItemPath();
    if (path.isEmpty()) {
        throw new SchemaException("Empty source path in " + getMappingContextDescription());
    }
    QName name = sourceType.getName();
    if (name == null) {
        name = ItemPath.getName(path.last());
    }
    ItemPath resolvePath = path;
    Object sourceObject = ExpressionUtil.resolvePath(path, variables, sourceContext, objectResolver, "source definition in " + getMappingContextDescription(), task, result);
    Item<IV, ID> itemOld = null;
    ItemDelta<IV, ID> delta = null;
    Item<IV, ID> itemNew = null;
    ItemPath residualPath = null;
    Collection<? extends ItemDelta<?, ?>> subItemDeltas = null;
    if (sourceObject != null) {
        if (sourceObject instanceof ItemDeltaItem<?, ?>) {
            itemOld = ((ItemDeltaItem<IV, ID>) sourceObject).getItemOld();
            delta = ((ItemDeltaItem<IV, ID>) sourceObject).getDelta();
            itemNew = ((ItemDeltaItem<IV, ID>) sourceObject).getItemNew();
            residualPath = ((ItemDeltaItem<IV, ID>) sourceObject).getResidualPath();
            resolvePath = ((ItemDeltaItem<IV, ID>) sourceObject).getResolvePath();
            subItemDeltas = ((ItemDeltaItem<IV, ID>) sourceObject).getSubItemDeltas();
        } else if (sourceObject instanceof Item<?, ?>) {
            itemOld = (Item<IV, ID>) sourceObject;
            itemNew = (Item<IV, ID>) sourceObject;
        } else {
            throw new IllegalStateException("Unknown resolve result " + sourceObject);
        }
    }
    // apply domain
    ValueSetDefinitionType domainSetType = sourceType.getSet();
    if (domainSetType != null) {
        ValueSetDefinition setDef = new ValueSetDefinition(domainSetType, name, "domain of " + name.getLocalPart() + " in " + getMappingContextDescription(), task, result);
        setDef.init(expressionFactory);
        try {
            if (itemOld != null) {
                itemOld = itemOld.clone();
                itemOld.filterValues(val -> setDef.containsTunnel(val));
            }
            if (itemNew != null) {
                itemNew = itemNew.clone();
                itemNew.filterValues(val -> setDef.containsTunnel(val));
            }
            if (delta != null) {
                delta = delta.clone();
                delta.filterValues(val -> setDef.containsTunnel(val));
            }
        } catch (TunnelException te) {
            Throwable cause = te.getCause();
            if (cause instanceof SchemaException) {
                throw (SchemaException) cause;
            } else if (cause instanceof ExpressionEvaluationException) {
                throw (ExpressionEvaluationException) cause;
            } else if (cause instanceof ObjectNotFoundException) {
                throw (ObjectNotFoundException) cause;
            }
        }
    }
    Source<IV, ID> source = new Source<>(itemOld, delta, itemNew, name);
    source.setResidualPath(residualPath);
    source.setResolvePath(resolvePath);
    source.setSubItemDeltas(subItemDeltas);
    return source;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) QName(javax.xml.namespace.QName) Source(com.evolveum.midpoint.repo.common.expression.Source) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) TunnelException(com.evolveum.midpoint.util.exception.TunnelException) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) ValueSetDefinition(com.evolveum.midpoint.repo.common.expression.ValueSetDefinition) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 99 with ItemPath

use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.

the class BasicExpressionFunctions method getPropertyValues.

public <T> Collection<T> getPropertyValues(ObjectType object, String path) {
    if (object == null) {
        return null;
    }
    ScriptExpressionEvaluationContext scriptContext = ScriptExpressionEvaluationContext.getThreadLocal();
    ScriptExpression scriptExpression = scriptContext.getScriptExpression();
    ItemPath itemPath = scriptExpression.parsePath(path);
    PrismProperty property = object.asPrismObject().findProperty(itemPath);
    if (property == null) {
        return new ArrayList<T>(0);
    }
    return property.getRealValues();
}
Also used : ScriptExpressionEvaluationContext(com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext) ArrayList(java.util.ArrayList) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 100 with ItemPath

use of com.evolveum.midpoint.prism.path.ItemPath in project midpoint by Evolveum.

the class ObjectWrapper method createAddingObjectDelta.

private ObjectDelta createAddingObjectDelta() throws SchemaException {
    PrismObject object = this.object.clone();
    List<ContainerWrapper<? extends Containerable>> containers = getContainers();
    // sort containers by path size
    Collections.sort(containers, new PathSizeComparator());
    for (ContainerWrapper containerWrapper : getContainers()) {
        if (containerWrapper.getItemDefinition().getName().equals(ShadowType.F_ASSOCIATION)) {
            PrismContainer associationContainer = object.findOrCreateContainer(ShadowType.F_ASSOCIATION);
            List<AssociationWrapper> associationItemWrappers = (List<AssociationWrapper>) containerWrapper.getItems();
            for (AssociationWrapper associationItemWrapper : associationItemWrappers) {
                List<ValueWrapper> assocValueWrappers = associationItemWrapper.getValues();
                for (ValueWrapper assocValueWrapper : assocValueWrappers) {
                    PrismContainerValue<ShadowAssociationType> assocValue = (PrismContainerValue<ShadowAssociationType>) assocValueWrapper.getValue();
                    associationContainer.add(assocValue.clone());
                }
            }
            continue;
        }
        if (!containerWrapper.hasChanged()) {
            continue;
        }
        PrismContainer container = containerWrapper.getItem();
        ItemPath path = containerWrapper.getPath();
        if (containerWrapper.getPath() != null) {
            container = container.clone();
            if (path.size() > 1) {
                ItemPath parentPath = path.allExceptLast();
                PrismContainer parent = object.findOrCreateContainer(parentPath);
                parent.add(container);
            } else {
                PrismContainer existing = object.findContainer(container.getElementName());
                if (existing == null) {
                    object.add(container);
                } else {
                    continue;
                }
            }
        } else {
            container = object;
        }
        for (ItemWrapper itemWrapper : (List<ItemWrapper>) containerWrapper.getItems()) {
            if (!itemWrapper.hasChanged()) {
                continue;
            }
            if (container.findItem(itemWrapper.getName()) != null) {
                continue;
            }
            Item updatedItem = ((PropertyOrReferenceWrapper) itemWrapper).getUpdatedItem(object.getPrismContext());
            if (!updatedItem.isEmpty()) {
                container.add(updatedItem);
            }
        }
    }
    // cleanup empty containers
    cleanupEmptyContainers(object);
    ObjectDelta delta = ObjectDelta.createAddDelta(object);
    // returning container to previous order
    Collections.sort(containers, new ItemWrapperComparator());
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Creating delta from wrapper {}: adding object, creating complete ADD delta:\n{}", this, delta.debugDump());
    }
    if (InternalsConfig.consistencyChecks) {
        delta.checkConsistence(true, true, true, ConsistencyCheckScope.THOROUGH);
    }
    return delta;
}
Also used : ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

ItemPath (com.evolveum.midpoint.prism.path.ItemPath)693 Test (org.testng.annotations.Test)184 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)143 QName (javax.xml.namespace.QName)137 Task (com.evolveum.midpoint.task.api.Task)104 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)84 ArrayList (java.util.ArrayList)79 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)71 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)68 ItemDelta (com.evolveum.midpoint.prism.delta.ItemDelta)61 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)48 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)46 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)41 NameItemPathSegment (com.evolveum.midpoint.prism.path.NameItemPathSegment)41 PrismObject (com.evolveum.midpoint.prism.PrismObject)38 ObjectDelta (com.evolveum.midpoint.prism.delta.ObjectDelta)38 PropertyDelta (com.evolveum.midpoint.prism.delta.PropertyDelta)38 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)33 IdItemPathSegment (com.evolveum.midpoint.prism.path.IdItemPathSegment)31 NotNull (org.jetbrains.annotations.NotNull)30