Search in sources :

Example 71 with VariablesMap

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

the class WebComponentUtil method getAllowedValues.

public static List<DisplayableValue<?>> getAllowedValues(SearchFilterParameterType parameter, PageBase pageBase) {
    List<DisplayableValue<?>> allowedValues = new ArrayList<>();
    if (parameter == null || parameter.getAllowedValuesExpression() == null) {
        return allowedValues;
    }
    Task task = pageBase.createSimpleTask("evaluate expression for allowed values");
    ExpressionType expression = parameter.getAllowedValuesExpression();
    Object value = null;
    try {
        value = ExpressionUtil.evaluateExpressionNative(null, new VariablesMap(), null, expression, MiscSchemaUtil.getExpressionProfile(), pageBase.getExpressionFactory(), "evaluate expression for allowed values", task, task.getResult());
    } catch (Exception e) {
        LOGGER.error("Couldn't execute expression " + expression, e);
        pageBase.error(pageBase.createStringResource("FilterSearchItem.message.error.evaluateAllowedValuesExpression", expression).getString());
        return allowedValues;
    }
    if (value instanceof PrismPropertyValue) {
        value = ((PrismPropertyValue) value).getRealValue();
    }
    if (!(value instanceof Set)) {
        LOGGER.error("Exception return unexpected type, expected Set<PPV<DisplayableValue>>, but was " + (value == null ? null : value.getClass()));
        pageBase.error(pageBase.createStringResource("FilterSearchItem.message.error.wrongType", expression).getString());
        return allowedValues;
    }
    if (!((Set<?>) value).isEmpty()) {
        if (!(((Set<?>) value).iterator().next() instanceof PrismPropertyValue) || !(((PrismPropertyValue) (((Set<?>) value).iterator().next())).getValue() instanceof DisplayableValue)) {
            LOGGER.error("Exception return unexpected type, expected Set<PPV<DisplayableValue>>, but was " + (value == null ? null : value.getClass()));
            pageBase.error(pageBase.createStringResource("FilterSearchItem.message.error.wrongType", expression).getString());
            return allowedValues;
        }
        return (List<DisplayableValue<?>>) ((Set<PrismPropertyValue<?>>) value).stream().map(PrismPropertyValue::getValue).collect(Collectors.toList());
    }
    return allowedValues;
}
Also used : Task(com.evolveum.midpoint.task.api.Task) PageTask(com.evolveum.midpoint.web.page.admin.server.PageTask) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException)

Example 72 with VariablesMap

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

the class Search method createObjectQuerySimple.

private ObjectQuery createObjectQuerySimple(VariablesMap defaultVariables, PageBase pageBase) {
    List<SearchItem> searchItems = getItems();
    if (searchItems.isEmpty()) {
        return null;
    }
    List<ObjectFilter> conditions = new ArrayList<>();
    for (PropertySearchItem item : getPropertyItems()) {
        if (item.isEnabled() && item.isApplyFilter()) {
            ObjectFilter filter = createFilterForSearchItem(item, pageBase.getPrismContext());
            if (filter != null) {
                conditions.add(filter);
            }
        }
    }
    VariablesMap variables = getFilterVariables(defaultVariables, pageBase);
    for (FilterSearchItem item : getFilterItems()) {
        if (item.isEnabled() && item.isApplyFilter()) {
            SearchFilterType filter = item.getPredefinedFilter().getFilter();
            if (filter == null && item.getPredefinedFilter().getFilterExpression() != null) {
                ItemDefinition outputDefinition = pageBase.getPrismContext().definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, SearchFilterType.COMPLEX_TYPE);
                Task task = pageBase.createSimpleTask("evaluate filter expression");
                try {
                    PrismValue filterValue = ExpressionUtil.evaluateExpression(variables, outputDefinition, item.getPredefinedFilter().getFilterExpression(), MiscSchemaUtil.getExpressionProfile(), pageBase.getExpressionFactory(), "", task, task.getResult());
                    if (filterValue == null || filterValue.getRealValue() == null) {
                        LOGGER.error("FilterExpression return null, ", item.getPredefinedFilter().getFilterExpression());
                    }
                    filter = filterValue.getRealValue();
                } catch (Exception e) {
                    LOGGER.error("Unable to evaluate filter expression, {} ", item.getPredefinedFilter().getFilterExpression());
                }
            }
            if (filter != null) {
                try {
                    ObjectFilter convertedFilter = pageBase.getQueryConverter().parseFilter(filter, getTypeClass());
                    convertedFilter = WebComponentUtil.evaluateExpressionsInFilter(convertedFilter, variables, new OperationResult("evaluated filter"), pageBase);
                    if (convertedFilter != null) {
                        conditions.add(convertedFilter);
                    }
                } catch (SchemaException e) {
                    LOGGER.error("Unable to parse filter {}, {} ", filter, e);
                }
            }
        }
    }
    ObjectQuery query;
    if (getTypeClass() != null) {
        query = pageBase.getPrismContext().queryFor(getTypeClass()).build();
    } else {
        query = pageBase.getPrismContext().queryFactory().createQuery();
    }
    switch(conditions.size()) {
        case 0:
            query = null;
            break;
        default:
            for (ObjectFilter filter : conditions) {
                query.addFilter(filter);
            }
    }
    return query;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) SearchFilterType(com.evolveum.prism.xml.ns._public.query_3.SearchFilterType) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) AbstractRoleCompositedSearchItem(com.evolveum.midpoint.web.page.admin.roles.AbstractRoleCompositedSearchItem) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 73 with VariablesMap

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

