public static <X> PrismProperty<X> getPropertyStatic(ExpressionType expressionType, PrismPropertyDefinition outputDefinition, String contextDescription, PrismContext prismContext) throws SchemaException { Collection<JAXBElement<?>> expressionEvaluatorElement = expressionType.getExpressionEvaluator(); return (PrismProperty) parseValueElements(expressionEvaluatorElement, outputDefinition, contextDescription); }
/** * Always returns collection, even for single-valued results. */ public static <X> Collection<X> getPropertyStaticRealValues(ExpressionType expressionType, PrismPropertyDefinition outputDefinition, String contextDescription, PrismContext prismContext) throws SchemaException { PrismProperty<X> output = getPropertyStatic(expressionType, outputDefinition, contextDescription, prismContext); return output.getRealValues(); }
private void doRoundtrip(PrismProperty<?> origProperty, ItemDefinition propDef, PrismContext prismContext) throws SchemaException, JAXBException { // WHEN List<JAXBElement<RawType>> valueElements = StaticExpressionUtil.serializeValueElements(origProperty, "here somewhere"); for (Object element: valueElements) { if (element instanceof JAXBElement) { System.out.println(PrismTestUtil.serializeJaxbElementToString((JAXBElement) element)); } else { AssertJUnit.fail("Unexpected element type "+element.getClass()); } } PrismProperty<String> parsedProperty = (PrismProperty<String>)(Item) StaticExpressionUtil.parseValueElements(valueElements, propDef, "here again"); // THEN assertEquals("Roundtrip failed", origProperty, parsedProperty); }
Collection<JAXBElement<RawType>> collection = StaticExpressionUtil.serializeValueElements(property, null); ObjectFactory of = new ObjectFactory(); for (JAXBElement<RawType> obj : collection) {
public static <IV extends PrismValue,ID extends ItemDefinition> Item<IV,ID> parseValueElements(Collection<?> valueElements, ID outputDefinition, String contextDescription) throws SchemaException { Item<IV,ID> output = null; for (Object valueElement: valueElements) { RawType rawType = getRawType(valueElement, contextDescription); Item<IV,ID> elementItem = rawType.getParsedItem(outputDefinition); if (output == null) { output = elementItem; } else { output.addAll(elementItem.getClonedValues()); } } return output; }
private static ItemDefinition<?> determineOutputDefinition(ScriptingVariableDefinitionType variableDefinition, VariableResolutionContext ctx, String shortDesc) throws SchemaException { List<JAXBElement<?>> evaluators = variableDefinition.getExpression().getExpressionEvaluator(); boolean isValue = !evaluators.isEmpty() && QNameUtil.match(evaluators.get(0).getName(), SchemaConstants.C_VALUE); QName elementName = new QName(variableDefinition.getName()); if (variableDefinition.getType() != null) { Integer maxOccurs; if (variableDefinition.getMaxOccurs() != null) { maxOccurs = XsdTypeMapper.multiplicityToInteger(variableDefinition.getMaxOccurs()); } else if (isValue) { // if we have constant values we can try to guess maxOccurs = evaluators.size() > 1 ? -1 : 1; } else { maxOccurs = null; // no idea } if (maxOccurs == null) { maxOccurs = -1; // to be safe } return ctx.prismContext.getSchemaRegistry().createAdHocDefinition(elementName, variableDefinition.getType(), 0, maxOccurs); } if (isValue) { return StaticExpressionUtil.deriveOutputDefinitionFromValueElements(elementName, evaluators, shortDesc, ctx.prismContext); } else { throw new SchemaException("The type of scripting variable " + variableDefinition.getName() + " is not defined"); } }
public static ExecuteProvisioningScriptOperation convertToScriptOperation( ProvisioningScriptType scriptType, String desc, PrismContext prismContext) throws SchemaException { ExecuteProvisioningScriptOperation scriptOperation = new ExecuteProvisioningScriptOperation(); MutablePrismPropertyDefinition scriptArgumentDefinition = prismContext.definitionFactory().createPropertyDefinition( FAKE_SCRIPT_ARGUMENT_NAME, DOMUtil.XSD_STRING); scriptArgumentDefinition.setMinOccurs(0); scriptArgumentDefinition.setMaxOccurs(-1); for (ProvisioningScriptArgumentType argument : scriptType.getArgument()) { ExecuteScriptArgument arg = new ExecuteScriptArgument(argument.getName(), StaticExpressionUtil.getStaticOutput(argument, scriptArgumentDefinition, desc, ExpressionReturnMultiplicityType.SINGLE, prismContext)); scriptOperation.getArgument().add(arg); } scriptOperation.setLanguage(scriptType.getLanguage()); scriptOperation.setTextCode(scriptType.getCode()); if (scriptType.getHost() != null && scriptType.getHost().equals(ProvisioningScriptHostType.CONNECTOR)) { scriptOperation.setConnectorHost(true); scriptOperation.setResourceHost(false); } if (scriptType.getHost() == null || scriptType.getHost().equals(ProvisioningScriptHostType.RESOURCE)) { scriptOperation.setConnectorHost(false); scriptOperation.setResourceHost(true); } scriptOperation.setCriticality(scriptType.getCriticality()); return scriptOperation; }
public static ItemDefinition<?> deriveOutputDefinitionFromValueElements(QName elementName, Collection<JAXBElement<?>> valueElements, String contextDescription, PrismContext prismContext) throws SchemaException { QName overallType = null; for (Object valueElement: valueElements) { RawType rawType = getRawType(valueElement, contextDescription); QName currentType = rawType.getExplicitTypeName(); if (currentType != null) { QName unified = prismContext.getSchemaRegistry().unifyTypes(overallType, currentType); if (unified == null) { throw new SchemaException("Couldn't unify types " + overallType + " and " + currentType + " in " + contextDescription); } overallType = unified; } } if (overallType == null) { overallType = DOMUtil.XSD_STRING; } int maxOccurs = valueElements.size() > 1 ? -1 : 1; return prismContext.getSchemaRegistry().createAdHocDefinition(elementName, overallType, 0, maxOccurs); }
@Override public <V extends PrismValue,D extends ItemDefinition> ExpressionEvaluator<V,D> createEvaluator(Collection<JAXBElement<?>> evaluatorElements, D outputDefinition, ExpressionFactory factory, String contextDescription, Task task, OperationResult result) throws SchemaException { Validate.notNull(outputDefinition, "output definition must be specified for literal expression evaluator"); Item<V,D> output = StaticExpressionUtil.parseValueElements(evaluatorElements, outputDefinition, contextDescription); PrismValueDeltaSetTriple<V> deltaSetTriple = ItemDeltaUtil.toDeltaSetTriple(output, null, prismContext); return new LiteralExpressionEvaluator<>(deltaSetTriple); }
/** * Returns either Object (if result is supposed to be single-value) or Collection<X> (if result is supposed to be multi-value) */ public static Object getStaticOutput(ExpressionType expressionType, PrismPropertyDefinition outputDefinition, String contextDescription, ExpressionReturnMultiplicityType preferredMultiplicity, PrismContext prismContext) throws SchemaException { PrismProperty<?> output = getPropertyStatic(expressionType, outputDefinition, contextDescription, prismContext); ExpressionReturnMultiplicityType multiplicity = preferredMultiplicity; if (expressionType.getReturnMultiplicity() != null) { multiplicity = expressionType.getReturnMultiplicity(); } else if (output != null && output.size() > 1) { multiplicity = ExpressionReturnMultiplicityType.MULTI; } if (output == null) { switch (multiplicity) { case MULTI: return new ArrayList<>(0); case SINGLE: return null; default: throw new IllegalStateException("Unknown return type "+multiplicity); } } else { Collection<?> realValues = output.getRealValues(); switch (multiplicity) { case MULTI: return realValues; case SINGLE: return MiscUtil.extractSingleton(realValues); default: throw new IllegalStateException("Unknown return type "+multiplicity); } } }