001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.addressbook.facade;
019:
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023: import java.util.Vector;
024:
025: import org.columba.addressbook.folder.AbstractFolder;
026: import org.columba.addressbook.folder.IContactFolder;
027: import org.columba.addressbook.folder.IFolder;
028: import org.columba.addressbook.folder.IGroupFolder;
029: import org.columba.addressbook.gui.tree.AddressbookTreeModel;
030: import org.columba.addressbook.gui.tree.util.SelectAddressbookFolderDialog;
031: import org.columba.addressbook.model.ContactModel;
032: import org.columba.addressbook.model.IContactModel;
033: import org.columba.addressbook.model.IContactModelPartial;
034: import org.columba.addressbook.model.IGroupModel;
035: import org.columba.api.exception.StoreException;
036:
037: /**
038: * Provides high-level contact management methods.
039: *
040: * @author fdietz
041: */
042: public final class ContactFacade implements IContactFacade {
043:
044: private static final java.util.logging.Logger LOG = java.util.logging.Logger
045: .getLogger("org.columba.addressbook.facade"); //$NON-NLS-1$
046:
047: /**
048: * @see org.columba.addressbook.facade.IContactFacade#addContact(int,
049: * IContactItem)
050: */
051: public void addContact(String uid, IContactItem contactItem)
052: throws StoreException {
053: if (contactItem == null)
054: throw new IllegalArgumentException("IContactItem is null");
055:
056: if (uid == null)
057: throw new IllegalArgumentException("uid == null");
058:
059: checkContactItemValidity(contactItem);
060:
061: AbstractFolder selectedFolder = (AbstractFolder) AddressbookTreeModel
062: .getInstance().getFolder(uid);
063:
064: IContactModel card = createContactModel(contactItem);
065:
066: try {
067: if (selectedFolder.findByEmailAddress(card
068: .getPreferredEmail()) == null)
069: selectedFolder.add(card);
070: } catch (Exception e) {
071: e.printStackTrace();
072: }
073:
074: }
075:
076: private ContactModel createContactModel(IContactItem contactItem) {
077: return new ContactModel(contactItem);
078:
079: }
080:
081: /**
082: * @see org.columba.addressbook.facade.IContactFacade#addContact(int,
083: * IContactItem)
084: */
085: public void addContacts(String uid, IContactItem[] contactItems)
086: throws StoreException {
087: if (uid == null)
088: throw new IllegalArgumentException("uid == null");
089:
090: if (contactItems == null)
091: throw new IllegalArgumentException(
092: "Zero IContactItem's were specified to add to addressbook folder");
093:
094: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
095: IContactFolder folder = (IContactFolder) model.getFolder(uid);
096:
097: for (int i = 0; i < contactItems.length; i++) {
098:
099: // skip if contact item is not valid
100: try {
101: checkContactItemValidity(contactItems[i]);
102: } catch (IllegalArgumentException e) {
103: continue;
104: }
105:
106: IContactModel card = createContactModel(contactItems[i]);
107:
108: // check if contact with given email address exists already
109: if (folder.findByEmailAddress(card.getPreferredEmail()) == null)
110: folder.add(card);
111:
112: }
113: }
114:
115: /**
116: * @see org.columba.addressbook.facade.IContactFacade#addContact(IContactItem)
117: */
118: public void addContacts(IContactItem[] contactItems)
119: throws StoreException {
120:
121: if (contactItems == null)
122: throw new IllegalArgumentException(
123: "Zero IContactItem's were specified to add to addressbook folder");
124:
125: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
126: SelectAddressbookFolderDialog dialog = new SelectAddressbookFolderDialog(
127: model);
128: if (dialog.success()) {
129: IFolder folder = dialog.getSelectedFolder();
130: String uid = folder.getId();
131:
132: addContacts(uid, contactItems);
133: } else
134: return;
135: }
136:
137: /**
138: * @see org.columba.addressbook.facade.IContactFacade#addContact(IContactItem)
139: */
140: public void addContact(IContactItem contactItem)
141: throws StoreException {
142: if (contactItem == null)
143: throw new IllegalArgumentException("IContactItem is null");
144:
145: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
146: SelectAddressbookFolderDialog dialog = new SelectAddressbookFolderDialog(
147: model);
148: if (dialog.success()) {
149: IFolder folder = dialog.getSelectedFolder();
150: String uid = folder.getId();
151:
152: addContact(uid, contactItem);
153: } else
154: return;
155: }
156:
157: private void checkContactItemValidity(IContactItem contactItem) {
158: if (contactItem.getFirstName() != null) {
159: // check if it contains comma character
160: if (contactItem.getFirstName().indexOf(",") != -1)
161: throw new IllegalArgumentException(
162: "Firstname contains illegal character <,>");
163: }
164:
165: if (contactItem.getLastName() != null) {
166: // check if it contains comma character
167: if (contactItem.getLastName().indexOf(",") != -1)
168: throw new IllegalArgumentException(
169: "Lastname contains illegal character <,>");
170: }
171:
172: }
173:
174: /**
175: * @see org.columba.addressbook.facade.IContactFacade#getAllHeaderItems(java.lang.String)
176: */
177: public List<IHeaderItem> getAllHeaderItems(String folderId,
178: boolean flattenGroupItems) throws StoreException {
179: if (folderId == null)
180: throw new IllegalArgumentException("folderId == null");
181:
182: Vector<IHeaderItem> v = new Vector<IHeaderItem>();
183: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
184: IFolder f = model.getFolder(folderId);
185: if (f == null)
186: return v;
187:
188: if (!(f instanceof IContactFolder))
189: return v;
190:
191: IContactFolder folder = (IContactFolder) f;
192: try {
193: Iterator<IContactModelPartial> it = folder
194: .getHeaderItemList().iterator();
195: while (it.hasNext()) {
196: IContactModelPartial itemPartial = it.next();
197:
198: IContactItem item = createContactItem(itemPartial);
199:
200: v.add(item);
201: }
202:
203: List<IGroupItem> groupList = getAllGroups(folderId);
204:
205: if (flattenGroupItems) {
206: // retrieve all contact items and add those to the list only
207: Iterator<IGroupItem> it2 = groupList.iterator();
208: while (it2.hasNext()) {
209: IGroupItem groupItem = it2.next();
210: List<IContactItem> l = groupItem.getContacts();
211: v.addAll(l);
212: }
213: } else {
214: // simply all all group items to the list
215: v.addAll(groupList);
216: }
217:
218: } catch (Exception e) {
219: e.printStackTrace();
220: }
221:
222: return v;
223: }
224:
225: /**
226: * @see org.columba.addressbook.facade.IContactFacade#getAllContacts(java.lang.String)
227: */
228: public List<IContactItem> getAllContacts(String folderId) {
229: if (folderId == null)
230: throw new IllegalArgumentException("folderId == null");
231:
232: Vector<IContactItem> v = new Vector<IContactItem>();
233: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
234: IFolder f = model.getFolder(folderId);
235: if (f == null)
236: return v;
237:
238: if (!(f instanceof IContactFolder))
239: return v;
240:
241: IContactFolder folder = (IContactFolder) f;
242: try {
243: Iterator<IContactModelPartial> it = folder
244: .getHeaderItemList().iterator();
245: while (it.hasNext()) {
246: IContactModelPartial contactModel = it.next();
247:
248: IContactItem item = createContactItem(contactModel);
249:
250: v.add(item);
251: }
252: } catch (Exception e) {
253: e.printStackTrace();
254: }
255:
256: return v;
257: }
258:
259: /**
260: * @see org.columba.addressbook.facade.IContactFacade#getAllGroups(java.lang.String)
261: */
262: public List<IGroupItem> getAllGroups(String folderId) {
263: if (folderId == null)
264: throw new IllegalArgumentException("folderId = null");
265:
266: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
267: IFolder f = (IFolder) model.getFolder(folderId);
268: if (f == null)
269: throw new IllegalArgumentException(
270: "contact folder does not exist");
271:
272: Vector<IGroupItem> v = new Vector<IGroupItem>();
273:
274: if (!(f instanceof IContactFolder))
275: return v;
276:
277: IContactFolder contactFolder = (IContactFolder) f;
278:
279: // add group items
280: for (int i = 0; i < f.getChildCount(); i++) {
281: IGroupFolder groupFolder = (IGroupFolder) f.getChildAt(i);
282: IGroupModel group = groupFolder.getGroup();
283:
284: IGroupItem groupItem = new GroupItem(folderId);
285: groupItem.setName(group.getName());
286: groupItem.setDescription(group.getDescription());
287:
288: String[] members = group.getMembers();
289: Map<String, IContactModelPartial> map = contactFolder
290: .getContactItemMap(members);
291: Iterator<IContactModelPartial> it = map.values().iterator();
292: while (it.hasNext()) {
293: IContactModelPartial partial = it.next();
294: IContactItem item = createContactItem(partial);
295: groupItem.addContact(item);
296: }
297: v.add(groupItem);
298: }
299:
300: return v;
301: }
302:
303: /**
304: * @see org.columba.addressbook.facade.IContactFacade#findByEmailAddress(java.lang.String,
305: * java.lang.String)
306: */
307: public String findByEmailAddress(String folderId,
308: String emailAddress) throws StoreException {
309: if (folderId == null)
310: throw new IllegalArgumentException("folderId = null");
311: if (emailAddress == null)
312: throw new IllegalArgumentException("emailAddress == null");
313:
314: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
315: IFolder f = (IFolder) model.getFolder(folderId);
316: if (f == null)
317: throw new IllegalArgumentException(
318: "contact folder does not exist");
319:
320: if (!(f instanceof IContactFolder))
321: return null;
322:
323: IContactFolder contactFolder = (IContactFolder) f;
324: String id = contactFolder.findByEmailAddress(emailAddress);
325: return id;
326: }
327:
328: /**
329: * @see org.columba.addressbook.facade.IContactFacade#findByName(java.lang.String,
330: * java.lang.String)
331: */
332: public String findByName(String folderId, String name)
333: throws StoreException, IllegalArgumentException {
334: if (folderId == null)
335: throw new IllegalArgumentException("folderId = null");
336: if (name == null)
337: throw new IllegalArgumentException("name == null");
338:
339: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
340: IFolder f = (IFolder) model.getFolder(folderId);
341: if (f == null)
342: throw new IllegalArgumentException(
343: "contact folder does not exist");
344:
345: if (!(f instanceof IContactFolder))
346: return null;
347:
348: IContactFolder contactFolder = (IContactFolder) f;
349: String id = contactFolder.findByName(name);
350: return id;
351: }
352:
353: private IContactItem createContactItem(
354: IContactModelPartial itemPartial) {
355: IContactItem item = new ContactItem(itemPartial.getId(),
356: itemPartial.getName(), itemPartial.getFirstname(),
357: itemPartial.getLastname(), itemPartial.getAddress());
358:
359: return item;
360: }
361:
362: /**
363: * @see org.columba.addressbook.facade.IContactFacade#getContactItem(java.lang.String,
364: * java.lang.String)
365: */
366: public IContactItem getContactItem(String folderId, String contactId)
367: throws StoreException, IllegalArgumentException {
368: if (folderId == null)
369: throw new IllegalArgumentException("folderId = null");
370: if (contactId == null)
371: throw new IllegalArgumentException("contactId == null");
372: AddressbookTreeModel model = AddressbookTreeModel.getInstance();
373: IFolder f = (IFolder) model.getFolder(folderId);
374: if (f == null)
375: throw new IllegalArgumentException(
376: "contact folder does not exist");
377:
378: if (!(f instanceof IContactFolder))
379: return null;
380:
381: IContactFolder contactFolder = (IContactFolder) f;
382: Map<String, IContactModelPartial> map = contactFolder
383: .getContactItemMap(new String[] { contactId });
384: if (map.isEmpty())
385: return null;
386:
387: IContactModelPartial partial = map.get(contactId);
388:
389: return createContactItem(partial);
390: }
391:
392: }
|