Search in sources :

Example 11 with TypedValue

use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.

the class ExpressionUtil method resolveReference.

private static TypedValue<?> resolveReference(TypedValue referenceTypedValue, String variableName, ObjectResolver objectResolver, String contextDescription, ObjectVariableModeType objectVariableMode, Task task, OperationResult result) throws ExpressionSyntaxException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    TypedValue<?> resolvedTypedValue;
    ObjectReferenceType reference = ((ObjectReferenceType) referenceTypedValue.getValue()).clone();
    // TODO proper op result handling (for tracing)
    OperationResult subResult = new OperationResult("Resolve reference");
    try {
        Collection<SelectorOptions<GetOperationOptions>> options = null;
        if (reference != null && QNameUtil.match(reference.getType(), ResourceType.COMPLEX_TYPE)) {
            options = GetOperationOptions.createNoFetchCollection();
        }
        resolvedTypedValue = resolveReference(referenceTypedValue, objectResolver, options, variableName, contextDescription, task, subResult);
    } catch (SchemaException e) {
        result.addSubresult(subResult);
        throw new ExpressionSyntaxException("Schema error during variable " + variableName + " resolution in " + contextDescription + ": " + e.getMessage(), e);
    } catch (ObjectNotFoundException e) {
        if (ObjectVariableModeType.OBJECT.equals(objectVariableMode)) {
            result.addSubresult(subResult);
            throw e;
        } else {
            resolvedTypedValue = null;
        }
    } catch (Exception e) {
        result.addSubresult(subResult);
        throw e;
    }
    if (objectVariableMode == ObjectVariableModeType.PRISM_REFERENCE) {
        if (resolvedTypedValue != null && resolvedTypedValue.getValue() instanceof PrismObject) {
            PrismReferenceValue value = reference.asReferenceValue();
            value.setObject((PrismObject) resolvedTypedValue.getValue());
            return new TypedValue<>(value, value.getDefinition());
        } else {
            return referenceTypedValue;
        }
    } else {
        return resolvedTypedValue;
    }
}
Also used : SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 12 with TypedValue

use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.

the class ExpressionUtil method resolveDefinitionPath.

public static <ID extends ItemDefinition> ID resolveDefinitionPath(@NotNull ItemPath path, VariablesMap variables, PrismContainerDefinition<?> defaultContext, String shortDesc) throws SchemaException {
    while (!path.isEmpty() && !path.startsWithName() && !path.startsWithVariable()) {
        path = path.rest();
    }
    Object root = defaultContext;
    ItemPath relativePath = path;
    Object first = path.first();
    if (ItemPath.isVariable(first)) {
        relativePath = path.rest();
        String varName = ItemPath.toVariableName(first).getLocalPart();
        if (variables.containsKey(varName)) {
            TypedValue typeVarValue = variables.get(varName);
            Object varValue = typeVarValue.getValue();
            if (varValue instanceof ItemDeltaItem<?, ?>) {
                root = ((ItemDeltaItem<?, ?>) varValue).getDefinition();
            } else if (varValue instanceof Item<?, ?>) {
                root = ((Item<?, ?>) varValue).getDefinition();
            } else if (varValue instanceof Objectable) {
                root = ((Objectable) varValue).asPrismObject().getDefinition();
            } else if (varValue instanceof ItemDefinition) {
                root = varValue;
            } else {
                throw new IllegalStateException("Unexpected content of variable " + varName + ": " + varValue + " (" + varValue.getClass() + ")");
            }
            if (root == null) {
                throw new IllegalStateException("Null definition in content of variable '" + varName + "': " + varValue);
            }
        } else {
            throw new SchemaException("No variable with name '" + varName + "' in " + shortDesc);
        }
    }
    if (root == null) {
        return null;
    }
    if (relativePath.isEmpty()) {
        return (ID) root;
    }
    if (root instanceof PrismObjectDefinition<?>) {
        return ((PrismObjectDefinition<?>) root).findItemDefinition(relativePath);
    } else if (root instanceof PrismContainerDefinition<?>) {
        return ((PrismContainerDefinition<?>) root).findItemDefinition(relativePath);
    } else if (root instanceof ItemDefinition) {
        // Except for container (which is handled above)
        throw new SchemaException("Cannot apply path " + relativePath + " to " + root + " in " + shortDesc);
    } else {
        throw new IllegalArgumentException("Unexpected root " + root + " in " + shortDesc);
    }
}
Also used : PolyString(com.evolveum.midpoint.prism.polystring.PolyString) GString(groovy.lang.GString) ItemDeltaItem(com.evolveum.midpoint.prism.util.ItemDeltaItem) ItemDeltaItem(com.evolveum.midpoint.prism.util.ItemDeltaItem) ObjectDeltaObject(com.evolveum.midpoint.prism.util.ObjectDeltaObject) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 13 with TypedValue

