2
votes

How to properly get sorted records from LDAP directory. Loading all to memory and then sorting is time consuming because of big amount of data. That is why i want (with Spring Boot/SpringLDAP) get already sorted records from LDAP.

I tried to create in @Repository

@Repository
public interface XxxRepository extends LdapRepository<Xxx>, Serializable {

   List<Xxx> findAllByOrderByNameAsc(LdapQuery ldapQuery);

}

It does not work, NullPointerException.

My @Entity looks like:

@Entry(base = "", objectClasses = {"xxx", "xxx"})
public class Xxx implements Serializable {
   @Id
   private Name dn;

   @Attribute(name = "name")
   @DnAttribute(value = "name", index = 0)
   private String name;

   .
   .
   .
}

My @Service

public void search() {
   String filterConditions = "(&(objectClass=Xxx)";
   filterConditions += "(name=*)";
   LdapQuery query = query().base(BASE_UNIT).filter(filterConditions + ")");
   List<Xxx> xxx= xxxRepository.findAllByOrderByNameAsc(query);
}
1
FYI this is nothing at all to do with the JPA API - user3973283

1 Answers

0
votes

I found answer here: Spring-LDAP LdapTemplateSortedSearchITest

public void testSearch_SortControl_ConvenienceMethod() {
    SortControlDirContextProcessor requestControl;

    // Prepare for first search
    requestControl = new SortControlDirContextProcessor("cn");
    tested.search(BASE, FILTER_STRING, searchControls, callbackHandler,
            requestControl);
    int resultCode = requestControl.getResultCode();
    boolean sorted = requestControl.isSorted();
    assertThat("Search result should have been sorted: " + resultCode, sorted).isTrue();
    List list = callbackHandler.getList();
    assertSortedList(list);
}

It uses SortControlDirContextProcessor.