use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class ScriptExpressionFactory method initializeCustomFunctionsLibraryCache.
private void initializeCustomFunctionsLibraryCache(ExpressionFactory expressionFactory, OperationResult result) throws ExpressionSyntaxException {
if (repositoryService != null) {
OperationResult subResult = result.createMinorSubresult(ScriptExpressionFactory.class.getName() + ".searchCustomFunctions");
ResultHandler<FunctionLibraryType> functionLibraryHandler = (object, parentResult) -> {
// TODO: determine profile from function library archetype
ExpressionProfile expressionProfile = MiscSchemaUtil.getExpressionProfile();
FunctionLibrary customLibrary = new FunctionLibrary();
customLibrary.setVariableName(object.getName().getOrig());
customLibrary.setGenericFunctions(new CustomFunctions(object.asObjectable(), expressionFactory, expressionProfile));
customLibrary.setNamespace(MidPointConstants.NS_FUNC_CUSTOM);
customFunctionLibraryCache.put(object.getName().getOrig(), customLibrary);
return true;
};
try {
repositoryService.searchObjectsIterative(FunctionLibraryType.class, null, functionLibraryHandler, createReadOnlyCollection(), true, subResult);
subResult.recordSuccessIfUnknown();
} catch (SchemaException | RuntimeException e) {
subResult.recordFatalError("Failed to initialize custom functions", e);
throw new ExpressionSyntaxException("An error occurred during custom libraries initialization. " + e.getMessage(), e);
}
} else {
LOGGER.warn("No repository service set for ScriptExpressionFactory; custom functions will not be loaded. This" + " can occur during low-level testing; never during standard system execution.");
}
}
use of com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary in project midpoint by Evolveum.
the class AbstractScriptEvaluator method prepareScriptVariablesValueMap.
/**
* Returns simple variable map: name -> value.
*/
protected Map<String, Object> prepareScriptVariablesValueMap(ScriptExpressionEvaluationContext context) throws ExpressionSyntaxException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
Map<String, Object> scriptVariableMap = new HashMap<>();
// Functions
if (context.getFunctions() != null) {
for (FunctionLibrary funcLib : context.getFunctions()) {
scriptVariableMap.put(funcLib.getVariableName(), funcLib.getGenericFunctions());
}
}
// Variables
VariablesMap variables = context.getVariables();
if (variables != null) {
for (Entry<String, TypedValue> variableEntry : variables.entrySet()) {
if (variableEntry.getKey() == null) {
// This is the "root" node. We have no use for it in script expressions, just skip it
continue;
}
String variableName = variableEntry.getKey();
ValueVariableModeType valueVariableMode = ObjectUtils.defaultIfNull(context.getExpressionType().getValueVariableMode(), ValueVariableModeType.REAL_VALUE);
// noinspection rawtypes
TypedValue variableTypedValue = ExpressionUtil.convertVariableValue(variableEntry.getValue(), variableName, context.getObjectResolver(), context.getContextDescription(), context.getExpressionType().getObjectVariableMode(), valueVariableMode, prismContext, context.getTask(), context.getResult());
scriptVariableMap.put(variableName, variableTypedValue.getValue());
if (context.getTrace() != null && !variables.isAlias(variableName)) {
ScriptVariableEvaluationTraceType variableTrace = new ScriptVariableEvaluationTraceType(prismContext);
variableTrace.setName(new QName(variableName));
Object clonedValue = cloneIfPossible(variableTypedValue.getValue());
variableTrace.getValue().addAll(TraceUtil.toAnyValueTypeList(clonedValue, prismContext));
variables.getAliases(variableName).forEach(alias -> variableTrace.getAlias().add(new QName(alias)));
context.getTrace().getVariable().add(variableTrace);
}
}
}
putIfMissing(scriptVariableMap, ExpressionConstants.VAR_PRISM_CONTEXT, prismContext);
putIfMissing(scriptVariableMap, ExpressionConstants.VAR_LOCALIZATION_SERVICE, localizationService);
return scriptVariableMap;
}
Aggregations