use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.

the class CustomFunctions method convertInput.

private TypedValue<?> convertInput(Map.Entry<String, Object> entry, ExpressionType expression) throws SchemaException {
    String argName = entry.getKey();
    Object argValue = entry.getValue();
    LOGGER.trace("Converting argument: {} = {}", argName, argValue);
    ExpressionParameterType matchingExpressionParameter = expression.getParameter().stream().filter(param -> param.getName().equals(argName)).findAny().orElseThrow(() -> new SchemaException(getUnknownParameterMessage(expression, argName)));
    QName paramType = matchingExpressionParameter.getType();
    Class<?> expressionParameterClass = prismContext.getSchemaRegistry().determineClassForType(paramType);
    Object value;
    if (expressionParameterClass != null && !DOMUtil.XSD_ANYTYPE.equals(paramType) && XmlTypeConverter.canConvert(expressionParameterClass)) {
        value = ExpressionUtil.convertValue(expressionParameterClass, null, argValue, prismContext.getDefaultProtector(), prismContext);
    } else {
        value = argValue;
    }
    Class<?> valueClass;
    if (value == null) {
        valueClass = expressionParameterClass;
    } else {
        valueClass = value.getClass();
    }
    // StatePolicyConstraintType class). So let's provide valueClass here only.
    return new TypedValue<>(value, valueClass);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) QName(javax.xml.namespace.QName) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 14 with TypedValue

use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.

the class AbstractScriptEvaluator method prepareScriptVariablesValueMap.

/**
 * Returns simple variable map: name -> value.
 */
