Search in sources :

Example 1 with SearchExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType in project midpoint by Evolveum.

the class SearchEvaluator method evaluate.

public <T extends ObjectType> PipelineData evaluate(SearchExpressionType searchExpression, PipelineData input, ExecutionContext context, OperationResult globalResult) throws ScriptExecutionException, SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, SecurityViolationException, ExpressionEvaluationException {
    Validate.notNull(searchExpression.getType());
    ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
    List<PipelineItem> data = input.getData();
    if (data.isEmpty()) {
        // TODO fix this brutal hack (with dummyValue)
        PrismContainerValue<?> dummyValue = prismContext.itemFactory().createContainerValue();
        PipelineItem dummyItem = new PipelineItem(dummyValue, PipelineData.newOperationResult(), context.getInitialVariables());
        data = Collections.singletonList(dummyItem);
    }
    final PipelineData outputData = PipelineData.createEmpty();
    final MutableBoolean atLeastOne = new MutableBoolean(false);
    for (PipelineItem item : data) {
        // TODO variables from current item
        // TODO operation result handling (global vs local)
        boolean noFetch = expressionHelper.getArgumentAsBoolean(searchExpression.getParameter(), PARAM_NO_FETCH, input, context, false, "search", globalResult);
        Class<T> objectClass = ObjectTypes.getObjectTypeFromTypeQName(searchExpression.getType()).getClassDefinition();
        ObjectQuery unresolvedObjectQuery = null;
        if (searchExpression.getQuery() != null) {
            try {
                unresolvedObjectQuery = context.getQueryConverter().createObjectQuery(objectClass, searchExpression.getQuery());
            } catch (SchemaException e) {
                throw new ScriptExecutionException("Couldn't parse object query. Reason: " + e.getMessage(), e);
            }
        } else if (searchExpression.getSearchFilter() != null) {
            unresolvedObjectQuery = prismContext.queryFactory().createQuery();
            try {
                ObjectFilter filter = prismContext.getQueryConverter().parseFilter(searchExpression.getSearchFilter(), objectClass);
                unresolvedObjectQuery.setFilter(filter);
            } catch (SchemaException e) {
                throw new ScriptExecutionException("Couldn't parse object query. Reason: " + e.getMessage(), e);
            }
        }
        ObjectQuery objectQuery;
        if (unresolvedObjectQuery != null) {
            VariablesMap variables = new VariablesMap();
            // noinspection unchecked
            item.getVariables().forEach((name, value) -> variables.put(name, cloneIfNecessary(name, value)));
            try {
                objectQuery = ExpressionUtil.evaluateQueryExpressions(unresolvedObjectQuery, variables, expressionProfile, expressionFactory, prismContext, "bulk action query", context.getTask(), globalResult);
            } catch (SchemaException | ObjectNotFoundException | ExpressionEvaluationException | CommunicationException | ConfigurationException | SecurityViolationException e) {
                // TODO continue on any error?
                throw new ScriptExecutionException("Couldn't evaluate expressions in object query: " + e.getMessage(), e);
            }
        } else {
            objectQuery = null;
        }
        final String variableName = searchExpression.getVariable();
        ResultHandler<T> handler = (object, parentResult) -> {
            context.checkTaskStop();
            atLeastOne.setValue(true);
            if (searchExpression.getScriptingExpression() != null) {
                if (variableName != null) {
                // TODO
                }
                JAXBElement<?> childExpression = searchExpression.getScriptingExpression();
                try {
                    PipelineData expressionResult = scriptingExpressionEvaluator.evaluateExpression((ScriptingExpressionType) childExpression.getValue(), PipelineData.create(object.getValue(), item.getVariables()), context, globalResult);
                    if (!BooleanUtils.isFalse(searchExpression.isAggregateOutput())) {
                        outputData.addAllFrom(expressionResult);
                    }
                    globalResult.setSummarizeSuccesses(true);
                    globalResult.summarize();
                } catch (ScriptExecutionException | SchemaException | ConfigurationException | ObjectNotFoundException | CommunicationException | SecurityViolationException | ExpressionEvaluationException e) {
                    // todo think about this
                    if (context.isContinueOnAnyError()) {
                        LoggingUtils.logUnexpectedException(LOGGER, "Exception when evaluating item from search result list.", e);
                    } else {
                        throw new SystemException(e);
                    }
                }
            } else {
                outputData.addValue(object.getValue(), item.getVariables());
            }
            return true;
        };
        try {
            Collection<SelectorOptions<GetOperationOptions>> options = operationsHelper.createGetOptions(searchExpression.getOptions(), noFetch);
            modelService.searchObjectsIterative(objectClass, objectQuery, handler, options, context.getTask(), globalResult);
        } catch (SchemaException | ObjectNotFoundException | SecurityViolationException | CommunicationException | ConfigurationException | ExpressionEvaluationException e) {
            // TODO continue on any error?
            throw new ScriptExecutionException("Couldn't execute searchObjects operation: " + e.getMessage(), e);
        }
    }
    if (atLeastOne.isFalse()) {
        // temporary hack, this will be configurable
        context.println("Warning: no matching object found");
    }
    return outputData;
}
Also used : ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) OperationsHelper(com.evolveum.midpoint.model.impl.scripting.helpers.OperationsHelper) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Autowired(org.springframework.beans.factory.annotation.Autowired) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Trace(com.evolveum.midpoint.util.logging.Trace) com.evolveum.midpoint.util.exception(com.evolveum.midpoint.util.exception) ExpressionHelper(com.evolveum.midpoint.model.impl.scripting.helpers.ExpressionHelper) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) MiscSchemaUtil(com.evolveum.midpoint.schema.util.MiscSchemaUtil) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) Collection(java.util.Collection) JAXBElement(javax.xml.bind.JAXBElement) ExecutionContext(com.evolveum.midpoint.model.impl.scripting.ExecutionContext) LoggingUtils(com.evolveum.midpoint.util.logging.LoggingUtils) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) List(java.util.List) Component(org.springframework.stereotype.Component) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) PrismContainerValue(com.evolveum.midpoint.prism.PrismContainerValue) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) VariablesUtil.cloneIfNecessary(com.evolveum.midpoint.model.impl.scripting.VariablesUtil.cloneIfNecessary) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) SearchExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType) Collections(java.util.Collections) Validate(org.apache.commons.lang.Validate) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) PipelineData(com.evolveum.midpoint.model.impl.scripting.PipelineData) ScriptingExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ScriptingExpressionType) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) MutableBoolean(org.apache.commons.lang.mutable.MutableBoolean) ExpressionProfile(com.evolveum.midpoint.schema.expression.ExpressionProfile) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) JAXBElement(javax.xml.bind.JAXBElement) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) PipelineItem(com.evolveum.midpoint.model.api.PipelineItem) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions)

