Search in sources :

Example 21 with PrismPropertyDefinitionImpl

use of com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl in project midpoint by Evolveum.

the class TestModelExpressions method executeScriptExpressionString.

private String executeScriptExpressionString(final String TEST_NAME, ExpressionVariables variables) throws SchemaException, IOException, JAXBException, ExpressionEvaluationException, ObjectNotFoundException {
    // GIVEN
    OperationResult result = new OperationResult(TestModelExpressions.class.getName() + "." + TEST_NAME);
    ScriptExpressionEvaluatorType scriptType = parseScriptType("expression-" + TEST_NAME + ".xml");
    ItemDefinition outputDefinition = new PrismPropertyDefinitionImpl(PROPERTY_NAME, DOMUtil.XSD_STRING, PrismTestUtil.getPrismContext());
    ScriptExpression scriptExpression = scriptExpressionFactory.createScriptExpression(scriptType, outputDefinition, TEST_NAME);
    if (variables == null) {
        variables = new ExpressionVariables();
    }
    // WHEN
    TestUtil.displayWhen(TEST_NAME);
    List<PrismPropertyValue<String>> scriptOutputs = evaluate(scriptExpression, variables, false, TEST_NAME, null, result);
    // THEN
    TestUtil.displayThen(TEST_NAME);
    display("Script output", scriptOutputs);
    result.computeStatus();
    TestUtil.assertSuccess(result);
    if (scriptOutputs.size() == 0) {
        return null;
    }
    assertEquals("Unexpected number of script outputs", 1, scriptOutputs.size());
    return scriptOutputs.get(0).getValue();
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) ItemDefinition(com.evolveum.midpoint.prism.ItemDefinition) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ScriptExpressionEvaluatorType(com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType) ScriptExpression(com.evolveum.midpoint.model.common.expression.script.ScriptExpression) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 22 with PrismPropertyDefinitionImpl

use of com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl in project midpoint by Evolveum.

the class CustomNotifier method evaluateExpression.

private List<NotificationMessageType> evaluateExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String shortDesc, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<NotificationMessageType> resultDef = new PrismPropertyDefinitionImpl<>(resultName, NotificationMessageType.COMPLEX_TYPE, prismContext);
    Expression<PrismPropertyValue<NotificationMessageType>, PrismPropertyDefinition<NotificationMessageType>> expression = expressionFactory.makeExpression(expressionType, resultDef, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<NotificationMessageType>> exprResult = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    return exprResult.getZeroSet().stream().map(PrismPropertyValue::getValue).collect(Collectors.toList());
}
Also used : PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) NotificationMessageType(com.evolveum.midpoint.xml.ns._public.common.common_3.NotificationMessageType) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 23 with PrismPropertyDefinitionImpl

use of com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl in project midpoint by Evolveum.

the class SchemaProcessorTest method testRoundTripGeneric.

@Test
public void testRoundTripGeneric() throws SchemaException {
    // GIVEN
    PrismSchemaImpl schema = new PrismSchemaImpl(SCHEMA_NS, PrismTestUtil.getPrismContext());
    // Ordinary property
    schema.createPropertyDefinition("number1", DOMUtil.XSD_INT);
    // Property container
    PrismContainerDefinitionImpl containerDefinition = schema.createPropertyContainerDefinition("ContainerType");
    // ... in it ordinary property
    containerDefinition.createPropertyDefinition("login", DOMUtil.XSD_STRING);
    // ... and local property with a type from another schema
    containerDefinition.createPropertyDefinition("password", ProtectedStringType.COMPLEX_TYPE);
    // ... property reference
    containerDefinition.createPropertyDefinition(SchemaConstants.C_CREDENTIALS, CredentialsType.COMPLEX_TYPE);
    // ... read-only int property 
    PrismPropertyDefinition counterProperty = containerDefinition.createPropertyDefinition("counter", DOMUtil.XSD_INT);
    ((PrismPropertyDefinitionImpl) counterProperty).setReadOnly();
    System.out.println("Generic schema before serializing to XSD: ");
    System.out.println(schema.debugDump());
    System.out.println();
    // WHEN
    Document xsd = schema.serializeToXsd();
    String stringXmlSchema = DOMUtil.serializeDOMToString(xsd);
    System.out.println("Generic schema after serializing to XSD: ");
    System.out.println(stringXmlSchema);
    System.out.println();
    Document parsedXsd = DOMUtil.parseDocument(stringXmlSchema);
    PrismSchema newSchema = PrismSchemaImpl.parse(DOMUtil.getFirstChildElement(parsedXsd), true, "serialized schema", PrismTestUtil.getPrismContext());
    System.out.println("Generic schema after parsing from XSD: ");
    System.out.println(newSchema.debugDump());
    System.out.println();
    // THEN
    PrismPropertyDefinition number1def = newSchema.findItemDefinitionByElementName(new QName(SCHEMA_NS, "number1"), PrismPropertyDefinition.class);
    assertEquals(new QName(SCHEMA_NS, "number1"), number1def.getName());
    assertEquals(DOMUtil.XSD_INT, number1def.getTypeName());
    PrismContainerDefinition newContainerDef = schema.findContainerDefinitionByType(new QName(SCHEMA_NS, "ContainerType"));
    assertEquals(new QName(SCHEMA_NS, "ContainerType"), newContainerDef.getTypeName());
    PrismPropertyDefinition loginDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "login"));
    assertEquals(new QName(SCHEMA_NS, "login"), loginDef.getName());
    assertEquals(DOMUtil.XSD_STRING, loginDef.getTypeName());
    assertTrue("Read flag is wrong", loginDef.canRead());
    assertTrue("Create flag is wrong", loginDef.canAdd());
    assertTrue("Update flag is wrong", loginDef.canModify());
    PrismPropertyDefinition passwdDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "password"));
    assertEquals(new QName(SCHEMA_NS, "password"), passwdDef.getName());
    assertEquals(ProtectedStringType.COMPLEX_TYPE, passwdDef.getTypeName());
    PrismPropertyDefinition credDef = newContainerDef.findPropertyDefinition(new QName(SchemaConstants.NS_C, "credentials"));
    assertEquals(new QName(SchemaConstants.NS_C, "credentials"), credDef.getName());
    assertEquals(new QName(SchemaConstants.NS_C, "CredentialsType"), credDef.getTypeName());
    PrismPropertyDefinition countDef = newContainerDef.findPropertyDefinition(new QName(SCHEMA_NS, "counter"));
    assertEquals(new QName(SCHEMA_NS, "counter"), countDef.getName());
    assertEquals(DOMUtil.XSD_INT, countDef.getTypeName());
    assertTrue("Read flag is wrong", countDef.canRead());
    assertFalse("Create flag is wrong", countDef.canAdd());
    assertFalse("Update flag is wrong", countDef.canModify());
}
Also used : PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) PrismSchemaImpl(com.evolveum.midpoint.prism.schema.PrismSchemaImpl) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) PrismContainerDefinitionImpl(com.evolveum.midpoint.prism.PrismContainerDefinitionImpl) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismContainerDefinition(com.evolveum.midpoint.prism.PrismContainerDefinition) Document(org.w3c.dom.Document) Test(org.testng.annotations.Test)

