public ExpressionType expressionEvaluator(JAXBElement<?> value) { getExpressionEvaluator().add(value); return this; }
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); }
public static Object getConstantIfPresent(ExpressionType expression) { if (expression == null || expression.getExpressionEvaluator().size() != 1) { return null; } JAXBElement<?> jaxb = expression.getExpressionEvaluator().get(0); if (QNameUtil.match(jaxb.getName(), SchemaConstants.C_VALUE)) { return jaxb.getValue(); } else { return null; } } }
public void parse(ExpressionFactory factory, String contextDescription, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException { if (expressionType == null) { evaluators.add(createDefaultEvaluator(factory, contextDescription, task, result)); return; } if (expressionType.getExpressionEvaluator() == null /* && expressionType.getSequence() == null */) { throw new SchemaException("No evaluator was specified in "+contextDescription); } if (expressionType.getExpressionEvaluator() != null) { ExpressionEvaluator evaluator = createEvaluator(expressionType.getExpressionEvaluator(), factory, contextDescription, task, result); evaluators.add(evaluator); } if (evaluators.isEmpty()) { evaluators.add(createDefaultEvaluator(factory, contextDescription, task, result)); } }
@Override public String getNodeStyleAttributes() { ExpressionType expression = getMapping().getExpression(); if (expression == null || expression.getExpressionEvaluator().isEmpty()) { return ""; } JAXBElement<?> evalElement = expression.getExpressionEvaluator().get(0); Object eval = evalElement.getValue(); if (QNameUtil.match(evalElement.getName(), SchemaConstants.C_VALUE)) { return "style=filled, fillcolor=ivory"; } else if (eval instanceof AsIsExpressionEvaluatorType) { return ""; } else if (eval instanceof ScriptExpressionEvaluatorType) { return "style=filled, fillcolor=wheat"; } else { return ""; } }
private String getTooltipString() { ExpressionType expression = getMapping().getExpression(); if (expression == null || expression.getExpressionEvaluator().isEmpty()) { return "asIs"; } JAXBElement<?> evalElement = expression.getExpressionEvaluator().get(0); Object eval = evalElement.getValue(); if (QNameUtil.match(evalElement.getName(), SchemaConstants.C_VALUE)) { return getStringConstant(eval); } else if (eval instanceof AsIsExpressionEvaluatorType) { return "asIs"; } else if (eval instanceof ScriptExpressionEvaluatorType) { return ((ScriptExpressionEvaluatorType) eval).getCode(); } else if (eval instanceof ItemPathType) { return String.valueOf(((ItemPathType) eval).getItemPath()); } else { return ""; } }
protected <T> void assertExpression(PrismProperty<T> prop, String evaluatorName) { PrismPropertyValue<T> pval = prop.getValue(); ExpressionWrapper expressionWrapper = pval.getExpression(); assertNotNull("No expression wrapper in "+prop, expressionWrapper); Object expressionObj = expressionWrapper.getExpression(); assertNotNull("No expression in "+prop, expressionObj); assertTrue("Wrong expression type: " +expressionObj.getClass(), expressionObj instanceof ExpressionType); ExpressionType expressionType = (ExpressionType)expressionObj; JAXBElement<?> evaluatorElement = expressionType.getExpressionEvaluator().iterator().next(); assertEquals("Wrong expression evaluator name", evaluatorName, evaluatorElement.getName().getLocalPart()); }
private static void addDerivedVariables(HashMap<String, Object> resultingVariables, ScriptingVariablesDefinitionType definitions, VariableResolutionContext ctx, OperationResult result) throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException { if (definitions == null) { return; } for (ScriptingVariableDefinitionType definition : definitions.getVariable()) { if (definition.getExpression() == null) { continue; // todo or throw an exception? } String shortDesc = "scripting variable " + definition.getName(); Object value; if (definition.getExpression().getExpressionEvaluator().size() == 1 && QNameUtil.match(SchemaConstantsGenerated.C_PATH, definition.getExpression().getExpressionEvaluator().get(0).getName())) { value = variableFromPathExpression(resultingVariables, definition.getExpression().getExpressionEvaluator().get(0), ctx, shortDesc, result); } else { value = variableFromOtherExpression(resultingVariables, definition, ctx, shortDesc, result); } putImmutableValue(resultingVariables, definition.getName(), value); } }
@Override protected void assertPrismContainerValueLocal(PrismContainerValue<MappingType> value) throws SchemaException { MappingType mappingType = value.getValue(); ExpressionType expressionType = mappingType.getExpression(); List<JAXBElement<?>> expressionEvaluatorElements = expressionType.getExpressionEvaluator(); assertEquals("Wrong number of expression evaluator elemenets", 1, expressionEvaluatorElements.size()); JAXBElement<?> expressionEvaluatorElement = expressionEvaluatorElements.get(0); Object evaluatorElementObject = expressionEvaluatorElement.getValue(); if (!(evaluatorElementObject instanceof ConstExpressionEvaluatorType)) { AssertJUnit.fail("Const expression is of type " + evaluatorElementObject.getClass().getName()); } ConstExpressionEvaluatorType constExpressionEvaluatorType = (ConstExpressionEvaluatorType)evaluatorElementObject; System.out.println("ConstExpressionEvaluatorType: "+constExpressionEvaluatorType); assertEquals("Wrong value in const evaluator", "foo", constExpressionEvaluatorType.getValue()); }
private <T> void assertExpression(PrismProperty<T> prop, String evaluatorName) { System.out.println("Prop:"); System.out.println(prop.debugDump(1)); PrismPropertyValue<T> pval = prop.getValue(); ExpressionWrapper expressionWrapper = pval.getExpression(); assertNotNull("No expression wrapper in "+prop, expressionWrapper); Object expressionObj = expressionWrapper.getExpression(); assertNotNull("No expression in "+prop, expressionObj); System.out.println("- Expression: "+expressionObj); if (namespaces) { assertTrue("Wrong expression type ("+language+","+(namespaces?"ns":"no-ns") + ") : " +expressionObj.getClass(), expressionObj instanceof ExpressionType); ExpressionType expressionType = (ExpressionType)expressionObj; JAXBElement<?> evaluatorElement = expressionType.getExpressionEvaluator().iterator().next(); assertEquals("Wrong expression evaluator name", evaluatorName, evaluatorElement.getName().getLocalPart()); } }
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"); } }
private void checkMapping(ResourceValidationContext ctx, ItemPath path, ResourceObjectTypeDefinitionType objectType, QName itemName, MappingType mapping, boolean outbound, int index, boolean implicitSourceOrTarget) { String inOut = outbound ? getString("ResourceValidator.outboundMapping") : getString("ResourceValidator.inboundMapping", index); String itemNameText = prettyPrintUsingStandardPrefix(itemName); if (outbound && mapping.getTarget() != null) { ctx.validationResult.add(Issue.Severity.INFO, CAT_SCHEMA_HANDLING, C_SUPERFLUOUS_MAPPING_TARGET, getString(CLASS_DOT + C_SUPERFLUOUS_MAPPING_TARGET, getName(objectType), inOut, itemNameText, format(mapping.getTarget())), ctx.resourceRef, path); } if (!implicitSourceOrTarget && (mapping.getExpression() == null || mapping.getExpression().getExpressionEvaluator().isEmpty() || (mapping.getExpression().getExpressionEvaluator().size() == 1 && QNameUtil.match(mapping.getExpression().getExpressionEvaluator().get(0).getName(), SchemaConstantsGenerated.C_AS_IS)))) { if ((outbound && (mapping.getSource() == null || mapping.getSource().isEmpty())) || (!outbound && mapping.getTarget() == null)) { String code = outbound ? C_MISSING_MAPPING_SOURCE : C_MISSING_MAPPING_TARGET; ctx.validationResult.add(Issue.Severity.WARNING, CAT_SCHEMA_HANDLING, code, getString(CLASS_DOT + code, getName(objectType), inOut, itemNameText), ctx.resourceRef, path); } } for (VariableBindingDefinitionType source : mapping.getSource()) { checkItemPath(ctx, path, objectType, itemName, mapping, outbound, itemNameText, true, index, source.getPath()); } if (mapping.getTarget() != null) { checkItemPath(ctx, path, objectType, itemName, mapping, outbound, itemNameText, false, index, mapping.getTarget().getPath()); } }
@Test public void testParseExpressionFromJaxb() throws SchemaException, SAXException, IOException, JAXBException { PrismContext prismContext = PrismTestUtil.getPrismContext(); RoleType roleType = PrismTestUtil.parseObjectable(new File(TestConstants.COMMON_DIR, "role.xml"), RoleType.class); // WHEN PrismObject<RoleType> role = roleType.asPrismObject(); role.revive(prismContext); // THEN System.out.println("Parsed role:"); System.out.println(role.debugDump()); role.checkConsistence(); assertPropertyValue(role, RoleType.F_NAME, PrismTestUtil.createPolyString("r3")); PrismAsserts.assertEquals("Wrong number of approver expressions", 1, role.asObjectable().getApproverExpression().size()); Object o = role.asObjectable().getApproverExpression().get(0).getExpressionEvaluator().get(0).getValue(); PrismAsserts.assertEquals("Invalid evaluator type", ScriptExpressionEvaluatorType.class, o.getClass()); String code = ((ScriptExpressionEvaluatorType) o).getCode(); PrismAsserts.assertEquals("Incorrect code parsed", "midpoint.oid2ort(user.getOid())", code); }
@Override public ValuePolicyType resolve() { if (mappingBuilder.getMappingType().getExpression() != null) { List<JAXBElement<?>> evaluators = mappingBuilder.getMappingType().getExpression().getExpressionEvaluator(); for (JAXBElement jaxbEvaluator : evaluators) { Object object = jaxbEvaluator.getValue(); if (object instanceof GenerateExpressionEvaluatorType && ((GenerateExpressionEvaluatorType) object).getValuePolicyRef() != null) { ObjectReferenceType ref = ((GenerateExpressionEvaluatorType) object).getValuePolicyRef(); try { ValuePolicyType valuePolicyType = mappingBuilder.getObjectResolver().resolve(ref, ValuePolicyType.class, null, "resolving value policy for generate attribute "+ outputDefinition.getName()+"value", task, new OperationResult("Resolving value policy")); if (valuePolicyType != null) { return valuePolicyType; } } catch (CommonException ex) { throw new SystemException(ex.getMessage(), ex); } } } } return null; } };
private void checkAssignmentConstruction(UserType jack, String attributeName, String value) throws SchemaException { assertEquals("jack's assignments", 1, jack.getAssignment().size()); AssignmentType assignmentType = jack.getAssignment().get(0); ConstructionType constructionType = assignmentType.getConstruction(); assertNotNull("construction is null", constructionType); boolean found = false; for (ResourceAttributeDefinitionType attributeDefinitionType : constructionType.getAttribute()) { if (attributeDefinitionType.getRef().equivalent(new ItemPathType(ItemPath.create(new QName(attributeName))))) { ExpressionType expressionType = attributeDefinitionType.getOutbound().getExpression(); assertNotNull("no expression", expressionType); assertEquals("wrong # of expression evaluators", 1, expressionType.getExpressionEvaluator().size()); JAXBElement<?> element = expressionType.getExpressionEvaluator().get(0); PrimitiveXNode valueXNode = (PrimitiveXNode) (((RawType) element.getValue()).serializeToXNode()); assertEquals("wrong outbound value", value, valueXNode.getStringValue()); found = true; } } assertTrue("attribute " + attributeName + " mapping not found", found); }
List<JAXBElement<?>> evaluators = mappingType.getExpression().getExpressionEvaluator(); if (evaluators != null) { for (JAXBElement jaxbEvaluator : evaluators) {
private MappingType createCondition(String conditionName, char conditionType) { ScriptExpressionEvaluatorType script = new ScriptExpressionEvaluatorType(); switch (conditionType) { case '+': script.setCode("basic.stringify(name) == 'jack1'"); break; case '-': script.setCode("basic.stringify(name) == 'jack'"); break; case '0': script.setCode("basic.stringify(name) == 'never there'"); break; case '!': script.setCode(createDumpConditionCode(conditionName)); break; default: throw new AssertionError(conditionType); } ExpressionType expression = new ExpressionType(); expression.getExpressionEvaluator().add(new ObjectFactory().createScript(script)); VariableBindingDefinitionType source = new VariableBindingDefinitionType(); source.setPath(new ItemPathType(UserType.F_NAME)); MappingType rv = new MappingType(); rv.setName(conditionName); rv.setExpression(expression); rv.getSource().add(source); return rv; }
private void assertObjectTemplateInternals(PrismObject<ObjectTemplateType> object, QName elementName) throws SchemaException { int assignmentValuesFound = 0; for (ObjectTemplateMappingType mappingType : object.asObjectable().getMapping()) { if (mappingType.getExpression() != null) { if (mappingType.getTarget() != null && mappingType.getTarget().getPath() != null && UserType.F_ASSIGNMENT.equivalent(mappingType.getTarget().getPath().getItemPath())) { ItemDefinition assignmentDef = getPrismContext().getSchemaRegistry() .findObjectDefinitionByCompileTimeClass(UserType.class) .findItemDefinition(UserType.F_ASSIGNMENT); for (JAXBElement evaluator : mappingType.getExpression().getExpressionEvaluator()) { if (evaluator.getValue() instanceof RawType) { RawType rawType = (RawType) evaluator.getValue(); System.out.println("\nraw assignment:\n" + rawType); Item assignment = rawType.getParsedItem(assignmentDef); System.out.println("\nassignment:\n" + assignment.debugDump()); assignmentValuesFound++; } } } } } assertEquals("wrong # of assignment values found in mapping", 2, assignmentValuesFound); }
private void createCustomConstruction(RoleType role, String name, int order) { ConstructionType c = new ConstructionType(prismContext); c.setDescription(name); c.setResourceRef(ObjectTypeUtil.createObjectRef(RESOURCE_DUMMY_EMPTY_OID, ObjectTypes.RESOURCE)); ResourceAttributeDefinitionType nameDef = new ResourceAttributeDefinitionType(); nameDef.setRef(new ItemPathType(ItemPath.create(new QName(SchemaConstants.NS_ICF_SCHEMA, "name")))); MappingType outbound = new MappingType(); outbound.setName(name); ExpressionType expression = new ExpressionType(); ScriptExpressionEvaluatorType script = new ScriptExpressionEvaluatorType(); script.setCode( "import com.evolveum.midpoint.model.impl.lens.TestAssignmentProcessor2\n\n" + "TestAssignmentProcessor2.startCallback('" + name + "')\n" + "this.binding.variables.each {k,v -> TestAssignmentProcessor2.variableCallback(k, v, '" + name + "')}\n" + "TestAssignmentProcessor2.finishCallback('" + name + "')\n" + "return null"); expression.getExpressionEvaluator().add(new ObjectFactory().createScript(script)); outbound.setExpression(expression); nameDef.setOutbound(outbound); c.getAttribute().add(nameDef); AssignmentType a = new AssignmentType(prismContext); a.setDescription("Assignment for " + c.getDescription()); a.setConstruction(c); addAssignmentOrInducement(role, order, a); }
expression.getExpressionEvaluator().add(new ObjectFactory().createValue(value)); outbound.setExpression(expression); attributeDefinitionType.setOutbound(outbound);