Example 2 with SearchExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType in project midpoint by Evolveum.

the class TestParseScriptingExpression method assertPrismPropertyValueLocal.

@SuppressWarnings("unchecked")
@Override
protected void assertPrismPropertyValueLocal(PrismPropertyValue<ExpressionPipelineType> value) {
    ExpressionPipelineType pipe = value.getValue();
    JAXBElement<ExpressionSequenceType> sequenceJaxb1 = (JAXBElement<ExpressionSequenceType>) pipe.getScriptingExpression().get(0);
    assertEquals("Wrong element name (1)", SchemaConstants.S_SEQUENCE, sequenceJaxb1.getName());
    assertEquals("Wrong element type (1)", ExpressionSequenceType.class, sequenceJaxb1.getValue().getClass());
    JAXBElement<SearchExpressionType> searchJaxb1_1 = (JAXBElement<SearchExpressionType>) sequenceJaxb1.getValue().getScriptingExpression().get(0);
    assertEquals("Wrong first element name", SchemaConstants.S_SEARCH, searchJaxb1_1.getName());
    assertEquals("Wrong element type (1.1)", SearchExpressionType.class, searchJaxb1_1.getValue().getClass());
    assertEquals(new QName("RoleType"), searchJaxb1_1.getValue().getType());
    assertNotNull(searchJaxb1_1.getValue().getSearchFilter());
    JAXBElement<ActionExpressionType> actionJaxb1_2 = (JAXBElement<ActionExpressionType>) sequenceJaxb1.getValue().getScriptingExpression().get(1);
    assertEquals("Wrong element type (1.2)", ActionExpressionType.class, actionJaxb1_2.getValue().getClass());
    assertEquals("log", actionJaxb1_2.getValue().getType());
    JAXBElement<ExpressionSequenceType> sequenceJaxb2 = (JAXBElement<ExpressionSequenceType>) pipe.getScriptingExpression().get(1);
    assertEquals("Wrong second element name", SchemaConstants.S_SEQUENCE, sequenceJaxb2.getName());
    assertEquals("Wrong element type (2)", ExpressionSequenceType.class, sequenceJaxb2.getValue().getClass());
    JAXBElement<ActionExpressionType> actionJaxb2_1 = (JAXBElement<ActionExpressionType>) sequenceJaxb2.getValue().getScriptingExpression().get(0);
    JAXBElement<ActionExpressionType> actionJaxb2_2 = (JAXBElement<ActionExpressionType>) sequenceJaxb2.getValue().getScriptingExpression().get(1);
    JAXBElement<SearchExpressionType> searchJaxb2_3 = (JAXBElement<SearchExpressionType>) sequenceJaxb2.getValue().getScriptingExpression().get(2);
    assertEquals("Wrong element name (2.1)", SchemaConstants.S_ACTION, actionJaxb2_1.getName());
    assertEquals("Wrong element type (2.1)", ActionExpressionType.class, actionJaxb2_1.getValue().getClass());
    assertEquals("Wrong element name (2.2)", SchemaConstants.S_ACTION, actionJaxb2_2.getName());
    assertEquals("Wrong element type (2.2)", ActionExpressionType.class, actionJaxb2_2.getValue().getClass());
    assertEquals("Wrong element name (2.3)", SchemaConstants.S_SEARCH, searchJaxb2_3.getName());
    assertEquals("Wrong element type (2.3)", SearchExpressionType.class, searchJaxb2_3.getValue().getClass());
}
Also used : ExpressionPipelineType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionPipelineType) ExpressionSequenceType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionSequenceType) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) SearchExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType) ActionExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType)

