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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations