1 package org.columba.addressbook.folder; 19 20 import java.io.File ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import javax.swing.event.EventListenerList ; 27 28 import org.columba.addressbook.config.FolderItem; 29 import org.columba.addressbook.model.ContactModelFactory; 30 import org.columba.addressbook.model.IContactModel; 31 import org.columba.addressbook.model.IContactModelPartial; 32 import org.columba.api.command.IWorkerStatusController; 33 import org.columba.api.exception.StoreException; 34 import org.columba.core.config.DefaultConfigDirectory; 35 import org.columba.core.io.DiskIO; 36 37 43 public abstract class AbstractFolder extends AddressbookTreeNode implements 44 IContactStorage, IContactFolder { 45 46 protected EventListenerList listenerList = new EventListenerList (); 47 48 protected int nextMessageUid; 49 50 53 private File directoryFile; 54 55 59 60 private ContactItemCacheStorage cacheStorage; 61 62 public AbstractFolder(String name, String dir) { 63 super(name); 64 65 if (DiskIO.ensureDirectory(dir)) { 66 directoryFile = new File (dir); 67 } 68 69 cacheStorage = new ContactItemCacheStorageImpl(this); 70 } 71 72 public AbstractFolder(FolderItem item) { 73 super(item); 74 75 String dir = DefaultConfigDirectory.getInstance().getCurrentPath() 76 + "/addressbook/" + getId(); 77 78 if (DiskIO.ensureDirectory(dir)) { 79 directoryFile = new File (dir); 80 } 81 82 cacheStorage = new ContactItemCacheStorageImpl(this); 83 } 84 85 88 public void addFolderListener(FolderListener l) { 89 listenerList.add(FolderListener.class, l); 90 } 91 92 95 public void removeFolderListener(FolderListener l) { 96 listenerList.remove(FolderListener.class, l); 97 } 98 99 103 protected void fireItemAdded(String uid) { 104 105 IFolderEvent e = new FolderEvent(this, null); 106 Object [] listeners = listenerList.getListenerList(); 108 109 for (int i = listeners.length - 2; i >= 0; i -= 2) { 112 if (listeners[i] == FolderListener.class) { 113 ((FolderListener) listeners[i + 1]).itemAdded(e); 114 } 115 } 116 } 117 118 122 protected void fireItemRemoved(String uid) { 123 124 IFolderEvent e = new FolderEvent(this, null); 125 Object [] listeners = listenerList.getListenerList(); 127 128 for (int i = listeners.length - 2; i >= 0; i -= 2) { 131 if (listeners[i] == FolderListener.class) { 132 ((FolderListener) listeners[i + 1]).itemRemoved(e); 133 } 134 } 135 } 136 137 141 protected void fireItemChanged(String uid) { 142 143 IFolderEvent e = new FolderEvent(this, null); 144 Object [] listeners = listenerList.getListenerList(); 146 147 for (int i = listeners.length - 2; i >= 0; i -= 2) { 150 if (listeners[i] == FolderListener.class) { 151 ((FolderListener) listeners[i + 1]).itemChanged(e); 152 } 153 } 154 } 155 156 161 public File getDirectoryFile() { 162 return directoryFile; 163 } 164 165 public void createChildren(IWorkerStatusController worker) { 166 } 167 168 171 public Map <String , IContactModelPartial> getContactItemMap() 172 throws StoreException { 173 return cacheStorage.getContactItemMap(); 174 } 175 176 179 public Map <String , IContactModelPartial> getContactItemMap(String [] ids) 180 throws StoreException { 181 return cacheStorage.getContactItemMap(ids); 182 } 183 184 187 public String findByEmailAddress(String emailAddress) throws StoreException { 188 Iterator it = getContactItemMap().values().iterator(); 189 while (it.hasNext()) { 190 IContactModelPartial item = (IContactModelPartial) it.next(); 191 String address = item.getAddress(); 192 193 if (address.equalsIgnoreCase(emailAddress)) { 194 String id = item.getId(); 195 return id; 196 } 197 198 } 199 return null; 200 } 201 202 205 public String findByName(String name) throws StoreException { 206 Iterator it = getContactItemMap().values().iterator(); 207 while (it.hasNext()) { 208 IContactModelPartial item = (IContactModelPartial) it.next(); 209 String sortas = item.getName(); 210 String lastname = item.getLastname(); 211 String firstname = item.getFirstname(); 212 213 if (name.equalsIgnoreCase(sortas)) { 214 String id = item.getId(); 215 return id; 216 } else if (name.equalsIgnoreCase(lastname)) { 217 String id = item.getId(); 218 return id; 219 } else if (name.equalsIgnoreCase(firstname)) { 220 String id = item.getId(); 221 return id; 222 } 223 224 } 225 return null; 226 } 227 228 231 public void save() throws StoreException { 232 233 } 234 235 238 public void load() throws StoreException { 239 240 } 241 242 245 256 257 262 273 274 277 public String add(IContactModel contact) throws StoreException { 278 String uid = generateNextMessageUid(); 279 280 IContactModelPartial item = ContactModelFactory 281 .createContactModelPartial(contact, uid); 282 283 cacheStorage.add(uid, item); 284 285 fireItemAdded(uid); 286 287 return uid; 288 } 289 290 295 public void modify(String uid, IContactModel contact) throws StoreException { 296 297 IContactModelPartial item = ContactModelFactory 298 .createContactModelPartial(contact, uid); 299 300 cacheStorage.modify(uid, item); 301 302 fireItemChanged(uid); 303 } 304 305 308 public void remove(String uid) throws StoreException { 309 cacheStorage.remove(uid); 310 311 fireItemRemoved(uid); 312 } 313 314 317 public abstract IContactModel get(String uid) throws StoreException; 318 319 322 public int count() throws StoreException { 323 return cacheStorage.count(); 324 } 325 326 329 public boolean exists(String uid) throws StoreException { 330 return cacheStorage.exists(uid); 331 } 332 333 336 public List <IContactModelPartial> getHeaderItemList() throws StoreException { 337 List <IContactModelPartial> list = new ArrayList <IContactModelPartial>( 339 getContactItemMap().values()); 340 341 return list; 342 } 343 344 347 public int getNextMessageUid() { 348 return nextMessageUid; 349 } 350 351 355 public void setNextMessageUid(int messageUid) { 356 this.nextMessageUid = messageUid; 357 } 358 359 private String generateNextMessageUid() { 360 return new Integer (nextMessageUid++).toString(); 361 } 362 363 366 public String [] add(IContactModel[] contacts) throws StoreException { 367 String [] uids = new String [contacts.length]; 368 for (int i = 0; i < contacts.length; i++) 369 uids[i] = add(contacts[i]); 370 371 return uids; 372 } 373 374 }
| Popular Tags
|