Search in sources :

Example 61 with VariablesMap

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

the class OperationRequestTransformer method transformOperationRequested.

@NotNull
public AsyncProvisioningRequest transformOperationRequested(@NotNull OperationRequested operationRequested, Task task, OperationResult result) {
    try {
        PredefinedOperationRequestTransformationType predefinedTransformation = connectorInstance.getPredefinedTransformation();
        if (predefinedTransformation != null) {
            return transformerHelper.applyPredefinedTransformation(operationRequested, predefinedTransformation);
        }
        ExpressionType transformExpression = connectorInstance.getTransformExpression();
        if (transformExpression != null) {
            VariablesMap variables = new VariablesMap();
            variables.put(VAR_OPERATION_REQUESTED, operationRequested, operationRequested.getClass());
            variables.put(VAR_TRANSFORMER_HELPER, transformerHelper, TransformerHelper.class);
            variables.put(VAR_REQUEST_FORMATTER, transformerHelper.jsonRequestFormatter(operationRequested), JsonRequestFormatter.class);
            List<?> list = connectorInstance.getUcfExpressionEvaluator().evaluate(transformExpression, variables, SchemaConstantsGenerated.C_ASYNC_PROVISIONING_REQUEST, "creating asynchronous provisioning request", task, result);
            if (list.isEmpty()) {
                throw new IllegalStateException("Transformational script returned no value");
            }
            if (list.size() > 1) {
                throw new IllegalStateException("Transformational script returned more than single value: " + list);
            }
            Object o = list.get(0);
            if (o == null) {
                // In the future we can call e.g. default request creator here
                throw new IllegalStateException("Transformational script returned no value");
            } else if (o instanceof AsyncProvisioningRequest) {
                return (AsyncProvisioningRequest) o;
            } else if (o instanceof String) {
                return StringAsyncProvisioningRequest.of((String) o);
            } else {
                throw new IllegalStateException("Transformational script should provide an AsyncProvisioningRequest but created " + MiscUtil.getClass(o) + " instead");
            }
        } else {
            return transformerHelper.applyPredefinedTransformation(operationRequested, SIMPLIFIED_JSON);
        }
    } catch (RuntimeException | SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException | IOException e) {
        throw new SystemException("Couldn't evaluate message transformation expression: " + e.getMessage(), e);
    }
}
Also used : IOException(java.io.IOException) PredefinedOperationRequestTransformationType(com.evolveum.midpoint.xml.ns._public.common.common_3.PredefinedOperationRequestTransformationType) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) StringAsyncProvisioningRequest(com.evolveum.midpoint.provisioning.ucf.api.async.StringAsyncProvisioningRequest) AsyncProvisioningRequest(com.evolveum.midpoint.provisioning.ucf.api.async.AsyncProvisioningRequest) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with VariablesMap

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

the class IterativeActivityRun method shouldProcessBucket.

