KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > folder > AbstractFolder


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.addressbook.folder;
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.swing.event.EventListenerList JavaDoc;
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 /**
38  * Abstract base class for every contact folder.
39  *
40  * @author fdietz
41  *
42  */

43 public abstract class AbstractFolder extends AddressbookTreeNode implements
44         IContactStorage, IContactFolder {
45
46     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
47
48     protected int nextMessageUid;
49
50     /**
51      * folder where we save everything name of folder is usually the UID-number
52      */

53     private File JavaDoc directoryFile;
54
55     /**
56      * FolderItem keeps information about the folder for example: name,
57      * accessrights, type
58      */

59
60     private ContactItemCacheStorage cacheStorage;
61
62     public AbstractFolder(String JavaDoc name, String JavaDoc dir) {
63         super(name);
64
65         if (DiskIO.ensureDirectory(dir)) {
66             directoryFile = new File JavaDoc(dir);
67         }
68
69         cacheStorage = new ContactItemCacheStorageImpl(this);
70     }
71
72     public AbstractFolder(FolderItem item) {
73         super(item);
74
75         String JavaDoc dir = DefaultConfigDirectory.getInstance().getCurrentPath()
76                 + "/addressbook/" + getId();
77
78         if (DiskIO.ensureDirectory(dir)) {
79             directoryFile = new File JavaDoc(dir);
80         }
81
82         cacheStorage = new ContactItemCacheStorageImpl(this);
83     }
84
85     /**
86      * Adds a listener.
87      */

88     public void addFolderListener(FolderListener l) {
89         listenerList.add(FolderListener.class, l);
90     }
91
92     /**
93      * Removes a previously registered listener.
94      */

95     public void removeFolderListener(FolderListener l) {
96         listenerList.remove(FolderListener.class, l);
97     }
98
99     /**
100      * Propagates an event to all registered listeners notifying them of a item
101      * addition.
102      */

103     protected void fireItemAdded(String JavaDoc uid) {
104
105         IFolderEvent e = new FolderEvent(this, null);
106         // Guaranteed to return a non-null array
107
Object JavaDoc[] listeners = listenerList.getListenerList();
108
109         // Process the listeners last to first, notifying
110
// those that are interested in this event
111
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     /**
119      * Propagates an event to all registered listeners notifying them of a item
120      * removal.
121      */

122     protected void fireItemRemoved(String JavaDoc uid) {
123
124         IFolderEvent e = new FolderEvent(this, null);
125         // Guaranteed to return a non-null array
126
Object JavaDoc[] listeners = listenerList.getListenerList();
127
128         // Process the listeners last to first, notifying
129
// those that are interested in this event
130
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     /**
138      * Propagates an event to all registered listeners notifying them of a item
139      * change.
140      */

141     protected void fireItemChanged(String JavaDoc uid) {
142
143         IFolderEvent e = new FolderEvent(this, null);
144         // Guaranteed to return a non-null array
145
Object JavaDoc[] listeners = listenerList.getListenerList();
146
147         // Process the listeners last to first, notifying
148
// those that are interested in this event
149
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     /**
157      * Returns folder where we save everything name of folder is usually the UID-number
158      *
159      * @return folder where we save everything name of folder is usually the UID-number
160      */

161     public File JavaDoc getDirectoryFile() {
162         return directoryFile;
163     }
164
165     public void createChildren(IWorkerStatusController worker) {
166     }
167
168     /**
169      * @see org.columba.addressbook.folder.IContactStorage#getHeaderItemList()
170      */

171     public Map JavaDoc<String JavaDoc, IContactModelPartial> getContactItemMap()
172             throws StoreException {
173         return cacheStorage.getContactItemMap();
174     }
175
176     /**
177      * @see org.columba.addressbook.folder.IContactFolder#getContactItemMap(java.lang.String[])
178      */

179     public Map JavaDoc<String JavaDoc, IContactModelPartial> getContactItemMap(String JavaDoc[] ids)
180             throws StoreException {
181         return cacheStorage.getContactItemMap(ids);
182     }
183
184     /**
185      * @see org.columba.addressbook.folder.IContactStorage#findByEmailAddress(java.lang.String)
186      */

187     public String JavaDoc findByEmailAddress(String JavaDoc emailAddress) throws StoreException {
188         Iterator JavaDoc it = getContactItemMap().values().iterator();
189         while (it.hasNext()) {
190             IContactModelPartial item = (IContactModelPartial) it.next();
191             String JavaDoc address = item.getAddress();
192
193             if (address.equalsIgnoreCase(emailAddress)) {
194                 String JavaDoc id = item.getId();
195                 return id;
196             }
197
198         }
199         return null;
200     }
201
202     /**
203      * @see org.columba.addressbook.folder.IContactStorage#findByName(java.lang.String)
204      */

205     public String JavaDoc findByName(String JavaDoc name) throws StoreException {
206         Iterator JavaDoc it = getContactItemMap().values().iterator();
207         while (it.hasNext()) {
208             IContactModelPartial item = (IContactModelPartial) it.next();
209             String JavaDoc sortas = item.getName();
210             String JavaDoc lastname = item.getLastname();
211             String JavaDoc firstname = item.getFirstname();
212
213             if (name.equalsIgnoreCase(sortas)) {
214                 String JavaDoc id = item.getId();
215                 return id;
216             } else if (name.equalsIgnoreCase(lastname)) {
217                 String JavaDoc id = item.getId();
218                 return id;
219             } else if (name.equalsIgnoreCase(firstname)) {
220                 String JavaDoc id = item.getId();
221                 return id;
222             }
223
224         }
225         return null;
226     }
227
228     /**
229      * save header-cache (HeaderItemList)
230      */

231     public void save() throws StoreException {
232
233     }
234
235     /**
236      * load header-cache (HeaderItemList)
237      */

238     public void load() throws StoreException {
239
240     }
241
242     /**
243      * @see javax.swing.tree.DefaultMutableTreeNode#getPathToRoot(TreeNode, int)
244      */

245     /*
246      * protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
247      * TreeNode[] retNodes;
248      *
249      * if (aNode == null) { if (depth == 0) { return null; } else { retNodes =
250      * new TreeNode[depth]; } } else { depth++; retNodes =
251      * getPathToRoot(aNode.getParent(), depth); retNodes[retNodes.length -
252      * depth] = aNode; }
253      *
254      * return retNodes; }
255      */

256
257     /**
258      * Method getTreePath.
259      *
260      * @return String
261      */

262     /*
263      * public String getTreePath() { TreeNode[] treeNode = getPathToRoot(this,
264      * 0);
265      *
266      * StringBuffer path = new StringBuffer();
267      *
268      * for (int i = 1; i < treeNode.length; i++) { AddressbookTreeNode folder =
269      * (AddressbookTreeNode) treeNode[i]; path.append("/" + folder.getName()); }
270      *
271      * return path.toString(); }
272      */

273
274     /**
275      * @see org.columba.addressbook.folder.IContactStorage#add(IContactModel)
276      */

277     public String JavaDoc add(IContactModel contact) throws StoreException {
278         String JavaDoc 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     /**
291      *
292      * @see org.columba.addressbook.folder.IContactStorage#modify(java.lang.Object,
293      * IContactModel)
294      */

295     public void modify(String JavaDoc 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     /**
306      * @see org.columba.addressbook.folder.IContactStorage#remove(java.lang.Object)
307      */

308     public void remove(String JavaDoc uid) throws StoreException {
309         cacheStorage.remove(uid);
310
311         fireItemRemoved(uid);
312     }
313
314     /**
315      * @see org.columba.addressbook.folder.IContactStorage#get(java.lang.Object)
316      */

317     public abstract IContactModel get(String JavaDoc uid) throws StoreException;
318
319     /**
320      * @see org.columba.addressbook.folder.IContactStorage#count()
321      */

322     public int count() throws StoreException {
323         return cacheStorage.count();
324     }
325
326     /**
327      * @see org.columba.addressbook.folder.IContactStorage#exists(java.lang.Object)
328      */

329     public boolean exists(String JavaDoc uid) throws StoreException {
330         return cacheStorage.exists(uid);
331     }
332
333     /**
334      * @see org.columba.addressbook.folder.IContactFolder#getHeaderItemList()
335      */

336     public List JavaDoc<IContactModelPartial> getHeaderItemList() throws StoreException {
337         // create list containing all contact item of this folder
338
List JavaDoc<IContactModelPartial> list = new ArrayList JavaDoc<IContactModelPartial>(
339                 getContactItemMap().values());
340
341         return list;
342     }
343
344     /**
345      * @return Returns the messageUid.
346      */

347     public int getNextMessageUid() {
348         return nextMessageUid;
349     }
350
351     /**
352      * @param messageUid
353      * The messageUid to set.
354      */

355     public void setNextMessageUid(int messageUid) {
356         this.nextMessageUid = messageUid;
357     }
358
359     private String JavaDoc generateNextMessageUid() {
360         return new Integer JavaDoc(nextMessageUid++).toString();
361     }
362
363     /**
364      * @see org.columba.addressbook.folder.IContactStorage#add(IContactModel[])
365      */

366     public String JavaDoc[] add(IContactModel[] contacts) throws StoreException {
367         String JavaDoc[] uids = new String JavaDoc[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