Skip to content

Commit

Permalink
Add options to use "numsubordinates" to get count sub entries
Browse files Browse the repository at this point in the history
  • Loading branch information
yurem committed Feb 27, 2019
1 parent ab39c60 commit f9c248d
Showing 1 changed file with 37 additions and 8 deletions.
Expand Up @@ -738,15 +738,17 @@ public <T> int countEntries(Object entry) {
}

public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter) {
return countEntries(baseDN, entryClass, filter, null);
}

public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope) {
if (StringHelper.isEmptyString(baseDN)) {
throw new MappingException("Base DN to find entries is null");
throw new MappingException("Base DN to count entries is null");
}

// Check entry class
checkEntryClass(entryClass, false);
String[] objectClasses = getTypeObjectClasses(entryClass);
String[] ldapReturnAttributes = new String[] { "" }; // Don't load
// attributes

// Find entries
Filter searchFilter;
Expand All @@ -755,16 +757,43 @@ public <T> int countEntries(String baseDN, Class<T> entryClass, Filter filter) {
} else {
searchFilter = filter;
}

CountBatchOperation<T> batchOperation = new CountBatchOperation<T>(this);

SearchScope searchScope = scope;
if (searchScope == null) {
searchScope = SearchScope.SUB;
}

String[] ldapReturnAttributes;
CountBatchOperation<T> batchOperation;
if (SearchScope.BASE == searchScope) {
ldapReturnAttributes = new String[] { "numsubordinates" }; // Don't load attributes
batchOperation = null;
} else {
ldapReturnAttributes = new String[] { "" }; // Don't load attributes
batchOperation = new CountBatchOperation<T>(this);
}

SearchResult searchResult;
try {
ldapOperationService.search(baseDN, searchFilter, SearchScope.SUB, batchOperation, 0, 100, 0, null, ldapReturnAttributes);
searchResult = ldapOperationService.search(baseDN, searchFilter, searchScope, batchOperation, 0, 100, 0, null, ldapReturnAttributes);
} catch (Exception ex) {
throw new EntryPersistenceException(String.format("Failed to calucalte count of entries with baseDN: %s, filter: %s", baseDN, searchFilter), ex);
throw new EntryPersistenceException(String.format("Failed to calucalte count of entries with baseDN: %s, scope: %s, filter: %s", baseDN, searchScope, searchFilter), ex);
}

return batchOperation.getCountEntries();
if (SearchScope.BASE != searchScope) {
return batchOperation.getCountEntries();
}

if (searchResult.getEntryCount() != 1) {
throw new EntryPersistenceException(String.format("Failed to calucalte count of entries due to missing result entry with baseDN: %s, filter: %s", baseDN, searchFilter));
}

Long result = searchResult.getSearchEntries().get(0).getAttributeValueAsLong("numsubordinates");
if (result == null) {
throw new EntryPersistenceException(String.format("Failed to calucalte count of entries due to missing attribute 'numsubordinates' with baseDN: %s, filter: %s", baseDN, searchFilter));
}

return result.intValue();
}

private <T> Filter createFilterByEntry(Object entry, Class<T> entryClass, List<AttributeData> attributes) {
Expand Down

0 comments on commit f9c248d

Please sign in to comment.