Update deps to Funambol 8.7
[jgroupdav.git] / src / main / java / net / bionicmessage / objects / RecurrenceManager.java
blobe03eae504638167974770aa3459e2fa1ae441385
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.text.ParseException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import net.fortuna.ical4j.model.Calendar;
32 import net.fortuna.ical4j.model.Component;
33 import net.fortuna.ical4j.model.ComponentList;
34 import net.fortuna.ical4j.model.Date;
35 import net.fortuna.ical4j.model.property.ExDate;
36 import net.fortuna.ical4j.model.property.RecurrenceId;
37 import net.fortuna.ical4j.model.property.Uid;
39 /**
41 * @author matt
43 public class RecurrenceManager {
45 public static List<String> getDetatchedRecurrenceUids(Calendar c) {
46 ArrayList<String> uids = new ArrayList();
47 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
48 for (int i = 0; i < clist.size(); i++) {
49 Component occurance = (Component) clist.get(i);
50 Uid ui = (Uid) occurance.getProperty(Uid.UID);
51 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) != null) {
52 RecurrenceId rid = (RecurrenceId) occurance.getProperty(RecurrenceId.RECURRENCE_ID);
53 uids.add(rid.getValue());
56 return uids;
59 public static Calendar obtainStandaloneRecurrence(Calendar c, String recurrenceId) {
60 ArrayList<String> uids = new ArrayList();
61 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
62 for (int i = 0; i < clist.size(); i++) {
63 Component occurance = (Component) clist.get(i);
64 Uid ui = (Uid) occurance.getProperty(Uid.UID);
65 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) != null) {
66 RecurrenceId rid = (RecurrenceId) occurance.getProperty(RecurrenceId.RECURRENCE_ID);
67 // Correct the UID
68 ui.setValue(ui.getValue()+"-r"+recurrenceId);
69 occurance.getProperties().remove(ui);
70 occurance.getProperties().add(ui);
71 if (rid.getValue().equals(recurrenceId)) {
72 Calendar standalone = new Calendar();
73 standalone.getComponents().add(occurance);
74 return standalone;
78 return null;
81 public static ExDate getExdateForCalendar(Calendar c, ExDate exd) {
82 ArrayList<Date> detatchedDates = new ArrayList();
83 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
84 for (int i = 0; i < clist.size(); i++) {
85 Component occurance = (Component) clist.get(i);
86 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) != null) {
87 RecurrenceId rid = (RecurrenceId) occurance.getProperty(RecurrenceId.RECURRENCE_ID);
88 detatchedDates.add(rid.getDate());
91 for (Date d : detatchedDates) {
92 exd.getDates().add(d);
94 return exd;
97 public static Calendar obtainMasterEvent(Calendar c) {
98 ComponentList clist = (ComponentList) c.getComponents(Component.VEVENT);
99 for (int i = 0; i < clist.size(); i++) {
100 Component occurance = (Component) clist.get(i);
101 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) == null) {
102 Calendar cm = new Calendar();
103 // Add the changed exdate
104 ExDate exd = (ExDate) occurance.getProperty(ExDate.EXDATE);
105 if (exd == null)
106 exd = new ExDate();
107 else
108 occurance.getProperties().remove(exd);
109 getExdateForCalendar(c, exd);
110 occurance.getProperties().add(exd);
111 cm.getComponents().add(occurance);
112 return cm;
115 return null;
117 public static Calendar mergeRecurrenceChange(Calendar master, Calendar recurrence, String masterUID, String recurrenceId) throws ParseException {
118 ComponentList clist = (ComponentList) master.getComponents(Component.VEVENT);
119 Calendar merged = master;
120 for (int i = 0; i < clist.size(); i++) {
121 Component occurance = (Component) clist.get(i);
122 if (occurance.getProperty(RecurrenceId.RECURRENCE_ID) != null
123 && ((RecurrenceId)occurance.getProperty(RecurrenceId.RECURRENCE_ID)).getValue().equals(recurrenceId)) {
124 merged.getComponents().remove(occurance);
125 Component replacementOccurance = recurrence.getComponent(Component.VEVENT);
126 // Delete the UID and replace it with the master
127 Uid uid = (Uid) replacementOccurance.getProperty(Uid.UID);
128 replacementOccurance.getProperties().remove(uid);
129 uid.setValue(masterUID);
130 replacementOccurance.getProperties().add(uid);
132 RecurrenceId recurId = (RecurrenceId) replacementOccurance.getProperty(RecurrenceId.RECURRENCE_ID);
133 if (recurId != null) {
134 replacementOccurance.getProperties().remove(recurId);
136 recurId = new RecurrenceId(recurrenceId);
137 replacementOccurance.getProperties().add(recurId);
138 merged.getComponents().add(replacementOccurance);
139 break;
142 return merged;