Commit b39bcf20 authored by Mickaël Guessant's avatar Mickaël Guessant

Caldav: EWS refactoring, implement multiple day recurrence and end date

git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@2505 3d1905a2-6b24-0410-a738-b14d5a86fcbd
parent 3f08048b
......@@ -1565,15 +1565,20 @@ public class EwsExchangeSession extends ExchangeSession {
VProperty rrule = vCalendar.getFirstVevent().getProperty("RRULE");
if (rrule != null) {
RecurrenceFieldUpdate recurrenceFieldUpdate = new RecurrenceFieldUpdate();
String rruleValue = rrule.getValue();
if (rruleValue.contains("FREQ=DAILY")) {
recurrenceFieldUpdate.setRecurrencePattern(RecurrenceFieldUpdate.RecurrencePattern.DailyRecurrence);
} else if (rruleValue.contains("FREQ=WEEKLY")) {
recurrenceFieldUpdate.setRecurrencePattern(RecurrenceFieldUpdate.RecurrencePattern.WeeklyRecurrence);
} else if (rruleValue.contains("FREQ=MONTHLY")) {
recurrenceFieldUpdate.setRecurrencePattern(RecurrenceFieldUpdate.RecurrencePattern.AbsoluteMonthly);
} else if (rruleValue.contains("FREQ=YEARLY")) {
recurrenceFieldUpdate.setRecurrencePattern(RecurrenceFieldUpdate.RecurrencePattern.AbsoluteYearly);
List<String> rruleValues = rrule.getValues();
for (String rruleValue: rruleValues) {
int index = rruleValue.indexOf("=");
if (index >=0) {
String key = rruleValue.substring(0, index);
String value = rruleValue.substring(index+1);
if ("FREQ".equals(key)) {
recurrenceFieldUpdate.setRecurrencePattern(value);
} else if ("UNTIL".equals(key)) {
recurrenceFieldUpdate.setEndDate(parseDateFromExchange(convertCalendarDateToExchange(value)));
} else if ("BYDAY".equals(key)) {
recurrenceFieldUpdate.setByDay(value.split(","));
}
}
}
recurrenceFieldUpdate.setStartDate(parseDateFromExchange(convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTSTART"))+"Z"));
updates.add(recurrenceFieldUpdate);
......
......@@ -24,21 +24,41 @@ import davmail.exchange.ExchangeSession;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.*;
/**
* Handle calendar item recurrence update
*/
public class RecurrenceFieldUpdate extends FieldUpdate {
private Date startDate;
static final HashMap<String, String> calDayToDayOfWeek = new HashMap<String, String>();
static {
calDayToDayOfWeek.put("SU", "Sunday");
calDayToDayOfWeek.put("MO", "Monday");
calDayToDayOfWeek.put("TU", "Tuesday");
calDayToDayOfWeek.put("WE", "Wednesday");
calDayToDayOfWeek.put("TH", "Thursday");
calDayToDayOfWeek.put("FR", "Friday");
calDayToDayOfWeek.put("SA", "Saturday");
}
protected Date startDate;
protected Date endDate;
protected HashSet<String> byDays = null;
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public void setByDay(String[] days) {
byDays = new HashSet<String>();
for (String day: days) {
byDays.add(calDayToDayOfWeek.get(day));
}
}
public enum RecurrencePattern {WeeklyRecurrence, DailyRecurrence, AbsoluteYearly, AbsoluteMonthly};
RecurrencePattern recurrencePattern;
int recurrenceInterval = 1;
......@@ -53,6 +73,18 @@ public class RecurrenceFieldUpdate extends FieldUpdate {
this.recurrencePattern = recurrencePattern;
}
public void setRecurrencePattern(String value) {
if ("DAILY".equals(value)) {
setRecurrencePattern(RecurrenceFieldUpdate.RecurrencePattern.DailyRecurrence);
} else if ("WEEKLY".equals(value)) {
setRecurrencePattern(RecurrencePattern.WeeklyRecurrence);
} else if ("MONTHLY".equals(value)) {
setRecurrencePattern(RecurrencePattern.AbsoluteMonthly);
} else if ("YEARLY".equals(value)) {
setRecurrencePattern(RecurrencePattern.AbsoluteYearly);
}
}
public void setRecurrenceInterval(RecurrencePattern recurrencePattern) {
this.recurrencePattern = recurrencePattern;
}
......@@ -83,11 +115,11 @@ public class RecurrenceFieldUpdate extends FieldUpdate {
writer.write("<t:Interval>");
writer.write(String.valueOf(recurrenceInterval));
writer.write("</t:Interval>");
writer.write("<t:DaysOfWeek>"+getDayOfWeek()+"</t:DaysOfWeek>");
writeDaysOfWeek(writer);
writer.write("</t:");
writer.write(recurrencePattern.toString());
writer.write(">");
writer.write("<t:NoEndRecurrence><t:StartDate>"+getFormattedStartDate()+"</t:StartDate></t:NoEndRecurrence>");
writeStartEnd(writer);
writer.write("</t:Recurrence>");
writer.write("</t:CalendarItem>");
......@@ -100,16 +132,52 @@ public class RecurrenceFieldUpdate extends FieldUpdate {
}
}
private void writeStartEnd(Writer writer) throws IOException {
if (endDate == null) {
writer.write("<t:NoEndRecurrence><t:StartDate>");
writer.write(getFormattedDate(startDate));
writer.write("</t:StartDate></t:NoEndRecurrence>");
} else {
writer.write("<t:EndDateRecurrence>");
writer.write("<t:StartDate>");
writer.write(getFormattedDate(startDate));
writer.write("</t:StartDate>");
writer.write("<t:EndDate>");
writer.write(getFormattedDate(endDate));
writer.write("</t:EndDate>");
writer.write("</t:EndDateRecurrence>");
}
}
private void writeDaysOfWeek(Writer writer) throws IOException {
writer.write("<t:DaysOfWeek>");
if (byDays != null) {
boolean first = true;
for (String dayOfeek:byDays) {
if (first) {
first = false;
} else {
writer.write(' ');
}
writer.write(dayOfeek);
}
} else {
writer.write(getDayOfWeek());
}
writer.write("</t:DaysOfWeek>");
}
private String getDayOfWeek() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE", Locale.ENGLISH);
simpleDateFormat.setTimeZone(ExchangeSession.GMT_TIMEZONE);
return simpleDateFormat.format(startDate);
}
private String getFormattedStartDate() {
private String getFormattedDate(Date date) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.setTimeZone(ExchangeSession.GMT_TIMEZONE);
return simpleDateFormat.format(startDate);
return simpleDateFormat.format(date);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment