From f9c248da4f53bc10fdbf991e6c60e9dde3d5e53e Mon Sep 17 00:00:00 2001 From: Yuriy Movchan Date: Fri, 1 Feb 2019 16:55:38 +0300 Subject: [PATCH] Add options to use "numsubordinates" to get count sub entries --- .../ldap/persistence/LdapEntryManager.java | 45 +++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/oxLdap/src/main/java/org/gluu/site/ldap/persistence/LdapEntryManager.java b/oxLdap/src/main/java/org/gluu/site/ldap/persistence/LdapEntryManager.java index 4a69e133..99971cad 100644 --- a/oxLdap/src/main/java/org/gluu/site/ldap/persistence/LdapEntryManager.java +++ b/oxLdap/src/main/java/org/gluu/site/ldap/persistence/LdapEntryManager.java @@ -738,15 +738,17 @@ public int countEntries(Object entry) { } public int countEntries(String baseDN, Class entryClass, Filter filter) { + return countEntries(baseDN, entryClass, filter, null); + } + + public int countEntries(String baseDN, Class 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; @@ -755,16 +757,43 @@ public int countEntries(String baseDN, Class entryClass, Filter filter) { } else { searchFilter = filter; } - - CountBatchOperation batchOperation = new CountBatchOperation(this); + SearchScope searchScope = scope; + if (searchScope == null) { + searchScope = SearchScope.SUB; + } + + String[] ldapReturnAttributes; + CountBatchOperation 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(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 Filter createFilterByEntry(Object entry, Class entryClass, List attributes) {