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 java.io.IOException;
22  import java.io.Writer;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  /**
27   * Field update with multiple values.
28   */
29  public class IndexedFieldUpdate extends FieldUpdate {
30      final Set<FieldUpdate> updates = new HashSet<FieldUpdate>();
31      protected final String collectionName;
32  
33      /**
34       * Create indexed field update object.
35       *
36       * @param collectionName values collection name e.g. EmailAddresses
37       */
38      public IndexedFieldUpdate(String collectionName) {
39          this.collectionName = collectionName;
40      }
41  
42      /**
43       * Add indexed field value.
44       *
45       * @param fieldUpdate field update object
46       */
47      public void addFieldValue(FieldUpdate fieldUpdate) {
48          updates.add(fieldUpdate);
49      }
50  
51      /**
52       * Write field to request writer.
53       *
54       * @param itemType item type
55       * @param writer   request writer
56       * @throws IOException on error
57       */
58      @Override
59      public void write(String itemType, Writer writer) throws IOException {
60          if (itemType == null) {
61              // check if at least one non null value
62              boolean hasValue = false;
63              for (FieldUpdate fieldUpdate : updates) {
64                  if (fieldUpdate.value != null) {hasValue = true; }
65              }
66              if (hasValue) {
67                  // use collection name on create
68                  writer.write("<t:");
69                  writer.write(collectionName);
70                  writer.write(">");
71  
72                  StringBuilder buffer = new StringBuilder();
73                  for (FieldUpdate fieldUpdate : updates) {
74                      fieldUpdate.fieldURI.appendValue(buffer, null, fieldUpdate.value);
75                  }
76                  writer.write(buffer.toString());
77  
78                  writer.write("</t:");
79                  writer.write(collectionName);
80                  writer.write(">");
81              }
82          } else {
83              // on update, write each fieldupdate
84              for (FieldUpdate fieldUpdate : updates) {
85                  fieldUpdate.write(itemType, writer);
86              }
87          }
88      }
89  
90  }