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.CreateAttachmentResponse;
033import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
034import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
035import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
036import microsoft.exchange.webservices.data.property.complex.Attachment;
037import microsoft.exchange.webservices.data.property.complex.ItemAttachment;
038
039import java.util.ArrayList;
040import java.util.ListIterator;
041
042/**
043 * Represents a CreateAttachment request.
044 */
045
046public final class CreateAttachmentRequest extends
047    MultiResponseServiceRequest<CreateAttachmentResponse> {
048
049  /**
050   * The parent item id.
051   */
052  private String parentItemId;
053
054  /**
055   * The attachments.
056   */
057  private ArrayList<Attachment> attachments = new ArrayList<Attachment>();
058
059  /**
060   * Gets the attachments.
061   *
062   * @return attachments
063   */
064  public ArrayList<Attachment> getAttachments() {
065    return attachments;
066  }
067
068  /**
069   * Initializes a new instance of the class.
070   *
071   * @param service           the service
072   * @param errorHandlingMode the error handling mode
073   * @throws Exception
074   */
075  public CreateAttachmentRequest(ExchangeService service, ServiceErrorHandling errorHandlingMode)
076      throws Exception {
077    super(service, errorHandlingMode);
078  }
079
080  /**
081   * Validate request..
082   *
083   * @throws Exception the exception
084   */
085  @Override
086  protected void validate() throws Exception {
087    super.validate();
088    EwsUtilities.validateParam(this.parentItemId, "ParentItemId");
089  }
090
091  /**
092   * Gets the expected response message count.
093   *
094   * @return Number of expected response messages.
095   */
096  @Override
097  protected int getExpectedResponseMessageCount() {
098    return this.attachments.size();
099  }
100
101  /**
102   * Gets the name of the XML element.
103   *
104   * @return XML element name.
105   */
106  @Override public String getXmlElementName() {
107    return XmlElementNames.CreateAttachment;
108  }
109
110  /**
111   * Gets the name of the response XML element.
112   *
113   * @return XML element name.
114   */
115  @Override
116  protected String getResponseXmlElementName() {
117    return XmlElementNames.CreateAttachmentResponse;
118  }
119
120  /**
121   * Gets the name of the response message XML element.
122   *
123   * @return XML element name.
124   */
125  @Override
126  protected String getResponseMessageXmlElementName() {
127    return XmlElementNames.CreateAttachmentResponseMessage;
128  }
129
130  /**
131   * Gets the request version.
132   *
133   * @return Earliest Exchange version in which this request is supported.
134   */
135  @Override
136  protected ExchangeVersion getMinimumRequiredServerVersion() {
137    return ExchangeVersion.Exchange2007_SP1;
138  }
139
140  /**
141   * Gets a value indicating whether the TimeZoneContext SOAP header should be
142   * emitted.
143   */
144  protected boolean emitTimeZoneHeader() throws ServiceLocalException, Exception {
145    {
146
147      ListIterator<Attachment> items = this.getAttachments()
148          .listIterator();
149
150      while (items.hasNext())
151
152      {
153
154        ItemAttachment itemAttachment = (ItemAttachment) items.next();
155
156        if ((itemAttachment.getItem() != null)
157            && itemAttachment
158            .getItem()
159            .getIsTimeZoneHeaderRequired(false /* isUpdateOperation */)) {
160          return true;
161        }
162      }
163
164      return false;
165    }
166  }
167
168  /**
169   * Gets the parent item id.
170   *
171   * @return parentItemId
172   */
173  public String getParentItemId() {
174    return parentItemId;
175  }
176
177  /**
178   * Sets the parent item id.
179   *
180   * @param parentItemId the new parent item id
181   */
182  public void setParentItemId(String parentItemId) {
183    this.parentItemId = parentItemId;
184  }
185
186  /**
187   * Writes the elements to XML.
188   *
189   * @param writer the writer
190   * @throws Exception the exception
191   */
192  @Override
193  protected void writeElementsToXml(EwsServiceXmlWriter writer)
194      throws Exception {
195
196    writer.writeStartElement(XmlNamespace.Messages,
197        XmlElementNames.ParentItemId);
198    writer.writeAttributeValue(XmlAttributeNames.Id, this.parentItemId);
199    writer.writeEndElement();
200
201    writer.writeStartElement(XmlNamespace.Messages,
202        XmlElementNames.Attachments);
203    for (Attachment attachment : this.attachments) {
204      attachment.writeToXml(writer, attachment.getXmlElementName());
205    }
206    writer.writeEndElement();
207
208  }
209
210  /**
211   * Creates the service response.
212   *
213   * @param service       the service
214   * @param responseIndex the response index
215   * @return the creates the attachment response
216   */
217  @Override
218  protected CreateAttachmentResponse createServiceResponse(
219      ExchangeService service, int responseIndex) {
220    return new CreateAttachmentResponse(
221        this.attachments.get(responseIndex));
222  }
223
224}