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<>();
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) {
65                      hasValue = true;
66                      break;
67                  }
68              }
69              if (hasValue) {
70                  // use collection name on create
71                  writer.write("<t:");
72                  writer.write(collectionName);
73                  writer.write(">");
74  
75                  StringBuilder buffer = new StringBuilder();
76                  for (FieldUpdate fieldUpdate : updates) {
77                      fieldUpdate.fieldURI.appendValue(buffer, null, fieldUpdate.value);
78                  }
79                  writer.write(buffer.toString());
80  
81                  writer.write("</t:");
82                  writer.write(collectionName);
83                  writer.write(">");
84              }
85          } else {
86              // on update, write each fieldupdate
87              for (FieldUpdate fieldUpdate : updates) {
88                  fieldUpdate.write(itemType, writer);
89              }
90          }
91      }
92  
93  }