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.Hashtable;
022: import java.util.List;
023: import java.util.Map;
024: import java.util.Vector;
025: import java.util.logging.Logger;
026:
027: import org.columba.addressbook.model.ContactModelFactory;
028: import org.columba.addressbook.model.ContactModelXMLFactory;
029: import org.columba.addressbook.model.IContactModel;
030: import org.columba.addressbook.model.IContactModelPartial;
031: import org.columba.api.exception.StoreException;
032: import org.columba.core.xml.XmlNewIO;
033: import org.jdom.Document;
034:
035: /**
036: * Contact item cache storage.
037: *
038: * @author fdietz
039: *
040: */
041: public class ContactItemCacheStorageImpl implements
042: ContactItemCacheStorage {
043:
044: /** JDK 1.4+ logging framework logger, used for logging. */
045: private static final Logger LOG = Logger
046: .getLogger("org.columba.addressbook.folder");
047:
048: /**
049: *
050: * keeps a list of HeaderItem's we need for the table-view
051: *
052: */
053: private Hashtable<String, IContactModelPartial> map;
054:
055: /**
056: *
057: * binary file named "header"
058: *
059: */
060: private File headerFile;
061:
062: /**
063: * directory where contact files are stored
064: */
065: private File directoryFile;
066:
067: private AbstractFolder folder;
068:
069: private boolean initialized = false;
070:
071: /**
072: *
073: */
074: public ContactItemCacheStorageImpl(AbstractFolder folder) {
075: super ();
076:
077: this .folder = folder;
078:
079: map = new Hashtable<String, IContactModelPartial>();
080:
081: directoryFile = folder.getDirectoryFile();
082:
083: headerFile = new File(directoryFile, ".header");
084:
085: /*
086: * if (headerFile.exists()) { try { load(); headerCacheAlreadyLoaded =
087: * true; } catch (Exception ex) { ex.printStackTrace();
088: *
089: * headerCacheAlreadyLoaded = false; } } else { sync(); }
090: */
091: }
092:
093: /**
094: * @see org.columba.addressbook.folder.ContactItemCacheStorage#getHeaderItemMap()
095: */
096: public Map<String, IContactModelPartial> getContactItemMap()
097: throws StoreException {
098: if (!initialized) {
099: initCache();
100: initialized = true;
101: }
102:
103: return map;
104: }
105:
106: /**
107: * @see org.columba.addressbook.folder.ContactItemCacheStorage#add(IContactModel)
108: */
109: public void add(String uid, IContactModelPartial item)
110: throws StoreException {
111: getContactItemMap().put(uid, item);
112:
113: }
114:
115: /**
116: * @see org.columba.addressbook.folder.ContactItemCacheStorage#remove(java.lang.Object)
117: */
118: public void remove(String uid) throws StoreException {
119: getContactItemMap().remove(uid);
120:
121: }
122:
123: /**
124: * @see org.columba.addressbook.folder.ContactItemCacheStorage#modify(java.lang.Object,
125: * IContactModel)
126: */
127: public void modify(String uid, IContactModelPartial item)
128: throws StoreException {
129: getContactItemMap().remove(item);
130: getContactItemMap().put(uid, item);
131:
132: }
133:
134: /**
135: * @see org.columba.addressbook.folder.ContactItemCacheStorage#save()
136: */
137: public void save() throws StoreException {
138:
139: }
140:
141: /**
142: * @see org.columba.addressbook.folder.ContactItemCacheStorage#load()
143: */
144: public void load() throws StoreException {
145:
146: }
147:
148: private void initCache() {
149:
150: File[] list = directoryFile.listFiles();
151: List<File> v = new Vector<File>();
152:
153: for (int i = 0; i < list.length; i++) {
154: File file = list[i];
155: File renamedFile;
156: String name = file.getName();
157: int index = name.indexOf("header");
158:
159: if (index == -1) {
160:
161: if ((file.exists()) && (file.length() > 0)) {
162: renamedFile = new File(file.getParentFile(), file
163: .getName() + '~');
164: file.renameTo(renamedFile);
165:
166: v.add(renamedFile);
167: }
168:
169: } else {
170: // header file found
171: headerFile.delete();
172: }
173: }
174:
175: for (int i = 0; i < v.size(); i++) {
176: File file = (File) v.get(i);
177:
178: File newFile = new File(file.getParentFile(), (new Integer(
179: i)).toString()
180: + ".xml");
181: file.renameTo(newFile);
182: try {
183:
184: Document doc = XmlNewIO.load(newFile);
185:
186: IContactModel model = ContactModelXMLFactory
187: .unmarshall(doc, new Integer(i).toString());
188:
189: IContactModelPartial item = ContactModelFactory
190: .createContactModelPartial(model,
191: new Integer(i).toString());
192: map.put(new Integer(i).toString(), item);
193:
194: folder.setNextMessageUid(i + 1);
195: } catch (Exception ex) {
196: ex.printStackTrace();
197: }
198: }
199:
200: LOG.info("map-size()==" + map.size());
201: }
202:
203: /**
204: * @see org.columba.addressbook.folder.ContactItemCacheStorage#count()
205: */
206: public int count() {
207: return map.size();
208: }
209:
210: /**
211: * @see org.columba.addressbook.folder.ContactItemCacheStorage#exists(java.lang.Object)
212: */
213: public boolean exists(String uid) {
214: return map.containsKey(uid);
215: }
216:
217: /**
218: * @see org.columba.addressbook.folder.ContactItemCacheStorage#getContactItemMap(java.lang.String[])
219: */
220: public Map<String, IContactModelPartial> getContactItemMap(
221: String[] ids) throws StoreException {
222: if (ids == null)
223: throw new IllegalArgumentException("ids == null");
224:
225: Map<String, IContactModelPartial> result = new Hashtable<String, IContactModelPartial>();
226:
227: for (int i = 0; i < ids.length; i++) {
228: // skip, if null
229: if (ids[i] == null)
230: continue;
231:
232: IContactModelPartial p = map.get(ids[i]);
233: if (p != null)
234: result.put(ids[i], p);
235: }
236:
237: return result;
238: }
239: }
|