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
024package microsoft.exchange.webservices.data.core.response;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsUtilities;
028import microsoft.exchange.webservices.data.core.ExchangeService;
029import microsoft.exchange.webservices.data.core.XmlElementNames;
030import microsoft.exchange.webservices.data.core.service.ServiceObject;
031import microsoft.exchange.webservices.data.core.service.item.Item;
032import microsoft.exchange.webservices.data.core.enumeration.service.ServiceResult;
033import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
034
035/**
036 * The Class UpdateItemResponse.
037 */
038public final class UpdateItemResponse extends ServiceResponse implements
039                                                              IGetObjectInstanceDelegate<ServiceObject> {
040
041  /**
042   * Represents the response to an individual item update operation.
043   */
044  private Item item;
045
046  /**
047   * The returned item.
048   */
049  private Item returnedItem;
050
051  /**
052   * The conflict count.
053   */
054  private int conflictCount;
055
056  /**
057   * Initializes a new instance of the class.
058   *
059   * @param item the item
060   */
061  public UpdateItemResponse(Item item) {
062    super();
063    EwsUtilities.ewsAssert(item != null, "UpdateItemResponse.ctor", "item is null");
064    this.item = item;
065  }
066
067  /**
068   * Reads response elements from XML.
069   *
070   * @param reader the reader
071   * @throws Exception the exception
072   */
073  @Override
074  protected void readElementsFromXml(EwsServiceXmlReader reader) throws Exception {
075    super.readElementsFromXml(reader);
076
077    reader.readServiceObjectsCollectionFromXml(XmlElementNames.Items, this,
078        false, null, false);
079
080    if (!reader.getService().getExchange2007CompatibilityMode()) {
081      reader.readStartElement(XmlNamespace.Messages,
082          XmlElementNames.ConflictResults);
083      this.conflictCount = reader.readElementValue(Integer.class,
084          XmlNamespace.Types, XmlElementNames.Count);
085      reader.readEndElement(XmlNamespace.Messages,
086          XmlElementNames.ConflictResults);
087    }
088
089    // If UpdateItem returned an item that has the same Id as the item that
090    // is being updated, this is a "normal" UpdateItem operation, and we
091    // need
092    // to update the ChangeKey of the item being updated with the one that
093    // was
094    // returned. Also set returnedItem to indicate that no new item was
095    // returned.
096    //
097    // Otherwise, this in a "special" UpdateItem operation, such as a
098    // recurring
099    // task marked as complete (the returned item in that case is the
100    // one-off
101    // task that represents the completed instance).
102    //
103    // Note that there can be no returned item at all, as in an UpdateItem
104    // call
105    // with MessageDisposition set to SendOnly or SendAndSaveCopy.
106    if (this.returnedItem != null) {
107      if (this.item.getId().getUniqueId().equals(
108          this.returnedItem.getId().getUniqueId())) {
109        this.item.getId().setChangeKey(
110            this.returnedItem.getId().getChangeKey());
111        this.returnedItem = null;
112      }
113    }
114  }
115
116  /*
117   * (non-Javadoc)
118   *
119   * @seemicrosoft.exchange.webservices.GetObjectInstanceDelegateInterface#
120   * getObjectInstanceDelegate(microsoft.exchange.webservices.ExchangeService,
121   * java.lang.String)
122   */
123  public ServiceObject getObjectInstanceDelegate(ExchangeService service,
124      String xmlElementName) throws Exception {
125    return this.getObjectInstance(service, xmlElementName);
126  }
127
128  /**
129   * Clears the change log of the created folder if the creation succeeded.
130   */
131  @Override
132  protected void loaded() {
133    if (this.getResult() == ServiceResult.Success) {
134      this.item.clearChangeLog();
135    }
136  }
137
138  /**
139   * Gets Item instance.
140   *
141   * @param service        the service
142   * @param xmlElementName the xml element name
143   * @return Item
144   * @throws Exception the exception
145   */
146  private Item getObjectInstance(ExchangeService service,
147      String xmlElementName) throws Exception {
148    this.returnedItem = EwsUtilities.createEwsObjectFromXmlElementName(
149        Item.class, service, xmlElementName);
150    return this.returnedItem;
151  }
152
153  /**
154   * Gets the item that was returned by the update operation. ReturnedItem
155   * is set only when a recurring Task is marked as complete or when its
156   * recurrence pattern changes.
157   *
158   * @return the returned item
159   */
160  public Item getReturnedItem() {
161    return this.returnedItem;
162  }
163
164  /**
165   * Gets the number of property conflicts that were resolved during the
166   * update operation.
167   *
168   * @return the conflict count
169   */
170  public int getConflictCount() {
171    return this.conflictCount;
172  }
173
174}