1 package org.columba.addressbook.folder; 19 20 import java.io.File ; 21 import java.util.Hashtable ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Vector ; 25 import java.util.logging.Logger ; 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 41 public class ContactItemCacheStorageImpl implements ContactItemCacheStorage { 42 43 44 private static final Logger LOG = Logger 45 .getLogger("org.columba.addressbook.folder"); 46 47 52 private Hashtable <String , IContactModelPartial> map; 53 54 59 private File headerFile; 60 61 64 private File directoryFile; 65 66 private AbstractFolder folder; 67 68 private boolean initialized = false; 69 70 73 public ContactItemCacheStorageImpl(AbstractFolder folder) { 74 super(); 75 76 this.folder = folder; 77 78 map = new Hashtable <String , IContactModelPartial>(); 79 80 directoryFile = folder.getDirectoryFile(); 81 82 headerFile = new File (directoryFile, ".header"); 83 84 90 } 91 92 95 public Map <String , IContactModelPartial> getContactItemMap() 96 throws StoreException { 97 if (!initialized) { 98 initCache(); 99 initialized = true; 100 } 101 102 return map; 103 } 104 105 108 public void add(String uid, IContactModelPartial item) 109 throws StoreException { 110 getContactItemMap().put(uid, item); 111 112 } 113 114 117 public void remove(String uid) throws StoreException { 118 getContactItemMap().remove(uid); 119 120 } 121 122 126 public void modify(String uid, IContactModelPartial item) 127 throws StoreException { 128 getContactItemMap().remove(item); 129 getContactItemMap().put(uid, item); 130 131 } 132 133 136 public void save() throws StoreException { 137 138 } 139 140 143 public void load() throws StoreException { 144 145 } 146 147 private void initCache() { 148 149 File [] list = directoryFile.listFiles(); 150 List <File > v = new Vector <File >(); 151 152 for (int i = 0; i < list.length; i++) { 153 File file = list[i]; 154 File renamedFile; 155 String 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 (file.getParentFile(), 162 file.getName() + '~'); 163 file.renameTo(renamedFile); 164 165 v.add(renamedFile); 166 } 167 168 } else { 169 headerFile.delete(); 171 } 172 } 173 174 for (int i = 0; i < v.size(); i++) { 175 File file = (File ) v.get(i); 176 177 File newFile = new File (file.getParentFile(), (new Integer (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 (i).toString()); 187 188 IContactModelPartial item = ContactModelFactory 189 .createContactModelPartial(model, new Integer (i) 190 .toString()); 191 map.put(new Integer (i).toString(), item); 192 193 folder.setNextMessageUid(i + 1); 194 } catch (Exception ex) { 195 ex.printStackTrace(); 196 } 197 } 198 199 LOG.info("map-size()==" + map.size()); 200 } 201 202 205 public int count() { 206 return map.size(); 207 } 208 209 212 public boolean exists(String uid) { 213 return map.containsKey(uid); 214 } 215 216 219 public Map <String , IContactModelPartial> getContactItemMap(String [] ids) 220 throws StoreException { 221 if (ids == null) 222 throw new IllegalArgumentException ("ids == null"); 223 224 Map <String , IContactModelPartial> result = new Hashtable <String , IContactModelPartial>(); 225 226 for (int i = 0; i < ids.length; i++) { 227 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 |