Search in sources :

Example 1 with ResultHandler

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

the class PageAccounts method exportPerformed.

private void exportPerformed(AjaxRequestTarget target) {
    if (resourceModel.getObject() == null) {
        warn(getString("pageAccounts.message.resourceNotSelected"));
        refreshEverything(target);
        return;
    }
    String fileName = "accounts-" + WebComponentUtil.formatDate("yyyy-MM-dd-HH-mm-ss", new Date()) + ".xml";
    OperationResult result = new OperationResult(OPERATION_EXPORT);
    Writer writer = null;
    try {
        Task task = createSimpleTask(OPERATION_EXPORT);
        writer = createWriter(fileName);
        writeHeader(writer);
        final Writer handlerWriter = writer;
        ResultHandler handler = new AbstractSummarizingResultHandler() {

            @Override
            protected boolean handleObject(PrismObject object, OperationResult parentResult) {
                OperationResult result = parentResult.createMinorSubresult(OPERATION_EXPORT_ACCOUNT);
                try {
                    String xml = getPrismContext().serializeObjectToString(object, PrismContext.LANG_XML);
                    handlerWriter.write(xml);
                    result.computeStatus();
                } catch (Exception ex) {
                    LoggingUtils.logUnexpectedException(LOGGER, "Couldn't serialize account", ex);
                    result.recordFatalError("Couldn't serialize account.", ex);
                    return false;
                }
                return true;
            }
        };
        try {
            ObjectQuery query = ObjectQuery.createObjectQuery(createResourceAndQueryFilter());
            getModelService().searchObjectsIterative(ShadowType.class, query, handler, SelectorOptions.createCollection(GetOperationOptions.createRaw()), task, result);
        } finally {
            writeFooter(writer);
        }
        result.recomputeStatus();
    } catch (Exception ex) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't export accounts", ex);
        error(getString("PageAccounts.exportException", ex.getMessage()));
    } finally {
        IOUtils.closeQuietly(writer);
    }
    filesModel.reset();
    success(getString("PageAccounts.message.success.export", fileName));
    target.add(getFeedbackPanel(), get(createComponentPath(ID_FORM_ACCOUNT, ID_FILES_CONTAINER)));
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) AbstractSummarizingResultHandler(com.evolveum.midpoint.schema.AbstractSummarizingResultHandler) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) AbstractSummarizingResultHandler(com.evolveum.midpoint.schema.AbstractSummarizingResultHandler) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) Date(java.util.Date) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) RestartResponseException(org.apache.wicket.RestartResponseException) IOException(java.io.IOException) CommonException(com.evolveum.midpoint.util.exception.CommonException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 2 with ResultHandler

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

the class AbstractSearchExpressionEvaluator method executeSearchAttempt.

private <O extends ObjectType> List<V> executeSearchAttempt(final List<PrismObject> rawResult, Class<O> targetTypeClass, final QName targetTypeQName, ObjectQuery query, boolean searchOnResource, boolean tryAlsoRepository, final List<ItemDelta<V, D>> additionalAttributeDeltas, final ExpressionEvaluationContext params, String contextDescription, Task task, OperationResult result) throws ExpressionEvaluationException, ObjectNotFoundException, SchemaException {
    final List<V> list = new ArrayList<V>();
    Collection<SelectorOptions<GetOperationOptions>> options = new ArrayList<>();
    if (!searchOnResource) {
        options.add(SelectorOptions.create(GetOperationOptions.createNoFetch()));
    }
    extendOptions(options, searchOnResource);
    ResultHandler<O> handler = new ResultHandler<O>() {

        @Override
        public boolean handle(PrismObject<O> object, OperationResult parentResult) {
            if (rawResult != null) {
                rawResult.add(object);
            }
            list.add(createPrismValue(object.getOid(), targetTypeQName, additionalAttributeDeltas, params));
            return true;
        }
    };
    try {
        objectResolver.searchIterative(targetTypeClass, query, options, handler, task, result);
    } catch (IllegalStateException e) {
        // this comes from checkConsistence methods
        throw new IllegalStateException(e.getMessage() + " in " + contextDescription, e);
    } catch (SchemaException e) {
        throw new SchemaException(e.getMessage() + " in " + contextDescription, e);
    } catch (SystemException e) {
        throw new SystemException(e.getMessage() + " in " + contextDescription, e);
    } catch (CommunicationException | ConfigurationException | SecurityViolationException e) {
        if (searchOnResource && tryAlsoRepository) {
            options = SelectorOptions.createCollection(GetOperationOptions.createNoFetch());
            try {
                objectResolver.searchIterative(targetTypeClass, query, options, handler, task, result);
            } catch (SchemaException e1) {
                throw new SchemaException(e1.getMessage() + " in " + contextDescription, e1);
            } catch (CommunicationException | ConfigurationException | SecurityViolationException e1) {
                // shadow for group doesn't exist? (MID-2107)
                throw new ExpressionEvaluationException("Unexpected expression exception " + e + ": " + e.getMessage(), e);
            }
        } else {
            throw new ExpressionEvaluationException("Unexpected expression exception " + e + ": " + e.getMessage(), e);
        }
    } catch (ObjectNotFoundException e) {
        throw e;
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Assignment expression resulted in {} objects, using query:\n{}", list.size(), query.debugDump());
    }
    return list;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) ArrayList(java.util.ArrayList) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PrismObject(com.evolveum.midpoint.prism.PrismObject) SystemException(com.evolveum.midpoint.util.exception.SystemException) SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException)

