View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package davmail.exchange.ews;
20  
21  import davmail.util.StringUtil;
22  
23  import java.util.List;
24  
25  /**
26   * Unindexed Field URI
27   */
28  public class UnindexedFieldURI implements FieldURI {
29      protected final String fieldURI;
30      protected final String fieldName;
31      protected String graphId;
32  
33      /**
34       * Create unindexed field URI.
35       *
36       * @param fieldURI field name
37       */
38      public UnindexedFieldURI(String fieldURI) {
39          this.fieldURI = fieldURI;
40          int colonIndex = fieldURI.indexOf(':');
41          if (colonIndex < 0) {
42              fieldName = fieldURI;
43          } else {
44              fieldName = fieldURI.substring(colonIndex + 1);
45          }
46      }
47  
48      public UnindexedFieldURI(String fieldURI, String graphId) {
49          this(fieldURI);
50          this.graphId = graphId;
51      }
52  
53      public void appendTo(StringBuilder buffer) {
54          buffer.append("<t:FieldURI FieldURI=\"").append(fieldURI).append("\"/>");
55      }
56  
57      public void appendValue(StringBuilder buffer, String itemType, String value) {
58          if (fieldURI.startsWith("message") && itemType != null) {
59              itemType = "Message";
60          } else if (fieldURI.startsWith("calendar") && itemType != null) {
61              itemType = "CalendarItem";
62          } else if (fieldURI.startsWith("task") && itemType != null) {
63              itemType = "Task";
64          } else if (fieldURI.startsWith("contacts") && itemType != null) {
65              itemType = "Contact";
66          }
67          if (itemType != null) {
68              appendTo(buffer);
69              buffer.append("<t:");
70              buffer.append(itemType);
71              buffer.append('>');
72          }
73          if ("MeetingTimeZone".equals(fieldName)) {
74              buffer.append("<t:MeetingTimeZone TimeZoneName=\"");
75              buffer.append(StringUtil.xmlEncodeAttribute(value));
76              buffer.append("\"></t:MeetingTimeZone>");
77          } else if ("StartTimeZone".equals(fieldName)) {
78              buffer.append("<t:StartTimeZone Id=\"");
79              buffer.append(StringUtil.xmlEncodeAttribute(value));
80              buffer.append("\"></t:StartTimeZone>");
81          } else if ("EndTimeZone".equals(fieldName)) {
82              buffer.append("<t:EndTimeZone Id=\"");
83              buffer.append(StringUtil.xmlEncodeAttribute(value));
84              buffer.append("\"></t:EndTimeZone>");
85          } else {
86              buffer.append("<t:");
87              buffer.append(fieldName);
88              buffer.append('>');
89              buffer.append(StringUtil.xmlEncodeAttribute(value));
90              buffer.append("</t:");
91              buffer.append(fieldName);
92              buffer.append('>');
93          }
94          if (itemType != null) {
95              buffer.append("</t:");
96              buffer.append(itemType);
97              buffer.append('>');
98          }
99      }
100 
101     public void appendValues(StringBuilder buffer, String itemType, List<String> values) {
102         if (fieldURI.startsWith("message") && itemType != null) {
103             itemType = "Message";
104         } else if (fieldURI.startsWith("calendar") && itemType != null) {
105             itemType = "CalendarItem";
106         } else if (fieldURI.startsWith("task") && itemType != null) {
107             itemType = "Task";
108         } else if (fieldURI.startsWith("contacts") && itemType != null) {
109             itemType = "Contact";
110         } else if (fieldURI.startsWith("distributionlist") && itemType != null) {
111             itemType = "DistributionList";
112         }
113         if (!values.isEmpty()) {
114             if (itemType != null) {
115                 appendTo(buffer);
116                 buffer.append("<t:");
117                 buffer.append(itemType);
118                 buffer.append('>');
119             }
120             buffer.append("<t:");
121             buffer.append(fieldName);
122             buffer.append('>');
123             for (String value : values) {
124                 if ("RequiredAttendees".equals(fieldName) || "OptionalAttendees".equals(fieldName)) {
125                     buffer.append("<t:Attendee><t:Mailbox><t:EmailAddress>");
126                     buffer.append(StringUtil.xmlEncodeAttribute(value));
127                     buffer.append("</t:EmailAddress></t:Mailbox></t:Attendee>");
128                 } else if ("Members".equals(fieldName)) {
129                     if (value.toLowerCase().startsWith("mailto:")) {
130                         buffer.append("<t:Member><t:Mailbox><t:EmailAddress>");
131                         buffer.append(StringUtil.xmlEncodeAttribute(value.substring(7)));
132                         buffer.append("</t:EmailAddress></t:Mailbox></t:Member>");
133                     } else if (value.startsWith("urn:uuid:")){
134                         buffer.append("<t:Member><t:Mailbox><t:MailboxType>PrivateDL</t:MailboxType><t:ItemId Id=\"");
135                         buffer.append(StringUtil.xmlEncodeAttribute(value.substring(9)));
136                         buffer.append("\"/></t:Mailbox></t:Member>");
137                     }
138                 } else {
139                     buffer.append(StringUtil.xmlEncodeAttribute(value));
140                 }
141             }
142 
143             buffer.append("</t:");
144             buffer.append(fieldName);
145             buffer.append('>');
146 
147             if (itemType != null) {
148                 buffer.append("</t:");
149                 buffer.append(itemType);
150                 buffer.append('>');
151             }
152         } else if (itemType != null) {
153             // append field name only to remove values
154             appendTo(buffer);
155         }
156 
157     }
158 
159     public String getResponseName() {
160         return fieldName;
161     }
162 
163     @Override
164     public String getGraphId() {
165         if (graphId != null) {
166             return graphId;
167         } else {
168             return fieldName;
169         }
170     }
171 
172     @Override
173     public boolean isMultiValued() {
174         return false;
175     }
176 
177     @Override
178     public boolean isNumber() {
179         // TODO
180         throw new UnsupportedOperationException();
181     }
182 
183     @Override
184     public boolean isBoolean() {
185         throw new UnsupportedOperationException();
186     }
187 
188 }