1 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 { 41 42 if (c == null) 43 throw new IllegalArgumentException ("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 model.setRecurrenceRule(r); 77 } 78 79 model.setCategories(eventInfo.getEvent().getCategories()); 80 81 model.setCalendar(eventInfo.getCalendar()); 82 } 83 84 return model.getDocument(); 86 87 } 88 89 public static IComponentInfo unmarshall(Document document) 90 throws SyntaxException, IllegalArgumentException { 91 92 if (document == null) 93 throw new IllegalArgumentException ("document == null"); 94 95 XCSDocumentParser model = new XCSDocumentParser(document); 96 97 if (model.getId() == null) 98 throw new IllegalArgumentException ("id == null"); 99 100 if (model.getComponentType().equals(ICALENDAR.VEVENT)) { 101 IEvent event = new Event((String ) 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 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 event.setRecurrence(columbaRecurrence); 137 } 138 139 IEventInfo eventInfo = new EventInfo((String ) model.getId(), model.getCalendar(), event); 140 eventInfo.setCalendar(model.getCalendar()); 141 142 144 return eventInfo; 145 } 146 147 else 148 throw new IllegalArgumentException ("unknown component type"); 149 150 } 151 152 } 199
| Popular Tags
|