Search in sources :

Example 41 with PrismContext

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

the class WfExpressionEvaluationHelper method evaluateExpression.

@SuppressWarnings("unchecked")
@NotNull
public <T> List<T> evaluateExpression(ExpressionType expressionType, ExpressionVariables variables, String contextDescription, Class<T> clazz, QName typeName, Function<Object, Object> additionalConvertor, Task task, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    ExpressionFactory expressionFactory = getExpressionFactory();
    PrismContext prismContext = expressionFactory.getPrismContext();
    PrismPropertyDefinition<String> resultDef = new PrismPropertyDefinitionImpl<>(new QName(SchemaConstants.NS_C, "result"), typeName, prismContext);
    Expression<PrismPropertyValue<String>, PrismPropertyDefinition<String>> expression = expressionFactory.makeExpression(expressionType, resultDef, contextDescription, task, result);
    ExpressionEvaluationContext context = new ExpressionEvaluationContext(null, variables, contextDescription, task, result);
    context.setAdditionalConvertor(additionalConvertor);
    PrismValueDeltaSetTriple<PrismPropertyValue<String>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, context, task, result);
    return exprResultTriple.getZeroSet().stream().map(ppv -> (T) ppv.getRealValue()).collect(Collectors.toList());
}
Also used : SchemaConstants(com.evolveum.midpoint.schema.constants.SchemaConstants) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) SpringApplicationContextHolder.getMiscDataUtil(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getMiscDataUtil) MiscDataUtil(com.evolveum.midpoint.wf.impl.util.MiscDataUtil) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DOMUtil(com.evolveum.midpoint.util.DOMUtil) QNameUtil(com.evolveum.midpoint.util.QNameUtil) PrismContext(com.evolveum.midpoint.prism.PrismContext) PrismValueDeltaSetTriple(com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) Expression(com.evolveum.midpoint.repo.common.expression.Expression) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) SpringApplicationContextHolder.getExpressionFactory(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getExpressionFactory) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) ModelExpressionThreadLocalHolder(com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) Task(com.evolveum.midpoint.task.api.Task) ApprovalUtils(com.evolveum.midpoint.wf.util.ApprovalUtils) JAXBException(javax.xml.bind.JAXBException) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) ExpressionVariables(com.evolveum.midpoint.repo.common.expression.ExpressionVariables) Component(org.springframework.stereotype.Component) List(java.util.List) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ExpressionType(com.evolveum.midpoint.xml.ns._public.common.common_3.ExpressionType) DelegateExecution(org.activiti.engine.delegate.DelegateExecution) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) ApprovalLevelOutcomeType(com.evolveum.midpoint.xml.ns._public.common.common_3.ApprovalLevelOutcomeType) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) ObjectReferenceType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) SpringApplicationContextHolder.getExpressionFactory(com.evolveum.midpoint.wf.impl.processes.common.SpringApplicationContextHolder.getExpressionFactory) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismContext(com.evolveum.midpoint.prism.PrismContext) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue) NotNull(org.jetbrains.annotations.NotNull)

Example 42 with PrismContext

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

the class GcpExpressionHelper method evaluateBooleanExpression.

private boolean evaluateBooleanExpression(ExpressionType expressionType, ExpressionVariables expressionVariables, String opContext, Task taskFromModel, OperationResult result) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException {
    PrismContext prismContext = expressionFactory.getPrismContext();
    QName resultName = new QName(SchemaConstants.NS_C, "result");
    PrismPropertyDefinition<Boolean> resultDef = new PrismPropertyDefinitionImpl(resultName, DOMUtil.XSD_BOOLEAN, prismContext);
    Expression<PrismPropertyValue<Boolean>, PrismPropertyDefinition<Boolean>> expression = expressionFactory.makeExpression(expressionType, resultDef, opContext, taskFromModel, result);
    ExpressionEvaluationContext params = new ExpressionEvaluationContext(null, expressionVariables, opContext, taskFromModel, result);
    PrismValueDeltaSetTriple<PrismPropertyValue<Boolean>> exprResultTriple = ModelExpressionThreadLocalHolder.evaluateExpressionInContext(expression, params, taskFromModel, result);
    Collection<PrismPropertyValue<Boolean>> exprResult = exprResultTriple.getZeroSet();
    if (exprResult.size() == 0) {
        return false;
    } else if (exprResult.size() > 1) {
        throw new IllegalStateException("Expression should return exactly one boolean value; it returned " + exprResult.size() + " ones");
    }
    Boolean boolResult = exprResult.iterator().next().getValue();
    return boolResult != null ? boolResult : false;
}
Also used : PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ExpressionEvaluationContext(com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext) PrismContext(com.evolveum.midpoint.prism.PrismContext) QName(javax.xml.namespace.QName) PrismPropertyDefinitionImpl(com.evolveum.midpoint.prism.PrismPropertyDefinitionImpl) PrismPropertyValue(com.evolveum.midpoint.prism.PrismPropertyValue)

Example 43 with PrismContext

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

the class ExportObjects method execute.