private boolean shouldProcessBucket(OperationResult result) {
    ExpressionType condition = getActivity().getControlFlowDefinition().getBucketProcessingCondition();
    if (condition == null) {
        return true;
    }
    VariablesMap variables = new VariablesMap();
    variables.put(ExpressionConstants.VAR_BUCKET, bucket, WorkBucketType.class);
    try {
        return ExpressionUtil.evaluateConditionDefaultTrue(variables, condition, null, beans.expressionFactory, "bucket condition expression", getRunningTask(), result);
    } catch (CommonException e) {
        throw new SystemException("Couldn't evaluate bucket processing condition: " + e.getMessage(), e);
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 63 with VariablesMap

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

the class ResourceObjectReferenceResolver method resolve.

/**
 * Resolves a {@link ResourceObjectReferenceType}.
 *
 * @param useRawDefinition If true, object class definition is used (instead of object type definition).
 * This is to avoid endless recursion when resolving the base context for object type.
 */
@Nullable
PrismObject<ShadowType> resolve(@NotNull ProvisioningContext ctx, @NotNull ResourceObjectReferenceType resourceObjectReference, boolean useRawDefinition, @NotNull String desc, @NotNull OperationResult result) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    ObjectReferenceType shadowRef = resourceObjectReference.getShadowRef();
    if (shadowRef != null && shadowRef.getOid() != null) {
        if (resourceObjectReference.getResolutionFrequency() == null || resourceObjectReference.getResolutionFrequency() == ResourceObjectReferenceResolutionFrequencyType.ONCE) {
            PrismObject<ShadowType> shadow = repositoryService.getObject(ShadowType.class, shadowRef.getOid(), null, result);
            shadowsFacade.applyDefinition(shadow, ctx.getTask(), result);
            return shadow;
        }
    } else if (resourceObjectReference.getResolutionFrequency() == ResourceObjectReferenceResolutionFrequencyType.NEVER) {
        throw new ObjectNotFoundException("No shadowRef OID in " + desc + " and resolution frequency set to NEVER");
    }
    argCheck(resourceObjectReference.getObjectClass() != null, "No object class name in object reference in %s", desc);
    QName objectClassName = QNameUtil.qualifyIfNeeded(resourceObjectReference.getObjectClass(), MidPointConstants.NS_RI);
    ProvisioningContext subCtx = useRawDefinition ? ctx.spawnForObjectClassWithRawDefinition(objectClassName) : ctx.spawnForObjectClass(objectClassName);
    subCtx.assertDefinition();
    ObjectQuery refQuery = prismContext.getQueryConverter().createObjectQuery(ShadowType.class, resourceObjectReference.getFilter());
    // No variables. At least not now. We expect that mostly constants will be used here.
    VariablesMap variables = new VariablesMap();
    ObjectQuery evaluatedRefQuery = ExpressionUtil.evaluateQueryExpressions(refQuery, variables, MiscSchemaUtil.getExpressionProfile(), expressionFactory, prismContext, desc, ctx.getTask(), result);
    ObjectFilter baseFilter = ObjectQueryUtil.createResourceAndObjectClassFilter(ctx.getResource().getOid(), objectClassName, prismContext);
    ObjectFilter filter = prismContext.queryFactory().createAnd(baseFilter, evaluatedRefQuery.getFilter());
    ObjectQuery query = prismContext.queryFactory().createQuery(filter);
    // TODO: implement "repo" search strategies, don't forget to apply definitions
    Holder<PrismObject<ShadowType>> shadowHolder = new Holder<>();
    ResultHandler<ShadowType> handler = (shadow, objResult) -> {
        if (shadowHolder.getValue() != null) {
            throw new IllegalStateException("More than one search results for " + desc);
        }
        shadowHolder.setValue(shadow);
        return true;
    };
    shadowsFacade.searchObjectsIterative(subCtx, query, null, handler, result);
    return shadowHolder.getValue();
}
Also used : ResourceAttributeDefinition(com.evolveum.midpoint.schema.processor.ResourceAttributeDefinition) Autowired(org.springframework.beans.factory.annotation.Autowired) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ResourceAttribute(com.evolveum.midpoint.schema.processor.ResourceAttribute) ConnectorInstance(com.evolveum.midpoint.provisioning.ucf.api.ConnectorInstance) QNameUtil(com.evolveum.midpoint.util.QNameUtil) MiscSchemaUtil(com.evolveum.midpoint.schema.util.MiscSchemaUtil) ObjectQueryUtil(com.evolveum.midpoint.schema.util.ObjectQueryUtil) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) Holder(com.evolveum.midpoint.util.Holder) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) GenericFrameworkException(com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException) Nullable(org.jetbrains.annotations.Nullable) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ReadCapabilityType(com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.ReadCapabilityType) ShadowsFacade(com.evolveum.midpoint.provisioning.impl.shadows.ShadowsFacade) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) ResourceObjectDefinition(com.evolveum.midpoint.schema.processor.ResourceObjectDefinition) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Trace(com.evolveum.midpoint.util.logging.Trace) PrettyPrinter(com.evolveum.midpoint.util.PrettyPrinter) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ArrayList(java.util.ArrayList) GenericConnectorException(com.evolveum.midpoint.provisioning.api.GenericConnectorException) ProvisioningContext(com.evolveum.midpoint.provisioning.impl.ProvisioningContext) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) PrismContext(com.evolveum.midpoint.prism.PrismContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) Containerable(com.evolveum.midpoint.prism.Containerable) AttributesToReturn(com.evolveum.midpoint.provisioning.ucf.api.AttributesToReturn) PrismObject(com.evolveum.midpoint.prism.PrismObject) ResourceObjectIdentification(com.evolveum.midpoint.schema.processor.ResourceObjectIdentification) Component(org.springframework.stereotype.Component) MidPointConstants(com.evolveum.midpoint.schema.constants.MidPointConstants) ShadowUtil(com.evolveum.midpoint.schema.util.ShadowUtil) MiscUtil.argCheck(com.evolveum.midpoint.util.MiscUtil.argCheck) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ShadowManager(com.evolveum.midpoint.provisioning.impl.shadows.manager.ShadowManager) QName(javax.xml.namespace.QName) Holder(com.evolveum.midpoint.util.Holder) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) ProvisioningContext(com.evolveum.midpoint.provisioning.impl.ProvisioningContext) PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) Nullable(org.jetbrains.annotations.Nullable)