Example 3 with ResultHandler

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

the class AddGetObjectTest method test.

//    @Test(enabled = false)
//    public void deltaOperationSerializationPerformanceTest() throws Exception {
//        List<PrismObject<? extends Objectable>> elements =
//                prismContext.processorFor(new File(FOLDER_BASIC, "objects.xml")).parseObjects();
//
//        //get user from objects.xml
//        ObjectDelta delta = ObjectDelta.createAddDelta(elements.get(0));
//
//        final int COUNT = 10000;
//        //first conversion option
//        System.out.println(DeltaConvertor.toObjectDeltaTypeXml(delta));
//        //second conversion option
//        //System.out.println("\n" + toRepo(DeltaConvertor.toObjectDeltaType(delta), prismContext));
//
//        long time = System.currentTimeMillis();
//        for (int i = 0; i < COUNT; i++) {
//            String xml = DeltaConvertor.toObjectDeltaTypeXml(delta);
//        }
//        time = System.currentTimeMillis() - time;
//        System.out.println(">>> " + time);
//
//        time = System.currentTimeMillis();
//        for (int i = 0; i < COUNT; i++) {
//            ObjectDeltaType type = DeltaConvertor.toObjectDeltaType(delta);
//            String xml = toRepo(type, prismContext);
//        }
//        time = System.currentTimeMillis() - time;
//        System.out.println(">>> " + time);
//    }
@Test
public void test() throws Exception {
    OperationResult result = new OperationResult("asdf");
    final List<PrismObject> objects = new ArrayList<PrismObject>();
    ResultHandler<ObjectType> handler = new ResultHandler<ObjectType>() {

        @Override
        public boolean handle(PrismObject<ObjectType> object, OperationResult parentResult) {
            objects.add(object);
            return true;
        }
    };
    repositoryService.searchObjectsIterative(ObjectType.class, null, handler, null, false, result);
    AssertJUnit.assertTrue(!objects.isEmpty());
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) Test(org.testng.annotations.Test)

Example 4 with ResultHandler

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

the class TestRetirement method reconcileAllOrgs.

private void reconcileAllOrgs() throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
    final Task task = createTask("reconcileAllOrgs");
    OperationResult result = task.getResult();
    ResultHandler<OrgType> handler = new ResultHandler<OrgType>() {

        @Override
        public boolean handle(PrismObject<OrgType> object, OperationResult parentResult) {
            try {
                display("reconciling " + object);
                reconcileOrg(object.getOid(), task, parentResult);
            } catch (SchemaException | PolicyViolationException | ExpressionEvaluationException | ObjectNotFoundException | ObjectAlreadyExistsException | CommunicationException | ConfigurationException | SecurityViolationException e) {
                throw new SystemException(e.getMessage(), e);
            }
            return true;
        }
    };
    display("Reconciling all orgs");
    modelService.searchObjectsIterative(OrgType.class, null, handler, null, task, result);
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) Task(com.evolveum.midpoint.task.api.Task) ExpressionEvaluationException(com.evolveum.midpoint.util.exception.ExpressionEvaluationException) CommunicationException(com.evolveum.midpoint.util.exception.CommunicationException) SecurityViolationException(com.evolveum.midpoint.util.exception.SecurityViolationException) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PrismObject(com.evolveum.midpoint.prism.PrismObject) SystemException(com.evolveum.midpoint.util.exception.SystemException) ConfigurationException(com.evolveum.midpoint.util.exception.ConfigurationException) OrgType(com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType) ObjectNotFoundException(com.evolveum.midpoint.util.exception.ObjectNotFoundException) PolicyViolationException(com.evolveum.midpoint.util.exception.PolicyViolationException) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Example 5 with ResultHandler

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

