Commit 3f08048b authored by Mickaël Guessant's avatar Mickaël Guessant

Caldav: EWS refactoring, first recurrence implementation step, improve ignore etag handling

git-svn-id: https://svn.code.sf.net/p/davmail/code/trunk@2504 3d1905a2-6b24-0410-a738-b14d5a86fcbd
parent 111e435a
......@@ -30,7 +30,9 @@ import davmail.http.DavGatewayHttpClientFacade;
import davmail.util.IOUtil;
import davmail.util.StringUtil;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import javax.mail.MessagingException;
......@@ -111,6 +113,7 @@ public class EwsExchangeSession extends ExchangeSession {
partstatToResponseMap.put("ACCEPTED", "AcceptItem");
partstatToResponseMap.put("TENTATIVE", "TentativelyAcceptItem");
partstatToResponseMap.put("DECLINED", "DeclineItem");
partstatToResponseMap.put("NEEDS-ACTION", "ReplyToItem");
}
protected Map<String, String> folderIdMap;
......@@ -1551,6 +1554,58 @@ public class EwsExchangeSession extends ExchangeSession {
super(folderPath, itemName, contentClass, itemBody, etag, noneMatch);
}
protected List<FieldUpdate> buildFieldUpdates(VCalendar vCalendar) throws DavMailException {
List<FieldUpdate> updates = new ArrayList<FieldUpdate>();
// TODO: update all event fields and handle other occurrences
updates.add(Field.createFieldUpdate("dtstart", convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTSTART"))));
updates.add(Field.createFieldUpdate("dtend", convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTEND"))));
updates.add(Field.createFieldUpdate("description", vCalendar.getFirstVeventPropertyValue("DESCRIPTION")));
updates.add(Field.createFieldUpdate("location", vCalendar.getFirstVeventPropertyValue("LOCATION")));
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);
}
recurrenceFieldUpdate.setStartDate(parseDateFromExchange(convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTSTART"))+"Z"));
updates.add(recurrenceFieldUpdate);
}
AttendeeFieldUpdate requiredAttendees = new AttendeeFieldUpdate(Field.get("requiredattendees"));
AttendeeFieldUpdate optionalAttendees = new AttendeeFieldUpdate(Field.get("optionalattendees"));
updates.add(requiredAttendees);
updates.add(optionalAttendees);
List<VProperty> attendees = vCalendar.getFirstVevent().getProperties("ATTENDEE");
if (attendees != null) {
for (VProperty property:attendees) {
String attendeeEmail = vCalendar.getEmailValue(property);
if (attendeeEmail != null && attendeeEmail.indexOf('@') >= 0) {
if (!email.equals(attendeeEmail)) {
String attendeeRole = property.getParamValue("ROLE");
if ("REQ-PARTICIPANT".equals(attendeeRole)) {
requiredAttendees.addValue(attendeeEmail);
} else {
optionalAttendees.addValue(attendeeEmail);
}
}
}
}
}
return updates;
}
@Override
public ItemResult createOrUpdate() throws IOException {
if (vCalendar.isTodo() && isMainCalendar(folderPath)) {
......@@ -1572,7 +1627,9 @@ public class EwsExchangeSession extends ExchangeSession {
currentEtag = currentItem.get(Field.get("etag").getResponseName());
LOGGER.debug("Existing item found with etag: " + currentEtag + " client etag: " + etag + " id: " + currentItemId.id);
}
if ("*".equals(noneMatch) && !Settings.getBooleanProperty("davmail.ignoreNoneMatchStar", true)) {
if (vCalendar.isMeeting() && !vCalendar.isMeetingOrganizer()) {
LOGGER.debug("Ignore etag check, meeting response");
} else if ("*".equals(noneMatch) && !Settings.getBooleanProperty("davmail.ignoreNoneMatchStar", true)) {
// create requested
//noinspection VariableNotUsedInsideIf
if (currentItemId != null) {
......@@ -1633,6 +1690,7 @@ public class EwsExchangeSession extends ExchangeSession {
} else {
// update existing item
if (currentItemId != null) {
if (vCalendar.isMeeting() && !vCalendar.isMeetingOrganizer()) {
// This is a meeting response
......@@ -1646,40 +1704,10 @@ public class EwsExchangeSession extends ExchangeSession {
item
);
} else {
List<FieldUpdate> updates = new ArrayList<FieldUpdate>();
// TODO: update all event fields and handle other occurrences
updates.add(Field.createFieldUpdate("dtstart", convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTSTART"))));
updates.add(Field.createFieldUpdate("dtend", convertCalendarDateToExchange(vCalendar.getFirstVeventPropertyValue("DTEND"))));
updates.add(Field.createFieldUpdate("description", vCalendar.getFirstVeventPropertyValue("DESCRIPTION")));
updates.add(Field.createFieldUpdate("location", vCalendar.getFirstVeventPropertyValue("LOCATION")));
AttendeeFieldUpdate requiredAttendees = new AttendeeFieldUpdate(Field.get("requiredattendees"));
AttendeeFieldUpdate optionalAttendees = new AttendeeFieldUpdate(Field.get("optionalattendees"));
updates.add(requiredAttendees);
updates.add(optionalAttendees);
List<VProperty> attendees = vCalendar.getFirstVevent().getProperties("ATTENDEE");
if (attendees != null) {
for (VProperty property:attendees) {
String attendeeEmail = vCalendar.getEmailValue(property);
if (attendeeEmail != null && attendeeEmail.indexOf('@') >= 0) {
if (!email.equals(attendeeEmail)) {
String attendeeRole = property.getParamValue("ROLE");
if ("REQ-PARTICIPANT".equals(attendeeRole)) {
requiredAttendees.addValue(attendeeEmail);
} else {
optionalAttendees.addValue(attendeeEmail);
}
}
}
}
}
createOrUpdateItemMethod = new UpdateItemMethod(MessageDisposition.SaveOnly,
ConflictResolution.AutoResolve,
SendMeetingInvitationsOrCancellations.SendToAllAndSaveCopy,
currentItemId, updates);
currentItemId, buildFieldUpdates(vCalendar));
}
} else {
// create
......
/*
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
* Copyright (C) 2010 Mickael Guessant
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package davmail.exchange.ews;
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;
/**
* Handle calendar item recurrence update
*/
public class RecurrenceFieldUpdate extends FieldUpdate {
private Date startDate;
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public enum RecurrencePattern {WeeklyRecurrence, DailyRecurrence, AbsoluteYearly, AbsoluteMonthly};
RecurrencePattern recurrencePattern;
int recurrenceInterval = 1;
/**
* Create recurrence field update.
*/
public RecurrenceFieldUpdate() {
}
public void setRecurrencePattern(RecurrencePattern recurrencePattern) {
this.recurrencePattern = recurrencePattern;
}
public void setRecurrenceInterval(RecurrencePattern recurrencePattern) {
this.recurrencePattern = recurrencePattern;
}
/**
* Write field to request writer.
*
* @param itemType item type
* @param writer request writer
* @throws IOException on error
*/
public void write(String itemType, Writer writer) throws IOException {
if (itemType != null) {
writer.write("<t:Set");
writer.write(itemType);
writer.write("Field>");
}
// do not try to set empty value on create
if (itemType != null) {
writer.write("<t:FieldURI FieldURI=\"calendar:Recurrence\"/>");
writer.write("<t:CalendarItem>");
writer.write("<t:Recurrence>");
writer.write("<t:");
writer.write(recurrencePattern.toString());
writer.write(">");
writer.write("<t:Interval>");
writer.write(String.valueOf(recurrenceInterval));
writer.write("</t:Interval>");
writer.write("<t:DaysOfWeek>"+getDayOfWeek()+"</t:DaysOfWeek>");
writer.write("</t:");
writer.write(recurrencePattern.toString());
writer.write(">");
writer.write("<t:NoEndRecurrence><t:StartDate>"+getFormattedStartDate()+"</t:StartDate></t:NoEndRecurrence>");
writer.write("</t:Recurrence>");
writer.write("</t:CalendarItem>");
}
if (itemType != null) {
writer.write("</t:Set");
writer.write(itemType);
writer.write("Field>");
}
}
private String getDayOfWeek() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE", Locale.ENGLISH);
simpleDateFormat.setTimeZone(ExchangeSession.GMT_TIMEZONE);
return simpleDateFormat.format(startDate);
}
private String getFormattedStartDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.setTimeZone(ExchangeSession.GMT_TIMEZONE);
return simpleDateFormat.format(startDate);
}
}
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