use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class PreInboundsProcessing method getVariablesMap.
// TODO !!!!!!!!!!!!!!
public VariablesMap getVariablesMap() throws SchemaException {
VariablesMap variables = ModelImplUtils.getDefaultVariablesMap(syncCtx.getPreFocus(), syncCtx.getShadowedResourceObject().asObjectable(), asObjectable(syncCtx.getResource()), asObjectable(syncCtx.getSystemConfiguration()));
variables.put(ExpressionConstants.VAR_SYNCHRONIZATION_CONTEXT, syncCtx, SynchronizationContext.class);
return variables;
}
use of com.evolveum.midpoint.schema.expression.VariablesMap 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;
}
use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class SynchronizationExpressionsEvaluator method evaluateConfirmationExpression.
private <F extends FocusType> boolean evaluateConfirmationExpression(Class<F> focusType, F user, ShadowType shadow, ResourceType resource, SystemConfigurationType configuration, ExpressionType expressionType, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException {
Validate.notNull(user, "User must not be null.");
Validate.notNull(shadow, "Resource object shadow must not be null.");
Validate.notNull(expressionType, "Expression must not be null.");
Validate.notNull(result, "Operation result must not be null.");
VariablesMap variables = ModelImplUtils.getDefaultVariablesMap(user, shadow, resource, configuration, prismContext);
String shortDesc = "confirmation expression for " + resource.asPrismObject();
PrismPropertyDefinition<Boolean> outputDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN);
Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, MiscSchemaUtil.getExpressionProfile(), shortDesc, task, result);
ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task);
PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
Collection<PrismPropertyValue<Boolean>> nonNegativeValues = outputTriple.getNonNegativeValues();
if (nonNegativeValues.isEmpty()) {
throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
if (nonNegativeValues.size() > 1) {
throw new ExpressionEvaluationException("Expression returned more than one value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
PrismPropertyValue<Boolean> resultpval = nonNegativeValues.iterator().next();
if (resultpval == null) {
throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
Boolean resultVal = resultpval.getValue();
if (resultVal == null) {
throw new ExpressionEvaluationException("Expression returned no value (" + nonNegativeValues.size() + ") in " + shortDesc);
}
return resultVal;
}
use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class SynchronizationServiceUtils method evaluateSynchronizationPolicyCondition.
private static <F extends FocusType> boolean evaluateSynchronizationPolicyCondition(ObjectSynchronizationType synchronizationPolicy, SynchronizationContext<F> syncCtx, ExpressionFactory expressionFactory, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
if (synchronizationPolicy.getCondition() == null) {
return true;
}
ExpressionType conditionExpressionBean = synchronizationPolicy.getCondition();
String desc = "condition in object synchronization " + synchronizationPolicy.getName();
VariablesMap variables = ModelImplUtils.getDefaultVariablesMap(null, syncCtx.getShadowedResourceObject(), null, syncCtx.getResource(), syncCtx.getSystemConfiguration(), null, syncCtx.getPrismContext());
try {
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(new ExpressionEnvironment<>(syncCtx.getTask(), result));
return ExpressionUtil.evaluateConditionDefaultTrue(variables, conditionExpressionBean, syncCtx.getExpressionProfile(), expressionFactory, desc, syncCtx.getTask(), result);
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
}
use of com.evolveum.midpoint.schema.expression.VariablesMap in project midpoint by Evolveum.
the class TestFilterExpression method evaluateExpressionAssertFilter.
private ObjectFilter evaluateExpressionAssertFilter(String filename, String input, Class<? extends ObjectFilter> expectedType, Task task, OperationResult result) throws SchemaException, IOException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
PrismContext prismContext = PrismTestUtil.getPrismContext();
SearchFilterType filterType = PrismTestUtil.parseAtomicValue(new File(TEST_DIR, filename), SearchFilterType.COMPLEX_TYPE);
ObjectFilter filter = prismContext.getQueryConverter().createObjectFilter(UserType.class, filterType);
PrismPropertyValue<String> pval = null;
if (input != null) {
pval = prismContext.itemFactory().createPropertyValue(input);
}
VariablesMap variables = createVariables(ExpressionConstants.VAR_INPUT, pval, PrimitiveType.STRING);
// WHEN
ObjectFilter evaluatedFilter = ExpressionUtil.evaluateFilterExpressions(filter, variables, MiscSchemaUtil.getExpressionProfile(), expressionFactory, prismContext, "evaluating filter with null value not allowed", task, result);
// THEN
displayDumpable("Evaluated filter", evaluatedFilter);
AssertJUnit.assertTrue("Expression should be evaluated to " + expectedType + ", but was " + evaluatedFilter, expectedType.isAssignableFrom(evaluatedFilter.getClass()));
return evaluatedFilter;
}
Aggregations