the class TestSanity method test016ProvisioningSearchAccountsIterative.

@Test
public void test016ProvisioningSearchAccountsIterative() throws Exception {
    TestUtil.displayTestTile("test016ProvisioningSearchAccountsIterative");
    // GIVEN
    OperationResult result = new OperationResult(TestSanity.class.getName() + ".test016ProvisioningSearchAccountsIterative");
    RefinedResourceSchema refinedSchema = RefinedResourceSchemaImpl.getRefinedSchema(resourceTypeOpenDjrepo, prismContext);
    final RefinedObjectClassDefinition refinedAccountDefinition = refinedSchema.getDefaultRefinedDefinition(ShadowKindType.ACCOUNT);
    QName objectClass = refinedAccountDefinition.getObjectClassDefinition().getTypeName();
    ObjectQuery q = ObjectQueryUtil.createResourceAndObjectClassQuery(resourceTypeOpenDjrepo.getOid(), objectClass, prismContext);
    //        ObjectQuery q = QueryConvertor.createObjectQuery(ResourceObjectShadowType.class, query, prismContext);
    final Collection<ObjectType> objects = new HashSet<ObjectType>();
    final MatchingRule caseIgnoreMatchingRule = matchingRuleRegistry.getMatchingRule(StringIgnoreCaseMatchingRule.NAME, DOMUtil.XSD_STRING);
    ResultHandler handler = new ResultHandler<ObjectType>() {

        @Override
        public boolean handle(PrismObject<ObjectType> prismObject, OperationResult parentResult) {
            ObjectType objectType = prismObject.asObjectable();
            objects.add(objectType);
            display("Found object", objectType);
            assertTrue(objectType instanceof ShadowType);
            ShadowType shadow = (ShadowType) objectType;
            assertNotNull(shadow.getOid());
            assertNotNull(shadow.getName());
            assertEquals(RESOURCE_OPENDJ_ACCOUNT_OBJECTCLASS, shadow.getObjectClass());
            assertEquals(RESOURCE_OPENDJ_OID, shadow.getResourceRef().getOid());
            String icfUid = getAttributeValue(shadow, getOpenDjPrimaryIdentifierQName());
            assertNotNull("No ICF UID", icfUid);
            String icfName = getNormalizedAttributeValue(shadow, refinedAccountDefinition, getOpenDjSecondaryIdentifierQName());
            assertNotNull("No ICF NAME", icfName);
            try {
                PrismAsserts.assertEquals("Wrong shadow name", caseIgnoreMatchingRule, shadow.getName().getOrig(), icfName);
            } catch (SchemaException e) {
                throw new IllegalArgumentException(e.getMessage(), e);
            }
            assertNotNull("Missing LDAP uid", getAttributeValue(shadow, new QName(ResourceTypeUtil.getResourceNamespace(resourceTypeOpenDjrepo), "uid")));
            assertNotNull("Missing LDAP cn", getAttributeValue(shadow, new QName(ResourceTypeUtil.getResourceNamespace(resourceTypeOpenDjrepo), "cn")));
            assertNotNull("Missing LDAP sn", getAttributeValue(shadow, new QName(ResourceTypeUtil.getResourceNamespace(resourceTypeOpenDjrepo), "sn")));
            assertNotNull("Missing activation", shadow.getActivation());
            assertNotNull("Missing activation status", shadow.getActivation().getAdministrativeStatus());
            return true;
        }
    };
    // WHEN
    provisioningService.searchObjectsIterative(ShadowType.class, q, null, handler, null, result);
    // THEN
    display("Count", objects.size());
}
Also used : QName(javax.xml.namespace.QName) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ResultHandler(com.evolveum.midpoint.schema.ResultHandler) PrismAsserts.assertEqualsPolyString(com.evolveum.midpoint.prism.util.PrismAsserts.assertEqualsPolyString) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) RefinedObjectClassDefinition(com.evolveum.midpoint.common.refinery.RefinedObjectClassDefinition) ObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType) GenericObjectType(com.evolveum.midpoint.xml.ns._public.common.common_3.GenericObjectType) RefinedResourceSchema(com.evolveum.midpoint.common.refinery.RefinedResourceSchema) StringIgnoreCaseMatchingRule(com.evolveum.midpoint.prism.match.StringIgnoreCaseMatchingRule) MatchingRule(com.evolveum.midpoint.prism.match.MatchingRule) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) AbstractModelIntegrationTest(com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)

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