Search in sources :

Example 21 with ResultHandler

use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.

the class OrgClosureManager method rebuild.

// endregion
// region Rebuilding or checking org closure
// we are already in the context of a transaction (and the org struct table is locked if possible)
// "check" here means "thorough check" (i.e. comparing with recomputed closure)
private void rebuild(boolean check, boolean rebuild, boolean stopOnFailure, final Context context, final Session session, OperationResult result) throws SchemaException {
    List existingEntries = null;
    if (check) {
        LOGGER.info("Reading from existing org closure table");
        NativeQuery selectQuery = session.createNativeQuery("SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME).addScalar("descendant_oid", StringType.INSTANCE).addScalar("ancestor_oid", StringType.INSTANCE).addScalar("val", IntegerType.INSTANCE);
        existingEntries = selectQuery.list();
        LOGGER.info("{} entries read", existingEntries.size());
    }
    LOGGER.info("Computing org closure table from scratch");
    NativeQuery deleteQuery = session.createNativeQuery("delete from " + CLOSURE_TABLE_NAME);
    deleteQuery.executeUpdate();
    LOGGER.trace("Closure table content deleted");
    final int orgsTotal = repositoryService.countObjects(OrgType.class, null, null, result);
    final MutableInt orgsProcessed = new MutableInt(0);
    ResultHandler<OrgType> handler = new ResultHandler<OrgType>() {

        @Override
        public boolean handle(PrismObject<OrgType> object, OperationResult parentResult) {
            LOGGER.trace("Processing {}", object);
            handleAdd(object.getOid(), getParentOidsFromObject(object), context, session);
            orgsProcessed.add(1);
            int currentState = orgsProcessed.intValue();
            if (currentState % 100 == 0) {
                LOGGER.info("{} organizations processed (out of {})", currentState, orgsTotal);
            }
            return true;
        }
    };
    repositoryService.searchObjectsIterative(OrgType.class, null, handler, null, true, result);
    LOGGER.info("Org closure table was successfully recomputed (not committed yet); all {} organizations processed", orgsTotal);
    if (check) {
        LOGGER.info("Reading from recomputed org closure table");
        NativeQuery selectQuery = session.createNativeQuery("SELECT descendant_oid, ancestor_oid, val from " + CLOSURE_TABLE_NAME).addScalar("descendant_oid", StringType.INSTANCE).addScalar("ancestor_oid", StringType.INSTANCE).addScalar("val", IntegerType.INSTANCE);
        List recomputedEntries = selectQuery.list();
        LOGGER.info("{} entries read", recomputedEntries.size());
        compareOrgClosureTables(existingEntries, recomputedEntries, rebuild, result);
    } else {
        result.recordSuccess();
    }
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) NativeQuery(org.hibernate.query.NativeQuery) OrgType(com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Collections.singletonList(java.util.Collections.singletonList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler)

Example 22 with ResultHandler

use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.

the class TestLdap method test800BigLdapSearch.

@Test
public void test800BigLdapSearch() throws Exception {
    // GIVEN
    assertUsers(NUM_INITIAL_USERS + 1);
    loadLdapEntries("a", NUM_LDAP_ENTRIES);
    Task task = getTestTask();
    task.setOwner(getUser(USER_ADMINISTRATOR_OID));
    OperationResult result = task.getResult();
    ObjectQuery query = ObjectQueryUtil.createResourceAndObjectClassQuery(RESOURCE_OPENDJ_OID, new QName(RESOURCE_OPENDJ_NAMESPACE, "inetOrgPerson"), prismContext);
    final MutableInt count = new MutableInt(0);
    ResultHandler<ShadowType> handler = new ResultHandler<ShadowType>() {

        @Override
        public boolean handle(PrismObject<ShadowType> shadow, OperationResult parentResult) {
            count.increment();
            display("Found", shadow);
            return true;
        }
    };
    // WHEN
    when();
    modelService.searchObjectsIterative(ShadowType.class, query, handler, null, task, result);
    // THEN
    then();
    result.computeStatus();
    TestUtil.assertSuccess(result);
    // THEN
    then();
    assertEquals("Unexpected number of search results", NUM_LDAP_ENTRIES + 8, count.intValue());
    assertUsers(NUM_INITIAL_USERS + 1);
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) QName(javax.xml.namespace.QName) MutableInt(org.apache.commons.lang3.mutable.MutableInt) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) Test(org.testng.annotations.Test)

Example 23 with ResultHandler

use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.

the class TestLdap method assertOpenDjAccountShadows.

private void assertOpenDjAccountShadows(int expected, boolean raw, Task task, OperationResult result) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    ObjectQuery query = ObjectQueryUtil.createResourceAndObjectClassQuery(RESOURCE_OPENDJ_OID, new QName(RESOURCE_OPENDJ_NAMESPACE, "inetOrgPerson"), prismContext);
    final MutableInt count = new MutableInt(0);
    ResultHandler<ShadowType> handler = new ResultHandler<ShadowType>() {

        @Override
        public boolean handle(PrismObject<ShadowType> shadow, OperationResult parentResult) {
            count.increment();
            display("Found", shadow);
            return true;
        }
    };
    Collection<SelectorOptions<GetOperationOptions>> options = null;
    if (raw) {
        options = SelectorOptions.createCollection(GetOperationOptions.createRaw());
    }
    modelService.searchObjectsIterative(ShadowType.class, query, handler, options, task, result);
    assertEquals("Unexpected number of search results (raw=" + raw + ")", expected, count.intValue());
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) QName(javax.xml.namespace.QName) MutableInt(org.apache.commons.lang3.mutable.MutableInt) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery)