Example 64 with VariablesMap

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

the class UcfExpressionEvaluatorImpl method evaluate.

@NotNull
@Override
public <O> List<O> evaluate(ExpressionType expressionBean, VariablesMap variables, QName outputPropertyName, String ctxDesc, Task task, OperationResult parentResult) throws SchemaException, ObjectNotFoundException, SecurityViolationException, CommunicationException, ConfigurationException, ExpressionEvaluationException {
    OperationResult result = parentResult.subresult(OP_EVALUATE).setMinor().build();
    try {
        Expression<PrismPropertyValue<O>, PrismPropertyDefinition<O>> expression = expressionFactory.makePropertyExpression(expressionBean, outputPropertyName, MiscSchemaUtil.getExpressionProfile(), ctxDesc, task, result);
        VariablesMap exprVariables = new VariablesMap();
        exprVariables.putAll(variables);
        ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, exprVariables, ctxDesc, task);
        PrismValueDeltaSetTriple<PrismPropertyValue<O>> exprResultTriple = expression.evaluate(context, result);
        List<O> list = new ArrayList<>();
        for (PrismPropertyValue<O> pv : exprResultTriple.getZeroSet()) {
            list.add(pv.getRealValue());
        }
        return list;
    } catch (Throwable t) {
        result.recordFatalError(t);
        throw t;
    } finally {
        result.computeStatusIfUnknown();
    }
}
Also used : PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with VariablesMap

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

the class AbstractReport method isRejectedByExpression.

private boolean isRejectedByExpression(@NotNull Containerable record, @NotNull RunningTask task, @NotNull OperationResult result) {
    if (recordFilteringExpression == null) {
        return false;
    }
    VariablesMap variables = new VariablesMap();
    variables.put(ExpressionConstants.VAR_RECORD, record, record.getClass());
    try {
        return !ExpressionUtil.evaluateConditionDefaultTrue(variables, recordFilteringExpression, null, beans.expressionFactory, "record filtering expression", task, result);
    } catch (CommonException e) {
        // Maybe we could propagate the checked exception upwards (later!)
        throw new SystemException("Couldn't evaluate record filtering expression for " + record + ": " + e.getMessage(), e);
    }
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Aggregations

VariablesMap (com.evolveum.midpoint.schema.expression.VariablesMap)166 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)48 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)30 Test (org.testng.annotations.Test)28 Task (com.evolveum.midpoint.task.api.Task)23 NotNull (org.jetbrains.annotations.NotNull)23 QName (javax.xml.namespace.QName)15 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)12 Source (com.evolveum.midpoint.repo.common.expression.Source)12 AbstractModelCommonTest (com.evolveum.midpoint.model.common.AbstractModelCommonTest)11 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)11 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)10 Trace (com.evolveum.midpoint.util.logging.Trace)10 TraceManager (com.evolveum.midpoint.util.logging.TraceManager)10 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)9 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)9 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)9 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)8 ExpressionType (com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType)8 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)7