001: /*
002: * %W% %G% Sun Microsystems
003: *
004: * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
006: */
007:
008: package com.sun.portal.wireless.taglibs.ab;
009:
010: import javax.servlet.jsp.*;
011: import javax.servlet.jsp.tagext.*;
012: import com.sun.portal.wireless.taglibs.base.*;
013: import com.sun.portal.log.common.PortalLogger;
014:
015: import java.util.List;
016: import java.util.ArrayList;
017: import java.util.logging.Logger;
018:
019: import com.sun.addressbook.Entry;
020: import com.sun.addressbook.Element;
021: import com.sun.addressbook.Group;
022:
023: /**
024: * The ABFetchTag class executes the address book search/fetch
025: * command.
026: *
027: * Attibutes (all optional): sd, sb, any, fn, ln, em, hp, bp, fp, mp, pp, cn,
028: *
029: * @author Mihir Sambhus
030: * @version 1.0 */
031: public class ABFetchTag extends ABBaseCommandTag {
032:
033: // Create a logger for this class
034: private static Logger debugLogger = PortalLogger
035: .getLogger(ABFetchTag.class);
036:
037: /*
038: * Entry object populated with the search fields
039: */
040: private Entry searchEntry = null;
041:
042: /*
043: * Sort by
044: */
045: private String sb = null;
046:
047: /*
048: * Sort Direction
049: */
050: private String sd = null;
051:
052: /*
053: * All fields
054: */
055: private String any = null;
056:
057: /*
058: * Boolean exact .. indicating whether search is exact or approximate
059: */
060: private boolean exact = false;
061:
062: /**
063: * Group id - index into the current collection.
064: * If specified search for the elements in this group
065: */
066: private String groupid = null;
067:
068: /**
069: * Collection to store the result in
070: */
071: private String collection = null;
072:
073: /**
074: * Constructor for ABFetchTag() - instantiates the searchEntry object
075: */
076: public ABFetchTag() {
077: // Create an entry object which is passed to the ContactDB for searching.
078: searchEntry = new Entry();
079: }
080:
081: /**
082: * Get searchEntry.
083: */
084: private Entry getSearchEntry() {
085: if (searchEntry == null) {
086: searchEntry = new Entry();
087: }
088: return searchEntry;
089: }
090:
091: /**
092: * Get sb - sort by attribute value
093: *
094: * @return sb - sort by attribute value
095: */
096: public String getSb() {
097: return sb;
098: }
099:
100: /**
101: * Get sd - sort direction attribute value
102: *
103: * @return sd - sort direction attribute value
104: */
105: public String getSd() {
106: return sd;
107: }
108:
109: /**
110: * Get fn - First name value from the Entry bean. Valid while searching Entry
111: *
112: * @return fn - String
113: */
114: public String getFn() {
115: return getSearchEntry().getFn();
116: }
117:
118: /**
119: * Get ln - Last name value from the Entry bean. Valid while searching Entry
120: *
121: * @return ln - String
122: */
123: public String getLn() {
124: return getSearchEntry().getLn();
125: }
126:
127: /**
128: * Get em - Email value from the Entry bean. Valid while searching Entry
129: *
130: * @return em - String
131: */
132: public String getEm() {
133: return getSearchEntry().getEm();
134: }
135:
136: /**
137: * Get bp - Business Phon valuee from the Entry bean. Valid while searching Entry
138: *
139: * @return bp - String
140: */
141: public String getBp() {
142: return getSearchEntry().getBp();
143: }
144:
145: /**
146: * Get hp - Home Phon valuee from the Entry bean. Valid while searching Entry
147: *
148: * @return hp - String
149: */
150: public String getHp() {
151: return getSearchEntry().getHp();
152: }
153:
154: /**
155: * Get fp - Fax value from the Entry bean. Valid while searching Entry
156: *
157: * @return fp - String
158: */
159: public String getFp() {
160: return getSearchEntry().getFp();
161: }
162:
163: /**
164: * Get mp - Mobile phone value from the Entry bean. Valid while searching Entry
165: *
166: * @return mp - String
167: */
168: public String getMp() {
169: return getSearchEntry().getMp();
170: }
171:
172: /**
173: * Get pp - Pager phone value from the Entry bean. Valid while searching Entry
174: *
175: * @return pp - String
176: */
177: public String getPp() {
178: return getSearchEntry().getPp();
179: }
180:
181: /**
182: * Get cn - Common name/display name of the element. Valid in Entry as well
183: * as Group.
184: *
185: * @return cn - String
186: */
187: public String getCn() {
188: return getSearchEntry().getCn();
189: }
190:
191: /**
192: * Get any - 'any' field value from the Entry bean
193: *
194: * @return 'any' - String
195: */
196: public String getAny() {
197: return any;
198: }
199:
200: /**
201: * Get element elementtype - Can be entry, group, all
202: *
203: * @return elementtype - String
204: */
205: public String getElementtype() {
206: if (getSearchEntry().getElementType() == Element.ENTRY) {
207: return "contact";
208: } else if (getSearchEntry().getElementType() == Element.GROUP) {
209: return "group";
210: } else if (getSearchEntry().getElementType() == Element.ALL) {
211: return "all";
212: } else {
213: // default type is entry
214: return "contact";
215: }
216: }
217:
218: /**
219: * Get group id - index into the current collection.
220: *
221: * @return groupid
222: */
223: public String getGroupid() {
224: return groupid;
225: }
226:
227: /**
228: * Get Collection - collection to store the results in
229: *
230: * @return collection
231: */
232: public String getCollection() {
233: return collection;
234: }
235:
236: /**
237: * Get Exact - Determines whether search is exact or approximate
238: *
239: * @return exact
240: */
241: public String getExact() {
242: return String.valueOf(exact);
243: }
244:
245: /**
246: * Set sb - 'Sort by' attribute
247: *
248: * @param sb the 'sort by' String
249: */
250: public void setSb(String sb) {
251: this .sb = evalAttribute(sb);
252: }
253:
254: /**
255: * Set sd - 'Sort direction' attribtue
256: *
257: * @param sd the 'sort direction' String
258: */
259: public void setSd(String sd) {
260: this .sd = evalAttribute(sd);
261: }
262:
263: private String getContainsSearchStr(String searchStr) {
264: String fullSearchStr = evalAttribute(searchStr);
265: if ((fullSearchStr == null) || (fullSearchStr.equals(""))) {
266: fullSearchStr = "*";
267: }
268: /*
269: else if(fullSearchStr.indexOf('*') < 0){
270: fullSearchStr = "*" + fullSearchStr + "*";
271: }*/
272: return fullSearchStr;
273: }
274:
275: /**
276: * Set fn - First name in Entry bean
277: *
278: * @param fn the fn String. Valid in Entry only
279: */
280: public void setFn(String fn) {
281: getSearchEntry().setFn(getContainsSearchStr(fn));
282: }
283:
284: /**
285: * Set ln - Last name in Entry bean. Valid in Entry only
286: *
287: * @param ln the ln String
288: */
289: public void setLn(String ln) {
290: getSearchEntry().setLn(getContainsSearchStr(ln));
291: }
292:
293: /**
294: * Set em - Email in Entry bean. Valid in Entry only
295: *
296: * @param em the em String
297: */
298: public void setEm(String em) {
299: getSearchEntry().setEm(getContainsSearchStr(em));
300: }
301:
302: /**
303: * Set bp - Business Phone in Entry bean. Valid in Entry only
304: *
305: * @param bp the bp String
306: */
307: public void setBp(String bp) {
308: getSearchEntry().setBp(getContainsSearchStr(bp));
309: }
310:
311: /**
312: * Set hp - Home Phone in Entry bean. Valid in Entry only
313: *
314: * @param hp the hp String
315: */
316: public void setHp(String hp) {
317: getSearchEntry().setHp(getContainsSearchStr(hp));
318: }
319:
320: /**
321: * Set fp - Fax in Entry bean. Valid in Entry only
322: *
323: * @param fp the fp String
324: */
325: public void setFp(String fp) {
326: getSearchEntry().setFp(getContainsSearchStr(fp));
327: }
328:
329: /**
330: * Set mp - Mobile Phone in Entry bean. Valid in Entry only
331: *
332: * @param mp the mp String
333: */
334: public void setMp(String mp) {
335: getSearchEntry().setMp(getContainsSearchStr(mp));
336: }
337:
338: /**
339: * Set pp - Pager phone in Entry bean. Valid in Entry only
340: *
341: * @param pp the pp String
342: */
343: public void setPp(String pp) {
344: getSearchEntry().setPp(getContainsSearchStr(pp));
345: }
346:
347: /**
348: * Set cn - Common name/display name of the element. Valid in Entry as well
349: * as Group.
350: *
351: * @param cn the cn String
352: */
353: public void setCn(String cn) {
354: getSearchEntry().setCn(getContainsSearchStr(cn));
355: }
356:
357: /**
358: * Set any - fn, ln, em, bp, hp, mp, fp, pp in Entry bean
359: *
360: * @param any the 'any' String
361: */
362: public void setAny(String any) {
363: this .any = evalAttribute(any);
364: }
365:
366: /**
367: * Set element type - Can be entry or group
368: *
369: * @param elementtype - String
370: */
371: public void setElementtype(String elementtype) {
372: if (evalAttribute(elementtype).equals("contact")) {
373: getSearchEntry().setElementType(Element.ENTRY);
374: } else if (evalAttribute(elementtype).equals("group")) {
375: getSearchEntry().setElementType(Element.GROUP);
376: } else if (evalAttribute(elementtype).equals("all")) {
377: getSearchEntry().setElementType(Element.ALL);
378: } else {
379: // default is of type entry
380: getSearchEntry().setElementType(Element.ENTRY);
381: }
382: }
383:
384: /**
385: * Set group id - index into the current collection.
386: * If specified, search for elements in the group
387: *
388: * @param groupid
389: */
390: public void setGroupid(String groupid) {
391: this .groupid = evalAttribute(groupid);
392: }
393:
394: /**
395: * Set Collection - collection to store the results in
396: *
397: * @param collection
398: */
399: public void setCollection(String collection) {
400: this .collection = evalAttribute(collection);
401: }
402:
403: /**
404: * Set Exact - Determines whether search is exact or approximate
405: *
406: * @param exact
407: */
408: public void setExact(String exact) {
409: this .exact = Boolean.valueOf(exact).booleanValue();
410: }
411:
412: /**
413: * Execute the search command with the given filters on the user's
414: * address book. Returns the command status.
415: * If the command fails then the tag body is included,
416: * Else if the command succeeds then the tag body is skipped.
417: *
418: * @return true if the command succeeds.
419: * false if the command fails.
420: * @exception JspException
421: */
422: public boolean execute() throws JspException {
423:
424: // Get the contactDB object interacting with the Backend LDAP server
425: ContactDB contactDB = getContactDB();
426: Group group = null;
427:
428: if (sb == null) {
429: sb = abContext.getSortBy();
430: }
431: if (sd == null) {
432: sd = abContext.getSortOrder();
433: }
434:
435: // if groupid specified, get the group from the collection
436: if (groupid != null) {
437:
438: Element tmp = abContext.getEntry(Integer.parseInt(groupid));
439: if (tmp.getElementType() != Element.GROUP) {
440: // Group id pointing to invalid element from collection
441: String msg = "ABFetchTag.execute(): Group id pointing to invalid element from collection";
442: debugLogger.fine("PSMA_CSPWTA0012");
443: getABContext().setErrorCode("AB_005");
444: return false;
445: }
446: group = (Group) tmp;
447: }
448:
449: // if none of the properties including any are set, then search for
450: // the whole address book.
451: if ((getFn() == null) && (getLn() == null) && (getEm() == null)
452: && (getBp() == null) && (getHp() == null)
453: && (getMp() == null) && (getFp() == null)
454: && (getPp() == null) && (getCn() == null)
455: && (any == null)) {
456: this .setAny("*");
457: }
458:
459: List elements = null;
460:
461: // if 'any' property specified, search only on any and ignore other fields
462: if (any != null) {
463: if (any.equals("")) {
464: setAny("*");
465: debugLogger.fine("PSMA_CSPWTA0013");
466: }
467:
468: if (any.indexOf("*") != -1)
469: exact = true; // when searching with "*" use exact == true
470:
471: if (collection == null) {
472: // by default collection is entries
473: collection = "entries";
474: }
475:
476: if (collection.equals("contacts")) {
477: List list = new ArrayList();
478: elements = contactDB.getContacts(group, list);
479: } else {
480: elements = contactDB.searchABook(getSearchEntry(),
481: group, any, sb, sd, exact);
482: }
483: } else {
484: debugLogger.finer("PSMA_CSPWTA0014");
485: if (collection.equals("contacts")) {
486: List list = new ArrayList();
487: elements = contactDB.getContacts(group, list);
488: } else {
489: elements = contactDB.searchABook(getSearchEntry(),
490: group, null, sb, sd, exact);
491: }
492: }
493:
494: if (elements == null) {
495: debugLogger.fine("PSMA_CSPWTA0015");
496: getABContext().setErrorCode("");
497: return false;
498: }
499:
500: if (collection.equals("groupmembers")) {
501: // Collection is a groupmembers collection
502: abContext.setGroupmembers(elements);
503: abContext.setNumGroupmembers(elements.size());
504: } else if (collection.equals("memberchoices")) {
505: // Collection is a memberchoices collection
506: abContext.setMemberchoices(elements);
507: abContext.setNumMemberchoices(elements.size());
508: } else if (collection.equals("contacts")) {
509: // Collection is a allContacts collection
510: abContext.setContacts(elements);
511: abContext.setNumContacts(elements.size());
512: } else {
513: // By default collection is Address Book top level entries collection
514: abContext.setEntries(elements);
515: abContext.setNumEntries(elements.size());
516: }
517: return true;
518: }
519:
520: /**
521: * Cleanup
522: */
523: public void release() {
524: super.release();
525: searchEntry = null;
526: any = null;
527: sd = null;
528: sb = null;
529: }
530:
531: }
|