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.core.request;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027    import microsoft.exchange.webservices.data.core.EwsUtilities;
028    import microsoft.exchange.webservices.data.core.ExchangeService;
029    import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030    import microsoft.exchange.webservices.data.core.XmlElementNames;
031    import microsoft.exchange.webservices.data.core.enumeration.service.error.ServiceErrorHandling;
032    import microsoft.exchange.webservices.data.core.response.UpdateItemResponse;
033    import microsoft.exchange.webservices.data.core.service.item.Item;
034    import microsoft.exchange.webservices.data.core.enumeration.service.ConflictResolutionMode;
035    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
036    import microsoft.exchange.webservices.data.core.enumeration.service.MessageDisposition;
037    import microsoft.exchange.webservices.data.core.enumeration.service.SendInvitationsOrCancellationsMode;
038    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
039    import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
040    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
041    import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
042    import microsoft.exchange.webservices.data.property.complex.FolderId;
043    
044    import java.util.ArrayList;
045    import java.util.List;
046    
047    /**
048     * The Class UpdateItemRequest.
049     */
050    public final class UpdateItemRequest extends
051        MultiResponseServiceRequest<UpdateItemResponse> {
052    
053      /**
054       * The item.
055       */
056      private List<Item> items = new ArrayList<Item>();
057    
058      /**
059       * The saved item destination folder.
060       */
061      private FolderId savedItemsDestinationFolder;
062    
063      /**
064       * The conflict resolution mode.
065       */
066      private ConflictResolutionMode conflictResolutionMode;
067    
068      /**
069       * The message disposition.
070       */
071      private MessageDisposition messageDisposition;
072    
073      /**
074       * The send invitations or cancellations mode.
075       */
076      private SendInvitationsOrCancellationsMode
077          sendInvitationsOrCancellationsMode;
078    
079      /**
080       * Instantiates a new update item request.
081       *
082       * @param service           the service
083       * @param errorHandlingMode the error handling mode
084       * @throws Exception
085       */
086      public UpdateItemRequest(ExchangeService service, ServiceErrorHandling errorHandlingMode)
087          throws Exception {
088        super(service, errorHandlingMode);
089      }
090    
091      /*
092       * (non-Javadoc)
093       *
094       * @see microsoft.exchange.webservices.ServiceRequestBase#validate()
095       */
096      @Override
097      protected void validate() throws ServiceLocalException, Exception {
098        super.validate();
099        EwsUtilities.validateParamCollection(this.getItems().iterator(), "Items");
100        for (int i = 0; i < this.getItems().size(); i++) {
101          if ((this.getItems().get(i) == null) ||
102              this.getItems().get(i).isNew()) {
103            throw new ArgumentException(String.format("Items[%d] is either null or does not have an Id.", i));
104          }
105        }
106    
107        if (this.savedItemsDestinationFolder != null) {
108          this.savedItemsDestinationFolder.validate(this.getService()
109              .getRequestedServerVersion());
110        }
111    
112        // Validate each item.
113        for (Item item : this.getItems()) {
114          item.validate();
115        }
116      }
117    
118      /*
119       * (non-Javadoc)
120       *
121       * @seemicrosoft.exchange.webservices.MultiResponseServiceRequest#
122       * createServiceResponse(microsoft.exchange.webservices.ExchangeService,
123       * int)
124       */
125      @Override
126      protected UpdateItemResponse createServiceResponse(ExchangeService service,
127          int responseIndex) {
128        return new UpdateItemResponse(this.getItems().get(responseIndex));
129      }
130    
131      /*
132       * (non-Javadoc)
133       *
134       * @see
135       * microsoft.exchange.webservices.ServiceRequestBase#getXmlElementName()
136       */
137      @Override public String getXmlElementName() {
138        return XmlElementNames.UpdateItem;
139      }
140    
141      /*
142       * (non-Javadoc)
143       *
144       * @see
145       * microsoft.exchange.webservices.ServiceRequestBase
146       * #getResponseXmlElementName
147       * ()
148       */
149      @Override
150      protected String getResponseXmlElementName() {
151        return XmlElementNames.UpdateItemResponse;
152      }
153    
154      /*
155       * (non-Javadoc)
156       *
157       * @seemicrosoft.exchange.webservices.MultiResponseServiceRequest#
158       * getResponseMessageXmlElementName()
159       */
160      @Override
161      protected String getResponseMessageXmlElementName() {
162        return XmlElementNames.UpdateItemResponseMessage;
163      }
164    
165      /*
166       * (non-Javadoc)
167       *
168       * @seemicrosoft.exchange.webservices.MultiResponseServiceRequest#
169       * getExpectedResponseMessageCount()
170       */
171      @Override
172      protected int getExpectedResponseMessageCount() {
173        return this.items.size();
174      }
175    
176      /*
177       * (non-Javadoc)
178       *
179       * @see
180       * microsoft.exchange.webservices.ServiceRequestBase#writeAttributesToXml
181       * (microsoft.exchange.webservices.EwsServiceXmlWriter)
182       */
183      @Override
184      protected void writeAttributesToXml(EwsServiceXmlWriter writer)
185          throws ServiceXmlSerializationException {
186        super.writeAttributesToXml(writer);
187    
188        if (this.messageDisposition != null) {
189          writer.writeAttributeValue(XmlAttributeNames.MessageDisposition,
190              this.messageDisposition);
191        }
192    
193        writer.writeAttributeValue(XmlAttributeNames.ConflictResolution,
194            this.conflictResolutionMode);
195    
196        if (this.sendInvitationsOrCancellationsMode != null) {
197          writer.writeAttributeValue(
198              XmlAttributeNames.SendMeetingInvitationsOrCancellations,
199              this.sendInvitationsOrCancellationsMode);
200        }
201      }
202    
203      /*
204       * (non-Javadoc)
205       *
206       * @see
207       * microsoft.exchange.webservices.ServiceRequestBase#writeElementsToXml(
208       * microsoft.exchange.webservices.EwsServiceXmlWriter)
209       */
210      @Override
211      protected void writeElementsToXml(EwsServiceXmlWriter writer)
212          throws Exception {
213        if (this.savedItemsDestinationFolder != null) {
214          writer.writeStartElement(XmlNamespace.Messages,
215              XmlElementNames.SavedItemFolderId);
216          this.savedItemsDestinationFolder.writeToXml(writer);
217          writer.writeEndElement();
218        }
219    
220        writer.writeStartElement(XmlNamespace.Messages,
221            XmlElementNames.ItemChanges);
222    
223        for (Item item : this.items) {
224          item.writeToXmlForUpdate(writer);
225        }
226    
227        writer.writeEndElement();
228      }
229    
230      /*
231       * (non-Javadoc)
232       *
233       * @seemicrosoft.exchange.webservices.ServiceRequestBase#
234       * getMinimumRequiredServerVersion()
235       */
236      @Override
237      protected ExchangeVersion getMinimumRequiredServerVersion() {
238        return ExchangeVersion.Exchange2007_SP1;
239      }
240    
241      /**
242       * Gets the message disposition.
243       *
244       * @return the message disposition
245       */
246      public MessageDisposition getMessageDisposition() {
247        return this.messageDisposition;
248      }
249    
250      /**
251       * Sets the message disposition.
252       *
253       * @param value the new message disposition
254       */
255      public void setMessageDisposition(MessageDisposition value) {
256        this.messageDisposition = value;
257      }
258    
259      /**
260       * Gets the conflict resolution mode.
261       *
262       * @return the conflict resolution mode
263       */
264      public ConflictResolutionMode getConflictResolutionMode() {
265        return this.conflictResolutionMode;
266      }
267    
268      /**
269       * Sets the conflict resolution mode.
270       *
271       * @param value the new conflict resolution mode
272       */
273      public void setConflictResolutionMode(ConflictResolutionMode value) {
274        this.conflictResolutionMode = value;
275      }
276    
277      /**
278       * Gets the send invitations or cancellations mode.
279       *
280       * @return the send invitations or cancellations mode
281       */
282      public SendInvitationsOrCancellationsMode
283      getSendInvitationsOrCancellationsMode() {
284        return this.sendInvitationsOrCancellationsMode;
285      }
286    
287      /**
288       * Sets the send invitations or cancellations mode.
289       *
290       * @param value the new send invitations or cancellations mode
291       */
292      public void setSendInvitationsOrCancellationsMode(
293          SendInvitationsOrCancellationsMode value) {
294        this.sendInvitationsOrCancellationsMode = value;
295      }
296    
297      /**
298       * Gets the item.
299       *
300       * @return the item
301       */
302      public List<Item> getItems() {
303        return this.items;
304      }
305    
306      /**
307       * Gets the saved item destination folder.
308       *
309       * @return the saved item destination folder
310       */
311      public FolderId getSavedItemsDestinationFolder() {
312        return this.savedItemsDestinationFolder;
313      }
314    
315      /**
316       * Sets the saved item destination folder.
317       *
318       * @param value the new saved item destination folder
319       */
320      public void setSavedItemsDestinationFolder(FolderId value) {
321        this.savedItemsDestinationFolder = value;
322      }
323    
324    }