Example 24 with ResultHandler

use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.

the class SearchProducerWorker method run.

@Override
public void run() {
    Log log = context.getLog();
    try {
        GetOperationOptionsBuilder optionsBuilder = context.getSchemaService().getOperationOptionsBuilder();
        if (options.isRaw()) {
            optionsBuilder = optionsBuilder.raw();
        }
        optionsBuilder = NinjaUtils.addIncludeOptionsForExport(optionsBuilder, type.getClassDefinition());
        ResultHandler<?> handler = (object, parentResult) -> {
            try {
                queue.put(object.asObjectable());
            } catch (InterruptedException ex) {
                log.error("Couldn't queue object {}, reason: {}", ex, object, ex.getMessage());
            }
            return true;
        };
        RepositoryService repository = context.getRepository();
        repository.searchObjectsIterative(type.getClassDefinition(), query, handler, optionsBuilder.build(), true, operation.getResult());
    } catch (SchemaException ex) {
        log.error("Unexpected exception, reason: {}", ex, ex.getMessage());
    } catch (NinjaException ex) {
        log.error(ex.getMessage(), ex);
    } finally {
        markDone();
        if (isWorkersDone()) {
            if (!operation.isFinished()) {
                operation.producerFinish();
            }
        }
    }
}
Also used : ResultHandler(com.evolveum.midpoint.schema.ResultHandler) ExportOptions(com.evolveum.midpoint.ninja.opts.ExportOptions) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) OperationStatus(com.evolveum.midpoint.ninja.util.OperationStatus) NinjaUtils(com.evolveum.midpoint.ninja.util.NinjaUtils) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) BlockingQueue(java.util.concurrent.BlockingQueue) NinjaContext(com.evolveum.midpoint.ninja.impl.NinjaContext) GetOperationOptionsBuilder(com.evolveum.midpoint.schema.GetOperationOptionsBuilder) List(java.util.List) Log(com.evolveum.midpoint.ninja.util.Log) ObjectTypes(com.evolveum.midpoint.schema.constants.ObjectTypes) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Log(com.evolveum.midpoint.ninja.util.Log) NinjaException(com.evolveum.midpoint.ninja.impl.NinjaException) GetOperationOptionsBuilder(com.evolveum.midpoint.schema.GetOperationOptionsBuilder) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService)

