KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.columba.calendar.model.Event;
21 import org.columba.calendar.model.EventInfo;
22 import org.columba.calendar.model.Recurrence;
23 import org.columba.calendar.model.api.ICALENDAR;
24 import org.columba.calendar.model.api.IComponentInfo;
25 import org.columba.calendar.model.api.IEvent;
26 import org.columba.calendar.model.api.IEventInfo;
27 import org.columba.calendar.model.api.IRecurrence;
28 import org.columba.calendar.model.api.IComponent.TYPE;
29 import org.jdom.Document;
30
31 import com.miginfocom.calendar.activity.recurrence.RecurrenceRule;
32
33 public final class VCalendarModelFactory {
34
35     public VCalendarModelFactory() {
36         super();
37     }
38
39     public static Document marshall(IComponentInfo c) throws SyntaxException,
40             IllegalArgumentException JavaDoc {
41
42         if (c == null)
43             throw new IllegalArgumentException JavaDoc("calendarmodel == null");
44
45         XCSDocumentParser model = new XCSDocumentParser(c.getType());
46
47         model.setId(c.getId());
48
49         if (c.getType() == TYPE.EVENT) {
50             IEventInfo eventInfo = (IEventInfo) c;
51             model.setDescription(eventInfo.getEvent().getDescription());
52             model.setSummary(eventInfo.getEvent().getSummary());
53
54             if (eventInfo.getEvent().getDtStart() != null)
55                 model.setDTStart(eventInfo.getEvent().getDtStart());
56
57             if (eventInfo.getEvent().getDtEnd() != null)
58                 model.setDTEnd(eventInfo.getEvent().getDtEnd());
59
60             if (eventInfo.getEvent().getDtStamp() != null)
61                 model.setDTStamp(eventInfo.getEvent().getDtStamp());
62
63             model.setEventClass(eventInfo.getEvent().getEventClass());
64             model.setLocation(eventInfo.getEvent().getLocation());
65             
66             if (eventInfo.getEvent().getRecurrence() != null && eventInfo.getEvent().getRecurrence().getType() != IRecurrence.RECURRENCE_NONE) {
67                 RecurrenceRule r = new RecurrenceRule();
68                 IRecurrence columbaRecurrence = eventInfo.getEvent().getRecurrence();
69                 r.setFrequency(Recurrence.toFrequency(columbaRecurrence.getType()));
70                 r.setInterval(columbaRecurrence.getInterval());
71                 if (columbaRecurrence.getEndType() == IRecurrence.RECURRENCE_END_MAXOCCURRENCES)
72                     r.setRepetitionCount(columbaRecurrence.getEndMaxOccurrences());
73                 else if (columbaRecurrence.getEndType() == IRecurrence.RECURRENCE_END_ENDDATE)
74                     r.setUntilDate(columbaRecurrence.getEndDate());
75                 // FIXME r.setPos();
76
model.setRecurrenceRule(r);
77             }
78             
79             model.setCategories(eventInfo.getEvent().getCategories());
80             
81             model.setCalendar(eventInfo.getCalendar());
82         }
83
84         // TODO finish marshalling of all available properties
85
return model.getDocument();
86
87     }
88
89     public static IComponentInfo unmarshall(Document document)
90             throws SyntaxException, IllegalArgumentException JavaDoc {
91
92         if (document == null)
93             throw new IllegalArgumentException JavaDoc("document == null");
94
95         XCSDocumentParser model = new XCSDocumentParser(document);
96
97         if (model.getId() == null)
98             throw new IllegalArgumentException JavaDoc("id == null");
99
100         if (model.getComponentType().equals(ICALENDAR.VEVENT)) {
101             IEvent event = new Event((String JavaDoc) model.getId());
102
103             event.setDescription(model.getDescription());
104             event.setSummary(model.getSummary());
105
106             if (model.getDTStart() != null)
107                 event.setDtStart(model.getDTStart());
108
109             if (model.getDTEnd() != null)
110                 event.setDtEnd(model.getDTEnd());
111
112             if (model.getDTStamp() != null)
113                 event.setDtStamp(model.getDTStamp());
114
115             event.setEventClass(model.getEventClass());
116             event.setLocation(model.getLocation());
117
118             event.setAllDayEvent(ParserHelper.isAllDayEvent(model.getDTStart(), model.getDTEnd()));
119             
120             event.setCategories(model.getCategories());
121             
122             RecurrenceRule r = model.getRecurrenceRule();
123             if (r != null) {
124                 // create recurrence
125
Recurrence columbaRecurrence = new Recurrence(Recurrence.fromFrequency(r.getFrequency()));
126                 columbaRecurrence.setEndType(IRecurrence.RECURRENCE_END_FOREVER);
127                 columbaRecurrence.setInterval(r.getInterval());
128                 if (r.getRepetitionCount() != null) {
129                     columbaRecurrence.setEndType(IRecurrence.RECURRENCE_END_MAXOCCURRENCES);
130                     columbaRecurrence.setEndMaxOccurrences(r.getRepetitionCount());
131                 } else if (r.getUntilDate() != null) {
132                     columbaRecurrence.setEndType(IRecurrence.RECURRENCE_END_ENDDATE);
133                     columbaRecurrence.setEndDate(r.getUntilDate());
134                 }
135                 // FIXME r.setPos();
136
event.setRecurrence(columbaRecurrence);
137             }
138             
139             IEventInfo eventInfo = new EventInfo((String JavaDoc) model.getId(), model.getCalendar(), event);
140             eventInfo.setCalendar(model.getCalendar());
141
142             // TODO finish unmarshalling of all available properties
143

144             return eventInfo;
145         }
146
147         else
148             throw new IllegalArgumentException JavaDoc("unknown component type");
149
150     }
151
152     // public static VEventModel createVEvent(Document doc)
153
// throws SyntaxException, InvocationException {
154
// if (doc == null)
155
// throw new InvocationException("doc == null");
156
//
157
// BasicDocumentModel model = new BasicDocumentModel(doc);
158
//
159
// VEventModel c = new VEventModel();
160
// c.setId((String) model.getId());
161
//
162
// c.setDescription(model.getDescription());
163
//
164
// // TODO createVEvent
165
// return null;
166
// }
167
//
168
// public static Document persistVEvent(VEventModel vEventComponent)
169
// throws SyntaxException, InvocationException {
170
// // TODO persistVEvent
171
// return null;
172
// }
173
//
174
// public static VTodoModel createVTodo(Document doc) throws
175
// SyntaxException,
176
// InvocationException {
177
// if (doc == null)
178
// throw new InvocationException("doc == null");
179
//
180
// // TODO createVTodo
181
// return null;
182
// }
183
//
184
// public static Document persistVTodo(VTodoModel vTodoComponent)
185
// throws SyntaxException, InvocationException {
186
// // TODO persistVTodo
187
// return null;
188
// }
189
//
190
// public static VFreeBusyModel createVFreeBusy(Document doc)
191
// throws SyntaxException, InvocationException {
192
// if (doc == null)
193
// throw new InvocationException("doc == null");
194
//
195
// // TODO createVFreeBusy
196
// return null;
197
// }
198
}
199
Free Books   Free Magazines  
Popular Tags