use of com.evolveum.midpoint.xml.ns._public.resource.capabilities_3.CountObjectsCapabilityType in project midpoint by Evolveum.
the class ShadowCache method countObjects.
public Integer countObjects(ObjectQuery query, Task task, final OperationResult result) throws SchemaException, ObjectNotFoundException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
ResourceShadowDiscriminator coordinates = ObjectQueryUtil.getCoordinates(query.getFilter());
final ProvisioningContext ctx = ctxFactory.create(coordinates, null, result);
ctx.assertDefinition();
applyDefinition(ctx, query);
RefinedObjectClassDefinition objectClassDef = ctx.getObjectClassDefinition();
ResourceType resourceType = ctx.getResource();
CountObjectsCapabilityType countObjectsCapabilityType = objectClassDef.getEffectiveCapability(CountObjectsCapabilityType.class);
if (countObjectsCapabilityType == null) {
// Unable to count. Return null which means "I do not know"
result.recordNotApplicableIfUnknown();
return null;
} else {
CountObjectsSimulateType simulate = countObjectsCapabilityType.getSimulate();
if (simulate == null) {
// We have native capability
ConnectorInstance connector = ctx.getConnector(ReadCapabilityType.class, result);
try {
ObjectQuery attributeQuery = createAttributeQuery(query);
int count;
try {
count = connector.count(objectClassDef.getObjectClassDefinition(), attributeQuery, objectClassDef.getPagedSearches(), ctx, result);
} catch (CommunicationException | GenericFrameworkException | SchemaException | UnsupportedOperationException e) {
result.recordFatalError(e);
throw e;
}
result.computeStatus();
result.cleanupResult();
return count;
} catch (GenericFrameworkException | UnsupportedOperationException e) {
SystemException ex = new SystemException("Couldn't count objects on resource " + resourceType + ": " + e.getMessage(), e);
result.recordFatalError(ex);
throw ex;
}
} else if (simulate == CountObjectsSimulateType.PAGED_SEARCH_ESTIMATE) {
if (!objectClassDef.isPagedSearchEnabled()) {
throw new ConfigurationException("Configured count object capability to be simulated using a paged search but paged search capability is not present");
}
final Holder<Integer> countHolder = new Holder<Integer>(0);
final ShadowHandler<ShadowType> handler = new ShadowHandler<ShadowType>() {
@Override
public boolean handle(ShadowType object) {
int count = countHolder.getValue();
count++;
countHolder.setValue(count);
return true;
}
};
query = query.clone();
ObjectPaging paging = ObjectPaging.createEmptyPaging();
paging.setMaxSize(1);
query.setPaging(paging);
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(new ItemPath(ShadowType.F_ASSOCIATION), GetOperationOptions.createRetrieve(RetrieveOption.EXCLUDE));
SearchResultMetadata resultMetadata;
try {
resultMetadata = searchObjectsIterative(query, options, handler, false, task, result);
} catch (SchemaException | ObjectNotFoundException | ConfigurationException | SecurityViolationException e) {
result.recordFatalError(e);
throw e;
}
result.computeStatus();
result.cleanupResult();
return resultMetadata.getApproxNumberOfAllResults();
} else if (simulate == CountObjectsSimulateType.SEQUENTIAL_SEARCH) {
// traditional way of counting objects (i.e. counting them one
// by one)
final Holder<Integer> countHolder = new Holder<Integer>(0);
final ShadowHandler<ShadowType> handler = new ShadowHandler<ShadowType>() {
@Override
public boolean handle(ShadowType object) {
int count = countHolder.getValue();
count++;
countHolder.setValue(count);
return true;
}
};
Collection<SelectorOptions<GetOperationOptions>> options = SelectorOptions.createCollection(new ItemPath(ShadowType.F_ASSOCIATION), GetOperationOptions.createRetrieve(RetrieveOption.EXCLUDE));
searchObjectsIterative(query, options, handler, false, task, result);
// TODO: better error handling
result.computeStatus();
result.cleanupResult();
return countHolder.getValue();
} else {
throw new IllegalArgumentException("Unknown count capability simulate type " + simulate);
}
}
}
Aggregations