public boolean execute() throws UnsupportedEncodingException, FileNotFoundException {
    System.out.println("Starting objects export.");
    File file = new File(filePath);
    if (file.exists() || file.canRead()) {
        System.out.println("XML file already exists, export won't be done.");
        return false;
    }
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(CONTEXTS);
    final OutputStreamWriter stream = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
    try {
        System.out.println("Loading spring contexts.");
        System.out.println("Set repository.");
        RepositoryService repository = context.getBean("repositoryService", RepositoryService.class);
        ResultHandler<ObjectType> handler = new ResultHandler<ObjectType>() {

            PrismContext prismContext = context.getBean(PrismContext.class);

            @Override
            public boolean handle(PrismObject<ObjectType> object, OperationResult parentResult) {
                String displayName = getDisplayName(object);
                System.out.println("Exporting object " + displayName);
                OperationResult resultExport = new OperationResult("Export " + displayName);
                try {
                    String stringObject = prismContext.serializeObjectToString(object, PrismContext.LANG_XML);
                    stream.write("\t" + stringObject + "\n");
                } catch (Exception ex) {
                    System.out.println("Failed to parse objects to string for xml. Reason: " + ex);
                    resultExport.recordFatalError("Failed to parse objects to string for xml. Reason: ", ex);
                }
                return true;
            }
        };
        stream.write(createHeaderForXml());
        OperationResult result = new OperationResult("search set");
        System.out.println("Creating xml file " + file.getName());
        repository.searchObjectsIterative(ObjectType.class, null, handler, null, false, result);
        stream.write("</objects>");
        System.out.println("Created xml file " + file.getName());
    } catch (Exception ex) {
        System.out.println("Exception occurred during context loading, reason: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        destroyContext(context);
        if (stream != null) {
            IOUtils.closeQuietly(stream);
        }
    }
    System.out.println("Objects export finished.");
    return true;
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService)

Example 44 with PrismContext

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

the class TestMigrator method testUserCredentials.

@Test
public void testUserCredentials() throws Exception {
    Migrator migrator = createMigrator();
    PrismContext prismContext = PrismTestUtil.getPrismContext();
    PrismObject<UserType> oldUser = prismContext.parseObject(new File(TEST_DIR + "/user-migrate-credentials.xml"));
    PrismObject<UserType> newUser = migrator.migrate(oldUser);
    UserType newUserType = newUser.asObjectable();
    assertNull("Credentials in migrated object must be null.", newUserType.getCredentials());
    assertNotNull("Migrated user must contain assignment.", newUserType.getAssignment());
    assertEquals("Migrated user must contain 1 assignment.", newUserType.getAssignment().size(), 1);
    AssignmentType superUserRole = newUserType.getAssignment().get(0);
    assertNotNull("Target ref in the user's assignment must not be null.", superUserRole.getTargetRef());
    assertEquals(superUserRole.getTargetRef().getOid(), SystemObjectsType.ROLE_SUPERUSER.value());
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) AssignmentType(com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType) Migrator(com.evolveum.midpoint.model.impl.migrator.Migrator) File(java.io.File) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) Test(org.testng.annotations.Test)

Example 45 with PrismContext

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

the class TestMigrator method assertSimpleMigration.

private <O extends ObjectType> void assertSimpleMigration(File fileOld, File fileNew) throws SchemaException, IOException {
    // GIVEN
    Migrator migrator = createMigrator();
    PrismContext prismContext = PrismTestUtil.getPrismContext();
    PrismObject<O> objectOld = prismContext.parseObject(fileOld);
    // WHEN
    PrismObject<O> objectNew = migrator.migrate(objectOld);
    // THEN
    IntegrationTestTools.display("Migrated object " + fileOld.getName(), objectNew);
    assertNotNull("No migrated object " + fileOld.getName(), objectNew);
    IntegrationTestTools.display("Migrated object " + fileOld.getName(), objectNew);
    String migratedXml = PrismTestUtil.serializeObjectToString(objectNew, PrismContext.LANG_XML);
    IntegrationTestTools.display("Migrated object XML " + fileOld.getName(), migratedXml);
    PrismObject<O> expectedObject = prismContext.parseObject(fileNew);
    IntegrationTestTools.display("Expected object " + fileOld.getName(), expectedObject);
    String expectedXml = PrismTestUtil.serializeObjectToString(expectedObject, PrismContext.LANG_XML);
    IntegrationTestTools.display("Expected object XML " + fileOld.getName(), expectedXml);
    List<String> expectedXmlLines = MiscUtil.splitLines(expectedXml);
    Patch patch = DiffUtils.diff(expectedXmlLines, MiscUtil.splitLines(migratedXml));
    List<String> diffLines = DiffUtils.generateUnifiedDiff(fileOld.getPath(), fileNew.getPath(), expectedXmlLines, patch, 3);
    IntegrationTestTools.display("XML textual diff", StringUtils.join(diffLines, '\n'));
    PrismAsserts.assertEquivalent("Unexpected migration result for " + fileOld.getName(), expectedObject, objectNew);
    assertEquals("Unexpected element name for " + fileOld.getName(), expectedObject.getElementName(), objectNew.getElementName());
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) Migrator(com.evolveum.midpoint.model.impl.migrator.Migrator) Patch(difflib.Patch)

Aggregations

PrismContext (com.evolveum.midpoint.prism.PrismContext)104 Test (org.testng.annotations.Test)59 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)15 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)15 QName (javax.xml.namespace.QName)15 UserType (com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)13 Task (com.evolveum.midpoint.task.api.Task)11 SchemaRegistry (com.evolveum.midpoint.prism.schema.SchemaRegistry)10 ResourceType (com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType)10 PrismObject (com.evolveum.midpoint.prism.PrismObject)9 SystemException (com.evolveum.midpoint.util.exception.SystemException)9 File (java.io.File)8 PrismPropertyDefinition (com.evolveum.midpoint.prism.PrismPropertyDefinition)7 ArrayList (java.util.ArrayList)7 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)6 AssignmentType (com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType)6 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)6 Protector (com.evolveum.midpoint.prism.crypto.Protector)5 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)5 MidPointApplication (com.evolveum.midpoint.web.security.MidPointApplication)5