ldapSearch

Purpose

This method, when given a firstName, lastName, and userId, will search for and return user matches in place of a normal LDAP search.

Applies To

LDAP

Signature

public TreeMap ldapSearch(String firstName,
                               String lastName,
                               String userId,
                               boolean exactMatch,
                               String sortBy);

Notes

The exactMatch specifies if the match is exact, the sortBy can be by "last", "first", "userId" or "" signifying how the returned TreeMap should be sorted. The values in the returned TreeMap must be HashMap's with the name/value pairs used by ldap. The names can be retrived from the Configuration.properties. The names may include wildcards (*), and this method is only called if ldapOverrive() returns true.

Example

public TreeMap ldapSearch (String firstName, 
                           String lastName, 
                           String userId, 
                           boolean exactMatch, 
                           String sortBy)
{
    TreeMap sortedResults = new TreeMap();
    boolean extraFilter = false;
    String extraFilterString = "";
    ArrayList searchAttrs = new ArrayList();
    ArrayList searchVals = new ArrayList();
    
    if (TextManager.isStringVisible(firstName)) {
        searchAttrs.add(Z.lu.LDAP_GIVENNAME);
        searchVals.add(firstName);
    }
    if (TextManager.isStringVisible(lastName)) {
        searchAttrs.add(Z.lu.LDAP_SURNAME);
        searchVals.add(lastName);
    }
    if (TextManager.isStringVisible(userId)) {
        searchAttrs.add(Z.lu.LDAP_PRIMARYKEY);
        searchVals.add(userId);
    }
    if (searchAttrs.size() == 0) {
        // We should never fall into this else but if we do here is an alert
        Z.log.writeToLog(Z.log.ERROR, "UC: ldapSearch: " +
            "Cannot seach LDAP with null information");
        return sortedResults;
    }
    
    extraFilterString = Z.config.getConfigValue("LDAP_SEARCH_FILTER");
    
    if (TextManager.isStringVisible(extraFilterString)) {
        extraFilterString.trim();
        extraFilter = true;
    }
    
    // Setting up the filter
    StringBuffer filter = new StringBuffer();
    
    if (searchAttrs.size() == 1 && ! extraFilter)
    {
        // a = b
        filter.append("(");
        filter.append((String)searchAttrs.get(0));
        filter.append("=");
        filter.append((String)searchVals.get(0));
    
        if (! exactMatch) {
            filter.append("*");
        }
    
        filter.append(")");
    }
    else
    {
        // (&(a=b)(c=d)(e=f))
        filter.append("(&");
    
        if (extraFilter) {
            filter.append(extraFilterString);
        }
    
        for (int x = 0; x  0) { tmp.append(", "); }
                        tmp.append(((String) nEnum.next()).trim());
                    }
                }
                
                if (TextManager.isStringInvisible(tmp.toString()) ) {
                    tmp = new StringBuffer(LdapUtil.NA);
                }
                if (Z.lu.LDAP_PRIMARYKEY.equals(key)) {
                    nameVals.put(key,tmp.toString().toUpperCase());
                } else {
                    nameVals.put(key,tmp.toString());
                }
            }
    
            sortedResults.put(TextManager.catStrings((String)nameVals.get(sortKey1),
                " ", (String)nameVals.get(sortKey2),
                " ", (String)nameVals.get(sortKey3)), nameVals
            );
        }
    
        results.close();
        ctx.close();
        
    } catch (javax.naming.PartialResultException pre) {
        // Don't throw and exception here...this is for MS Active Server stuff
        // throwing this exception probably because of a referral problem...
        // but in any case the results are OK according to the data retrieved
        Z.log.writeToLog(Z.log.WARN, "Partial Result Exception: " + pre.toString());
        
    } catch (Exception e) {
        Z.log.writeToLog(Z.log.ERROR, "UC: ldapSearch Exception: " + e);
        ErrorWriter.write(e, ErrorWriter.LOGERR);
    }
    
    return sortedResults;
}