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 org.columba.calendar.model.Event;
021: import org.columba.calendar.model.EventInfo;
022: import org.columba.calendar.model.Recurrence;
023: import org.columba.calendar.model.api.ICALENDAR;
024: import org.columba.calendar.model.api.IComponentInfo;
025: import org.columba.calendar.model.api.IEvent;
026: import org.columba.calendar.model.api.IEventInfo;
027: import org.columba.calendar.model.api.IRecurrence;
028: import org.columba.calendar.model.api.IComponent.TYPE;
029: import org.jdom.Document;
030:
031: import com.miginfocom.calendar.activity.recurrence.RecurrenceRule;
032:
033: public final class VCalendarModelFactory {
034:
035: public VCalendarModelFactory() {
036: super ();
037: }
038:
039: public static Document marshall(IComponentInfo c)
040: throws SyntaxException, IllegalArgumentException {
041:
042: if (c == null)
043: throw new IllegalArgumentException("calendarmodel == null");
044:
045: XCSDocumentParser model = new XCSDocumentParser(c.getType());
046:
047: model.setId(c.getId());
048:
049: if (c.getType() == TYPE.EVENT) {
050: IEventInfo eventInfo = (IEventInfo) c;
051: model.setDescription(eventInfo.getEvent().getDescription());
052: model.setSummary(eventInfo.getEvent().getSummary());
053:
054: if (eventInfo.getEvent().getDtStart() != null)
055: model.setDTStart(eventInfo.getEvent().getDtStart());
056:
057: if (eventInfo.getEvent().getDtEnd() != null)
058: model.setDTEnd(eventInfo.getEvent().getDtEnd());
059:
060: if (eventInfo.getEvent().getDtStamp() != null)
061: model.setDTStamp(eventInfo.getEvent().getDtStamp());
062:
063: model.setEventClass(eventInfo.getEvent().getEventClass());
064: model.setLocation(eventInfo.getEvent().getLocation());
065:
066: if (eventInfo.getEvent().getRecurrence() != null
067: && eventInfo.getEvent().getRecurrence().getType() != IRecurrence.RECURRENCE_NONE) {
068: RecurrenceRule r = new RecurrenceRule();
069: IRecurrence columbaRecurrence = eventInfo.getEvent()
070: .getRecurrence();
071: r.setFrequency(Recurrence.toFrequency(columbaRecurrence
072: .getType()));
073: r.setInterval(columbaRecurrence.getInterval());
074: if (columbaRecurrence.getEndType() == IRecurrence.RECURRENCE_END_MAXOCCURRENCES)
075: r.setRepetitionCount(columbaRecurrence
076: .getEndMaxOccurrences());
077: else if (columbaRecurrence.getEndType() == IRecurrence.RECURRENCE_END_ENDDATE)
078: r.setUntilDate(columbaRecurrence.getEndDate());
079: // FIXME r.setPos();
080: model.setRecurrenceRule(r);
081: }
082:
083: model.setCategories(eventInfo.getEvent().getCategories());
084:
085: model.setCalendar(eventInfo.getCalendar());
086: }
087:
088: // TODO finish marshalling of all available properties
089: return model.getDocument();
090:
091: }
092:
093: public static IComponentInfo unmarshall(Document document)
094: throws SyntaxException, IllegalArgumentException {
095:
096: if (document == null)
097: throw new IllegalArgumentException("document == null");
098:
099: XCSDocumentParser model = new XCSDocumentParser(document);
100:
101: if (model.getId() == null)
102: throw new IllegalArgumentException("id == null");
103:
104: if (model.getComponentType().equals(ICALENDAR.VEVENT)) {
105: IEvent event = new Event((String) model.getId());
106:
107: event.setDescription(model.getDescription());
108: event.setSummary(model.getSummary());
109:
110: if (model.getDTStart() != null)
111: event.setDtStart(model.getDTStart());
112:
113: if (model.getDTEnd() != null)
114: event.setDtEnd(model.getDTEnd());
115:
116: if (model.getDTStamp() != null)
117: event.setDtStamp(model.getDTStamp());
118:
119: event.setEventClass(model.getEventClass());
120: event.setLocation(model.getLocation());
121:
122: event.setAllDayEvent(ParserHelper.isAllDayEvent(model
123: .getDTStart(), model.getDTEnd()));
124:
125: event.setCategories(model.getCategories());
126:
127: RecurrenceRule r = model.getRecurrenceRule();
128: if (r != null) {
129: // create recurrence
130: Recurrence columbaRecurrence = new Recurrence(
131: Recurrence.fromFrequency(r.getFrequency()));
132: columbaRecurrence
133: .setEndType(IRecurrence.RECURRENCE_END_FOREVER);
134: columbaRecurrence.setInterval(r.getInterval());
135: if (r.getRepetitionCount() != null) {
136: columbaRecurrence
137: .setEndType(IRecurrence.RECURRENCE_END_MAXOCCURRENCES);
138: columbaRecurrence.setEndMaxOccurrences(r
139: .getRepetitionCount());
140: } else if (r.getUntilDate() != null) {
141: columbaRecurrence
142: .setEndType(IRecurrence.RECURRENCE_END_ENDDATE);
143: columbaRecurrence.setEndDate(r.getUntilDate());
144: }
145: // FIXME r.setPos();
146: event.setRecurrence(columbaRecurrence);
147: }
148:
149: IEventInfo eventInfo = new EventInfo(
150: (String) model.getId(), model.getCalendar(), event);
151: eventInfo.setCalendar(model.getCalendar());
152:
153: // TODO finish unmarshalling of all available properties
154:
155: return eventInfo;
156: }
157:
158: else
159: throw new IllegalArgumentException("unknown component type");
160:
161: }
162:
163: // public static VEventModel createVEvent(Document doc)
164: // throws SyntaxException, InvocationException {
165: // if (doc == null)
166: // throw new InvocationException("doc == null");
167: //
168: // BasicDocumentModel model = new BasicDocumentModel(doc);
169: //
170: // VEventModel c = new VEventModel();
171: // c.setId((String) model.getId());
172: //
173: // c.setDescription(model.getDescription());
174: //
175: // // TODO createVEvent
176: // return null;
177: // }
178: //
179: // public static Document persistVEvent(VEventModel vEventComponent)
180: // throws SyntaxException, InvocationException {
181: // // TODO persistVEvent
182: // return null;
183: // }
184: //
185: // public static VTodoModel createVTodo(Document doc) throws
186: // SyntaxException,
187: // InvocationException {
188: // if (doc == null)
189: // throw new InvocationException("doc == null");
190: //
191: // // TODO createVTodo
192: // return null;
193: // }
194: //
195: // public static Document persistVTodo(VTodoModel vTodoComponent)
196: // throws SyntaxException, InvocationException {
197: // // TODO persistVTodo
198: // return null;
199: // }
200: //
201: // public static VFreeBusyModel createVFreeBusy(Document doc)
202: // throws SyntaxException, InvocationException {
203: // if (doc == null)
204: // throw new InvocationException("doc == null");
205: //
206: // // TODO createVFreeBusy
207: // return null;
208: // }
209: }
|