001: package com.sun.addressbook.wabp;
002:
003: import java.util.Vector;
004: import java.util.Properties;
005: import java.util.Enumeration;
006: import java.util.logging.Logger;
007: import java.util.logging.Level;
008: import java.lang.reflect.Field;
009:
010: import com.sun.addressbook.ABSearchTerm;
011: import com.sun.addressbook.ABStoreException;
012: import com.sun.addressbook.ABLogger;
013:
014: /**
015: * Address Book SearchTerm does the recursive computation of the search
016: * filter from a combination of search terms. It implements the compute
017: * command of the ABSearchTerm.
018: * The search terms can be nested in a tree structure and combined with
019: * an 'and' or an 'or' e.g. "ln=doe & (fn=john | fn=joe)"
020: *
021: */
022: public class WabpABSearchTerm extends ABSearchTerm {
023:
024: // Create a Logger for this class
025: private static Logger debugLogger = ABLogger
026: .getLogger("com.sun.portal.addressbook.wabp");
027:
028: /**
029: * Initializes the search term object. This constructor indicates this search term
030: * is an end node and does not contain recursive search terms.
031: *
032: * @param name Name of the attribute to search on.
033: * @param value Value of the attribute to search.
034: * @param exact Boolean indicating whether search is 'exact'(true) or
035: * 'contains'(false).
036: */
037: public WabpABSearchTerm(String name, String value, boolean exact) {
038: super (name, value, exact);
039: };
040:
041: /**
042: * Initializes the search term object. This constructor is used for the unary
043: * operator NOT on the ABSearchTerm. Usage of this constructor means that ABSearchTerm
044: * is recursive and is not the end node, and it will contain terms, op and not name,
045: * value.
046: *
047: * @param The recursive ABSearchTerm.
048: * @param Operator binding the recursive searchTerms. The value can only be NOT.
049: */
050: public WabpABSearchTerm(ABSearchTerm term, int op)
051: throws ABStoreException {
052: super (term, op);
053: };
054:
055: /**
056: * Initializes the search term object. This constructor is used for the binary
057: * operators like AND and OR on the ABSearchTerms array. Usage of this constructor
058: * means that ABSearchTerm is recursive and is not the end node, and it will
059: * contain terms, op and not name, value.
060: *
061: * @param The recursive ABSearchTerm array.
062: * @param Operator binding the recursive searchTerms. The values can be AND or OR.
063: */
064: public WabpABSearchTerm(ABSearchTerm[] terms, int op)
065: throws ABStoreException {
066: super (terms, op);
067: };
068:
069: /**
070: * Appends the search terms recursively. Also computes the exactness of each terms.
071: *
072: * @return The computed search Filter object.
073: */
074: public Object compute() throws ABStoreException {
075:
076: String searchFilter = null;
077: String operator = null;
078:
079: // if the Search term is recursive then follow the recursion
080: //
081: if (terms != null) {
082: if (op == NOT) {
083: searchFilter = "(!" + terms[0].compute() + ")";
084: return searchFilter;
085:
086: } else if (op == AND) {
087: operator = "&";
088:
089: } else if (op == OR) {
090: operator = "|";
091:
092: } else {
093: String msg = "WabpABSearchTerm: invalid operator: "
094: + op
095: + ". Operator can only be one of AND/OR/NOT";
096: throw new ABStoreException(msg);
097: }
098:
099: // Wabp only accepts a combination of two terms at a time
100: //searchFilter = "(" + operator ;
101: searchFilter = operator;
102: for (int i = 0; i < terms.length; i++) {
103: searchFilter += "(" + terms[i].compute() + ")";
104: }
105: //searchFilter += ")";
106:
107: } else { // else compute the end node search term
108: searchFilter = computeTerm(this );
109: }
110:
111: debugLogger.log(Level.FINER, "PSJA_CSAW0001", searchFilter);
112:
113: return searchFilter;
114: }
115:
116: private String computeTerm(ABSearchTerm searchTerm) {
117:
118: String term = null;
119: String newValue = null;
120:
121: // check to see if the search is an exact or contains search. if the search
122: // type is contains, then wrap the value with wildcards, i.e. '*<value>*'
123: //
124: String searchVal = searchTerm.getValue();
125:
126: if ((!searchTerm.isExact()) && (searchVal != null)
127: && (searchVal.indexOf('*') < 0)) {
128: newValue = "*" + searchVal + "*";
129: } else {
130: newValue = searchVal;
131: }
132:
133: // check to see if the name of the attribute to search on is "any". if it
134: // is then set the equivalent to "entry/displayname" for WABP filter.
135: //
136: if (searchTerm.getName().equals("any")) {
137: term = WabpABConstants.WABP_ENTRY_DISPLAYNAME + "="
138: + newValue;
139: //
140: // if the searchTerm is not "any", then check the searchTerm name and
141: // perform the necessary translation to the corresponding WABP attribute
142: // name. only specified the fields found in the Mobile Access Address
143: // Book taglibs. otherwise, the searchTerm name is not translated.
144: //
145: } else {
146: String key = searchTerm.getName();
147:
148: if (key.equals("fn")) {
149: key = WabpABConstants.WABP_PERSON_GIVENNAME;
150: } else if (key.equals("ln")) {
151: key = WabpABConstants.WABP_PERSON_SURNAME;
152: } else if (key.equals("cn")) {
153: key = WabpABConstants.WABP_ENTRY_DISPLAYNAME;
154: } else if (key.equals("em")) {
155: key = WabpABConstants.WABP_EMAIL;
156: } else if (key.equals("bp")) {
157: key = WabpABConstants.WABP_PHONE;
158: } else if (key.equals("fp")) {
159: key = WabpABConstants.WABP_PHONE;
160: } else if (key.equals("hp")) {
161: key = WabpABConstants.WABP_PHONE;
162: } else if (key.equals("mp")) {
163: key = WabpABConstants.WABP_PHONE;
164: } else if (key.equals("pp")) {
165: key = WabpABConstants.WABP_PHONE;
166: }
167:
168: //term = "(" + key + "=" + newValue + ")";
169: term = key + "=" + newValue;
170: }
171:
172: debugLogger.log(Level.FINER, "PSJA_CSAW0002", term);
173: return term;
174: }
175: }
|