KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Vector JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import org.columba.addressbook.model.ContactModelFactory;
28 import org.columba.addressbook.model.ContactModelXMLFactory;
29 import org.columba.addressbook.model.IContactModel;
30 import org.columba.addressbook.model.IContactModelPartial;
31 import org.columba.api.exception.StoreException;
32 import org.columba.core.xml.XmlNewIO;
33 import org.jdom.Document;
34
35 /**
36  * Contact item cache storage.
37  *
38  * @author fdietz
39  *
40  */

41 public class ContactItemCacheStorageImpl implements ContactItemCacheStorage {
42
43     /** JDK 1.4+ logging framework logger, used for logging. */
44     private static final Logger JavaDoc LOG = Logger
45             .getLogger("org.columba.addressbook.folder");
46
47     /**
48      *
49      * keeps a list of HeaderItem's we need for the table-view
50      *
51      */

52     private Hashtable JavaDoc<String JavaDoc, IContactModelPartial> map;
53
54     /**
55      *
56      * binary file named "header"
57      *
58      */

59     private File JavaDoc headerFile;
60
61     /**
62      * directory where contact files are stored
63      */

64     private File JavaDoc directoryFile;
65
66     private AbstractFolder folder;
67
68     private boolean initialized = false;
69
70     /**
71      *
72      */

73     public ContactItemCacheStorageImpl(AbstractFolder folder) {
74         super();
75
76         this.folder = folder;
77
78         map = new Hashtable JavaDoc<String JavaDoc, IContactModelPartial>();
79
80         directoryFile = folder.getDirectoryFile();
81
82         headerFile = new File JavaDoc(directoryFile, ".header");
83
84         /*
85          * if (headerFile.exists()) { try { load(); headerCacheAlreadyLoaded =
86          * true; } catch (Exception ex) { ex.printStackTrace();
87          *
88          * headerCacheAlreadyLoaded = false; } } else { sync(); }
89          */

90     }
91
92     /**
93      * @see org.columba.addressbook.folder.ContactItemCacheStorage#getHeaderItemMap()
94      */

95     public Map JavaDoc<String JavaDoc, IContactModelPartial> getContactItemMap()
96             throws StoreException {
97         if (!initialized) {
98             initCache();
99             initialized = true;
100         }
101
102         return map;
103     }
104
105     /**
106      * @see org.columba.addressbook.folder.ContactItemCacheStorage#add(IContactModel)
107      */

108     public void add(String JavaDoc uid, IContactModelPartial item)
109             throws StoreException {
110         getContactItemMap().put(uid, item);
111
112     }
113
114     /**
115      * @see org.columba.addressbook.folder.ContactItemCacheStorage#remove(java.lang.Object)
116      */

117     public void remove(String JavaDoc uid) throws StoreException {
118         getContactItemMap().remove(uid);
119
120     }
121
122     /**
123      * @see org.columba.addressbook.folder.ContactItemCacheStorage#modify(java.lang.Object,
124      * IContactModel)
125      */

126     public void modify(String JavaDoc uid, IContactModelPartial item)
127             throws StoreException {
128         getContactItemMap().remove(item);
129         getContactItemMap().put(uid, item);
130
131     }
132
133     /**
134      * @see org.columba.addressbook.folder.ContactItemCacheStorage#save()
135      */

136     public void save() throws StoreException {
137
138     }
139
140     /**
141      * @see org.columba.addressbook.folder.ContactItemCacheStorage#load()
142      */

143     public void load() throws StoreException {
144
145     }
146
147     private void initCache() {
148
149         File JavaDoc[] list = directoryFile.listFiles();
150         List JavaDoc<File JavaDoc> v = new Vector JavaDoc<File JavaDoc>();
151
152         for (int i = 0; i < list.length; i++) {
153             File JavaDoc file = list[i];
154             File JavaDoc renamedFile;
155             String JavaDoc name = file.getName();
156             int index = name.indexOf("header");
157
158             if (index == -1) {
159
160                 if ((file.exists()) && (file.length() > 0)) {
161                     renamedFile = new File JavaDoc(file.getParentFile(),
162                             file.getName() + '~');
163                     file.renameTo(renamedFile);
164
165                     v.add(renamedFile);
166                 }
167
168             } else {
169                 // header file found
170
headerFile.delete();
171             }
172         }
173
174         for (int i = 0; i < v.size(); i++) {
175             File JavaDoc file = (File JavaDoc) v.get(i);
176
177             File JavaDoc newFile = new File JavaDoc(file.getParentFile(), (new Integer JavaDoc(i))
178                     .toString()
179                     + ".xml");
180             file.renameTo(newFile);
181             try {
182
183                 Document doc = XmlNewIO.load(newFile);
184
185                 IContactModel model = ContactModelXMLFactory.unmarshall(doc,
186                         new Integer JavaDoc(i).toString());
187
188                 IContactModelPartial item = ContactModelFactory
189                         .createContactModelPartial(model, new Integer JavaDoc(i)
190                                 .toString());
191                 map.put(new Integer JavaDoc(i).toString(), item);
192
193                 folder.setNextMessageUid(i + 1);
194             } catch (Exception JavaDoc ex) {
195                 ex.printStackTrace();
196             }
197         }
198
199         LOG.info("map-size()==" + map.size());
200     }
201
202     /**
203      * @see org.columba.addressbook.folder.ContactItemCacheStorage#count()
204      */

205     public int count() {
206         return map.size();
207     }
208
209     /**
210      * @see org.columba.addressbook.folder.ContactItemCacheStorage#exists(java.lang.Object)
211      */

212     public boolean exists(String JavaDoc uid) {
213         return map.containsKey(uid);
214     }
215
216     /**
217      * @see org.columba.addressbook.folder.ContactItemCacheStorage#getContactItemMap(java.lang.String[])
218      */

219     public Map JavaDoc<String JavaDoc, IContactModelPartial> getContactItemMap(String JavaDoc[] ids)
220             throws StoreException {
221         if (ids == null)
222             throw new IllegalArgumentException JavaDoc("ids == null");
223
224         Map JavaDoc<String JavaDoc, IContactModelPartial> result = new Hashtable JavaDoc<String JavaDoc, IContactModelPartial>();
225
226         for (int i = 0; i < ids.length; i++) {
227             // skip, if null
228
if (ids[i] == null)
229                 continue;
230
231             IContactModelPartial p = map.get(ids[i]);
232             if (p != null)
233                 result.put(ids[i], p);
234         }
235
236         return result;
237     }
238 }
Popular Tags