Search in sources :

Example 1 with GenerateExpressionEvaluatorType

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

the class GenerateExpressionEvaluatorFactory method createEvaluator.

/* (non-Javadoc)
	 * @see com.evolveum.midpoint.common.expression.ExpressionEvaluatorFactory#createEvaluator(javax.xml.bind.JAXBElement, 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, ObjectNotFoundException {
    Validate.notNull(outputDefinition, "output definition must be specified for 'generate' expression evaluator");
    if (evaluatorElements.size() > 1) {
        throw new SchemaException("More than one evaluator specified in " + contextDescription);
    }
    JAXBElement<?> evaluatorElement = evaluatorElements.iterator().next();
    Object evaluatorTypeObject = null;
    if (evaluatorElement != null) {
        evaluatorTypeObject = evaluatorElement.getValue();
    }
    if (evaluatorTypeObject != null && !(evaluatorTypeObject instanceof GenerateExpressionEvaluatorType)) {
        throw new SchemaException("Generate expression evaluator cannot handle elements of type " + evaluatorTypeObject.getClass().getName() + " in " + contextDescription);
    }
    GenerateExpressionEvaluatorType generateEvaluatorType = (GenerateExpressionEvaluatorType) evaluatorTypeObject;
    return new GenerateExpressionEvaluator<V, D>(generateEvaluatorType, outputDefinition, protector, objectResolver, valuePolicyGenerator, prismContext);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) GenerateExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.GenerateExpressionEvaluatorType)

Example 2 with GenerateExpressionEvaluatorType

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

the class OutboundProcessor method evaluateMapping.

private <F extends FocusType, V extends PrismValue, D extends ItemDefinition> Mapping<V, D> evaluateMapping(final Mapping.Builder<V, D> mappingBuilder, QName mappingQName, D targetDefinition, ObjectDeltaObject<F> focusOdo, ObjectDeltaObject<ShadowType> projectionOdo, String operation, RefinedObjectClassDefinition rOcDef, RefinedObjectClassDefinition assocTargetObjectClassDefinition, LensContext<F> context, LensProjectionContext projCtx, final Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    if (!mappingBuilder.isApplicableToChannel(context.getChannel())) {
        LOGGER.trace("Skipping outbound mapping for {} because the channel does not match", mappingQName);
        return null;
    }
    // TODO: check access
    // This is just supposed to be an optimization. The consolidation should deal with the weak mapping
    // even if it is there. But in that case we do not need to evaluate it at all.
    // Edit 2017-02-16 pmed: It's not quite true. If the attribute is non-tolerant, it will get removed if we would
    // skip evaluation of this mapping. So we really need to do this.
    //		if (mappingBuilder.getStrength() == MappingStrengthType.WEAK && projCtx.hasValueForAttribute(mappingQName)) {
    //			LOGGER.trace("Skipping outbound mapping for {} because it is weak", mappingQName);
    //			return null;
    //		}
    mappingBuilder.setDefaultTargetDefinition(targetDefinition);
    mappingBuilder.setSourceContext(focusOdo);
    mappingBuilder.setMappingQName(mappingQName);
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_USER, focusOdo);
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_FOCUS, focusOdo);
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_ACCOUNT, projectionOdo);
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_SHADOW, projectionOdo);
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_PROJECTION, projectionOdo);
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_CONFIGURATION, context.getSystemConfiguration());
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_ITERATION, LensUtil.getIterationVariableValue(projCtx));
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_ITERATION_TOKEN, LensUtil.getIterationTokenVariableValue(projCtx));
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_RESOURCE, projCtx.getResource());
    mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_OPERATION, operation);
    if (assocTargetObjectClassDefinition != null) {
        mappingBuilder.addVariableDefinition(ExpressionConstants.VAR_ASSOCIATION_TARGET_OBJECT_CLASS_DEFINITION, assocTargetObjectClassDefinition);
    }
    mappingBuilder.setRootNode(focusOdo);
    mappingBuilder.setOriginType(OriginType.OUTBOUND);
    mappingBuilder.setRefinedObjectClassDefinition(rOcDef);
    StringPolicyResolver stringPolicyResolver = new StringPolicyResolver() {

        private ItemPath outputPath;

        private ItemDefinition outputDefinition;

        @Override
        public void setOutputPath(ItemPath outputPath) {
            this.outputPath = outputPath;
        }

        @Override
        public void setOutputDefinition(ItemDefinition outputDefinition) {
            this.outputDefinition = outputDefinition;
        }

        @Override
        public StringPolicyType resolve() {
            if (mappingBuilder.getMappingType().getExpression() != null) {
                List<JAXBElement<?>> evaluators = mappingBuilder.getMappingType().getExpression().getExpressionEvaluator();
                for (JAXBElement jaxbEvaluator : evaluators) {
                    Object object = jaxbEvaluator.getValue();
                    if (object instanceof GenerateExpressionEvaluatorType && ((GenerateExpressionEvaluatorType) object).getValuePolicyRef() != null) {
                        ObjectReferenceType ref = ((GenerateExpressionEvaluatorType) object).getValuePolicyRef();
                        try {
                            ValuePolicyType valuePolicyType = mappingBuilder.getObjectResolver().resolve(ref, ValuePolicyType.class, null, "resolving value policy for generate attribute " + outputDefinition.getName() + "value", task, new OperationResult("Resolving value policy"));
                            if (valuePolicyType != null) {
                                return valuePolicyType.getStringPolicy();
                            }
                        } catch (CommonException ex) {
                            throw new SystemException(ex.getMessage(), ex);
                        }
                    }
                }
            }
            return null;
        }
    };
    mappingBuilder.setStringPolicyResolver(stringPolicyResolver);
    // (e.g. in old values in ADD situations and new values in DELETE situations).
    if (focusOdo.getOldObject() == null) {
        mappingBuilder.setConditionMaskOld(false);
    }
    if (focusOdo.getNewObject() == null) {
        mappingBuilder.setConditionMaskNew(false);
    }
    Mapping<V, D> mapping = mappingBuilder.build();
    mappingEvaluator.evaluateMapping(mapping, context, projCtx, task, result);
    return mapping;
}
Also used : ValuePolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) StringPolicyResolver(com.evolveum.midpoint.repo.common.expression.StringPolicyResolver) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) JAXBElement(javax.xml.bind.JAXBElement) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) GenerateExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.GenerateExpressionEvaluatorType) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 3 with GenerateExpressionEvaluatorType

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

the class MappingEvaluator method createFocusMapping.

public <V extends PrismValue, D extends ItemDefinition, F extends FocusType, T extends FocusType> Mapping<V, D> createFocusMapping(final MappingFactory mappingFactory, final LensContext<F> context, final MappingType mappingType, ObjectType originObject, ObjectDeltaObject<F> focusOdo, PrismObject<T> defaultTargetObject, AssignmentPathVariables assignmentPathVariables, Integer iteration, String iterationToken, PrismObject<SystemConfigurationType> configuration, XMLGregorianCalendar now, String contextDesc, final Task task, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
    if (!Mapping.isApplicableToChannel(mappingType, context.getChannel())) {
        LOGGER.trace("Mapping {} not applicable to channel {}, skipping.", mappingType, context.getChannel());
        return null;
    }
    StringPolicyResolver stringPolicyResolver = new StringPolicyResolver() {

        private ItemPath outputPath;

        private ItemDefinition outputDefinition;

        @Override
        public void setOutputPath(ItemPath outputPath) {
            this.outputPath = outputPath;
        }

        @Override
        public void setOutputDefinition(ItemDefinition outputDefinition) {
            this.outputDefinition = outputDefinition;
        }

        @Override
        public StringPolicyType resolve() {
            // TODO need to switch to ObjectValuePolicyEvaluator
            if (outputDefinition.getName().equals(PasswordType.F_VALUE)) {
                ValuePolicyType passwordPolicy = credentialsProcessor.determinePasswordPolicy(context.getFocusContext(), task, result);
                if (passwordPolicy == null) {
                    return null;
                }
                return passwordPolicy.getStringPolicy();
            }
            if (mappingType.getExpression() != null) {
                List<JAXBElement<?>> evaluators = mappingType.getExpression().getExpressionEvaluator();
                if (evaluators != null) {
                    for (JAXBElement jaxbEvaluator : evaluators) {
                        Object object = jaxbEvaluator.getValue();
                        if (object instanceof GenerateExpressionEvaluatorType && ((GenerateExpressionEvaluatorType) object).getValuePolicyRef() != null) {
                            ObjectReferenceType ref = ((GenerateExpressionEvaluatorType) object).getValuePolicyRef();
                            try {
                                ValuePolicyType valuePolicyType = mappingFactory.getObjectResolver().resolve(ref, ValuePolicyType.class, null, "resolving value policy for generate attribute " + outputDefinition.getName() + " value", task, new OperationResult("Resolving value policy"));
                                if (valuePolicyType != null) {
                                    return valuePolicyType.getStringPolicy();
                                }
                            } catch (CommonException ex) {
                                throw new SystemException(ex.getMessage(), ex);
                            }
                        }
                    }
                }
            }
            return null;
        }
    };
    ExpressionVariables variables = new ExpressionVariables();
    FOCUS_VARIABLE_NAMES.forEach(name -> variables.addVariableDefinition(name, focusOdo));
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION, iteration);
    variables.addVariableDefinition(ExpressionConstants.VAR_ITERATION_TOKEN, iterationToken);
    variables.addVariableDefinition(ExpressionConstants.VAR_CONFIGURATION, configuration);
    Collection<V> targetValues = computeTargetValues(mappingType.getTarget(), defaultTargetObject, variables, mappingFactory.getObjectResolver(), contextDesc, task, result);
    Mapping.Builder<V, D> mappingBuilder = mappingFactory.<V, D>createMappingBuilder(mappingType, contextDesc).sourceContext(focusOdo).targetContext(defaultTargetObject.getDefinition()).variables(variables).originalTargetValues(targetValues).originType(OriginType.USER_POLICY).originObject(originObject).stringPolicyResolver(stringPolicyResolver).rootNode(focusOdo).now(now);
    mappingBuilder = LensUtil.addAssignmentPathVariables(mappingBuilder, assignmentPathVariables);
    Mapping<V, D> mapping = mappingBuilder.build();
    ItemPath itemPath = mapping.getOutputPath();
    if (itemPath == null) {
        // no output element, i.e. this is a "validation mapping"
        return mapping;
    }
    if (defaultTargetObject != null) {
        Item<V, D> existingTargetItem = (Item<V, D>) defaultTargetObject.findItem(itemPath);
        if (existingTargetItem != null && !existingTargetItem.isEmpty() && mapping.getStrength() == MappingStrengthType.WEAK) {
            LOGGER.trace("Mapping {} is weak and target already has a value {}, skipping.", mapping, existingTargetItem);
            return null;
        }
    }
    return mapping;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ValuePolicyType(com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) StringPolicyResolver(com.evolveum.midpoint.repo.common.expression.StringPolicyResolver) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Mapping(com.evolveum.midpoint.model.common.mapping.Mapping) JAXBElement(javax.xml.bind.JAXBElement) Item(com.evolveum.midpoint.prism.Item) ItemDeltaItem(com.evolveum.midpoint.repo.common.expression.ItemDeltaItem) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) SystemException(com.evolveum.midpoint.util.exception.SystemException) ObjectDeltaObject(com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject) PrismObject(com.evolveum.midpoint.prism.PrismObject) GenerateExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.GenerateExpressionEvaluatorType) CommonException(com.evolveum.midpoint.util.exception.CommonException) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

GenerateExpressionEvaluatorType (com.evolveum.midpoint.xml.ns._public.common.common_3.GenerateExpressionEvaluatorType)3 ItemDefinition (com.evolveum.midpoint.prism.ItemDefinition)2 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)2 ObjectDeltaObject (com.evolveum.midpoint.repo.common.expression.ObjectDeltaObject)2 StringPolicyResolver (com.evolveum.midpoint.repo.common.expression.StringPolicyResolver)2 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)2 ObjectReferenceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType)2 ValuePolicyType (com.evolveum.midpoint.xml.ns._public.common.common_3.ValuePolicyType)2 JAXBElement (javax.xml.bind.JAXBElement)2 Mapping (com.evolveum.midpoint.model.common.mapping.Mapping)1 Item (com.evolveum.midpoint.prism.Item)1 PrismObject (com.evolveum.midpoint.prism.PrismObject)1 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)1 ItemDeltaItem (com.evolveum.midpoint.repo.common.expression.ItemDeltaItem)1 CommonException (com.evolveum.midpoint.util.exception.CommonException)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1