Search in sources :

Example 56 with ObjectQuery

use of com.evolveum.midpoint.prism.query.ObjectQuery in project midpoint by Evolveum.

the class ShadowManager method lookupShadowInRepository.

/**
	 * Locates the appropriate Shadow in repository that corresponds to the
	 * provided resource object.
	 *
	 * DEAD flag is cleared - in memory as well as in repository.
	 * 
	 * @param parentResult
	 * 
	 * @return current shadow object that corresponds to provided
	 *         resource object or null if the object does not exist
	 */
public PrismObject<ShadowType> lookupShadowInRepository(ProvisioningContext ctx, PrismObject<ShadowType> resourceShadow, OperationResult parentResult) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
    ObjectQuery query = createSearchShadowQuery(ctx, resourceShadow, prismContext, parentResult);
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Searching for shadow using filter:\n{}", query.debugDump());
    }
    //		PagingType paging = new PagingType();
    // TODO: check for errors
    List<PrismObject<ShadowType>> results = repositoryService.searchObjects(ShadowType.class, query, null, parentResult);
    MiscSchemaUtil.reduceSearchResult(results);
    LOGGER.trace("lookupShadow found {} objects", results.size());
    if (results.size() == 0) {
        return null;
    }
    if (results.size() > 1) {
        for (PrismObject<ShadowType> result : results) {
            LOGGER.trace("Search result:\n{}", result.debugDump());
        }
        LOGGER.error("More than one shadow found for " + resourceShadow);
        // TODO: Better error handling later
        throw new IllegalStateException("More than one shadow found for " + resourceShadow);
    }
    PrismObject<ShadowType> shadow = results.get(0);
    checkConsistency(shadow);
    if (Boolean.TRUE.equals(shadow.asObjectable().isDead())) {
        LOGGER.debug("Repository shadow {} is marked as dead - resetting the flag", ObjectTypeUtil.toShortString(shadow));
        shadow.asObjectable().setDead(false);
        List<ItemDelta<?, ?>> deltas = DeltaBuilder.deltaFor(ShadowType.class, prismContext).item(ShadowType.F_DEAD).replace().asItemDeltas();
        try {
            repositoryService.modifyObject(ShadowType.class, shadow.getOid(), deltas, parentResult);
        } catch (ObjectAlreadyExistsException e) {
            throw new SystemException("Unexpected exception when resetting 'dead' flag: " + e.getMessage(), e);
        }
    }
    return shadow;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) SystemException(com.evolveum.midpoint.util.exception.SystemException) ItemDelta(com.evolveum.midpoint.prism.delta.ItemDelta) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) ObjectAlreadyExistsException(com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)

Example 57 with ObjectQuery

use of com.evolveum.midpoint.prism.query.ObjectQuery in project midpoint by Evolveum.

the class ShadowManager method lookupShadowsBySecondaryIdentifiers.

private List<PrismObject<ShadowType>> lookupShadowsBySecondaryIdentifiers(ProvisioningContext ctx, Collection<ResourceAttribute<?>> secondaryIdentifiers, OperationResult parentResult) throws SchemaException, ConfigurationException, ObjectNotFoundException, CommunicationException, ExpressionEvaluationException {
    if (secondaryIdentifiers.size() < 1) {
        LOGGER.trace("Shadow does not contain secondary identifier. Skipping lookup shadows according to name.");
        return null;
    }
    S_FilterEntry q = QueryBuilder.queryFor(ShadowType.class, prismContext).block();
    for (ResourceAttribute<?> secondaryIdentifier : secondaryIdentifiers) {
        // There may be identifiers that come from associations and they will have parent set to association/identifiers
        // For the search to succeed we need all attribute to have "attributes" parent path.
        secondaryIdentifier = ShadowUtil.fixAttributePath(secondaryIdentifier);
        q = q.item(secondaryIdentifier.getPath(), secondaryIdentifier.getDefinition()).eq(getNormalizedValue(secondaryIdentifier, ctx.getObjectClassDefinition())).or();
    }
    ObjectQuery query = q.none().endBlock().and().item(ShadowType.F_RESOURCE_REF).ref(ctx.getResourceOid()).build();
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("Searching for shadow using filter on secondary identifier:\n{}", query.debugDump());
    }
    // TODO: check for errors
    List<PrismObject<ShadowType>> results = repositoryService.searchObjects(ShadowType.class, query, null, parentResult);
    MiscSchemaUtil.reduceSearchResult(results);
    LOGGER.trace("lookupShadow found {} objects", results.size());
    if (LOGGER.isTraceEnabled() && results.size() == 1) {
        LOGGER.trace("lookupShadow found\n{}", results.get(0).debugDump(1));
    }
    return results;
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) S_FilterEntry(com.evolveum.midpoint.prism.query.builder.S_FilterEntry) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery)

