001: /**
002: * $Id: JPimAddressBook.java,v 1.4 2003/08/25 18:38:17 mihir Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All rights reserved. Use of this
004: * product is subject to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users Subject to Standard License Terms
006: * and Conditions.
007: *
008: * Sun, Sun Microsystems, the Sun logo, and Sun ONE are trademarks or
009: * registered trademarks of Sun Microsystems, Inc. in the United States
010: * and other countries.
011: */package com.sun.ssoadapter.ab.pim;
012:
013: import java.lang.reflect.Method;
014:
015: import com.sun.addressbook.ABSession;
016: import com.sun.addressbook.ABSearchTerm;
017: import com.sun.addressbook.ABStore;
018: import com.sun.addressbook.AddressBook;
019: import com.sun.addressbook.ABFilter;
020: import com.sun.addressbook.Entry;
021: import com.sun.addressbook.Group;
022: import com.sun.addressbook.Element;
023:
024: import com.sun.addressbook.ABStoreException;
025: import com.sun.addressbook.MissingPropertiesException;
026: import com.sun.addressbook.OperationNotSupportedException;
027:
028: import com.aligo.pim.PimFactory;
029: import com.aligo.pim.PimContainerType;
030: import com.aligo.pim.PimFolderType;
031: import com.aligo.pim.PimSortType;
032:
033: import com.aligo.pim.interfaces.PimContainer;
034: import com.aligo.pim.interfaces.PimUserInfo;
035: import com.aligo.pim.interfaces.PimAddressBook;
036: import com.aligo.pim.interfaces.PimAddressEntryItem;
037: import com.aligo.pim.interfaces.PimAddressEntryItems;
038: import com.aligo.pim.interfaces.PimAddressEntryItemFilter;
039: import com.aligo.pim.PimFieldType;
040:
041: import com.aligo.pim.exceptions.PimException;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: public class JPimAddressBook extends AddressBook implements
047: JPimABConstants {
048: private PimAddressBook pimAB = null;
049: private SchemaElements schemaElements = null;
050:
051: protected JPimABStore jPimStore = null;
052: private String addressBookID = null;
053:
054: public JPimAddressBook(ABStore store, PimAddressBook pBook,
055: String abID) {
056: super (store, abID);
057: JPimABStore jPimStore = (JPimABStore) store;
058: this .schemaElements = jPimStore.getSchemaElements();
059: this .pimAB = pBook;
060: }
061:
062: public Element[] fetch(ABFilter abFilter) throws ABStoreException,
063: OperationNotSupportedException {
064: try {
065: return pimFetch(abFilter);
066: } catch (PimException pe) {
067: pe.printStackTrace();
068: throw new ABStoreException("Fetch failed: " + pe);
069: }
070:
071: }
072:
073: private Element[] pimFetch(ABFilter abFilter) throws PimException,
074: ABStoreException {
075: System.out
076: .println("======================================================");
077: PimAddressEntryItems pimAEItems = pimAB.getAddressEntryItems();
078: if (abFilter.getGroup() != null) {
079: // If fetching members of a group, find the group first and then
080: // get its members
081: Group g = abFilter.getGroup();
082: PimAddressEntryItem pimGroup = pimAEItems
083: .getAddressEntryItem(g.getUn());
084: pimAEItems = pimGroup.getMembers();
085: }
086: PimAddressEntryItemFilter filter = pimAEItems
087: .getAddressEntryItemFilter();
088: JPimABSearchTerm searchTerm = (JPimABSearchTerm) abFilter
089: .getSearchTerm();
090: searchTerm.setAddressEntryItemFilter(filter); // reqd b4 compute()
091: abFilter.getSearchFilter(); // calls JPimABSearchTerm::compute()
092:
093: //
094: // set sortOrder. The filter operations are already done.
095: //
096: switch (abFilter.getSortOrder()) {
097: case ABFilter.ASCENDING:
098: pimAEItems.sort(PimSortType.ASCENDING);
099: break;
100: case ABFilter.DESCENDING:
101: pimAEItems.sort(PimSortType.DESCENDING);
102: break;
103: default:
104: // Let the adapter use its default
105: break;
106: }
107:
108: //
109: // set sortBy. Can set multiple fields to orderBy
110: //
111: String sortBy = abFilter.getSortBy();
112: if (sortBy != null) {
113: sortBy = sortBy.trim();
114: List list = new ArrayList(1);
115:
116: if (sortBy.equalsIgnoreCase("fn")) // Need Entry.FN
117: list.add(PimFieldType.FIRST_NAME);
118:
119: if (sortBy.equalsIgnoreCase("ln")) // Need Entry.LN
120: list.add(PimFieldType.LAST_NAME);
121:
122: int size = list.size();
123: if (size > 0) {
124: PimFieldType[] pimFieldTypes = new PimFieldType[size];
125: list.toArray(pimFieldTypes);
126: pimAEItems.setOrderBy(pimFieldTypes);
127: }
128: }
129:
130: int count = pimAEItems.getCount();
131: if (count <= 0) {
132: return null;
133: }
134:
135: ArrayList elements = new ArrayList(count);
136: if (abFilter.getElementType() == Element.ENTRY) {
137: // Get only entries
138: for (int i = 0; i < count; i++) {
139: PimAddressEntryItem pimAEItem = pimAEItems
140: .getAddressEntryItem(i);
141: if (pimAEItem.getMembers() == null) {
142: // This is an entry and not a group
143: elements.add(toEntry(pimAEItem, i));
144: }
145: }
146: } else if (abFilter.getElementType() == Element.GROUP) {
147: // Get only groups
148: for (int i = 0; i < count; i++) {
149: PimAddressEntryItem pimAEItem = pimAEItems
150: .getAddressEntryItem(i);
151: if (pimAEItem.getMembers() != null) {
152: // This is a group and not an entry
153: elements.add(toGroup(pimAEItem, i));
154: }
155: }
156: } else {
157: // Get entries and groups
158: for (int i = 0; i < count; i++) {
159: PimAddressEntryItem pimAEItem = pimAEItems
160: .getAddressEntryItem(i);
161: if (pimAEItem.getMembers() == null) {
162: // This is an entry and not a group
163: Entry entry = toEntry(pimAEItem, i);
164: elements.add(entry);
165: } else {
166: // This is a group and not an entry
167: Group group = toGroup(pimAEItem, i);
168: elements.add(group);
169: }
170:
171: }
172: }
173: System.out
174: .println("JPimAddressBook.pimFetch : returning elements, count = "
175: + elements.size());
176: System.out
177: .println("======================================================");
178: return (Element[]) elements
179: .toArray(new Element[elements.size()]);
180: }
181:
182: /**
183: * add
184: */
185: public void add(Element element) throws ABStoreException,
186: OperationNotSupportedException {
187: int status = -1;
188: String msg = null;
189: try {
190: PimAddressEntryItems pimAEItems = pimAB
191: .getAddressEntryItems();
192: PimAddressEntryItem newAEItem = pimAEItems
193: .addAddressEntryItem();
194:
195: if (element.getElementType() == Element.GROUP) {
196: //
197: // Add all Group properties into newItem:
198: //
199: status = copyGroupToAddressEntryItem((Group) element,
200: newAEItem);
201: newAEItem.addMembers();
202: } else {
203: // Else .. default is Entry.
204: status = copyToAddressEntryItem((Entry) element,
205: newAEItem);
206: }
207:
208: if (status == 0) { // if conversion goes well
209: newAEItem.update();
210: }
211: } catch (PimException pe) {
212: msg = pe.toString();
213: pe.printStackTrace();
214: }
215:
216: if (status != 0) {
217: throw new ABStoreException("Add element failed: " + msg);
218: }
219:
220: return;
221: }
222:
223: /**
224: * modify
225: */
226:
227: public void modify(Element oldElement, Element newElement)
228: throws ABStoreException, OperationNotSupportedException {
229: int status = -1;
230: String msg = null;
231:
232: try {
233: PimAddressEntryItems pimAEItems = pimAB
234: .getAddressEntryItems();
235: String un = oldElement.getUn(); // Unique Id
236:
237: PimAddressEntryItem modAEItem = pimAEItems
238: .getAddressEntryItem(un);
239:
240: if (newElement.getElementType() == Element.GROUP) {
241: //
242: // Add all Group members into newContactItem:
243: //
244: status = copyGroupToAddressEntryItem(
245: (Group) newElement, modAEItem);
246: } else {
247: // Else .. default is Entry.
248: status = copyToAddressEntryItem((Entry) newElement,
249: modAEItem);
250: }
251:
252: if (status == 0) { // if conversion goes well
253: modAEItem.update();
254: }
255: } catch (PimException pe) {
256: msg = pe.toString();
257: status = -1;
258: pe.printStackTrace();
259: }
260:
261: if (status != 0) {
262: throw new ABStoreException("Modify contact failed: " + msg);
263: }
264:
265: return;
266: }
267:
268: /**
269: * delete
270: */
271:
272: public void delete(Element element) throws ABStoreException,
273: OperationNotSupportedException {
274: try {
275: String un = element.getUn(); // Unique ID
276: PimAddressEntryItems pimAEItems = pimAB
277: .getAddressEntryItems();
278: PimAddressEntryItem delAEItem = pimAEItems
279: .getAddressEntryItem(un);
280: delAEItem.delete();
281: } catch (PimException pe) {
282: pe.printStackTrace();
283: throw new ABStoreException("Delete contact failed: " + pe);
284: }
285:
286: return;
287: }
288:
289: /**
290: * Return the ABSearchTerm object corresponding to the service type.
291: *
292: * @return ABSearchTerm object corresponding to the service type.
293: */
294: public ABSearchTerm newABSearchTerm(String name, String value,
295: boolean exact) {
296: return new JPimABSearchTerm(name, value, exact);
297: }
298:
299: /**
300: * Return the ABSearchTerm object corresponding to the service type.
301: *
302: * @return ABSearchTerm object corresponding to the service type.
303: */
304: public ABSearchTerm newABSearchTerm(ABSearchTerm term, int op)
305: throws ABStoreException {
306: return new JPimABSearchTerm(term, op);
307: }
308:
309: /**
310: * Return the ABSearchTerm object corresponding to the service type.
311: *
312: * @return ABSearchTerm object corresponding to the service type.
313: */
314: public ABSearchTerm newABSearchTerm(ABSearchTerm[] terms, int op)
315: throws ABStoreException {
316: return new JPimABSearchTerm(terms, op);
317: }
318:
319: //
320: // TODO: Groups
321: //
322:
323: public Element[] fetchGroupMembers(ABFilter filter, Group group)
324: throws ABStoreException, OperationNotSupportedException {
325: filter.setGroup(group);
326: Element[] elements = fetch(filter);
327: filter.setGroup(null);
328: return elements;
329:
330: }
331:
332: public void addGroupMember(Element element, Group group)
333: throws ABStoreException, OperationNotSupportedException {
334: int status = -1;
335: String msg = null;
336:
337: try {
338: PimAddressEntryItems pimAEItems = pimAB
339: .getAddressEntryItems();
340: PimAddressEntryItem pimGroup = pimAEItems
341: .getAddressEntryItem(group.getUn());
342: System.out.println("JPimAddressBook.addGroupMember : "
343: + " Add element " + element.getCn() + " to group "
344: + pimGroup + "[" + group.getUn() + "]");
345: PimAddressEntryItems memberItems = pimGroup.addMembers();
346: PimAddressEntryItem memberItem = memberItems
347: .addAddressEntryItem();
348: if (element.getElementType() == Element.GROUP) {
349: //
350: // Add all Group members into newContactItem:
351: //
352: status = copyGroupToAddressEntryItem((Group) element,
353: memberItem);
354: } else {
355: // Else .. default is Entry.
356: status = copyToAddressEntryItem((Entry) element,
357: memberItem);
358: }
359:
360: if (status == 0) { // if conversion goes well
361: memberItem.update();
362: pimGroup.update();
363: }
364: } catch (PimException pe) {
365: msg = pe.toString();
366: pe.printStackTrace();
367: }
368:
369: if (status != 0) {
370: throw new ABStoreException("Add element to Group failed: "
371: + msg);
372: }
373: }
374:
375: public void deleteGroupMember(Element element, Group group)
376: throws ABStoreException, OperationNotSupportedException {
377: try {
378: String un = element.getUn(); // Unique ID
379: PimAddressEntryItems pimAEItems = pimAB
380: .getAddressEntryItems();
381: PimAddressEntryItem pimGroup = pimAEItems
382: .getAddressEntryItem(group.getUn());
383: PimAddressEntryItems memberItems = pimGroup.getMembers();
384:
385: PimAddressEntryItem delMemberItem = memberItems
386: .getAddressEntryItem(un);
387: delMemberItem.delete();
388: pimGroup.update();
389:
390: } catch (PimException pe) {
391: pe.printStackTrace();
392: throw new ABStoreException(
393: "Delete element from group failed: " + pe);
394: }
395:
396: return;
397:
398: }
399:
400: // END Groups
401:
402: /**
403: * Populate the Entry object with properties from PimAddressEntryItem.
404: * Handle the properties known to Entry. And put the rest of the stuff in
405: * Properties.
406: */
407: private Entry toEntry(PimAddressEntryItem pimAEItem, int index)
408: throws PimException {
409: Object[] commonPropNames = schemaElements
410: .getAllCommonPropertyNames();
411: if (commonPropNames == null) {
412: return null;
413: }
414:
415: Entry entry = new Entry();
416:
417: String entryId = Integer.toString(index);
418: entry.setEntryid(entryId);
419:
420: //
421: // Set the unique id first; Entry::Un == Pim::ID
422: //
423: String un = pimAEItem.getID();
424: entry.setUn(un);
425:
426: Class pimAEItemClass = pimAEItem.getClass();
427: Class entryClass = entry.getClass();
428:
429: for (int i = 0; i < commonPropNames.length; i++) {
430: // Get the Entry property
431: String entryPropertyName = (String) commonPropNames[i];
432:
433: // Get the corresponding PimProperty
434: String pimPropertyName = schemaElements
435: .getPimElementName(entryPropertyName);
436:
437: String getterMethodName = GET + pimPropertyName;
438: String setterMethodName = SET + entryPropertyName;
439:
440: try {
441: //
442: // get from pimAEItem
443: //
444: Method getterMethod = pimAEItemClass.getMethod(
445: getterMethodName, null);
446: String value = (String) getterMethod.invoke(pimAEItem,
447: null);
448:
449: //
450: // dont to set null or empty values. We track empty values
451: // to removing an Entry property.
452: //
453: if (value == null || value.trim().equals(""))
454: continue;
455:
456: //
457: // and set in Entry
458: //
459: String[] values = { value };
460:
461: Method setterMethod = entryClass.getMethod(
462: setterMethodName, beanParams);
463: setterMethod.invoke(entry, values); // set Entry
464: } catch (Exception e) {
465: // Ignore all Exceptions !! TODO
466: }
467: }
468:
469: //
470: // stuff all the unknown ones into Properties: TODO
471: //
472:
473: return entry;
474: }
475:
476: /**
477: * Populate the Group object with properties from PimAddressEntryItem.
478: * Handle the properties known to Group. And put the rest of the stuff in
479: * Properties.
480: */
481: private Group toGroup(PimAddressEntryItem pimAEItem, int index)
482: throws PimException {
483: Object[] commonPropNames = schemaElements
484: .getAllCommonPropertyNamesForGroup();
485: if (commonPropNames == null) {
486: return null;
487: }
488:
489: Group group = new Group();
490:
491: String entryId = Integer.toString(index);
492: group.setEntryid(entryId);
493:
494: //
495: // Set the unique id first; Group::Un == Pim::ID
496: //
497: String un = pimAEItem.getID();
498: group.setUn(un);
499:
500: Class pimAEItemClass = pimAEItem.getClass();
501: Class groupClass = group.getClass();
502:
503: for (int i = 0; i < commonPropNames.length; i++) {
504: // Get the Group property
505: String groupPropertyName = (String) commonPropNames[i];
506:
507: // Get the corresponding PimProperty
508: String pimPropertyName = schemaElements
509: .getPimElementNameForGroup(groupPropertyName);
510:
511: String getterMethodName = GET + pimPropertyName;
512: String setterMethodName = SET + groupPropertyName;
513: try {
514: //
515: // get from pimAEItem
516: //
517: Method getterMethod = pimAEItemClass.getMethod(
518: getterMethodName, null);
519: String value = (String) getterMethod.invoke(pimAEItem,
520: null);
521:
522: //
523: // dont to set null or empty values. We track empty values
524: // to removing a Group property.
525: //
526: if (value == null || value.trim().equals(""))
527: continue;
528:
529: //
530: // and set in Group
531: //
532: String[] values = { value };
533: Method setterMethod = groupClass.getMethod(
534: setterMethodName, beanParams);
535: setterMethod.invoke(group, values); // set Entry
536: } catch (Exception e) {
537: e.printStackTrace();
538: // Ignore all Exceptions !! TODO
539: }
540: }
541:
542: //
543: // stuff all the unknown ones into Properties: TODO
544: //
545:
546: return group;
547: }
548:
549: /**
550: * Populate The PimAddressEntryItem's object with Entry. Handle
551: * the properties known to Entry. And put the rest of the stuff in
552: * Properties.
553: */
554: private int copyToAddressEntryItem(Entry entry,
555: PimAddressEntryItem pimAEItem) throws PimException {
556: Object[] commonPropNames = schemaElements
557: .getAllCommonPropertyNames();
558: if (commonPropNames == null) {
559: return -1; // return error
560: }
561:
562: Class pimAEItemClass = pimAEItem.getClass();
563: Class entryClass = entry.getClass();
564:
565: for (int i = 0; i < commonPropNames.length; i++) {
566: String entryPropertyName = (String) commonPropNames[i];
567: String pimPropertyName = schemaElements
568: .getPimElementName(entryPropertyName);
569:
570: String getterMethodName = GET + entryPropertyName;
571: String setterMethodName = SET + pimPropertyName;
572:
573: try {
574: //
575: // get from Entry
576: //
577: Method getterMethod = entryClass.getMethod(
578: getterMethodName, null);
579: String value = (String) getterMethod
580: .invoke(entry, null);
581:
582: //
583: // We track empty values to removing a property.
584: //
585: if (value == null)
586: continue;
587:
588: //
589: // set in PimAddressEntryElement
590: //
591: String[] values = { value };
592:
593: Method setterMethod = pimAEItemClass.getMethod(
594: setterMethodName, beanParams);
595: setterMethod.invoke(pimAEItem, values); // set PimCntct
596: } catch (Exception e) {
597: // Ignore all Exceptions !! TODO
598: }
599: }
600:
601: //
602: // Add back the unknown Properties - TODO
603: //
604: return 0;
605: }
606:
607: /**
608: * Populate The PimAddressEntryItem's object with Group. Handle
609: * the properties known to Group. And put the rest of the stuff in
610: * Properties.
611: */
612: private int copyGroupToAddressEntryItem(Group group,
613: PimAddressEntryItem pimAEItem) throws PimException {
614: Object[] commonPropNames = schemaElements
615: .getAllCommonPropertyNamesForGroup();
616: if (commonPropNames == null) {
617: return -1; // return error
618: }
619:
620: Class pimAEItemClass = pimAEItem.getClass();
621: Class groupClass = group.getClass();
622:
623: for (int i = 0; i < commonPropNames.length; i++) {
624: String groupPropertyName = (String) commonPropNames[i];
625: String pimPropertyName = schemaElements
626: .getPimElementNameForGroup(groupPropertyName);
627:
628: String getterMethodName = GET + groupPropertyName;
629: String setterMethodName = SET + pimPropertyName;
630:
631: try {
632: //
633: // get from Group
634: //
635: Method getterMethod = groupClass.getMethod(
636: getterMethodName, null);
637: String value = (String) getterMethod
638: .invoke(group, null);
639:
640: //
641: // We track empty values to removing a property.
642: //
643: if (value == null)
644: continue;
645:
646: //
647: // set in PimAddressEntryElement
648: //
649: String[] values = { value };
650:
651: Method setterMethod = pimAEItemClass.getMethod(
652: setterMethodName, beanParams);
653: setterMethod.invoke(pimAEItem, values); // set PimCntct
654: } catch (Exception e) {
655: e.printStackTrace();
656: // Ignore all Exceptions !! TODO
657: }
658: }
659:
660: //
661: // Add back the unknown Properties - TODO
662: //
663: return 0;
664: }
665:
666: }
|