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
026/**
027 * Represents a DeleteAttachment request.
028 */
029import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
030import microsoft.exchange.webservices.data.core.EwsUtilities;
031import microsoft.exchange.webservices.data.core.ExchangeService;
032import microsoft.exchange.webservices.data.core.XmlAttributeNames;
033import microsoft.exchange.webservices.data.core.XmlElementNames;
034import microsoft.exchange.webservices.data.core.enumeration.service.error.ServiceErrorHandling;
035import microsoft.exchange.webservices.data.core.response.DeleteAttachmentResponse;
036import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
037import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
038import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
039import microsoft.exchange.webservices.data.property.complex.Attachment;
040import org.apache.commons.logging.Log;
041import org.apache.commons.logging.LogFactory;
042
043import java.util.ArrayList;
044import java.util.List;
045
046/**
047 * The Class DeleteAttachmentRequest.
048 */
049public final class DeleteAttachmentRequest extends
050    MultiResponseServiceRequest<DeleteAttachmentResponse> {
051
052  private static final Log LOG = LogFactory.getLog(DeleteAttachmentRequest.class);
053
054  /**
055   * The attachments.
056   */
057  private List<Attachment> attachments = new ArrayList<Attachment>();
058
059  /**
060   * Initializes a new instance of the DeleteAttachmentRequest class.
061   *
062   * @param service           the service
063   * @param errorHandlingMode the error handling mode
064   * @throws Exception
065   */
066  public DeleteAttachmentRequest(ExchangeService service, ServiceErrorHandling errorHandlingMode)
067      throws Exception {
068    super(service, errorHandlingMode);
069  }
070
071  /**
072   * Validate request.
073   */
074  @Override
075  protected void validate() {
076    try {
077      super.validate();
078      EwsUtilities.validateParamCollection(this.getAttachments().iterator(), "Attachments");
079      for (int i = 0; i < this.attachments.size(); i++) {
080        EwsUtilities.validateParam(this.attachments.get(i).getId(),
081            String.format("Attachment[%d].Id ", i));
082      }
083    } catch (ServiceLocalException e) {
084      LOG.error(e);
085    } catch (Exception e) {
086      LOG.error(e);
087    }
088  }
089
090  /**
091   * Creates the service response.
092   *
093   * @param service       the service
094   * @param responseIndex the response index
095   * @return Service object.
096   */
097  @Override
098  protected DeleteAttachmentResponse createServiceResponse(
099      ExchangeService service, int responseIndex) {
100    return new DeleteAttachmentResponse(
101        this.attachments.get(responseIndex));
102  }
103
104  /**
105   * Gets the expected response message count.
106   *
107   * @return Number of expected response messages.
108   */
109  @Override
110  protected int getExpectedResponseMessageCount() {
111    return this.attachments.size();
112  }
113
114  /**
115   * Gets the name of the XML element.
116   *
117   * @return XML element name.
118   */
119  @Override public String getXmlElementName() {
120    return XmlElementNames.DeleteAttachment;
121  }
122
123  /**
124   * Gets the name of the response XML element.
125   *
126   * @return XML element name.
127   */
128  @Override
129  protected String getResponseXmlElementName() {
130    return XmlElementNames.DeleteAttachmentResponse;
131  }
132
133  /**
134   * Gets the name of the response message XML element.
135   *
136   * @return XML element name.
137   */
138  @Override
139  protected String getResponseMessageXmlElementName() {
140    return XmlElementNames.DeleteAttachmentResponseMessage;
141  }
142
143  /**
144   * Writes XML elements.
145   *
146   * @param writer the writer
147   * @throws Exception the exception
148   */
149  @Override
150  protected void writeElementsToXml(EwsServiceXmlWriter writer)
151      throws Exception {
152    writer.writeStartElement(XmlNamespace.Messages,
153        XmlElementNames.AttachmentIds);
154
155    for (Attachment attachment : this.attachments) {
156      writer.writeStartElement(XmlNamespace.Types,
157          XmlElementNames.AttachmentId);
158      writer
159          .writeAttributeValue(XmlAttributeNames.Id, attachment
160              .getId());
161      writer.writeEndElement();
162    }
163
164    writer.writeEndElement();
165  }
166
167  /**
168   * Gets the request version.
169   *
170   * @return Earliest Exchange version in which this request is supported.
171   */
172  @Override
173  protected ExchangeVersion getMinimumRequiredServerVersion() {
174    return ExchangeVersion.Exchange2007_SP1;
175  }
176
177  /**
178   * Gets the attachments.
179   *
180   * @return the attachments
181   */
182  public List<Attachment> getAttachments() {
183    return this.attachments;
184  }
185}