001: /**
002: * $Id: JPimABStore.java,v 1.4 2005/03/09 23:10:41 rakeshn 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:
023: import com.sun.addressbook.ABStoreException;
024: import com.sun.addressbook.MissingPropertiesException;
025: import com.sun.addressbook.OperationNotSupportedException;
026:
027: import com.aligo.pim.PimFactory;
028: import com.aligo.pim.PimContainerType;
029: import com.aligo.pim.PimFolderType;
030: import com.aligo.pim.PimSortType;
031: import com.aligo.pim.interfaces.PimFolders;
032: import com.aligo.pim.interfaces.PimContainer;
033: import com.aligo.pim.interfaces.PimUserInfo;
034: import com.aligo.pim.interfaces.PimAddressBook;
035: import com.aligo.pim.interfaces.PimAddressEntryItem;
036: import com.aligo.pim.interfaces.PimAddressEntryItems;
037: import com.aligo.pim.interfaces.PimAddressEntryItemFilter;
038:
039: import com.aligo.pim.exceptions.PimException;
040:
041: /**
042: * The JABAPI ABStore implementation for Pim objects
043: */
044: public class JPimABStore extends ABStore implements JPimABConstants {
045: private PimContainer pimContainer = null;
046: private boolean isConnected = false;
047:
048: //
049: // The TopLevel AddBook. Points to "Contacts" folder in Exchange/Notes
050: //
051: protected PimAddressBook topLevelPimAB = null;
052:
053: //
054: // Store the JABAPI Entry & Aligo AddressBook names
055: //
056: private SchemaElements schemaElements = new SchemaElements();
057:
058: public SchemaElements getSchemaElements() {
059: return schemaElements;
060: }
061:
062: /**
063: * Returns the Container Type corresponding to the backend
064: * default == Exchange
065: */
066: protected PimContainerType getPimContainerType(ABSession session) {
067: return PimContainerType.EXCHANGE;
068: }
069:
070: public void init(ABSession session) throws ABStoreException,
071: MissingPropertiesException {
072: if (schemaElements.getAllCommonPropertyNames() == null) {
073: throw new MissingPropertiesException(
074: "Cannot find PropertiesMap: "
075: + ENTRY_PIM_ELEMENTS_FILE);
076: }
077:
078: PimContainerType containerType = getPimContainerType(session);
079: try {
080: pimContainer = PimFactory.getContainer(containerType);
081: } catch (Exception e) {
082: throw new ABStoreException(e.toString());
083: }
084:
085: this .session = session; // store session
086:
087: //
088: // Create PimUserInfo
089: //
090: try {
091: PimUserInfo pimUserInfo = pimContainer.addUserInfo();
092: init(session, pimUserInfo);
093: } catch (PimException pe) {
094: throw new ABStoreException("PimUserInfo set failed: " + pe);
095: }
096: }
097:
098: public void init(ABSession session, PimUserInfo pimC)
099: throws MissingPropertiesException {
100: //
101: // All creds init is done in Notes/ExchangeABStore - nothing here
102: //
103: }
104:
105: public void connect() throws ABStoreException {
106: if (pimContainer == null) {
107: throw new ABStoreException("PimContainer == null");
108: }
109:
110: String abType = session.getProperty(AB_TYPE);
111: PimFolderType type = getFolderType(abType);
112:
113: try {
114: pimContainer.logon();
115: postConnect(pimContainer);
116: topLevelPimAB = (PimAddressBook) pimContainer
117: .getFolder(type);
118: isConnected = true;
119: } catch (PimException pe) {
120: pe.printStackTrace();
121: throw new ABStoreException("Connect failed: " + pe);
122: }
123: }
124:
125: /**
126: * This postConnect gets the CurrentUser info from the pimContainer
127: * used for Notes in case of SSO
128: */
129:
130: protected void postConnect(PimContainer pimContainer)
131: throws ABStoreException {
132: //Nothing to do here
133: //NotesABStore will implement the details.
134:
135: }
136:
137: public void disconnect() throws ABStoreException {
138: if (pimContainer == null) {
139: throw new ABStoreException("PimContainer == null");
140: }
141:
142: try {
143: pimContainer.logoff();
144: } catch (PimException pe) {
145: throw new ABStoreException("Disconnect failed: " + pe);
146: }
147: }
148:
149: public boolean isConnected() throws ABStoreException {
150: //TODO - Alligo - Equivalent ??
151: return isConnected;
152: }
153:
154: /**
155: * Get the list of address books id's of the user. With JABAPI every
156: * folder is an AddressBook.
157: *
158: * @return array of user's address book ids
159: */
160: public String[] getAddressBooks() throws ABStoreException,
161: OperationNotSupportedException {
162: String[] list = null;
163:
164: try {
165: PimFolders folders = topLevelPimAB.getFolders();
166: int count = folders.getCount();
167:
168: list = new String[count];
169:
170: for (int i = 0; i < count; i++) {
171: String id = folders.getFolder(i).getID();
172: list[i] = id;
173: }
174:
175: } catch (PimException pe) {
176: pe.printStackTrace();
177: throw new ABStoreException("getAddressBooks() failed: "
178: + pe);
179: }
180:
181: return list;
182: }
183:
184: /**
185: * Method to get this user's default address book id.
186: *
187: * @return The address book id corresponding to the authenticated user.
188: */
189: protected String getDefaultAbID() throws ABStoreException {
190: return "";
191: }
192:
193: /**
194: * Map id to Address Book Type. default is PERSONAL_ADDRESS_BOOK. overridden
195: * by the backend specific implementations.
196: */
197: protected PimFolderType getFolderType(String id) {
198: PimFolderType type = PimFolderType.PERSONAL_ADDRESS_BOOK;
199:
200: if (id != null) {
201: if (id.equalsIgnoreCase(PERSONAL_ADDRESS_BOOK)) {
202: type = PimFolderType.PERSONAL_ADDRESS_BOOK;
203: } else if (id.equalsIgnoreCase(GLOBAL_ADDRESS_BOOK)) {
204: type = PimFolderType.GLOBAL_ADDRESS_BOOK;
205: }
206: }
207:
208: return type;
209: }
210:
211: /**
212: * Method to retrieve a address book from the backend service.
213: *
214: * @param abID A specific address book ID. (Its not the same as name).
215: * @return The specified address book
216: *
217: * @exception ABStoreException if unable to load the specified calendar
218: */
219: public AddressBook openAddressBook(String abID)
220: throws ABStoreException {
221:
222: String abType = session.getProperty(AB_TYPE);
223: PimFolderType type = getFolderType(abType);
224:
225: try {
226: topLevelPimAB = (PimAddressBook) pimContainer
227: .getFolder(type);
228: } catch (PimException pie) {
229: pie.printStackTrace();
230: throw new ABStoreException("open address book getFolder() "
231: + "failed: " + pie);
232: }
233:
234: PimAddressBook pBook = topLevelPimAB;
235:
236: if ((abID != null) && (!abID.trim().equals(""))) {
237: try {
238: PimFolders pimFolders = pBook.getFolders();
239: pBook = (PimAddressBook) pimFolders.getFolder(abID);
240: } catch (PimException pe) {
241: pe.printStackTrace();
242: throw new ABStoreException("getFolder() failed: " + pe);
243: }
244: }
245:
246: AddressBook aBook = new JPimAddressBook(this , pBook, abID);
247: return aBook;
248: }
249:
250: /**
251: * Method to close a currently opened address book.
252: *
253: * @param ab A specific address book.
254: *
255: * @exception ABStoreException if unable to close the specified calendar
256: */
257: public void closeAddressBook(AddressBook ab)
258: throws ABStoreException {
259: ab = null;
260: }
261: }
|