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.folder;
019:
020: import java.io.File;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.swing.event.EventListenerList;
027:
028: import org.columba.addressbook.config.FolderItem;
029: import org.columba.addressbook.model.ContactModelFactory;
030: import org.columba.addressbook.model.IContactModel;
031: import org.columba.addressbook.model.IContactModelPartial;
032: import org.columba.api.command.IWorkerStatusController;
033: import org.columba.api.exception.StoreException;
034: import org.columba.core.config.DefaultConfigDirectory;
035: import org.columba.core.io.DiskIO;
036:
037: /**
038: * Abstract base class for every contact folder.
039: *
040: * @author fdietz
041: *
042: */
043: public abstract class AbstractFolder extends AddressbookTreeNode
044: implements IContactStorage, IContactFolder {
045:
046: protected EventListenerList listenerList = new EventListenerList();
047:
048: protected int nextMessageUid;
049:
050: /**
051: * folder where we save everything name of folder is usually the UID-number
052: */
053: private File directoryFile;
054:
055: /**
056: * FolderItem keeps information about the folder for example: name,
057: * accessrights, type
058: */
059:
060: private ContactItemCacheStorage cacheStorage;
061:
062: public AbstractFolder(String name, String dir) {
063: super (name);
064:
065: if (DiskIO.ensureDirectory(dir)) {
066: directoryFile = new File(dir);
067: }
068:
069: cacheStorage = new ContactItemCacheStorageImpl(this );
070: }
071:
072: public AbstractFolder(FolderItem item) {
073: super (item);
074:
075: String dir = DefaultConfigDirectory.getInstance()
076: .getCurrentPath()
077: + "/addressbook/" + getId();
078:
079: if (DiskIO.ensureDirectory(dir)) {
080: directoryFile = new File(dir);
081: }
082:
083: cacheStorage = new ContactItemCacheStorageImpl(this );
084: }
085:
086: /**
087: * Adds a listener.
088: */
089: public void addFolderListener(FolderListener l) {
090: listenerList.add(FolderListener.class, l);
091: }
092:
093: /**
094: * Removes a previously registered listener.
095: */
096: public void removeFolderListener(FolderListener l) {
097: listenerList.remove(FolderListener.class, l);
098: }
099:
100: /**
101: * Propagates an event to all registered listeners notifying them of a item
102: * addition.
103: */
104: protected void fireItemAdded(String uid) {
105:
106: IFolderEvent e = new FolderEvent(this , null);
107: // Guaranteed to return a non-null array
108: Object[] listeners = listenerList.getListenerList();
109:
110: // Process the listeners last to first, notifying
111: // those that are interested in this event
112: for (int i = listeners.length - 2; i >= 0; i -= 2) {
113: if (listeners[i] == FolderListener.class) {
114: ((FolderListener) listeners[i + 1]).itemAdded(e);
115: }
116: }
117: }
118:
119: /**
120: * Propagates an event to all registered listeners notifying them of a item
121: * removal.
122: */
123: protected void fireItemRemoved(String uid) {
124:
125: IFolderEvent e = new FolderEvent(this , null);
126: // Guaranteed to return a non-null array
127: Object[] listeners = listenerList.getListenerList();
128:
129: // Process the listeners last to first, notifying
130: // those that are interested in this event
131: for (int i = listeners.length - 2; i >= 0; i -= 2) {
132: if (listeners[i] == FolderListener.class) {
133: ((FolderListener) listeners[i + 1]).itemRemoved(e);
134: }
135: }
136: }
137:
138: /**
139: * Propagates an event to all registered listeners notifying them of a item
140: * change.
141: */
142: protected void fireItemChanged(String uid) {
143:
144: IFolderEvent e = new FolderEvent(this , null);
145: // Guaranteed to return a non-null array
146: Object[] listeners = listenerList.getListenerList();
147:
148: // Process the listeners last to first, notifying
149: // those that are interested in this event
150: for (int i = listeners.length - 2; i >= 0; i -= 2) {
151: if (listeners[i] == FolderListener.class) {
152: ((FolderListener) listeners[i + 1]).itemChanged(e);
153: }
154: }
155: }
156:
157: /**
158: * Returns folder where we save everything name of folder is usually the UID-number
159: *
160: * @return folder where we save everything name of folder is usually the UID-number
161: */
162: public File getDirectoryFile() {
163: return directoryFile;
164: }
165:
166: public void createChildren(IWorkerStatusController worker) {
167: }
168:
169: /**
170: * @see org.columba.addressbook.folder.IContactStorage#getHeaderItemList()
171: */
172: public Map<String, IContactModelPartial> getContactItemMap()
173: throws StoreException {
174: return cacheStorage.getContactItemMap();
175: }
176:
177: /**
178: * @see org.columba.addressbook.folder.IContactFolder#getContactItemMap(java.lang.String[])
179: */
180: public Map<String, IContactModelPartial> getContactItemMap(
181: String[] ids) throws StoreException {
182: return cacheStorage.getContactItemMap(ids);
183: }
184:
185: /**
186: * @see org.columba.addressbook.folder.IContactStorage#findByEmailAddress(java.lang.String)
187: */
188: public String findByEmailAddress(String emailAddress)
189: throws StoreException {
190: Iterator it = getContactItemMap().values().iterator();
191: while (it.hasNext()) {
192: IContactModelPartial item = (IContactModelPartial) it
193: .next();
194: String address = item.getAddress();
195:
196: if (address.equalsIgnoreCase(emailAddress)) {
197: String id = item.getId();
198: return id;
199: }
200:
201: }
202: return null;
203: }
204:
205: /**
206: * @see org.columba.addressbook.folder.IContactStorage#findByName(java.lang.String)
207: */
208: public String findByName(String name) throws StoreException {
209: Iterator it = getContactItemMap().values().iterator();
210: while (it.hasNext()) {
211: IContactModelPartial item = (IContactModelPartial) it
212: .next();
213: String sortas = item.getName();
214: String lastname = item.getLastname();
215: String firstname = item.getFirstname();
216:
217: if (name.equalsIgnoreCase(sortas)) {
218: String id = item.getId();
219: return id;
220: } else if (name.equalsIgnoreCase(lastname)) {
221: String id = item.getId();
222: return id;
223: } else if (name.equalsIgnoreCase(firstname)) {
224: String id = item.getId();
225: return id;
226: }
227:
228: }
229: return null;
230: }
231:
232: /**
233: * save header-cache (HeaderItemList)
234: */
235: public void save() throws StoreException {
236:
237: }
238:
239: /**
240: * load header-cache (HeaderItemList)
241: */
242: public void load() throws StoreException {
243:
244: }
245:
246: /**
247: * @see javax.swing.tree.DefaultMutableTreeNode#getPathToRoot(TreeNode, int)
248: */
249: /*
250: * protected TreeNode[] getPathToRoot(TreeNode aNode, int depth) {
251: * TreeNode[] retNodes;
252: *
253: * if (aNode == null) { if (depth == 0) { return null; } else { retNodes =
254: * new TreeNode[depth]; } } else { depth++; retNodes =
255: * getPathToRoot(aNode.getParent(), depth); retNodes[retNodes.length -
256: * depth] = aNode; }
257: *
258: * return retNodes; }
259: */
260:
261: /**
262: * Method getTreePath.
263: *
264: * @return String
265: */
266: /*
267: * public String getTreePath() { TreeNode[] treeNode = getPathToRoot(this,
268: * 0);
269: *
270: * StringBuffer path = new StringBuffer();
271: *
272: * for (int i = 1; i < treeNode.length; i++) { AddressbookTreeNode folder =
273: * (AddressbookTreeNode) treeNode[i]; path.append("/" + folder.getName()); }
274: *
275: * return path.toString(); }
276: */
277:
278: /**
279: * @see org.columba.addressbook.folder.IContactStorage#add(IContactModel)
280: */
281: public String add(IContactModel contact) throws StoreException {
282: String uid = generateNextMessageUid();
283:
284: IContactModelPartial item = ContactModelFactory
285: .createContactModelPartial(contact, uid);
286:
287: cacheStorage.add(uid, item);
288:
289: fireItemAdded(uid);
290:
291: return uid;
292: }
293:
294: /**
295: *
296: * @see org.columba.addressbook.folder.IContactStorage#modify(java.lang.Object,
297: * IContactModel)
298: */
299: public void modify(String uid, IContactModel contact)
300: throws StoreException {
301:
302: IContactModelPartial item = ContactModelFactory
303: .createContactModelPartial(contact, uid);
304:
305: cacheStorage.modify(uid, item);
306:
307: fireItemChanged(uid);
308: }
309:
310: /**
311: * @see org.columba.addressbook.folder.IContactStorage#remove(java.lang.Object)
312: */
313: public void remove(String uid) throws StoreException {
314: cacheStorage.remove(uid);
315:
316: fireItemRemoved(uid);
317: }
318:
319: /**
320: * @see org.columba.addressbook.folder.IContactStorage#get(java.lang.Object)
321: */
322: public abstract IContactModel get(String uid) throws StoreException;
323:
324: /**
325: * @see org.columba.addressbook.folder.IContactStorage#count()
326: */
327: public int count() throws StoreException {
328: return cacheStorage.count();
329: }
330:
331: /**
332: * @see org.columba.addressbook.folder.IContactStorage#exists(java.lang.Object)
333: */
334: public boolean exists(String uid) throws StoreException {
335: return cacheStorage.exists(uid);
336: }
337:
338: /**
339: * @see org.columba.addressbook.folder.IContactFolder#getHeaderItemList()
340: */
341: public List<IContactModelPartial> getHeaderItemList()
342: throws StoreException {
343: // create list containing all contact item of this folder
344: List<IContactModelPartial> list = new ArrayList<IContactModelPartial>(
345: getContactItemMap().values());
346:
347: return list;
348: }
349:
350: /**
351: * @return Returns the messageUid.
352: */
353: public int getNextMessageUid() {
354: return nextMessageUid;
355: }
356:
357: /**
358: * @param messageUid
359: * The messageUid to set.
360: */
361: public void setNextMessageUid(int messageUid) {
362: this .nextMessageUid = messageUid;
363: }
364:
365: private String generateNextMessageUid() {
366: return new Integer(nextMessageUid++).toString();
367: }
368:
369: /**
370: * @see org.columba.addressbook.folder.IContactStorage#add(IContactModel[])
371: */
372: public String[] add(IContactModel[] contacts) throws StoreException {
373: String[] uids = new String[contacts.length];
374: for (int i = 0; i < contacts.length; i++)
375: uids[i] = add(contacts[i]);
376:
377: return uids;
378: }
379:
380: }
|