From 2c95fc14dc0932f9a90f42a95802f6a12c7021fd Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 20 Jul 2010 16:33:58 +0000 Subject: [PATCH] Carddav: Add Anniversary support git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1235 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 63 ++++++++++++------- .../exchange/dav/DavExchangeSession.java | 7 ++- src/java/davmail/exchange/dav/Field.java | 1 + .../exchange/ews/EwsExchangeSession.java | 8 +-- src/java/davmail/exchange/ews/Field.java | 1 + .../exchange/TestExchangeSessionContact.java | 17 +++++ 6 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 138622e1..0aedab9b 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -1731,16 +1731,9 @@ public abstract class ExchangeSession { writer.appendProperty("ROLE", get("profession")); writer.appendProperty("NICKNAME", get("nickname")); writer.appendProperty("X-AIM", get("im")); - // convert bday value - String bday = get("bday"); - if (bday != null && bday.length() > 0) { - try { - SimpleDateFormat parser = ExchangeSession.getZuluDateFormat(); - writer.appendProperty("BDAY", ExchangeSession.getVcardBdayFormat().format(parser.parse(bday))); - } catch (ParseException e) { - LOGGER.warn("Invalid date: " + bday); - } - } + + writer.appendProperty("BDAY", convertZuluDateToBday(get("bday"))); + writer.appendProperty("X-ANNIVERSARY", convertZuluDateToBday(get("anniversary"))); writer.appendProperty("CATEGORIES", get("keywords")); @@ -1767,6 +1760,19 @@ public abstract class ExchangeSession { return writer.toString(); } + protected String convertZuluDateToBday(String value) { + String result = null; + if (value != null && value.length() > 0) { + try { + SimpleDateFormat parser = ExchangeSession.getZuluDateFormat(); + result = ExchangeSession.getVcardBdayFormat().format(parser.parse(value)); + } catch (ParseException e) { + LOGGER.warn("Invalid date: " + value); + } + } + return result; + } + } /** @@ -2771,20 +2777,9 @@ public abstract class ExchangeSession { } else if ("X-AIM".equals(property.getKey())) { properties.put("im", property.getValue()); } else if ("BDAY".equals(property.getKey())) { - String value = property.getValue(); - if (value != null) { - try { - SimpleDateFormat parser; - if (value.length() == 10) { - parser = ExchangeSession.getVcardBdayFormat(); - } else { - parser = ExchangeSession.getExchangeZuluDateFormat(); - } - properties.put("bday", ExchangeSession.getExchangeZuluDateFormatMillisecond().format(parser.parse(value))); - } catch (ParseException e) { - LOGGER.warn("Invalid date: " + value); - } - } + properties.put("bday", convertBDayToZulu(property.getValue())); + } else if ("X-ANNIVERSARY".equals(property.getKey())) { + properties.put("anniversary", convertBDayToZulu(property.getValue())); } else if ("CATEGORIES".equals(property.getKey())) { properties.put("keywords", property.getValue()); } else if ("CLASS".equals(property.getKey())) { @@ -2818,6 +2813,25 @@ public abstract class ExchangeSession { return internalCreateOrUpdateContact(folderPath, itemName, properties, etag, noneMatch); } + protected String convertBDayToZulu(String value) { + String result = null; + if (value != null && value.length() > 0) { + try { + SimpleDateFormat parser; + if (value.length() == 10) { + parser = ExchangeSession.getVcardBdayFormat(); + } else { + parser = ExchangeSession.getExchangeZuluDateFormat(); + } + result = ExchangeSession.getExchangeZuluDateFormatMillisecond().format(parser.parse(value)); + } catch (ParseException e) { + LOGGER.warn("Invalid date: " + value); + } + } + + return result; + } + protected abstract ItemResult internalCreateOrUpdateContact(String folderPath, String itemName, Map properties, String etag, String noneMatch) throws IOException; @@ -3117,6 +3131,7 @@ public abstract class ExchangeSession { CONTACT_ATTRIBUTES.add("extensionattribute3"); CONTACT_ATTRIBUTES.add("extensionattribute4"); CONTACT_ATTRIBUTES.add("bday"); + CONTACT_ATTRIBUTES.add("anniversary"); CONTACT_ATTRIBUTES.add("businesshomepage"); CONTACT_ATTRIBUTES.add("personalHomePage"); CONTACT_ATTRIBUTES.add("cn"); diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 8cb0f1bd..164f000b 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -571,8 +571,9 @@ public class DavExchangeSession extends ExchangeSession { for (String attributeName : CONTACT_ATTRIBUTES) { String value = getPropertyIfExists(properties, attributeName); if (value != null) { - if ("bday".equals(attributeName) || "lastmodified".equals(attributeName) || "datereceived".equals(attributeName)) { - value = convertDate(value); + if ("bday".equals(attributeName) || "anniversary".equals(attributeName) + || "lastmodified".equals(attributeName) || "datereceived".equals(attributeName)) { + value = convertDateFromExchange(value); } else if ("haspicture".equals(attributeName) || "private".equals(attributeName)) { value = "1".equals(value) ? "true" : "false"; } @@ -1731,7 +1732,7 @@ public class DavExchangeSession extends ExchangeSession { LOGGER.debug("Deleted to :" + destination); } - protected String convertDate(String exchangeDateValue) throws DavMailException { + protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException { String zuluDateValue; try { zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue)); diff --git a/src/java/davmail/exchange/dav/Field.java b/src/java/davmail/exchange/dav/Field.java index d4630a40..7008bfc1 100644 --- a/src/java/davmail/exchange/dav/Field.java +++ b/src/java/davmail/exchange/dav/Field.java @@ -193,6 +193,7 @@ public class Field { createField(SCHEMAS_EXCHANGE, "extensionattribute4"); // DistinguishedPropertySetType.Address/0x00008052/String createField(URN_SCHEMAS_CONTACTS, "bday"); // PR_BIRTHDAY 0x3A42 SystemTime + createField("anniversary", DistinguishedPropertySetType.Address, 0x80DF, "anniversary"); // WeddingAnniversaryLocal createField(URN_SCHEMAS_CONTACTS, "businesshomepage"); // PR_BUSINESS_HOME_PAGE 0x3A51 String createField(URN_SCHEMAS_CONTACTS, "personalHomePage"); // PR_PERSONAL_HOME_PAGE 0x3A50 String //createField(URN_SCHEMAS_CONTACTS, "c"); // country DistinguishedPropertySetType.PublicStrings/urn:schemas:contacts:c/String diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 9e916a49..76774fde 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -254,7 +254,7 @@ public class EwsExchangeSession extends ExchangeSession { String lastVerbExecuted = response.get(Field.get("lastVerbExecuted").getResponseName()); message.answered = "102".equals(lastVerbExecuted) || "103".equals(lastVerbExecuted); message.forwarded = "104".equals(lastVerbExecuted); - message.date = convertDate(response.get(Field.get("date").getResponseName())); + message.date = convertDateFromExchange(response.get(Field.get("date").getResponseName())); message.deleted = "1".equals(response.get(Field.get("deleted").getResponseName())); if (LOGGER.isDebugEnabled()) { @@ -658,8 +658,8 @@ public class EwsExchangeSession extends ExchangeSession { for (String attributeName : CONTACT_ATTRIBUTES) { String value = response.get(Field.get(attributeName).getResponseName()); if (value != null) { - if ("bday".equals(attributeName) || "lastmodified".equals(attributeName) || "datereceived".equals(attributeName)) { - value = convertDate(value); + if ("bday".equals(attributeName) || "anniversary".equals(attributeName) || "lastmodified".equals(attributeName) || "datereceived".equals(attributeName)) { + value = convertDateFromExchange(value); } put(attributeName, value); } @@ -1121,7 +1121,7 @@ public class EwsExchangeSession extends ExchangeSession { } } - protected String convertDate(String exchangeDateValue) throws DavMailException { + protected String convertDateFromExchange(String exchangeDateValue) throws DavMailException { String zuluDateValue; try { zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue)); diff --git a/src/java/davmail/exchange/ews/Field.java b/src/java/davmail/exchange/ews/Field.java index b6daf08e..524f3741 100644 --- a/src/java/davmail/exchange/ews/Field.java +++ b/src/java/davmail/exchange/ews/Field.java @@ -88,6 +88,7 @@ public class Field { FIELD_MAP.put("extensionattribute4", new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.Address, 0x8052, ExtendedFieldURI.PropertyType.String)); FIELD_MAP.put("bday", new ExtendedFieldURI(0x3A42, ExtendedFieldURI.PropertyType.SystemTime)); + FIELD_MAP.put("anniversary", new ExtendedFieldURI(ExtendedFieldURI.DistinguishedPropertySetType.Address, 0x80DF, ExtendedFieldURI.PropertyType.SystemTime)); FIELD_MAP.put("businesshomepage", new ExtendedFieldURI(0x3A51, ExtendedFieldURI.PropertyType.String)); FIELD_MAP.put("personalHomePage", new ExtendedFieldURI(0x3A50, ExtendedFieldURI.PropertyType.String)); diff --git a/src/test/davmail/exchange/TestExchangeSessionContact.java b/src/test/davmail/exchange/TestExchangeSessionContact.java index 48496641..721275a6 100644 --- a/src/test/davmail/exchange/TestExchangeSessionContact.java +++ b/src/test/davmail/exchange/TestExchangeSessionContact.java @@ -423,4 +423,21 @@ public class TestExchangeSessionContact extends AbstractExchangeSessionTestCase assertEquals("20000102T000000Z", contact.get("bday")); System.out.println(contact.getBody()); } + + public void testAnniversary() throws IOException { + ExchangeSession.Contact contact = getCurrentContact(); + + VCardWriter vCardWriter = new VCardWriter(); + vCardWriter.startCard(); + vCardWriter.appendProperty("X-ANNIVERSARY", "2000-01-02"); + vCardWriter.endCard(); + + ExchangeSession.ItemResult result = session.createOrUpdateContact("testcontactfolder", itemName, vCardWriter.toString(), contact.etag, null); + assertEquals(200, result.status); + + contact = getCurrentContact(); + + assertEquals("20000102T000000Z", contact.get("anniversary")); + System.out.println(contact.getBody()); + } }