KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > store > LocalCalendarStore


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.calendar.store;
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Vector JavaDoc;
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 JavaDoc directory) throws StoreException {
49         super();
50
51         if (directory == null)
52             throw new IllegalArgumentException JavaDoc("directory == null");
53
54         DiskIO.ensureDirectory(directory);
55
56         dataStorage = new LocalXMLFileStore(directory);
57     }
58
59     /**
60      * @see org.columba.calendar.store.AbstractCalendarStore#add(org.columba.calendar.model.api.IComponent)
61      */

62     public void add(IComponentInfo basicModel) throws StoreException {
63
64         if (basicModel == null)
65             throw new IllegalArgumentException JavaDoc("basicModel == null");
66
67         String JavaDoc id = basicModel.getId();
68         // generate new UUID if it does not exist yet
69
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 JavaDoc e) {
78             throw new StoreException(e);
79         }
80
81         dataStorage.save(id, document);
82
83         fireItemAdded(id);
84
85     }
86
87     /**
88      * @see org.columba.calendar.store.AbstractCalendarStore#exists(java.lang.Object)
89      */

90     public boolean exists(Object JavaDoc id) throws StoreException {
91
92         if (id == null)
93             throw new IllegalArgumentException JavaDoc("id == null");
94
95         return dataStorage.exists(id);
96     }
97
98     /**
99      * @see org.columba.calendar.store.AbstractCalendarStore#get(java.lang.Object)
100      */

101     public IComponentInfo get(Object JavaDoc id) throws StoreException {
102
103         if (id == null)
104             throw new IllegalArgumentException JavaDoc("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 JavaDoc e) {
116             throw new StoreException(e);
117         }
118
119         return basicModel;
120     }
121
122     /**
123      * @see org.columba.calendar.store.AbstractCalendarStore#modify(java.lang.Object,
124      * org.columba.calendar.model.api.IComponent)
125      */

126     public void modify(Object JavaDoc id, IComponentInfo basicModel) throws StoreException {
127         if (id == null)
128             throw new IllegalArgumentException JavaDoc("id == null");
129
130         if (basicModel == null)
131             throw new IllegalArgumentException JavaDoc("basicModel == null");
132
133         // remove old data
134
dataStorage.remove(id);
135
136         // generate xml document
137
Document document = null;
138         try {
139             document = VCalendarModelFactory.marshall(basicModel);
140         } catch (SyntaxException e) {
141             throw new StoreException(e);
142         } catch (IllegalArgumentException JavaDoc e) {
143             throw new StoreException(e);
144         }
145
146         // add new data to local store
147
dataStorage.save(id, document);
148
149         super.modify(id, basicModel);
150     }
151
152     /**
153      * @see org.columba.calendar.store.AbstractCalendarStore#remove(java.lang.Object)
154      */

155     public void remove(Object JavaDoc id) throws StoreException {
156         if (id == null)
157             throw new IllegalArgumentException JavaDoc("id == null");
158
159         dataStorage.remove(id);
160
161         super.remove(id);
162     }
163
164     /**
165      * @see org.columba.calendar.store.AbstractCalendarStore#getComponentInfoList()
166      */

167     public IComponentInfoList getComponentInfoList() throws StoreException {
168
169         IComponentInfoList list = new ComponentInfoList();
170
171         Iterator JavaDoc 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 JavaDoc 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 JavaDoc calendarId)
194             throws StoreException {
195         IComponentInfoList list = new ComponentInfoList();
196
197         Iterator JavaDoc 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 JavaDoc 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 JavaDoc<String JavaDoc> getIdIterator() throws StoreException {
222         ArrayList JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
223
224         Iterator JavaDoc 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 JavaDoc 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 JavaDoc<String JavaDoc> getIdIterator(String JavaDoc calendarId)
247             throws StoreException {
248         ArrayList JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
249
250         Iterator JavaDoc 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 JavaDoc 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     /**
274      * *********************** very slow search implementation
275      * **********************
276      */

277
278     /**
279      * @see org.columba.calendar.store.api.ICalendarStore#findByDateRange(org.columba.calendar.model.api.IDateRange)
280      */

281     public Iterator JavaDoc<String JavaDoc> findByDateRange(IDateRange dateRange)
282             throws StoreException {
283         Vector JavaDoc<String JavaDoc> result = new Vector JavaDoc<String JavaDoc>();
284
285         Iterator JavaDoc<String JavaDoc> it = getIdIterator();
286         while (it.hasNext()) {
287             String JavaDoc id = it.next();
288             IComponentInfo c = get(id);
289             if (c.getType().equals(IComponent.TYPE.EVENT)) {
290                 IEvent event = (IEvent) c;
291                 Calendar JavaDoc startDate = event.getDtStart();
292                 Calendar JavaDoc 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 JavaDoc startDate = todo.getDtStart();
299                 Calendar JavaDoc 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 JavaDoc(
305                         "unsupported component type " + c.getType());
306         }
307
308         return result.listIterator();
309     }
310
311     /**
312      * @see org.columba.calendar.store.api.ICalendarStore#findByStartDate(java.util.Calendar)
313      */

314     public Iterator JavaDoc<String JavaDoc> findByStartDate(Calendar JavaDoc startDate)
315             throws StoreException {
316         Vector JavaDoc<String JavaDoc> result = new Vector JavaDoc<String JavaDoc>();
317
318         Iterator JavaDoc<String JavaDoc> it = getIdIterator();
319         while (it.hasNext()) {
320             String JavaDoc id = it.next();
321             IComponentInfo c = get(id);
322             if (c.getType().equals(IComponent.TYPE.EVENT)) {
323                 IEvent event = (IEvent) c;
324                 Calendar JavaDoc 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 JavaDoc sd = todo.getDtStart();
330                 Calendar JavaDoc 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 JavaDoc(
336                         "unsupported component type " + c.getType());
337         }
338
339         return result.listIterator();
340     }
341
342     /**
343      * @see org.columba.calendar.store.api.ICalendarStore#findBySummary(java.lang.String)
344      */

345     public Iterator JavaDoc<String JavaDoc> findBySummary(String JavaDoc searchTerm)
346             throws StoreException {
347         Vector JavaDoc<String JavaDoc> result = new Vector JavaDoc<String JavaDoc>();
348
349         Iterator JavaDoc<String JavaDoc> it = getIdIterator();
350         while (it.hasNext()) {
351             String JavaDoc id = it.next();
352             IComponentInfo c = get(id);
353             if (c.getType().equals(IComponent.TYPE.EVENT)) {
354                 IEventInfo event = (IEventInfo) c;
355                 String JavaDoc 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 JavaDoc summary = todo.getSummary();
361                 if (summary.indexOf(searchTerm) != -1)
362                     result.add(id);
363             } else
364                 throw new IllegalArgumentException JavaDoc(
365                         "unsupported component type " + c.getType());
366         }
367
368         return result.listIterator();
369     }
370
371 }
372
Popular Tags