Example 24 with PrismPropertyDefinitionImpl

use of com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl in project midpoint by Evolveum.

the class ExpressionHandler method evaluateConfirmationExpression.

public boolean evaluateConfirmationExpression(UserType user, ShadowType shadow, ExpressionType expressionType, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    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.");
    ResourceType resource = resolveResource(shadow, result);
    ExpressionVariables variables = getDefaultXPathVariables(user, shadow, resource);
    String shortDesc = "confirmation expression for " + resource.asPrismObject();
    PrismPropertyDefinition<Boolean> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_BOOLEAN, prismContext);
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    Collection<PrismPropertyValue<Boolean>> nonNegativeValues = outputTriple.getNonNegativeValues();
    if (nonNegativeValues == null || 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;
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 25 with PrismPropertyDefinitionImpl

use of com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl in project midpoint by Evolveum.

the class ExpressionHandler method evaluateExpression.

public String evaluateExpression(ShadowType shadow, ExpressionType expressionType, String shortDesc, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    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.");
    ResourceType resource = resolveResource(shadow, result);
    ExpressionVariables variables = getDefaultXPathVariables(null, shadow, resource);
    PrismPropertyDefinition<String> outputDefinition = new PrismPropertyDefinitionImpl<>(ExpressionConstants.OUTPUT_ELEMENT_NAME, DOMUtil.XSD_STRING, prismContext);
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, outputDefinition, shortDesc, task, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, variables, shortDesc, task, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> outputTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, task, result);
    if (outputTriple == null) {
        return null;
    }
    Collection<PrismPropertyValue<String>> nonNegativeValues = outputTriple.getNonNegativeValues();
    if (nonNegativeValues == null || nonNegativeValues.isEmpty()) {
        return null;
    }
    if (nonNegativeValues.size() > 1) {
        throw new ExpressionEvaluationException("Expression returned more than one value (" + nonNegativeValues.size() + ") in " + shortDesc);
    }
    return nonNegativeValues.iterator().next().getValue();
}
Also used : ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Aggregations

PrismPropertyDefinitionImpl (com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl)26 PrismPropertyValue (com.evolveum.midpoint.prism.PrismPropertyValue)19 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)18 QName (javax.xml.namespace.QName)16 ExpressionEvaluationContext (com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext)11 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)10 Test (org.testng.annotations.Test)10 ExpressionVariables (com.evolveum.midpoint.repo.common.expression.ExpressionVariables)6 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)6 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)5 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)4 ScriptExpression (com.evolveum.midpoint.model.common.expression.script.ScriptExpression)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)3 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)3 AbstractInternalModelIntegrationTest (com.evolveum.midpoint.model.impl.AbstractInternalModelIntegrationTest)2 PrismObject (com.evolveum.midpoint.prism.PrismObject)2 PrismProperty (com.evolveum.midpoint.prism.PrismProperty)2 Task (com.evolveum.midpoint.task.api.Task)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 ScriptExpressionEvaluatorType (com.evolveum.midpoint.xml.ns._public.common.common_3.ScriptExpressionEvaluatorType)2