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;
}
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;
}
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());
}
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());
}
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());
}
Aggregations