1 package org.columba.calendar.store; 19 20 import java.io.File ; 21 import java.util.ArrayList ; 22 import java.util.Calendar ; 23 import java.util.Iterator ; 24 import java.util.Vector ; 25 26 import org.columba.calendar.base.UUIDGenerator; 27 import org.columba.calendar.model.ComponentInfoList; 28 import org.columba.calendar.model.DateRange; 29 import org.columba.calendar.model.api.IComponent; 30 import org.columba.calendar.model.api.IComponentInfo; 31 import org.columba.calendar.model.api.IComponentInfoList; 32 import org.columba.calendar.model.api.IDateRange; 33 import org.columba.calendar.model.api.IEvent; 34 import org.columba.calendar.model.api.IEventInfo; 35 import org.columba.calendar.model.api.ITodo; 36 import org.columba.calendar.parser.SyntaxException; 37 import org.columba.calendar.parser.VCalendarModelFactory; 38 import org.columba.calendar.store.api.ICalendarStore; 39 import org.columba.calendar.store.api.StoreException; 40 import org.columba.core.io.DiskIO; 41 import org.jdom.Document; 42 43 public class LocalCalendarStore extends AbstractCalendarStore implements 44 ICalendarStore { 45 46 private LocalXMLFileStore dataStorage; 47 48 public LocalCalendarStore(File directory) throws StoreException { 49 super(); 50 51 if (directory == null) 52 throw new IllegalArgumentException ("directory == null"); 53 54 DiskIO.ensureDirectory(directory); 55 56 dataStorage = new LocalXMLFileStore(directory); 57 } 58 59 62 public void add(IComponentInfo basicModel) throws StoreException { 63 64 if (basicModel == null) 65 throw new IllegalArgumentException ("basicModel == null"); 66 67 String id = basicModel.getId(); 68 if (id == null) 70 id = new UUIDGenerator().newUUID(); 71 72 Document document = null; 73 try { 74 document = VCalendarModelFactory.marshall(basicModel); 75 } catch (SyntaxException e) { 76 throw new StoreException(e); 77 } catch (IllegalArgumentException e) { 78 throw new StoreException(e); 79 } 80 81 dataStorage.save(id, document); 82 83 fireItemAdded(id); 84 85 } 86 87 90 public boolean exists(Object id) throws StoreException { 91 92 if (id == null) 93 throw new IllegalArgumentException ("id == null"); 94 95 return dataStorage.exists(id); 96 } 97 98 101 public IComponentInfo get(Object id) throws StoreException { 102 103 if (id == null) 104 throw new IllegalArgumentException ("id == null"); 105 106 Document document = dataStorage.load(id); 107 if (document == null) 108 throw new StoreException("document == null, id=" + id); 109 110 IComponentInfo basicModel = null; 111 try { 112 basicModel = VCalendarModelFactory.unmarshall(document); 113 } catch (SyntaxException e) { 114 throw new StoreException(e); 115 } catch (IllegalArgumentException e) { 116 throw new StoreException(e); 117 } 118 119 return basicModel; 120 } 121 122 126 public void modify(Object id, IComponentInfo basicModel) throws StoreException { 127 if (id == null) 128 throw new IllegalArgumentException ("id == null"); 129 130 if (basicModel == null) 131 throw new IllegalArgumentException ("basicModel == null"); 132 133 dataStorage.remove(id); 135 136 Document document = null; 138 try { 139 document = VCalendarModelFactory.marshall(basicModel); 140 } catch (SyntaxException e) { 141 throw new StoreException(e); 142 } catch (IllegalArgumentException e) { 143 throw new StoreException(e); 144 } 145 146 dataStorage.save(id, document); 148 149 super.modify(id, basicModel); 150 } 151 152 155 public void remove(Object id) throws StoreException { 156 if (id == null) 157 throw new IllegalArgumentException ("id == null"); 158 159 dataStorage.remove(id); 160 161 super.remove(id); 162 } 163 164 167 public IComponentInfoList getComponentInfoList() throws StoreException { 168 169 IComponentInfoList list = new ComponentInfoList(); 170 171 Iterator it = dataStorage.iterator(); 172 while (it.hasNext()) { 173 Document document = (Document) it.next(); 174 175 IComponentInfo basicModel = null; 176 try { 177 basicModel = VCalendarModelFactory.unmarshall(document); 178 } catch (SyntaxException e) { 179 throw new StoreException(e); 180 } catch (IllegalArgumentException e) { 181 throw new StoreException(e); 182 } 183 184 if (basicModel.getType() == IComponent.TYPE.EVENT) { 185 IEventInfo event = (IEventInfo) basicModel; 186 list.add(event); 187 } 188 } 189 190 return list; 191 } 192 193 public IComponentInfoList getComponentInfoList(String calendarId) 194 throws StoreException { 195 IComponentInfoList list = new ComponentInfoList(); 196 197 Iterator it = dataStorage.iterator(); 198 while (it.hasNext()) { 199 Document document = (Document) it.next(); 200 201 IComponentInfo basicModel = null; 202 try { 203 basicModel = VCalendarModelFactory.unmarshall(document); 204 } catch (SyntaxException e) { 205 throw new StoreException(e); 206 } catch (IllegalArgumentException e) { 207 throw new StoreException(e); 208 } 209 210 if (basicModel.getType() == IComponent.TYPE.EVENT) { 211 IEventInfo event = (IEventInfo) basicModel; 212 if (event.getCalendar().equals(calendarId)) { 213 list.add(event); 214 } 215 } 216 } 217 218 return list; 219 } 220 221 public Iterator <String > getIdIterator() throws StoreException { 222 ArrayList <String > result = new ArrayList <String >(); 223 224 Iterator it = dataStorage.iterator(); 225 while (it.hasNext()) { 226 Document document = (Document) it.next(); 227 228 IComponentInfo basicModel = null; 229 try { 230 basicModel = VCalendarModelFactory.unmarshall(document); 231 } catch (SyntaxException e) { 232 throw new StoreException(e); 233 } catch (IllegalArgumentException e) { 234 throw new StoreException(e); 235 } 236 237 if (basicModel.getType() == IComponent.TYPE.EVENT) { 238 IEventInfo event = (IEventInfo) basicModel; 239 result.add(event.getId()); 240 } 241 } 242 243 return result.iterator(); 244 } 245 246 public Iterator <String > getIdIterator(String calendarId) 247 throws StoreException { 248 ArrayList <String > result = new ArrayList <String >(); 249 250 Iterator it = dataStorage.iterator(); 251 while (it.hasNext()) { 252 Document document = (Document) it.next(); 253 254 IComponentInfo basicModel = null; 255 try { 256 basicModel = VCalendarModelFactory.unmarshall(document); 257 } catch (SyntaxException e) { 258 throw new StoreException(e); 259 } catch (IllegalArgumentException e) { 260 throw new StoreException(e); 261 } 262 263 if (basicModel.getType() == IComponent.TYPE.EVENT) { 264 IEventInfo event = (IEventInfo) basicModel; 265 if (event.getCalendar().equals(calendarId)) 266 result.add(event.getId()); 267 } 268 } 269 270 return result.iterator(); 271 } 272 273 277 278 281 public Iterator <String > findByDateRange(IDateRange dateRange) 282 throws StoreException { 283 Vector <String > result = new Vector <String >(); 284 285 Iterator <String > it = getIdIterator(); 286 while (it.hasNext()) { 287 String id = it.next(); 288 IComponentInfo c = get(id); 289 if (c.getType().equals(IComponent.TYPE.EVENT)) { 290 IEvent event = (IEvent) c; 291 Calendar startDate = event.getDtStart(); 292 Calendar endDate = event.getDtEnd(); 293 IDateRange dr = new DateRange(startDate, endDate); 294 if (dateRange.equals(dr)) 295 result.add(id); 296 } else if (c.getType().equals(IComponent.TYPE.TODO)) { 297 ITodo todo = (ITodo) c; 298 Calendar startDate = todo.getDtStart(); 299 Calendar endDate = todo.getDue(); 300 IDateRange dr = new DateRange(startDate, endDate); 301 if (dateRange.equals(dr)) 302 result.add(id); 303 } else 304 throw new IllegalArgumentException ( 305 "unsupported component type " + c.getType()); 306 } 307 308 return result.listIterator(); 309 } 310 311 314 public Iterator <String > findByStartDate(Calendar startDate) 315 throws StoreException { 316 Vector <String > result = new Vector <String >(); 317 318 Iterator <String > it = getIdIterator(); 319 while (it.hasNext()) { 320 String id = it.next(); 321 IComponentInfo c = get(id); 322 if (c.getType().equals(IComponent.TYPE.EVENT)) { 323 IEvent event = (IEvent) c; 324 Calendar sd = event.getDtStart(); 325 if (startDate.equals(sd)) 326 result.add(id); 327 } else if (c.getType().equals(IComponent.TYPE.TODO)) { 328 ITodo todo = (ITodo) c; 329 Calendar sd = todo.getDtStart(); 330 Calendar endDate = todo.getDue(); 331 IDateRange dr = new DateRange(startDate, endDate); 332 if (startDate.equals(sd)) 333 result.add(id); 334 } else 335 throw new IllegalArgumentException ( 336 "unsupported component type " + c.getType()); 337 } 338 339 return result.listIterator(); 340 } 341 342 345 public Iterator <String > findBySummary(String searchTerm) 346 throws StoreException { 347 Vector <String > result = new Vector <String >(); 348 349 Iterator <String > it = getIdIterator(); 350 while (it.hasNext()) { 351 String id = it.next(); 352 IComponentInfo c = get(id); 353 if (c.getType().equals(IComponent.TYPE.EVENT)) { 354 IEventInfo event = (IEventInfo) c; 355 String summary = event.getEvent().getSummary(); 356 if (summary.indexOf(searchTerm) != -1) 357 result.add(id); 358 } else if (c.getType().equals(IComponent.TYPE.TODO)) { 359 ITodo todo = (ITodo) c; 360 String summary = todo.getSummary(); 361 if (summary.indexOf(searchTerm) != -1) 362 result.add(id); 363 } else 364 throw new IllegalArgumentException ( 365 "unsupported component type " + c.getType()); 366 } 367 368 return result.listIterator(); 369 } 370 371 } 372 | Popular Tags |