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.request;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027import microsoft.exchange.webservices.data.core.EwsUtilities;
028import microsoft.exchange.webservices.data.core.ExchangeService;
029import microsoft.exchange.webservices.data.core.XmlAttributeNames;
030import microsoft.exchange.webservices.data.core.XmlElementNames;
031import microsoft.exchange.webservices.data.core.enumeration.service.error.ServiceErrorHandling;
032import microsoft.exchange.webservices.data.core.response.ServiceResponse;
033import microsoft.exchange.webservices.data.core.service.item.Item;
034import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
035import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
036import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
037import microsoft.exchange.webservices.data.property.complex.FolderId;
038
039/**
040 * Represents a SendItem request.
041 */
042public final class SendItemRequest extends
043    MultiResponseServiceRequest<ServiceResponse> {
044
045  /**
046   * The item.
047   */
048  private Iterable<Item> items;
049
050  /**
051   * The saved copy destination folder id.
052   */
053  private FolderId savedCopyDestinationFolderId;
054
055  /**
056   * Asserts the valid.
057   *
058   * @throws Exception the exception
059   */
060  @Override
061  protected void validate() throws Exception {
062    super.validate();
063    EwsUtilities.validateParam(this.items, "Items");
064
065    if (this.savedCopyDestinationFolderId != null) {
066      this.savedCopyDestinationFolderId.validate(this.getService()
067          .getRequestedServerVersion());
068    }
069  }
070
071  /**
072   * Creates the service response.
073   *
074   * @param service       the service
075   * @param responseIndex the response index
076   * @return Service response.
077   */
078  @Override
079  protected ServiceResponse createServiceResponse(ExchangeService service,
080      int responseIndex) {
081    return new ServiceResponse();
082  }
083
084  /**
085   * Gets the expected response message count.
086   *
087   * @return Number of expected response messages.
088   */
089  @Override
090  protected int getExpectedResponseMessageCount() {
091    return EwsUtilities.getEnumeratedObjectCount(this.items.iterator());
092  }
093
094  /**
095   * Gets the name of the XML element.
096   *
097   * @return XML element name
098   */
099  @Override public String getXmlElementName() {
100    return XmlElementNames.SendItem;
101  }
102
103  /**
104   * Gets the name of the response XML element.
105   *
106   * @return XML element name
107   */
108  @Override
109  protected String getResponseXmlElementName() {
110    return XmlElementNames.SendItemResponse;
111  }
112
113  /**
114   * Gets the name of the response message XML element.
115   *
116   * @return XML element name
117   */
118  @Override
119  protected String getResponseMessageXmlElementName() {
120    return XmlElementNames.SendItemResponseMessage;
121  }
122
123  /**
124   * Writes the attribute to XML.
125   *
126   * @param writer the writer
127   * @throws ServiceXmlSerializationException the service xml serialization exception
128   */
129  @Override
130  protected void writeAttributesToXml(EwsServiceXmlWriter writer)
131      throws ServiceXmlSerializationException {
132    super.writeAttributesToXml(writer);
133
134    writer.writeAttributeValue(XmlAttributeNames.SaveItemToFolder,
135        this.savedCopyDestinationFolderId != null);
136  }
137
138  /**
139   * Writes the elements to XML.
140   *
141   * @param writer the writer
142   * @throws Exception the exception
143   */
144  @Override
145  protected void writeElementsToXml(EwsServiceXmlWriter writer) throws Exception {
146    writer
147        .writeStartElement(XmlNamespace.Messages,
148            XmlElementNames.ItemIds);
149
150    for (Item item : this.getItems()) {
151      item.getId().writeToXml(writer, XmlElementNames.ItemId);
152    }
153
154    writer.writeEndElement(); // ItemIds
155
156    if (this.savedCopyDestinationFolderId != null) {
157      writer.writeStartElement(XmlNamespace.Messages,
158          XmlElementNames.SavedItemFolderId);
159      this.savedCopyDestinationFolderId.writeToXml(writer);
160      writer.writeEndElement();
161    }
162  }
163
164  /**
165   * Gets the request version.
166   *
167   * @return Earliest Exchange version in which this request is supported.
168   */
169  @Override
170  protected ExchangeVersion getMinimumRequiredServerVersion() {
171    return ExchangeVersion.Exchange2007_SP1;
172  }
173
174  /**
175   * Initializes a new instance of the class.
176   *
177   * @param service           the service
178   * @param errorHandlingMode the error handling mode
179   * @throws Exception
180   */
181  public SendItemRequest(ExchangeService service, ServiceErrorHandling errorHandlingMode)
182      throws Exception {
183    super(service, errorHandlingMode);
184  }
185
186  /**
187   * Gets the item. 
188   *
189   * @return the item
190   */
191  public Iterable<Item> getItems() {
192    return this.items;
193  }
194
195  /**
196   * Sets the item.
197   *
198   * @param items the new item
199   */
200  public void setItems(Iterable<Item> items) {
201    this.items = items;
202  }
203
204  /**
205   * Gets the saved copy destination folder id.
206   *
207   * @return the saved copy destination folder id
208   */
209  public FolderId getSavedCopyDestinationFolderId() {
210    return this.savedCopyDestinationFolderId;
211  }
212
213  /**
214   * Sets the saved copy destination folder id.
215   *
216   * @param savedCopyDestinationFolderId the new saved copy destination folder id
217   */
218  public void setSavedCopyDestinationFolderId(
219      FolderId savedCopyDestinationFolderId) {
220    this.savedCopyDestinationFolderId = savedCopyDestinationFolderId;
221  }
222
223}