Example 3 with SearchExpressionType

use of com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType in project midpoint by Evolveum.

the class TestParseScriptingExpressionXsiType method assertPrismPropertyValueLocal.

@SuppressWarnings("unchecked")
@Override
protected void assertPrismPropertyValueLocal(PrismPropertyValue<ExpressionPipelineType> value) throws SchemaException {
    ExpressionPipelineType pipe = value.getValue();
    JAXBElement<ExpressionSequenceType> sequenceJaxb1 = (JAXBElement<ExpressionSequenceType>) pipe.getScriptingExpression().get(0);
    assertEquals("Wrong element name (1)", SchemaConstants.S_SEQUENCE, sequenceJaxb1.getName());
    assertEquals("Wrong element type (1)", ExpressionSequenceType.class, sequenceJaxb1.getValue().getClass());
    JAXBElement<SearchExpressionType> searchJaxb1_1 = (JAXBElement<SearchExpressionType>) sequenceJaxb1.getValue().getScriptingExpression().get(0);
    assertEquals("Wrong first element name", SchemaConstants.S_SEARCH, searchJaxb1_1.getName());
    assertEquals("Wrong element type (1.1)", SearchExpressionType.class, searchJaxb1_1.getValue().getClass());
    assertEquals(new QName("RoleType"), searchJaxb1_1.getValue().getType());
    assertNotNull(searchJaxb1_1.getValue().getSearchFilter());
    JAXBElement<ActionExpressionType> actionJaxb1_2 = (JAXBElement<ActionExpressionType>) sequenceJaxb1.getValue().getScriptingExpression().get(1);
    assertEquals("Wrong element type (1.2)", ActionExpressionType.class, actionJaxb1_2.getValue().getClass());
    assertEquals("log", actionJaxb1_2.getValue().getType());
    JAXBElement<ExpressionSequenceType> sequenceJaxb2 = (JAXBElement<ExpressionSequenceType>) pipe.getScriptingExpression().get(1);
    assertEquals("Wrong second element name", SchemaConstants.S_SEQUENCE, sequenceJaxb2.getName());
    assertEquals("Wrong element type (2)", ExpressionSequenceType.class, sequenceJaxb2.getValue().getClass());
    JAXBElement<ActionExpressionType> actionJaxb2_1 = (JAXBElement<ActionExpressionType>) sequenceJaxb2.getValue().getScriptingExpression().get(0);
    JAXBElement<ActionExpressionType> actionJaxb2_2 = (JAXBElement<ActionExpressionType>) sequenceJaxb2.getValue().getScriptingExpression().get(1);
    JAXBElement<SearchExpressionType> searchJaxb2_3 = (JAXBElement<SearchExpressionType>) sequenceJaxb2.getValue().getScriptingExpression().get(2);
    assertEquals("Wrong element name (2.1)", SchemaConstants.S_ACTION, actionJaxb2_1.getName());
    assertEquals("Wrong element type (2.1)", ActionExpressionType.class, actionJaxb2_1.getValue().getClass());
    assertEquals("Wrong element name (2.2)", SchemaConstants.S_ACTION, actionJaxb2_2.getName());
    assertEquals("Wrong element type (2.2)", ActionExpressionType.class, actionJaxb2_2.getValue().getClass());
    assertEquals("Wrong element name (2.3)", SchemaConstants.S_SEARCH, searchJaxb2_3.getName());
    assertEquals("Wrong element type (2.3)", SearchExpressionType.class, searchJaxb2_3.getValue().getClass());
}
Also used : ExpressionPipelineType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionPipelineType) ExpressionSequenceType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionSequenceType) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) SearchExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType) ActionExpressionType(com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType)