Example 25 with ResultHandler

use of com.evolveum.midpoint.schema.ResultHandler in project midpoint by Evolveum.

the class ResourceObjectReferenceResolver method resolve.

/**
 * Resolves a {@link ResourceObjectReferenceType}.
 *
 * @param useRawDefinition If true, object class definition is used (instead of object type definition).
 * This is to avoid endless recursion when resolving the base context for object type.
 */
@Nullable
PrismObject<ShadowType> resolve(@NotNull ProvisioningContext ctx, @NotNull ResourceObjectReferenceType resourceObjectReference, boolean useRawDefinition, @NotNull String desc, @NotNull OperationResult result) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    ObjectReferenceType shadowRef = resourceObjectReference.getShadowRef();
    if (shadowRef != null && shadowRef.getOid() != null) {
        if (resourceObjectReference.getResolutionFrequency() == null || resourceObjectReference.getResolutionFrequency() == ResourceObjectReferenceResolutionFrequencyType.ONCE) {
            PrismObject<ShadowType> shadow = repositoryService.getObject(ShadowType.class, shadowRef.getOid(), null, result);
            shadowsFacade.applyDefinition(shadow, ctx.getTask(), result);
            return shadow;
        }
    } else if (resourceObjectReference.getResolutionFrequency() == ResourceObjectReferenceResolutionFrequencyType.NEVER) {
        throw new ObjectNotFoundException("No shadowRef OID in " + desc + " and resolution frequency set to NEVER");
    }
    argCheck(resourceObjectReference.getObjectClass() != null, "No object class name in object reference in %s", desc);
    QName objectClassName = QNameUtil.qualifyIfNeeded(resourceObjectReference.getObjectClass(), MidPointConstants.NS_RI);
    ProvisioningContext subCtx = useRawDefinition ? ctx.spawnForObjectClassWithRawDefinition(objectClassName) : ctx.spawnForObjectClass(objectClassName);
    subCtx.assertDefinition();
    ObjectQuery refQuery = prismContext.getQueryConverter().createObjectQuery(ShadowType.class, resourceObjectReference.getFilter());
    // No variables. At least not now. We expect that mostly constants will be used here.
    VariablesMap variables = new VariablesMap();
    ObjectQuery evaluatedRefQuery = ExpressionUtil.evaluateQueryExpressions(refQuery, variables, MiscSchemaUtil.getExpressionProfile(), expressionFactory, prismContext, desc, ctx.getTask(), result);
    ObjectFilter baseFilter = ObjectQueryUtil.createResourceAndObjectClassFilter(ctx.getResource().getOid(), objectClassName, prismContext);
    ObjectFilter filter = prismContext.queryFactory().createAnd(baseFilter, evaluatedRefQuery.getFilter());
    ObjectQuery query = prismContext.queryFactory().createQuery(filter);
    // TODO: implement "repo" search strategies, don't forget to apply definitions
    Holder<PrismObject<ShadowType>> shadowHolder = new Holder<>();
    ResultHandler<ShadowType> handler = (shadow, objResult) -> {
        if (shadowHolder.getValue() != null) {
            throw new IllegalStateException("More than one search results for " + desc);
        }
        shadowHolder.setValue(shadow);
        return true;
    };
    shadowsFacade.searchObjectsIterative(subCtx, query, null, handler, result);
    return shadowHolder.getValue();
}
Also used : ResourceAttributeDefinition(com.evolveum.midpoint.schema.processor.ResourceAttributeDefinition) Autowired(org.springframework.beans.factory.annotation.Autowired) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ResourceAttribute(com.evolveum.midpoint.schema.processor.ResourceAttribute) ConnectorInstance(com.evolveum.midpoint.provisioning.ucf.api.ConnectorInstance) QNameUtil(com.evolveum.midpoint.util.QNameUtil) MiscSchemaUtil(com.evolveum.midpoint.schema.util.MiscSchemaUtil) ObjectQueryUtil(com.evolveum.midpoint.schema.util.ObjectQueryUtil) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) Holder(com.evolveum.midpoint.util.Holder) PrismProperty(com.evolveum.midpoint.prism.PrismProperty) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) Collection(java.util.Collection) GenericFrameworkException(com.evolveum.midpoint.provisioning.ucf.api.GenericFrameworkException) Nullable(org.jetbrains.annotations.Nullable) ExpressionFactory(com.evolveum.midpoint.repo.common.expression.ExpressionFactory) ReadCapabilityType(com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.ReadCapabilityType) ShadowsFacade(com.evolveum.midpoint.provisioning.impl.shadows.ShadowsFacade) ExpressionUtil(com.evolveum.midpoint.repo.common.expression.ExpressionUtil) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) QName(javax.xml.namespace.QName) NotNull(org.jetbrains.annotations.NotNull) ResourceObjectDefinition(com.evolveum.midpoint.schema.processor.ResourceObjectDefinition) com.evolveum.midpoint.xml.ns._public.common.common_3(com.evolveum.midpoint.xml.ns._public.common.common_3) PrismContainer(com.evolveum.midpoint.prism.PrismContainer) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Trace(com.evolveum.midpoint.util.logging.Trace) PrettyPrinter(com.evolveum.midpoint.util.PrettyPrinter) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) ArrayList(java.util.ArrayList) GenericConnectorException(com.evolveum.midpoint.provisioning.api.GenericConnectorException) ProvisioningContext(com.evolveum.midpoint.provisioning.impl.ProvisioningContext) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) PrismContext(com.evolveum.midpoint.prism.PrismContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) RepositoryService(com.evolveum.midpoint.repo.api.RepositoryService) Containerable(com.evolveum.midpoint.prism.Containerable) AttributesToReturn(com.evolveum.midpoint.provisioning.ucf.api.AttributesToReturn) PrismObject(com.evolveum.midpoint.prism.PrismObject) ResourceObjectIdentification(com.evolveum.midpoint.schema.processor.ResourceObjectIdentification) Component(org.springframework.stereotype.Component) MidPointConstants(com.evolveum.midpoint.schema.constants.MidPointConstants) ShadowUtil(com.evolveum.midpoint.schema.util.ShadowUtil) MiscUtil.argCheck(com.evolveum.midpoint.util.MiscUtil.argCheck) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) TraceManager(com.evolveum.midpoint.util.logging.TraceManager) ShadowManager(com.evolveum.midpoint.provisioning.impl.shadows.manager.ShadowManager) QName(javax.xml.namespace.QName) Holder(com.evolveum.midpoint.util.Holder) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) ProvisioningContext(com.evolveum.midpoint.provisioning.impl.ProvisioningContext) PrismObject(com.evolveum.midpoint.prism.PrismObject) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) VariablesMap(com.evolveum.midpoint.schema.expression.VariablesMap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ResultHandler (com.evolveum.midpoint.schema.ResultHandler)54 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)53 PrismObject (com.evolveum.midpoint.prism.PrismObject)39 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)33 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)29 Test (org.testng.annotations.Test)29 ArrayList (java.util.ArrayList)24 SystemException (com.evolveum.midpoint.util.exception.SystemException)23 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)22 Task (com.evolveum.midpoint.task.api.Task)19 QName (javax.xml.namespace.QName)19 DirtiesContext (org.springframework.test.annotation.DirtiesContext)19 ContextConfiguration (org.springframework.test.context.ContextConfiguration)19 AssertJUnit (org.testng.AssertJUnit)17 RepositoryService (com.evolveum.midpoint.repo.api.RepositoryService)16 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)16 List (java.util.List)16 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)15 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)14 com.evolveum.midpoint.xml.ns._public.common.common_3 (com.evolveum.midpoint.xml.ns._public.common.common_3)13