KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import net.fortuna.ical4j.data.CalendarOutputter;
28 import net.fortuna.ical4j.model.Calendar;
29 import net.fortuna.ical4j.model.DateTime;
30 import net.fortuna.ical4j.model.ParameterList;
31 import net.fortuna.ical4j.model.Property;
32 import net.fortuna.ical4j.model.PropertyFactory;
33 import net.fortuna.ical4j.model.PropertyFactoryImpl;
34 import net.fortuna.ical4j.model.component.VEvent;
35 import net.fortuna.ical4j.model.property.CalScale;
36 import net.fortuna.ical4j.model.property.ProdId;
37 import net.fortuna.ical4j.model.property.Version;
38
39 import org.columba.calendar.model.api.IComponent;
40 import org.columba.calendar.model.api.IComponentInfo;
41 import org.columba.calendar.model.api.IEvent;
42 import org.columba.calendar.store.api.ICalendarStore;
43
44 public class CalendarExporter {
45
46     public CalendarExporter() {
47         super();
48     }
49
50     public void exportCalendar(File JavaDoc file, String JavaDoc calendarId,
51             ICalendarStore store) throws Exception JavaDoc {
52         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(file);
53
54         CalendarOutputter outputter = new CalendarOutputter();
55         outputter.setValidating(false);
56
57         Calendar calendar = new Calendar();
58         calendar.getProperties().add(
59                 new ProdId("-//Columba Project//iCal4j 1.0//EN"));
60         calendar.getProperties().add(Version.VERSION_2_0);
61         calendar.getProperties().add(CalScale.GREGORIAN);
62
63         Iterator JavaDoc<String JavaDoc> it = store.getIdIterator(calendarId);
64         while (it.hasNext()) {
65             String JavaDoc id = it.next();
66             IComponentInfo c = store.get(id);
67             if (c.getType() == IComponent.TYPE.EVENT) {
68                 IEvent event = (IEvent) c;
69
70                 VEvent v = createVEvent(event);
71
72                 calendar.getComponents().add(v);
73             }
74
75         }
76         outputter.output(calendar, fout);
77     }
78
79     public void exportSingleEvent(File JavaDoc file, IComponentInfo component,
80             ICalendarStore store) throws Exception JavaDoc {
81         FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc(file);
82
83         CalendarOutputter outputter = new CalendarOutputter();
84         outputter.setValidating(true);
85
86         Calendar calendar = new Calendar();
87         calendar.getProperties().add(
88                 new ProdId("-//Columba Project//iCal4j 1.0//EN"));
89         calendar.getProperties().add(Version.VERSION_2_0);
90         calendar.getProperties().add(CalScale.GREGORIAN);
91
92         
93         if (component.getType() == IComponent.TYPE.EVENT) {
94             IEvent event = (IEvent) component;
95
96             VEvent v = createVEvent(event);
97
98             calendar.getComponents().add(v);
99         }
100
101         outputter.output(calendar, fout);
102     }
103
104     /**
105      * @param event
106      * @return
107      * @throws IOException
108      * @throws URISyntaxException
109      * @throws ParseException
110      */

111     private VEvent createVEvent(IEvent event) throws IOException JavaDoc,
112             URISyntaxException JavaDoc, ParseException JavaDoc {
113         PropertyFactory factory = PropertyFactoryImpl.getInstance();
114         java.util.Calendar JavaDoc start = event.getDtStart();
115         java.util.Calendar JavaDoc end = event.getDtEnd();
116         // java.util.Calendar stamp = event.getDtStamp();
117
String JavaDoc summary = event.getSummary();
118         String JavaDoc location = event.getLocation();
119         String JavaDoc uid = event.getId();
120
121         VEvent v = new VEvent(new DateTime(start.getTime()),
122                 new DateTime(end.getTime()), summary);
123
124         //v.getEndDate().getParameters().add(Value.DATE);
125

126         // if (stamp != null) {
127
// DateTime dateTime = new DateTime(stamp.getTime());
128
//
129
// ParameterList list = new ParameterList();
130
// list.add(new TzId( TimeZone.getDefault().toString()));
131
//
132
// v.getProperties().add(
133
// factory.createProperty(Property.DTSTAMP, list,
134
// dateTime.toString()));
135
//
136
// }
137

138         if (location != null)
139             v.getProperties().add(
140                     factory.createProperty(Property.LOCATION,
141                             new ParameterList(), location));
142
143         if (uid != null)
144             v.getProperties().add(
145                     factory.createProperty(Property.UID, new ParameterList(),
146                             uid));
147         return v;
148     }
149 }
150
Popular Tags