Example 58 with ObjectQuery

use of com.evolveum.midpoint.prism.query.ObjectQuery in project midpoint by Evolveum.

the class AbstractDummyTest method checkConsistency.

protected void checkConsistency(PrismObject<? extends ShadowType> object) throws SchemaException {
    OperationResult result = new OperationResult(TestDummyNegative.class.getName() + ".checkConsistency");
    PrismPropertyDefinition itemDef = ShadowUtil.getAttributesContainer(object).getDefinition().findAttributeDefinition(SchemaConstants.ICFS_NAME);
    LOGGER.info("item definition: {}", itemDef.debugDump());
    //TODO: matching rule
    ObjectQuery query = QueryBuilder.queryFor(ShadowType.class, prismContext).itemWithDef(itemDef, ShadowType.F_ATTRIBUTES, itemDef.getName()).eq(getWillRepoIcfName()).build();
    System.out.println("Looking for shadows of \"" + getWillRepoIcfName() + "\" with filter " + query.debugDump());
    display("Looking for shadows of \"" + getWillRepoIcfName() + "\" with filter " + query.debugDump());
    List<PrismObject<ShadowType>> objects = repositoryService.searchObjects(ShadowType.class, query, null, result);
    assertEquals("Wrong number of repo shadows for ICF NAME \"" + getWillRepoIcfName() + "\"", 1, objects.size());
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) PrismPropertyDefinition(com.evolveum.midpoint.prism.PrismPropertyDefinition) ShadowType(com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery)

Example 59 with ObjectQuery

use of com.evolveum.midpoint.prism.query.ObjectQuery 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)

Example 60 with ObjectQuery

use of com.evolveum.midpoint.prism.query.ObjectQuery in project midpoint by Evolveum.

the class ImportRefTest method test010GoodRefImport.

@Test
public void test010GoodRefImport() throws Exception {
    final String TEST_NAME = "test010GoodRefImport";
    TestUtil.displayTestTile(this, TEST_NAME);
    // GIVEN
    Task task = taskManager.createTaskInstance();
    OperationResult result = new OperationResult(ImportRefTest.class.getName() + "." + TEST_NAME);
    FileInputStream stream = new FileInputStream(IMPORT_FILE_NAME);
    // WHEN
    modelService.importObjectsFromStream(stream, getDefaultImportOptions(), task, result);
    // THEN
    result.computeStatus("Failed import.");
    display("Result after good import", result);
    TestUtil.assertSuccessOrWarning("Import has failed (result)", result, 2);
    //		EqualsFilter equal = EqualsFilter.createEqual(UserType.F_NAME, UserType.class, PrismTestUtil.getPrismContext(), null, "jack");
    //		ObjectQuery query = ObjectQuery.createObjectQuery(equal);
    ObjectQuery query = ObjectQueryUtil.createNameQuery("jack", PrismTestUtil.getPrismContext());
    List<PrismObject<UserType>> users = repositoryService.searchObjects(UserType.class, query, null, result);
    assertNotNull(users);
    assertEquals("Search retuned unexpected results", 1, users.size());
    UserType jack = users.get(0).asObjectable();
    assertNotNull(jack);
    PrismAsserts.assertEqualsPolyString("wrong givenName", "Jack", jack.getGivenName());
    PrismAsserts.assertEqualsPolyString("wrong familyName", "Sparrow", jack.getFamilyName());
    PrismAsserts.assertEqualsPolyString("wrong fullName", "Cpt. Jack Sparrow", jack.getFullName());
}
Also used : PrismObject(com.evolveum.midpoint.prism.PrismObject) Task(com.evolveum.midpoint.task.api.Task) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) ObjectQuery(com.evolveum.midpoint.prism.query.ObjectQuery) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test) AbstractConfiguredModelIntegrationTest(com.evolveum.midpoint.model.intest.AbstractConfiguredModelIntegrationTest)

Aggregations

ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)697 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)453 Test (org.testng.annotations.Test)335 PrismObject (com.evolveum.midpoint.prism.PrismObject)284 Task (com.evolveum.midpoint.task.api.Task)268 QName (javax.xml.namespace.QName)111 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)78 SearchResultMetadata (com.evolveum.midpoint.schema.SearchResultMetadata)76 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)64 ArrayList (java.util.ArrayList)61 ObjectPaging (com.evolveum.midpoint.prism.query.ObjectPaging)58 ShadowType (com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType)53 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)41 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)41 SystemException (com.evolveum.midpoint.util.exception.SystemException)38 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)37 NotNull (org.jetbrains.annotations.NotNull)35 List (java.util.List)33 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)32 ObjectType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType)27