001: /**
002: * $Id: LdapABSearchTerm.java,v 1.8 2005/09/21 10:47:40 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.addressbook.ldap;
014:
015: import java.util.Vector;
016: import java.util.Properties;
017: import java.util.Enumeration;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020: import java.lang.reflect.Field;
021: import com.sun.addressbook.ABSearchTerm;
022: import com.sun.addressbook.ABStoreException;
023: import com.sun.addressbook.ABLogger;
024:
025: /**
026: * Address Book SearchTerm does the recursive computation of the search
027: * filter from a combination of search terms. It implements the compute
028: * command of the ABSearchTerm.
029: * The search terms can be nested in a tree structure and combined with
030: * an 'and' or an 'or' e.g. "ln=doe & (fn=john | fn=joe)"
031: *
032: */
033: public class LdapABSearchTerm extends ABSearchTerm {
034:
035: // Create a Logger for this class
036: private static Logger debugLogger = ABLogger
037: .getLogger("com.sun.portal.addressbook.ldap");
038:
039: /**
040: * Initializes the search term object. This constructor indicates this search term
041: * is an end node and does not contain recursive search terms.
042: *
043: * @param name Name of the attribute to search on.
044: * @param value Value of the attribute to search.
045: * @param exact Boolean indicating whether search is 'exact'(true) or
046: * 'contains'(false).
047: */
048: public LdapABSearchTerm(String name, String value, boolean exact) {
049: super (name, value, exact);
050: };
051:
052: /**
053: * Initializes the search term object. This constructor is used for the unary
054: * operator NOT on the ABSearchTerm. Usage of this constructor means that ABSearchTerm
055: * is recursive and is not the end node, and it will contain terms, op and not name,
056: * value.
057: *
058: * @param The recursive ABSearchTerm.
059: * @param Operator binding the recursive searchTerms. The value can only be NOT.
060: */
061: public LdapABSearchTerm(ABSearchTerm term, int op)
062: throws ABStoreException {
063: super (term, op);
064: };
065:
066: /**
067: * Initializes the search term object. This constructor is used for the binary
068: * operators like AND and OR on the ABSearchTerms array. Usage of this constructor
069: * means that ABSearchTerm is recursive and is not the end node, and it will
070: * contain terms, op and not name, value.
071: *
072: * @param The recursive ABSearchTerm array.
073: * @param Operator binding the recursive searchTerms. The values can be AND or OR.
074: */
075: public LdapABSearchTerm(ABSearchTerm[] terms, int op)
076: throws ABStoreException {
077: super (terms, op);
078: };
079:
080: /**
081: * Appends the search terms recursively. Also computes the exactness of each terms.
082: * In LDAP it will return a string corresponding to the Ldap search filter
083: *
084: * @return The computed search Filter object.
085: */
086: public Object compute() throws ABStoreException {
087:
088: String searchFilter = null;
089: String operator;
090: // If the Search term is recursive .. follow the recursion
091: if (terms != null) {
092: if (op == NOT) {
093: searchFilter = "(!" + terms[0].compute() + ")";
094: return searchFilter;
095: } else if (op == AND) {
096: operator = "&";
097: } else if (op == OR) {
098: operator = "|";
099: } else {
100: String msg = "LdapABSearchTerm: Invalid operator - "
101: + op
102: + ". Operator can only be one of AND/OR/NOT";
103: throw new ABStoreException(msg);
104: }
105: // Ldap only accepts a combination of two terms at a time
106: searchFilter = "(" + operator;
107: for (int i = 0; i < terms.length; i++) {
108: searchFilter += terms[i].compute();
109: }
110: searchFilter += ")";
111: } else { // else compute the end node search term
112: searchFilter = computeTerm(this );
113: }
114: debugLogger.log(Level.FINER, "PSJA_CSAL0001", searchFilter);
115: return searchFilter;
116: }
117:
118: private String computeTerm(ABSearchTerm searchTerm) {
119:
120: String term = null;
121: String newValue = null;
122: // If search is not exact .. that is it is contains search, add *
123: // to the beginning and end.
124: String searchVal = searchTerm.getValue();
125: if ((!searchTerm.isExact()) && (searchVal != null)
126: && (searchVal.indexOf('*') < 0)) {
127: newValue = "*" + searchVal + "*";
128: } else {
129: newValue = searchVal;
130: }
131: if (searchTerm.getName().equals("any")) {
132: term = "(|(" + LdapABConstants.ATTR_fn + "=" + newValue
133: + ")" + "(" + LdapABConstants.ATTR_ln + "="
134: + newValue + ")" + "(" + LdapABConstants.ATTR_cn
135: + "=" + newValue + ")" + "("
136: + LdapABConstants.ATTR_em + "=" + newValue + ")"
137: + "(" + LdapABConstants.ATTR_bp + "=" + newValue
138: + ")" + "(" + LdapABConstants.ATTR_hp + "="
139: + newValue + ")" + "(" + LdapABConstants.ATTR_fp
140: + "=" + newValue + ")" + "("
141: + LdapABConstants.ATTR_mp + "=" + newValue + ")"
142: + "(" + LdapABConstants.ATTR_pp + "=" + newValue
143: + "))";
144: } else {
145: LdapABConstants constants = new LdapABConstants();
146: try {
147: Field attr = constants.getClass().getField(
148: "ATTR_" + searchTerm.getName());
149: String attrName = (String) attr.get(null);
150: debugLogger.log(Level.FINER, "PSJA_CSAL0002", attrName);
151: term = "(" + attrName + "=" + newValue + ")";
152: } catch (IllegalAccessException iae) {
153: debugLogger.log(Level.INFO, "PSJA_CSAL0003", searchTerm
154: .getName());
155: } catch (NoSuchFieldException nfe) {
156: debugLogger.log(Level.INFO, "PSJA_CSAL0004", searchTerm
157: .getName());
158: }
159: }
160: debugLogger.log(Level.FINER, "PSJA_CSAL0005", term);
161: return term;
162:
163: }
164: }
|