001 /*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024 package microsoft.exchange.webservices.data.property.complex;
025
026 import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027 import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028 import microsoft.exchange.webservices.data.core.ILazyMember;
029 import microsoft.exchange.webservices.data.core.LazyMember;
030 import microsoft.exchange.webservices.data.core.SimplePropertyBag;
031 import microsoft.exchange.webservices.data.core.XmlAttributeNames;
032 import microsoft.exchange.webservices.data.core.XmlElementNames;
033 import microsoft.exchange.webservices.data.core.service.ServiceObject;
034 import microsoft.exchange.webservices.data.core.enumeration.property.PhysicalAddressKey;
035 import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
036 import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
037
038 import javax.xml.stream.XMLStreamException;
039
040 import java.util.ArrayList;
041 import java.util.List;
042
043 /**
044 * Represents an entry of an PhysicalAddressDictionary.
045 */
046 public final class PhysicalAddressEntry extends DictionaryEntryProperty<PhysicalAddressKey> implements
047 IPropertyBagChangedDelegate<String> {
048
049 /**
050 * The property bag.
051 */
052 private SimplePropertyBag<String> propertyBag;
053
054 /**
055 * Initializes a new instance of PhysicalAddressEntry.
056 */
057 public PhysicalAddressEntry() {
058 super(PhysicalAddressKey.class);
059 this.propertyBag = new SimplePropertyBag<String>();
060 this.propertyBag.addOnChangeEvent(this);
061 }
062
063 /**
064 * Property was changed.
065 *
066 * @param simplePropertyBag the simple property bag
067 */
068 public void propertyBagChanged(SimplePropertyBag<String> simplePropertyBag) {
069 this.changed();
070 }
071
072 /**
073 * Gets the street.
074 *
075 * @return the street
076 * @throws Exception the exception
077 */
078 public String getStreet() throws Exception {
079 return (String) this.propertyBag
080 .getSimplePropertyBag(PhysicalAddressSchema.Street);
081 }
082
083 /**
084 * Sets the street.
085 *
086 * @param value the new street
087 * @throws Exception the exception
088 */
089 public void setStreet(String value) throws Exception {
090 this.propertyBag.setSimplePropertyBag(PhysicalAddressSchema.Street,
091 value);
092
093 }
094
095 /**
096 * Gets the city.
097 *
098 * @return the city
099 * @throws Exception the exception
100 */
101 public String getCity() throws Exception {
102 return (String) this.propertyBag
103 .getSimplePropertyBag(PhysicalAddressSchema.City);
104 }
105
106 /**
107 * Sets the city.
108 *
109 * @param value the new city
110 */
111 public void setCity(String value) {
112 this.propertyBag
113 .setSimplePropertyBag(PhysicalAddressSchema.City, value);
114 }
115
116 /**
117 * Gets the state.
118 *
119 * @return the state
120 * @throws Exception the exception
121 */
122 public String getState() throws Exception {
123 return (String) this.propertyBag
124 .getSimplePropertyBag(PhysicalAddressSchema.State);
125 }
126
127 /**
128 * Sets the state.
129 *
130 * @param value the new state
131 */
132 public void setState(String value) {
133 this.propertyBag.setSimplePropertyBag(PhysicalAddressSchema.State,
134 value);
135 }
136
137 /**
138 * Gets the country or region.
139 *
140 * @return the country or region
141 * @throws Exception the exception
142 */
143 public String getCountryOrRegion() throws Exception {
144 return (String) this.propertyBag
145 .getSimplePropertyBag(PhysicalAddressSchema.CountryOrRegion);
146 }
147
148 /**
149 * Sets the country or region.
150 *
151 * @param value the new country or region
152 */
153 public void setCountryOrRegion(String value) {
154 this.propertyBag.setSimplePropertyBag(
155 PhysicalAddressSchema.CountryOrRegion, value);
156 }
157
158 /**
159 * Gets the postal code.
160 *
161 * @return the postal code
162 */
163 public String getPostalCode() {
164 return (String) this.propertyBag
165 .getSimplePropertyBag(PhysicalAddressSchema.PostalCode);
166 }
167
168 /**
169 * Sets the postal code.
170 *
171 * @param value the new postal code
172 */
173 public void setPostalCode(String value) {
174 this.propertyBag.setSimplePropertyBag(PhysicalAddressSchema.PostalCode,
175 value);
176 }
177
178 /**
179 * Clears the change log.
180 */
181 @Override public void clearChangeLog() {
182 this.propertyBag.clearChangeLog();
183 }
184
185 /**
186 * Writes elements to XML.
187 *
188 * @param reader the reader
189 * @return true, if successful
190 * @throws Exception the exception
191 */
192 @Override
193 public boolean tryReadElementFromXml(EwsServiceXmlReader reader)
194 throws Exception {
195 if (PhysicalAddressSchema.getXmlElementNames().contains(
196 reader.getLocalName())) {
197 this.propertyBag.setSimplePropertyBag(reader.getLocalName(), reader
198 .readElementValue());
199 return true;
200 } else {
201 return false;
202 }
203 }
204
205 /**
206 * Writes elements to XML.
207 *
208 * @param writer the writer
209 * @throws XMLStreamException the XML stream exception
210 * @throws ServiceXmlSerializationException the service xml serialization exception
211 */
212 @Override
213 public void writeElementsToXml(EwsServiceXmlWriter writer)
214 throws XMLStreamException, ServiceXmlSerializationException {
215 for (String xmlElementName : PhysicalAddressSchema.getXmlElementNames()) {
216 writer.writeElementValue(XmlNamespace.Types, xmlElementName,
217 this.propertyBag.getSimplePropertyBag(xmlElementName));
218
219 }
220 }
221
222 /**
223 * Writes the update to XML.
224 *
225 * @param writer the writer
226 * @param ewsObject the ews object
227 * @param ownerDictionaryXmlElementName the owner dictionary xml element name
228 * @return true if update XML was written
229 * @throws XMLStreamException the XML stream exception
230 * @throws ServiceXmlSerializationException the service xml serialization exception
231 */
232 @Override
233 protected boolean writeSetUpdateToXml(EwsServiceXmlWriter writer,
234 ServiceObject ewsObject, String ownerDictionaryXmlElementName)
235 throws XMLStreamException, ServiceXmlSerializationException {
236 List<String> fieldsToSet = new ArrayList<String>();
237
238 for (String xmlElementName : this.propertyBag.getAddedItems()) {
239 fieldsToSet.add(xmlElementName);
240 }
241
242 for (String xmlElementName : this.propertyBag.getModifiedItems()) {
243 fieldsToSet.add(xmlElementName);
244 }
245
246 for (String xmlElementName : fieldsToSet) {
247 writer.writeStartElement(XmlNamespace.Types, ewsObject
248 .getSetFieldXmlElementName());
249
250 writer.writeStartElement(XmlNamespace.Types,
251 XmlElementNames.IndexedFieldURI);
252 writer.writeAttributeValue(XmlAttributeNames.FieldURI,
253 getFieldUri(xmlElementName));
254 writer.writeAttributeValue(XmlAttributeNames.FieldIndex, this
255 .getKey().toString());
256 writer.writeEndElement(); // IndexedFieldURI
257
258 writer.writeStartElement(XmlNamespace.Types, ewsObject
259 .getXmlElementName());
260 writer.writeStartElement(XmlNamespace.Types,
261 ownerDictionaryXmlElementName);
262 writer.writeStartElement(XmlNamespace.Types, XmlElementNames.Entry);
263 this.writeAttributesToXml(writer);
264 writer.writeElementValue(XmlNamespace.Types, xmlElementName,
265 this.propertyBag.getSimplePropertyBag(xmlElementName));
266 writer.writeEndElement(); // Entry
267 writer.writeEndElement(); // ownerDictionaryXmlElementName
268 writer.writeEndElement(); // ewsObject.GetXmlElementName()
269
270 writer.writeEndElement(); // ewsObject.GetSetFieldXmlElementName()
271 }
272
273 for (String xmlElementName : this.propertyBag.getRemovedItems()) {
274 this.internalWriteDeleteFieldToXml(writer, ewsObject,
275 xmlElementName);
276 }
277
278 return true;
279 }
280
281 /**
282 * Writes the delete update to XML.
283 *
284 * @param writer the writer
285 * @param ewsObject the ews object
286 * @return true if update XML was written
287 * @throws XMLStreamException the XML stream exception
288 * @throws ServiceXmlSerializationException the service xml serialization exception
289 */
290 @Override
291 protected boolean writeDeleteUpdateToXml(EwsServiceXmlWriter writer,
292 ServiceObject ewsObject) throws XMLStreamException,
293 ServiceXmlSerializationException {
294 for (String xmlElementName : PhysicalAddressSchema.getXmlElementNames()) {
295 this.internalWriteDeleteFieldToXml(writer, ewsObject,
296 xmlElementName);
297 }
298 return true;
299 }
300
301 /**
302 * Gets the field URI.
303 *
304 * @param xmlElementName the xml element name
305 * @return Field URI.
306 */
307 private static String getFieldUri(String xmlElementName) {
308 return "contacts:PhysicalAddress:" + xmlElementName;
309 }
310
311 /**
312 * Write field deletion to XML.
313 *
314 * @param writer the writer
315 * @param ewsObject the ews object
316 * @param fieldXmlElementName the field xml element name
317 * @throws XMLStreamException the XML stream exception
318 * @throws ServiceXmlSerializationException the service xml serialization exception
319 */
320 private void internalWriteDeleteFieldToXml(EwsServiceXmlWriter writer,
321 ServiceObject ewsObject, String fieldXmlElementName)
322 throws XMLStreamException, ServiceXmlSerializationException {
323 writer.writeStartElement(XmlNamespace.Types, ewsObject
324 .getDeleteFieldXmlElementName());
325 writer.writeStartElement(XmlNamespace.Types,
326 XmlElementNames.IndexedFieldURI);
327 writer.writeAttributeValue(XmlAttributeNames.FieldURI,
328 getFieldUri(fieldXmlElementName));
329 writer.writeAttributeValue(XmlAttributeNames.FieldIndex, this.getKey()
330 .toString());
331 writer.writeEndElement(); // IndexedFieldURI
332 writer.writeEndElement(); // ewsObject.GetDeleteFieldXmlElementName()
333 }
334
335 /**
336 * Schema definition for PhysicalAddress.
337 */
338 private static class PhysicalAddressSchema {
339
340 /**
341 * The Constant Street.
342 */
343 public static final String Street = "Street";
344
345 /**
346 * The Constant City.
347 */
348 public static final String City = "City";
349
350 /**
351 * The Constant State.
352 */
353 public static final String State = "State";
354
355 /**
356 * The Constant CountryOrRegion.
357 */
358 public static final String CountryOrRegion = "CountryOrRegion";
359
360 /**
361 * The Constant PostalCode.
362 */
363 public static final String PostalCode = "PostalCode";
364
365 /**
366 * List of XML element names.
367 */
368 private static LazyMember<List<String>> xmlElementNames =
369 new LazyMember<List<String>>(
370
371 new ILazyMember<List<String>>() {
372 @Override
373 public List<String> createInstance() {
374 List<String> result = new ArrayList<String>();
375 result.add(Street);
376 result.add(City);
377 result.add(State);
378 result.add(CountryOrRegion);
379 result.add(PostalCode);
380 return result;
381 }
382 });
383
384 /**
385 * Gets the XML element names.
386 *
387 * @return The XML element names.
388 */
389 public static List<String> getXmlElementNames() {
390 return xmlElementNames.getMember();
391 }
392 }
393
394 }