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
24 /**
25 * Field update
26 */
27 public class FieldUpdate {
28 FieldURI fieldURI;
29 String value;
30
31 /**
32 * Create field update with value.
33 *
34 * @param fieldURI target field
35 * @param value field value
36 */
37 public FieldUpdate(FieldURI fieldURI, String value) {
38 this.fieldURI = fieldURI;
39 this.value = value;
40 }
41
42 protected FieldUpdate() {
43 // empty constructor for subclass
44 }
45
46 /**
47 * Write field to request writer.
48 *
49 * @param itemType item type
50 * @param writer request writer
51 * @throws IOException on error
52 */
53 public void write(String itemType, Writer writer) throws IOException {
54 String action;
55 //noinspection VariableNotUsedInsideIf
56 if (value == null || value.length() == 0) {
57 action = "Delete";
58 } else {
59 action = "Set";
60 }
61 if (itemType != null) {
62 writer.write("<t:");
63 writer.write(action);
64 writer.write(itemType);
65 writer.write("Field>");
66 }
67
68 // do not try to set empty value on create
69 if (itemType != null || (value != null && value.length() > 0)) {
70 StringBuilder buffer = new StringBuilder();
71 if (value == null || value.length() == 0) {
72 fieldURI.appendTo(buffer);
73 } else {
74 fieldURI.appendValue(buffer, itemType, value);
75 }
76 writer.write(buffer.toString());
77 }
78
79 if (itemType != null) {
80 writer.write("</t:");
81 writer.write(action);
82 writer.write(itemType);
83 writer.write("Field>");
84 }
85 }
86 }