Tuesday, October 30, 2012

Spring's LDAP And/Or Filter Combo

I was just working with Spring's LDAP filters and came into an issue good ol Google couldn't help me with. You have the powerful AndFilter and OrFilter, and all the tutorials explicitly show how to use one or the other. But what if you want to use both?

I was able to solve it with this, if there is an easier way please comment.

public List findUsers(boolean lockedOut, boolean signedAgreement) {
  AndFilter andFilter = new AndFilter();
  OrFilter orFilter = new OrFilter();

  andFilter.and(new EqualsFilter("lockedOut", lockedOut);

  if(signedAgreement) {
    andFilter.and(new EqualsFilter("signedAgreement", signedAgreement);
  } else {
    orFilter.or(new EqualsFilter("signedAgreement", signedAgreement);
    orFilter.or(new NotPresentFilter("signedAgreement");
The main part is the signedAgreement, if its true we just want to add it to the filter to check for the property to be true in LDAP, but if we want to check for False, we want to check if its False OR if its not present at all. This allows us to have both the AND and OR filter.

No comments:

Post a Comment