use of com.evolveum.midpoint.xml.ns._public.common.common_3.ProvisioningScriptArgumentType in project midpoint by Evolveum.
the class CommandLineScriptExecutor method expandMacros.
private String expandMacros(CommandLineScriptType scriptType, VariablesMap variables, String shortDesc, Task task, OperationResult result) throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException {
String code = scriptType.getCode();
for (ProvisioningScriptArgumentType macroDef : scriptType.getMacro()) {
String macroName = macroDef.getName();
QName macroQName = new QName(SchemaConstants.NS_C, macroName);
String expressionOutput = "";
MutablePrismPropertyDefinition<String> outputDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_STRING);
outputDefinition.setMaxOccurs(1);
Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(macroDef, outputDefinition, MiscSchemaUtil.getExpressionProfile(), shortDesc, task, result);
Collection<Source<?, ?>> sources = new ArrayList<>(1);
ExpressionEvaluationContext context = new ExpressionEvaluationContext(sources, variables, shortDesc, task);
TypedValue defaultObjectValAndDef = variables.get(macroName);
if (defaultObjectValAndDef != null) {
Object defaultObjectVal = defaultObjectValAndDef.getValue();
Item sourceItem;
if (defaultObjectVal instanceof Item) {
sourceItem = (Item) defaultObjectVal;
} else if (defaultObjectVal instanceof String) {
MutablePrismPropertyDefinition<String> sourceDefinition = prismContext.definitionFactory().createPropertyDefinition(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_STRING);
sourceDefinition.setMaxOccurs(1);
PrismProperty<String> sourceProperty = sourceDefinition.instantiate();
sourceProperty.setRealValue(defaultObjectVal.toString());
sourceItem = sourceProperty;
} else {
sourceItem = null;
}
Source<?, ?> defaultSource = new Source<>(sourceItem, null, sourceItem, macroQName, defaultObjectValAndDef.getDefinition());
context.setDefaultSource(defaultSource);
sources.add(defaultSource);
}
PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = expression.evaluate(context, result);
LOGGER.trace("Result of the expression evaluation: {}", outputTriple);
if (outputTriple != null) {
Collection<PrismPropertyValue<String>> nonNegativeValues = outputTriple.getNonNegativeValues();
if (!nonNegativeValues.isEmpty()) {
Collection<String> expressionOutputs = PrismValueCollectionsUtil.getRealValuesOfCollection((Collection) nonNegativeValues);
if (!expressionOutputs.isEmpty()) {
expressionOutput = StringUtils.join(expressionOutputs, ",");
}
}
}
code = replaceMacro(code, macroDef.getName(), expressionOutput);
}
return code;
}
Aggregations