the class Search method getFilterVariables.

public VariablesMap getFilterVariables(VariablesMap defaultVariables, PageBase pageBase) {
    VariablesMap variables = defaultVariables == null ? new VariablesMap() : defaultVariables;
    for (FilterSearchItem item : getFilterItems()) {
        SearchFilterParameterType functionParameter = item.getPredefinedFilter().getParameter();
        if (functionParameter != null && functionParameter.getType() != null) {
            TypedValue value;
            if (item.getInput() == null || item.getInput().getValue() == null) {
                Class<?> inputClass = pageBase.getPrismContext().getSchemaRegistry().determineClassForType(functionParameter.getType());
                value = new TypedValue(null, inputClass);
            } else {
                value = new TypedValue(item.getInput().getValue(), item.getInput().getValue().getClass());
            }
            variables.put(functionParameter.getName(), value);
        }
    }
    return variables;
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) TypedValue(com.evolveum.midpoint.schema.expression.TypedValue)

Example 74 with VariablesMap

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

the class ReportObjectsListPanel method getReportVariables.

public VariablesMap getReportVariables() {
    VariablesMap variablesMap = getSearchModel().getObject().getFilterVariables(null, getPageBase());
    processReferenceVariables(variablesMap);
    return variablesMap;
}
Also used : VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap)

Example 75 with VariablesMap

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

the class TestExpression method test154ScriptGroovySystemDeny.

@Test
public void test154ScriptGroovySystemDeny() throws Exception {
    // GIVEN
    OperationResult result = createOperationResult();
    rememberScriptExecutionCount();
    ExpressionType expressionType = parseExpression(EXPRESSION_SCRIPT_GROOVY_SYSTEM_DENY_FILE);
    Collection<Source<?, ?>> sources = prepareStringSources();
    VariablesMap variables = prepareBasicVariables();
    ExpressionEvaluationContext expressionContext = new ExpressionEvaluationContext(sources, variables, getTestNameShort(), null);
    // WHEN
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = evaluatePropertyExpression(expressionType, PrimitiveType.STRING, expressionContext, result);
    // THEN
    assertOutputTriple(outputTriple).assertEmptyMinus().assertEmptyPlus().zeroSet().assertSinglePropertyValue(GROOVY_SCRIPT_OUTPUT_SUCCESS);
    assertScriptExecutionIncrement(1);
}
Also used : ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) Source(com.evolveum.midpoint.repo.common.expression.Source) Test(org.testng.annotations.Test) AbstractModelCommonTest(com.evolveum.midpoint.model.common.AbstractModelCommonTest)

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