Aggregations

SearchExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.SearchExpressionType)3 JAXBElement (javax.xml.bind.JAXBElement)3 ActionExpressionType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ActionExpressionType)2 ExpressionPipelineType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionPipelineType)2 ExpressionSequenceType (com.evolveum.midpoint.xml.ns._public.model.scripting_3.ExpressionSequenceType)2 QName (javax.xml.namespace.QName)2 PipelineItem (com.evolveum.midpoint.model.api.PipelineItem)1 ExecutionContext (com.evolveum.midpoint.model.impl.scripting.ExecutionContext)1 PipelineData (com.evolveum.midpoint.model.impl.scripting.PipelineData)1 VariablesUtil.cloneIfNecessary (com.evolveum.midpoint.model.impl.scripting.VariablesUtil.cloneIfNecessary)1 ExpressionHelper (com.evolveum.midpoint.model.impl.scripting.helpers.ExpressionHelper)1 OperationsHelper (com.evolveum.midpoint.model.impl.scripting.helpers.OperationsHelper)1 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)1 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)1 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)1 ExpressionFactory (com.evolveum.midpoint.repo.common.expression.ExpressionFactory)1 ExpressionUtil (com.evolveum.midpoint.repo.common.expression.ExpressionUtil)1 GetOperationOptions (com.evolveum.midpoint.schema.GetOperationOptions)1 ResultHandler (com.evolveum.midpoint.schema.ResultHandler)1 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)1