Working implementation of ical detatched events manager, can get split events from...
[jgroupdav.git] / src / main / java / net / bionicmessage / objects / RecurrenceManager.java
blob55e587279222adbb6fc4b89c5ce2617d201482a5
1 /*
2 * Redistribution and use in source and binary forms, with or without modification, are
3 * permitted provided that the following conditions are met:
4 *
5 * 1. Redistributions of source code must retain the above copyright notice, this list of
6 * conditions and the following disclaimer.
7 *
8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
9 * of conditions and the following disclaimer in the documentation and/or other materials
10 * provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED BY Mathew McBride ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
14 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
15 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
18 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
20 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 * The views and conclusions contained in the software and documentation are those of the
23 * authors and should not be interpreted as representing official policies, either expressed
24 * or implied, of Mathew McBride.
26 package net.bionicmessage.objects;
28 import java.util.ArrayList;
29 import java.util.List;
30 import net.fortuna.ical4j.model.Calendar;
31 import net.fortuna.ical4j.model.Component;
32 import net.fortuna.ical4j.model.ComponentList;
33 import net.fortuna.ical4j.model.Date;
34 import net.fortuna.ical4j.model.property.ExDate;
35 import net.fortuna.ical4j.model.property.RecurrenceId;
36 import net.fortuna.ical4j.model.property.Uid;
38 /**
40 * @author matt
42 public class RecurrenceManager {
44 public static List<String> getDetatchedRecurrenceUids(Calendar c) {
45 ArrayList<String> uids = new ArrayList();
46 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
47 for (int i = 0; i < clist.size(); i++) {
48 Component occurance = (Component) clist.get(i);
49 Uid ui = (Uid) occurance.getProperty(Uid.UID);
50 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) != null) {
51 RecurrenceId rid = (RecurrenceId) occurance.getProperty(RecurrenceId.RECURRENCE_ID);
52 uids.add(rid.getValue());
55 return uids;
58 public static Calendar obtainStandaloneRecurrence(Calendar c, String recurrenceId) {
59 ArrayList<String> uids = new ArrayList();
60 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
61 for (int i = 0; i < clist.size(); i++) {
62 Component occurance = (Component) clist.get(i);
63 Uid ui = (Uid) occurance.getProperty(Uid.UID);
64 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) != null) {
65 RecurrenceId rid = (RecurrenceId) occurance.getProperty(RecurrenceId.RECURRENCE_ID);
66 // Correct the UID
67 ui.setValue(ui.getValue()+"-r"+recurrenceId);
68 occurance.getProperties().remove(ui);
69 occurance.getProperties().add(ui);
70 if (rid.getValue().equals(recurrenceId)) {
71 Calendar standalone = new Calendar();
72 standalone.getComponents().add(occurance);
73 return standalone;
77 return null;
80 public static ExDate getExdateForCalendar(Calendar c, ExDate exd) {
81 ArrayList<Date> detatchedDates = new ArrayList();
82 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
83 for (int i = 0; i < clist.size(); i++) {
84 Component occurance = (Component) clist.get(i);
85 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) != null) {
86 RecurrenceId rid = (RecurrenceId) occurance.getProperty(RecurrenceId.RECURRENCE_ID);
87 detatchedDates.add(rid.getDate());
90 for (Date d : detatchedDates) {
91 exd.getDates().add(d);
93 return exd;
96 public static Calendar obtainMasterEvent(Calendar c) {
97 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
98 for (int i = 0; i < clist.size(); i++) {
99 Component occurance = (Component) clist.get(i);
100 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) == null) {
101 Calendar cm = new Calendar();
102 // Add the changed exdate
103 ExDate exd = (ExDate) occurance.getProperty(ExDate.EXDATE);
104 if (exd == null)
105 exd = new ExDate();
106 else
107 occurance.getProperties().remove(exd);
108 getExdateForCalendar(c, exd);
109 occurance.getProperties().add(exd);
110 cm.getComponents().add(occurance);
111 return cm;
114 return null;