protected Map<String, Object> prepareScriptVariablesValueMap(ScriptExpressionEvaluationContext context) throws ExpressionSyntaxException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    Map<String, Object> scriptVariableMap = new HashMap<>();
    // Functions
    if (context.getFunctions() != null) {
        for (FunctionLibrary funcLib : context.getFunctions()) {
            scriptVariableMap.put(funcLib.getVariableName(), funcLib.getGenericFunctions());
        }
    }
    // Variables
    VariablesMap variables = context.getVariables();
    if (variables != null) {
        for (Entry<String, TypedValue> variableEntry : variables.entrySet()) {
            if (variableEntry.getKey() == null) {
                // This is the "root" node. We have no use for it in script expressions, just skip it
                continue;
            }
            String variableName = variableEntry.getKey();
            ValueVariableModeType valueVariableMode = ObjectUtils.defaultIfNull(context.getExpressionType().getValueVariableMode(), ValueVariableModeType.REAL_VALUE);
            // noinspection rawtypes
            TypedValue variableTypedValue = ExpressionUtil.convertVariableValue(variableEntry.getValue(), variableName, context.getObjectResolver(), context.getContextDescription(), context.getExpressionType().getObjectVariableMode(), valueVariableMode, prismContext, context.getTask(), context.getResult());
            scriptVariableMap.put(variableName, variableTypedValue.getValue());
            if (context.getTrace() != null && !variables.isAlias(variableName)) {
                ScriptVariableEvaluationTraceType variableTrace = new ScriptVariableEvaluationTraceType(prismContext);
                variableTrace.setName(new QName(variableName));
                Object clonedValue = cloneIfPossible(variableTypedValue.getValue());
                variableTrace.getValue().addAll(TraceUtil.toAnyValueTypeList(clonedValue, prismContext));
                variables.getAliases(variableName).forEach(alias -> variableTrace.getAlias().add(new QName(alias)));
                context.getTrace().getVariable().add(variableTrace);
            }
        }
    }
    putIfMissing(scriptVariableMap, ExpressionConstants.VAR_PRISM_CONTEXT, prismContext);
    putIfMissing(scriptVariableMap, ExpressionConstants.VAR_LOCALIZATION_SERVICE, localizationService);
    return scriptVariableMap;
}
Also used : ScriptVariableEvaluationTraceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptVariableEvaluationTraceType) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) ValueVariableModeType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValueVariableModeType) FunctionLibrary(com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 15 with TypedValue

use of com.evolveum.midpoint.schema.expression.TypedValue in project midpoint by Evolveum.

the class MappedItem method createMappings.

/**
 * Creates the respective mapping(s).
 */
void createMappings(@NotNull PathKeyedMap<List<InboundMappingInContext<?, ?>>> mappingsMap) throws SchemaException, ExpressionEvaluationException, CommunicationException, SecurityViolationException, ConfigurationException, ObjectNotFoundException {
    boolean fromAbsoluteState = processingMode == ProcessingMode.ABSOLUTE_STATE || processingMode == ProcessingMode.ABSOLUTE_STATE_IF_KNOWN;
    if (fromAbsoluteState && !source.isAbsoluteStateAvailable()) {
        LOGGER.trace("Skipping inbound mapping(s) for {} as they should be processed from absolute state, but we don't" + " have one", itemDescription);
        return;
    }
    Item<V, D> currentProjectionItem = itemProvider.provide();
    if (postProcessor != null) {
        postProcessor.postProcess(itemAPrioriDelta, currentProjectionItem);
    }
    LOGGER.trace("Creating {} inbound mapping(s) for {} in {} ({}). Relevant values are:\n" + "- a priori item delta:\n{}\n" + "- current item:\n{}", mappingBeans.size(), itemDescription, source.getProjectionHumanReadableNameLazy(), fromAbsoluteState ? "absolute mode" : "relative mode", DebugUtil.debugDumpLazily(itemAPrioriDelta, 1), DebugUtil.debugDumpLazily(currentProjectionItem, 1));
    if (currentProjectionItem != null && currentProjectionItem.hasRaw()) {
        throw new SystemException("Property " + currentProjectionItem + " has raw parsing state," + " such property cannot be used in inbound expressions");
    }
    source.setValueMetadata(currentProjectionItem, itemAPrioriDelta);
    ResourceType resource = source.getResource();
    // Value for the $shadow ($projection, $account) variable.
    // TODO Why do we use "object new" here? (We should perhaps go with ODO, shouldn't we?)
    // Bear in mind that the value might not contain the full shadow (for example)
    PrismObject<ShadowType> shadowVariableValue = source.getResourceObjectNew();
    PrismObjectDefinition<ShadowType> shadowVariableDef = getShadowDefinition(shadowVariableValue);
    Source<V, D> defaultSource = new Source<>(currentProjectionItem, itemAPrioriDelta, null, ExpressionConstants.VAR_INPUT_QNAME, itemDefinition);
    defaultSource.recompute();
    for (MappingType mappingBean : mappingBeans) {
        String channel = source.getChannel();
        if (!MappingImpl.isApplicableToChannel(mappingBean, channel)) {
            LOGGER.trace("Mapping is not applicable to channel {}", channel);
            continue;
        }
        MappingBuilder<V, D> builder = beans.mappingFactory.<V, D>createMappingBuilder().mappingBean(mappingBean).mappingKind(MappingKindType.INBOUND).implicitSourcePath(implicitSourcePath).contextDescription("inbound expression for " + itemDescription + " in " + resource).defaultSource(defaultSource).targetContext(target.focusDefinition).addVariableDefinition(ExpressionConstants.VAR_USER, target.focus, target.focusDefinition).addVariableDefinition(ExpressionConstants.VAR_FOCUS, target.focus, target.focusDefinition).addAliasRegistration(ExpressionConstants.VAR_USER, ExpressionConstants.VAR_FOCUS).addVariableDefinition(ExpressionConstants.VAR_ACCOUNT, shadowVariableValue, shadowVariableDef).addVariableDefinition(ExpressionConstants.VAR_SHADOW, shadowVariableValue, shadowVariableDef).addVariableDefinition(ExpressionConstants.VAR_PROJECTION, shadowVariableValue, shadowVariableDef).addAliasRegistration(ExpressionConstants.VAR_ACCOUNT, ExpressionConstants.VAR_PROJECTION).addAliasRegistration(ExpressionConstants.VAR_SHADOW, ExpressionConstants.VAR_PROJECTION).addVariableDefinition(ExpressionConstants.VAR_RESOURCE, resource, resource.asPrismObject().getDefinition()).addVariableDefinition(ExpressionConstants.VAR_CONFIGURATION, context.getSystemConfiguration(), getSystemConfigurationDefinition()).addVariableDefinition(ExpressionConstants.VAR_OPERATION, context.getOperation(), String.class).variableResolver(variableProducer).valuePolicySupplier(context.createValuePolicySupplier()).originType(OriginType.INBOUND).originObject(resource).now(context.env.now);
        if (!target.isFocusBeingDeleted()) {
            assert target.focus != null;
            TypedValue<PrismObject<F>> targetContext = new TypedValue<>(target.focus);
            builder.originalTargetValues(ExpressionUtil.computeTargetValues(mappingBean.getTarget(), targetContext, builder.getVariables(), beans.mappingFactory.getObjectResolver(), "resolving target values", beans.prismContext, context.env.task, context.result));
        }
        MappingImpl<V, D> mapping = builder.build();
        if (checkWeakSkip(mapping)) {
            LOGGER.trace("Skipping because of mapping is weak and focus property has already a value");
            continue;
        }
        InboundMappingInContext<V, D> mappingStruct = source.createInboundMappingInContext(mapping);
        ItemPath targetFocusItemPath = mapping.getOutputPath();
        if (ItemPath.isEmpty(targetFocusItemPath)) {
            throw new ConfigurationException("Empty target path in " + mapping.getContextDescription());
        }
        checkTargetItemDefinitionKnown(targetFocusItemPath);
        mappingsMap.computeIfAbsent(targetFocusItemPath, k -> new ArrayList<>()).add(mappingStruct);
    }
}
Also used : com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) ModelBeans(com.evolveum.midpoint.model.impl.ModelBeans) InboundMappingInContext(com.evolveum.midpoint.model.impl.lens.projector.focus.inbounds.InboundMappingInContext) Trace(com.evolveum.midpoint.util.logging.Trace) DebugUtil(com.evolveum.midpoint.util.DebugUtil) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) VariableProducer(com.evolveum.midpoint.repo.common.expression.VariableProducer) ExpressionConstants(com.evolveum.midpoint.schema.constants.ExpressionConstants) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ArrayList(java.util.ArrayList) MappingImpl(com.evolveum.midpoint.model.common.mapping.MappingImpl) PathKeyedMap(com.evolveum.midpoint.prism.path.PathKeyedMap) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) Collection(java.util.Collection) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue) MiscUtil(com.evolveum.midpoint.util.MiscUtil) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Objects(java.util.Objects) List(java.util.List) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) MiscUtil.argCheck(com.evolveum.midpoint.util.MiscUtil.argCheck) Source(com.evolveum.midpoint.repo.common.expression.Source) MappingBuilder(com.evolveum.midpoint.model.common.mapping.MappingBuilder) NotNull(org.jetbrains.annotations.NotNull) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ArrayList(java.util.ArrayList) Source(com.evolveum.midpoint.repo.common.expression.Source) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

TypedValue (com.evolveum.midpoint.schema.expression.TypedValue)15 VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)5 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)4 ObjectDeltaObject (com.evolveum.midpoint.prism.util.ObjectDeltaObject)4 ItemDeltaItem (com.evolveum.midpoint.prism.util.ItemDeltaItem)3 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)3 QName (javax.xml.namespace.QName)3 FunctionLibrary (com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)2 Source (com.evolveum.midpoint.repo.common.expression.Source)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 JAXBElement (javax.xml.bind.JAXBElement)2 NotNull (org.jetbrains.annotations.NotNull)2 ScriptExpressionEvaluationContext (com.evolveum.midpoint.model.common.expression.script.ScriptExpressionEvaluationContext)1 MappingBuilder (com.evolveum.midpoint.model.common.mapping.MappingBuilder)1 MappingImpl (com.evolveum.midpoint.model.common.mapping.MappingImpl)1 ModelBeans (com.evolveum.midpoint.model.impl.ModelBeans)1 InboundMappingInContext (com.evolveum.midpoint.model.impl.lens.projector.focus.inbounds.InboundMappingInContext)1 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)1