001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.parser;
019:
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.net.URISyntaxException;
024: import java.text.ParseException;
025: import java.util.Iterator;
026:
027: import net.fortuna.ical4j.data.CalendarOutputter;
028: import net.fortuna.ical4j.model.Calendar;
029: import net.fortuna.ical4j.model.DateTime;
030: import net.fortuna.ical4j.model.ParameterList;
031: import net.fortuna.ical4j.model.Property;
032: import net.fortuna.ical4j.model.PropertyFactory;
033: import net.fortuna.ical4j.model.PropertyFactoryImpl;
034: import net.fortuna.ical4j.model.component.VEvent;
035: import net.fortuna.ical4j.model.property.CalScale;
036: import net.fortuna.ical4j.model.property.ProdId;
037: import net.fortuna.ical4j.model.property.Version;
038:
039: import org.columba.calendar.model.api.IComponent;
040: import org.columba.calendar.model.api.IComponentInfo;
041: import org.columba.calendar.model.api.IEvent;
042: import org.columba.calendar.store.api.ICalendarStore;
043:
044: public class CalendarExporter {
045:
046: public CalendarExporter() {
047: super ();
048: }
049:
050: public void exportCalendar(File file, String calendarId,
051: ICalendarStore store) throws Exception {
052: FileOutputStream fout = new FileOutputStream(file);
053:
054: CalendarOutputter outputter = new CalendarOutputter();
055: outputter.setValidating(false);
056:
057: Calendar calendar = new Calendar();
058: calendar.getProperties().add(
059: new ProdId("-//Columba Project//iCal4j 1.0//EN"));
060: calendar.getProperties().add(Version.VERSION_2_0);
061: calendar.getProperties().add(CalScale.GREGORIAN);
062:
063: Iterator<String> it = store.getIdIterator(calendarId);
064: while (it.hasNext()) {
065: String id = it.next();
066: IComponentInfo c = store.get(id);
067: if (c.getType() == IComponent.TYPE.EVENT) {
068: IEvent event = (IEvent) c;
069:
070: VEvent v = createVEvent(event);
071:
072: calendar.getComponents().add(v);
073: }
074:
075: }
076: outputter.output(calendar, fout);
077: }
078:
079: public void exportSingleEvent(File file, IComponentInfo component,
080: ICalendarStore store) throws Exception {
081: FileOutputStream fout = new FileOutputStream(file);
082:
083: CalendarOutputter outputter = new CalendarOutputter();
084: outputter.setValidating(true);
085:
086: Calendar calendar = new Calendar();
087: calendar.getProperties().add(
088: new ProdId("-//Columba Project//iCal4j 1.0//EN"));
089: calendar.getProperties().add(Version.VERSION_2_0);
090: calendar.getProperties().add(CalScale.GREGORIAN);
091:
092: if (component.getType() == IComponent.TYPE.EVENT) {
093: IEvent event = (IEvent) component;
094:
095: VEvent v = createVEvent(event);
096:
097: calendar.getComponents().add(v);
098: }
099:
100: outputter.output(calendar, fout);
101: }
102:
103: /**
104: * @param event
105: * @return
106: * @throws IOException
107: * @throws URISyntaxException
108: * @throws ParseException
109: */
110: private VEvent createVEvent(IEvent event) throws IOException,
111: URISyntaxException, ParseException {
112: PropertyFactory factory = PropertyFactoryImpl.getInstance();
113: java.util.Calendar start = event.getDtStart();
114: java.util.Calendar end = event.getDtEnd();
115: // java.util.Calendar stamp = event.getDtStamp();
116: String summary = event.getSummary();
117: String location = event.getLocation();
118: String uid = event.getId();
119:
120: VEvent v = new VEvent(new DateTime(start.getTime()),
121: new DateTime(end.getTime()), summary);
122:
123: //v.getEndDate().getParameters().add(Value.DATE);
124:
125: // if (stamp != null) {
126: // DateTime dateTime = new DateTime(stamp.getTime());
127: //
128: // ParameterList list = new ParameterList();
129: // list.add(new TzId( TimeZone.getDefault().toString()));
130: //
131: // v.getProperties().add(
132: // factory.createProperty(Property.DTSTAMP, list,
133: // dateTime.toString()));
134: //
135: // }
136:
137: if (location != null)
138: v.getProperties().add(
139: factory.createProperty(Property.LOCATION,
140: new ParameterList(), location));
141:
142: if (uid != null)
143: v.getProperties().add(
144: factory.createProperty(Property.UID,
145: new ParameterList(), uid));
146: return v;
147: }
148: }
|