1 package org.columba.addressbook.facade; 19 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Vector ; 24 25 import org.columba.addressbook.folder.AbstractFolder; 26 import org.columba.addressbook.folder.IContactFolder; 27 import org.columba.addressbook.folder.IFolder; 28 import org.columba.addressbook.folder.IGroupFolder; 29 import org.columba.addressbook.gui.tree.AddressbookTreeModel; 30 import org.columba.addressbook.gui.tree.util.SelectAddressbookFolderDialog; 31 import org.columba.addressbook.model.ContactModel; 32 import org.columba.addressbook.model.IContactModel; 33 import org.columba.addressbook.model.IContactModelPartial; 34 import org.columba.addressbook.model.IGroupModel; 35 import org.columba.api.exception.StoreException; 36 37 42 public final class ContactFacade implements IContactFacade { 43 44 private static final java.util.logging.Logger LOG = java.util.logging.Logger 45 .getLogger("org.columba.addressbook.facade"); 47 51 public void addContact(String uid, IContactItem contactItem) 52 throws StoreException { 53 if (contactItem == null) 54 throw new IllegalArgumentException ("IContactItem is null"); 55 56 if (uid == null) 57 throw new IllegalArgumentException ("uid == null"); 58 59 checkContactItemValidity(contactItem); 60 61 AbstractFolder selectedFolder = (AbstractFolder) AddressbookTreeModel 62 .getInstance().getFolder(uid); 63 64 IContactModel card = createContactModel(contactItem); 65 66 try { 67 if (selectedFolder.findByEmailAddress(card.getPreferredEmail()) == null) 68 selectedFolder.add(card); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 73 } 74 75 private ContactModel createContactModel(IContactItem contactItem) { 76 return new ContactModel(contactItem); 77 78 } 79 80 84 public void addContacts(String uid, IContactItem[] contactItems) 85 throws StoreException { 86 if (uid == null) 87 throw new IllegalArgumentException ("uid == null"); 88 89 if (contactItems == null) 90 throw new IllegalArgumentException ( 91 "Zero IContactItem's were specified to add to addressbook folder"); 92 93 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 94 IContactFolder folder = (IContactFolder) model.getFolder(uid); 95 96 for (int i = 0; i < contactItems.length; i++) { 97 98 try { 100 checkContactItemValidity(contactItems[i]); 101 } 102 catch (IllegalArgumentException e) { 103 continue; 104 } 105 106 IContactModel card = createContactModel(contactItems[i]); 107 108 if (folder.findByEmailAddress(card.getPreferredEmail()) == null) 110 folder.add(card); 111 112 } 113 } 114 115 118 public void addContacts(IContactItem[] contactItems) throws StoreException { 119 120 if (contactItems == null) 121 throw new IllegalArgumentException ( 122 "Zero IContactItem's were specified to add to addressbook folder"); 123 124 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 125 SelectAddressbookFolderDialog dialog = new SelectAddressbookFolderDialog( 126 model); 127 if (dialog.success()) { 128 IFolder folder = dialog.getSelectedFolder(); 129 String uid = folder.getId(); 130 131 addContacts(uid, contactItems); 132 } else 133 return; 134 } 135 136 139 public void addContact(IContactItem contactItem) throws StoreException { 140 if (contactItem == null) 141 throw new IllegalArgumentException ("IContactItem is null"); 142 143 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 144 SelectAddressbookFolderDialog dialog = new SelectAddressbookFolderDialog( 145 model); 146 if (dialog.success()) { 147 IFolder folder = dialog.getSelectedFolder(); 148 String uid = folder.getId(); 149 150 addContact(uid, contactItem); 151 } else 152 return; 153 } 154 155 private void checkContactItemValidity(IContactItem contactItem) { 156 if (contactItem.getFirstName() != null) { 157 if (contactItem.getFirstName().indexOf(",") != -1) 159 throw new IllegalArgumentException ( 160 "Firstname contains illegal character <,>"); 161 } 162 163 if (contactItem.getLastName() != null) { 164 if (contactItem.getLastName().indexOf(",") != -1) 166 throw new IllegalArgumentException ( 167 "Lastname contains illegal character <,>"); 168 } 169 170 } 171 172 175 public List <IHeaderItem> getAllHeaderItems(String folderId, 176 boolean flattenGroupItems) throws StoreException { 177 if (folderId == null) 178 throw new IllegalArgumentException ("folderId == null"); 179 180 Vector <IHeaderItem> v = new Vector <IHeaderItem>(); 181 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 182 IFolder f = model.getFolder(folderId); 183 if (f == null) 184 return v; 185 186 if (!(f instanceof IContactFolder)) 187 return v; 188 189 IContactFolder folder = (IContactFolder) f; 190 try { 191 Iterator <IContactModelPartial> it = folder.getHeaderItemList() 192 .iterator(); 193 while (it.hasNext()) { 194 IContactModelPartial itemPartial = it.next(); 195 196 IContactItem item = createContactItem(itemPartial); 197 198 v.add(item); 199 } 200 201 List <IGroupItem> groupList = getAllGroups(folderId); 202 203 if (flattenGroupItems) { 204 Iterator <IGroupItem> it2 = groupList.iterator(); 206 while (it2.hasNext()) { 207 IGroupItem groupItem = it2.next(); 208 List <IContactItem> l = groupItem.getContacts(); 209 v.addAll(l); 210 } 211 } else { 212 v.addAll(groupList); 214 } 215 216 } catch (Exception e) { 217 e.printStackTrace(); 218 } 219 220 return v; 221 } 222 223 226 public List <IContactItem> getAllContacts(String folderId) { 227 if (folderId == null) 228 throw new IllegalArgumentException ("folderId == null"); 229 230 Vector <IContactItem> v = new Vector <IContactItem>(); 231 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 232 IFolder f = model.getFolder(folderId); 233 if (f == null) 234 return v; 235 236 if (!(f instanceof IContactFolder)) 237 return v; 238 239 IContactFolder folder = (IContactFolder) f; 240 try { 241 Iterator <IContactModelPartial> it = folder.getHeaderItemList() 242 .iterator(); 243 while (it.hasNext()) { 244 IContactModelPartial contactModel = it.next(); 245 246 IContactItem item = createContactItem(contactModel); 247 248 v.add(item); 249 } 250 } catch (Exception e) { 251 e.printStackTrace(); 252 } 253 254 return v; 255 } 256 257 260 public List <IGroupItem> getAllGroups(String folderId) { 261 if (folderId == null) 262 throw new IllegalArgumentException ("folderId = null"); 263 264 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 265 IFolder f = (IFolder) model.getFolder(folderId); 266 if (f == null) 267 throw new IllegalArgumentException ("contact folder does not exist"); 268 269 Vector <IGroupItem> v = new Vector <IGroupItem>(); 270 271 if (!(f instanceof IContactFolder)) 272 return v; 273 274 IContactFolder contactFolder = (IContactFolder) f; 275 276 for (int i = 0; i < f.getChildCount(); i++) { 278 IGroupFolder groupFolder = (IGroupFolder) f.getChildAt(i); 279 IGroupModel group = groupFolder.getGroup(); 280 281 IGroupItem groupItem = new GroupItem(folderId); 282 groupItem.setName(group.getName()); 283 groupItem.setDescription(group.getDescription()); 284 285 String [] members = group.getMembers(); 286 Map <String , IContactModelPartial> map = contactFolder 287 .getContactItemMap(members); 288 Iterator <IContactModelPartial> it = map.values().iterator(); 289 while (it.hasNext()) { 290 IContactModelPartial partial = it.next(); 291 IContactItem item = createContactItem(partial); 292 groupItem.addContact(item); 293 } 294 v.add(groupItem); 295 } 296 297 return v; 298 } 299 300 304 public String findByEmailAddress(String folderId, String emailAddress) 305 throws StoreException { 306 if (folderId == null) 307 throw new IllegalArgumentException ("folderId = null"); 308 if (emailAddress == null) 309 throw new IllegalArgumentException ("emailAddress == null"); 310 311 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 312 IFolder f = (IFolder) model.getFolder(folderId); 313 if (f == null) 314 throw new IllegalArgumentException ("contact folder does not exist"); 315 316 if (!(f instanceof IContactFolder)) 317 return null; 318 319 IContactFolder contactFolder = (IContactFolder) f; 320 String id = contactFolder.findByEmailAddress(emailAddress); 321 return id; 322 } 323 324 328 public String findByName(String folderId, String name) 329 throws StoreException, IllegalArgumentException { 330 if (folderId == null) 331 throw new IllegalArgumentException ("folderId = null"); 332 if (name == null) 333 throw new IllegalArgumentException ("name == null"); 334 335 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 336 IFolder f = (IFolder) model.getFolder(folderId); 337 if (f == null) 338 throw new IllegalArgumentException ("contact folder does not exist"); 339 340 if (!(f instanceof IContactFolder)) 341 return null; 342 343 IContactFolder contactFolder = (IContactFolder) f; 344 String id = contactFolder.findByName(name); 345 return id; 346 } 347 348 private IContactItem createContactItem(IContactModelPartial itemPartial) { 349 IContactItem item = new ContactItem(itemPartial.getId(), itemPartial 350 .getName(), itemPartial.getFirstname(), itemPartial 351 .getLastname(), itemPartial.getAddress()); 352 353 return item; 354 } 355 356 360 public IContactItem getContactItem(String folderId, String contactId) 361 throws StoreException, IllegalArgumentException { 362 if (folderId == null) 363 throw new IllegalArgumentException ("folderId = null"); 364 if (contactId == null) 365 throw new IllegalArgumentException ("contactId == null"); 366 AddressbookTreeModel model = AddressbookTreeModel.getInstance(); 367 IFolder f = (IFolder) model.getFolder(folderId); 368 if (f == null) 369 throw new IllegalArgumentException ("contact folder does not exist"); 370 371 if (!(f instanceof IContactFolder)) 372 return null; 373 374 IContactFolder contactFolder = (IContactFolder) f; 375 Map <String , IContactModelPartial> map = contactFolder 376 .getContactItemMap(new String [] { contactId }); 377 if (map.isEmpty()) 378 return null; 379 380 IContactModelPartial partial = map.get(contactId); 381 382 return createContactItem(partial); 383 } 384 385 } | Popular Tags |