KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > parser > CalendarImporter


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.parser;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Random JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import net.fortuna.ical4j.data.CalendarBuilder;
33 import net.fortuna.ical4j.model.Component;
34 import net.fortuna.ical4j.model.Date;
35 import net.fortuna.ical4j.model.DateTime;
36 import net.fortuna.ical4j.model.Property;
37 import net.fortuna.ical4j.model.WeekDay;
38 import net.fortuna.ical4j.model.property.Categories;
39 import net.fortuna.ical4j.model.property.DtEnd;
40 import net.fortuna.ical4j.model.property.DtStamp;
41 import net.fortuna.ical4j.model.property.DtStart;
42 import net.fortuna.ical4j.model.property.RRule;
43
44 import org.columba.calendar.base.UUIDGenerator;
45 import org.columba.calendar.base.api.ICalendarItem;
46 import org.columba.calendar.model.Event;
47 import org.columba.calendar.model.EventInfo;
48 import org.columba.calendar.model.Recurrence;
49 import org.columba.calendar.model.api.IEvent;
50 import org.columba.calendar.model.api.IEventInfo;
51 import org.columba.calendar.model.api.IRecurrence;
52 import org.columba.calendar.model.api.IWeekDay;
53
54 public class CalendarImporter {
55
56     public CalendarImporter() {
57         super();
58     }
59
60     public Iterator JavaDoc<IEventInfo> importCalendar(ICalendarItem calendarItem, File JavaDoc file) throws Exception JavaDoc {
61         Vector JavaDoc<IEventInfo> v = new Vector JavaDoc<IEventInfo>();
62
63         FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
64
65         CalendarBuilder builder = new CalendarBuilder();
66
67         net.fortuna.ical4j.model.Calendar calendar = builder.build(in);
68         
69         Map JavaDoc<String JavaDoc, Integer JavaDoc> frequency = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
70         frequency.put("YEARLY", new Integer JavaDoc(IRecurrence.RECURRENCE_ANNUALLY));
71         frequency.put("MONTHLY", new Integer JavaDoc(IRecurrence.RECURRENCE_MONTHLY));
72         frequency.put("WEEKLY", new Integer JavaDoc(IRecurrence.RECURRENCE_WEEKLY));
73         frequency.put("DAILY", new Integer JavaDoc(IRecurrence.RECURRENCE_DAILY));
74
75         for (Iterator JavaDoc i = calendar.getComponents().iterator(); i.hasNext();) {
76             net.fortuna.ical4j.model.Component component = (net.fortuna.ical4j.model.Component) i
77                     .next();
78             // System.out.println("Component [" + component.getName() + "]");
79

80             // only import VEVENT and VTODO for now
81
if (component.getName().equals(Component.VEVENT)) {
82
83                 Calendar JavaDoc dtStart = null;
84                 Calendar JavaDoc dtEnd = null;
85                 Calendar JavaDoc dtStamp = null;
86                 String JavaDoc summary = null;
87                 String JavaDoc location = null;
88                 String JavaDoc description = null;
89                 String JavaDoc uid = null;
90                 URL JavaDoc url = null;
91                 String JavaDoc eventClass = null;
92                 String JavaDoc priority = null;
93                 String JavaDoc status = null;
94                 
95                 List JavaDoc<String JavaDoc> categories = new LinkedList JavaDoc<String JavaDoc>();
96                 
97                 int freq = -1;
98                 int count = -1;
99                 int interval = 0;
100                 Calendar JavaDoc until = null;
101                 List JavaDoc<WeekDay> weekdays = null;
102                 
103                 for (Iterator JavaDoc j = component.getProperties().iterator(); j
104                         .hasNext();) {
105                     Property property = (Property) j.next();
106                     String JavaDoc name = property.getName();
107                     String JavaDoc value = property.getValue();
108
109                     // System.out.println("Property [" + property.getName() + ", "
110
// + property.getValue() + "]");
111

112                     if (name.equals(Property.DTSTART)) {
113                         DtStart dtStart1 = (DtStart) property;
114                         if (dtStart1.getDate() instanceof DateTime) {
115                             DateTime dateTime = (DateTime) dtStart1.getDate();
116                             // ensure tzid matches date-time timezone..
117
// Parameter tzId =
118
// dtStart1.getParameters().getParameter(
119
// Parameter.TZID);
120

121                             dtStart = Calendar.getInstance();
122                             dtStart.setTimeInMillis(dateTime.getTime());
123                             if (dateTime.getTimeZone() != null)
124                                 dtStart.setTimeZone(dateTime.getTimeZone());
125                         } else {
126                             Date dateTime = dtStart1.getDate();
127                             // ensure tzid matches date-time timezone..
128
// Parameter tzId =
129
// dtStart1.getParameters().getParameter(
130
// Parameter.TZID);
131

132                             dtStart = Calendar.getInstance();
133                             dtStart.setTimeInMillis(dateTime.getTime());
134                         }
135
136                     } else if (name.equals(Property.DTEND)) {
137                         DtEnd dtEnd1 = (DtEnd) property;
138                         if (dtEnd1.getDate() instanceof DateTime) {
139                             DateTime dateTime = (DateTime) dtEnd1.getDate();
140                             // ensure tzid matches date-time timezone..
141
// Parameter tzId = dtEnd1.getParameters().getParameter(
142
// Parameter.TZID);
143

144                             dtEnd = Calendar.getInstance();
145                             dtEnd.setTimeInMillis(dateTime.getTime());
146                             if (dateTime.getTimeZone() != null)
147                                 dtEnd.setTimeZone(dateTime.getTimeZone());
148                         } else {
149                             Date dateTime = dtEnd1.getDate();
150                             // ensure tzid matches date-time timezone..
151
// Parameter tzId = dtEnd1.getParameters().getParameter(
152
// Parameter.TZID);
153

154                             dtEnd = Calendar.getInstance();
155                             dtEnd.setTimeInMillis(dateTime.getTime());
156                         }
157                     } else if (name.equals(Property.SUMMARY)) {
158                         summary = value;
159                     } else if (name.equals(Property.LOCATION)) {
160                         location = value;
161                     } else if (name.equals(Property.DTSTAMP)) {
162                         DtStamp dtStamp1 = (DtStamp) property;
163                         if (dtStamp1.getDate() instanceof DateTime) {
164                             DateTime dateTime = (DateTime) dtStamp1.getDate();
165                             // ensure tzid matches date-time timezone..
166
// Parameter tzId =
167
// dtStamp1.getParameters().getParameter(
168
// Parameter.TZID);
169

170                             dtStamp = Calendar.getInstance();
171     
172                             dtStamp.setTimeInMillis(dateTime.getTime());
173                             if (dateTime.getTimeZone() != null)
174                                 dtStamp.setTimeZone(dateTime.getTimeZone());
175                         } else {
176                             Date dateTime = dtStamp1.getDate();
177                             // ensure tzid matches date-time timezone..
178
// Parameter tzId =
179
// dtStamp1.getParameters().getParameter(
180
// Parameter.TZID);
181

182                             dtStamp = Calendar.getInstance();
183
184                             dtStamp.setTimeInMillis(dateTime.getTime());
185                         }
186                     } else if (name.equals(Property.UID)) {
187                         // remove everything which is not A-Za-z0-9-_
188
uid = correctUid(value);
189                     } else if (name.equals(Property.URL)) {
190                         url = new URL JavaDoc(value);
191                     } else if (name.equals(Property.DESCRIPTION)) {
192                         description = value;
193                     } else if (name.equals(Property.CATEGORIES)) {
194                         Categories categorylist = (Categories) property;
195                         for (Iterator JavaDoc iter = categorylist.getCategories().iterator(); iter.hasNext(); ) {
196                             String JavaDoc c = (String JavaDoc) iter.next();
197                             categories.add(c);
198                         }
199                     } else if (name.equals(Property.CLASS)) {
200                         eventClass = value;
201                     } else if (name.equals(Property.PRIORITY)) {
202                         priority = value;
203                     } else if (name.equals(Property.STATUS)) {
204                         status = value;
205                     } else if (name.equals(Property.RRULE)) {
206                         // RRULE:FREQ=YEARLY;COUNT=5;INTERVAL=1
207
// RRULE:FREQ=WEEKLY;UNTIL=20060725T215959;INTERVAL=1;BYDAY=TU
208
// RRULE:FREQ=YEARLY;INTERVAL=1
209

210                         // RRULE:FREQ=WEEKLY;COUNT=8;INTERVAL=2;BYDAY=TU,TH
211
// RRULE:FREQ=MONTHLY;UNTIL=20070627T215959;INTERVAL=2;BYDAY=4WE
212
// RRULE:FREQ=YEARLY;INTERVAL=1
213
// RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=3
214
// RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=10
215

216                         RRule rrule = (RRule) property;
217                         freq = ((Integer JavaDoc) frequency.get(rrule.getRecur().getFrequency())).intValue();
218                         if (rrule.getRecur().getCount() > 0)
219                             count = rrule.getRecur().getCount();
220                         if (rrule.getRecur().getInterval() > 0)
221                             interval = rrule.getRecur().getInterval();
222                         weekdays = rrule.getRecur().getDayList();
223                         /*for (Iterator iter = rrule.getRecur().getWeekNoList().iterator(); iter.hasNext(); ) {
224                             Object o = iter.next();
225                             System.out.println("weeknolist = " + o);
226                         }
227                         for (Iterator iter = rrule.getRecur().getMonthList().iterator(); iter.hasNext(); ) {
228                             Object o = iter.next();
229                             System.out.println("monthlist = " + o);
230                         }*/

231                         if (rrule.getRecur().getUntil() != null) {
232                             until = Calendar.getInstance();
233                             until.setTime(rrule.getRecur().getUntil());
234                             // System.out.println("until = " + until);
235
}
236                     }
237
238                 }
239
240                 // skip, if UID, dtStart or dtEnd is not defined
241
if (uid == null || dtStart == null || dtEnd == null)
242                     continue;
243                 
244                 // hack: if start and end time is at 00:00:00:000 it is
245
// an all day event
246
boolean allDayEvent = ParserHelper.isAllDayEvent(dtStart, dtEnd);
247                 
248                 IEvent event = new Event(uid);
249
250                 event.setDtStart(dtStart);
251                 event.setDtEnd(dtEnd);
252                 event.setDtStamp(dtStamp);
253                 event.setAllDayEvent(allDayEvent);
254
255                 if (summary != null)
256                     event.setSummary(summary);
257                 if (eventClass != null)
258                     event.setEventClass(eventClass);
259                 if (location != null)
260                     event.setLocation(location);
261                 if (priority != null)
262                     event.setPriority(priority);
263                 if (status != null)
264                     event.setStatus(status);
265                 if (url != null)
266                     event.setUrl(url);
267                 if (description != null)
268                     event.setDescription(description);
269                 
270                 if (freq > -1) {
271                     // create recurrence
272
Recurrence r = new Recurrence(freq);
273                     
274                     r.setInterval(interval);
275                     
276                     List JavaDoc<IWeekDay> wdlist = new LinkedList JavaDoc<IWeekDay>();
277                     if (weekdays != null) {
278                         for (Iterator JavaDoc iter = weekdays.iterator(); iter.hasNext(); ) {
279                             WeekDay owd = (WeekDay) iter.next();
280                             org.columba.calendar.model.WeekDay wd = new org.columba.calendar.model.WeekDay(owd.getDay(), owd.getOffset());
281                             wdlist.add(wd);
282                         }
283                         
284                         r.setWeekDays(wdlist);
285                     }
286
287                     r.setEndType(IRecurrence.RECURRENCE_END_FOREVER);
288                     if (until != null) {
289                         r.setEndDate(until);
290                         r.setEndType(IRecurrence.RECURRENCE_END_ENDDATE);
291                     } else if (count > -1) {
292                         r.setEndMaxOccurrences(count);
293                         r.setEndType(IRecurrence.RECURRENCE_END_MAXOCCURRENCES);
294                     }
295                     
296                     event.setRecurrence(r);
297                     
298                 } else {
299                     event.setRecurrence(null);
300                 }
301                 
302                 for (String JavaDoc category : categories)
303                     event.addCategory(category);
304                 
305                 IEventInfo eventInfo = new EventInfo(uid, calendarItem.getId(), event);
306
307                 v.add(eventInfo);
308             } else if (component.getName().equals(Component.VTIMEZONE)) {
309                 for (Iterator JavaDoc j = component.getProperties().iterator(); j
310                         .hasNext();) {
311                     Property property = (Property) j.next();
312
313                     System.out.println("Property [" + property.getName() + ", "
314                             + property.getValue() + "]");
315                 }
316             } else if (component.getName().equals(Component.VTODO)) {
317                 
318                 for (Iterator JavaDoc j = component.getProperties().iterator(); j
319                         .hasNext();) {
320                     Property property = (Property) j.next();
321                     String JavaDoc name = property.getName();
322                     String JavaDoc value = property.getValue();
323
324                     System.out.println("Property [" + property.getName() + ", "
325                             + property.getValue() + "]");
326                 }
327
328             }
329         }
330
331         in.close();
332
333         return v.iterator();
334     }
335     
336     public static String JavaDoc correctUid(String JavaDoc value) {
337         // if there is no string given, return a number
338
Random JavaDoc r = new Random JavaDoc();
339         if (value == null || value.length() == 0)
340             return (new UUIDGenerator()).newUUID();
341         return value.replaceAll("[^a-z,0-9,A-Z,_-]", "-");
342     }
343
344 }
345
Popular Tags