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.ConvertIdResponse;
033import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
034import microsoft.exchange.webservices.data.core.enumeration.misc.IdFormat;
035import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
036import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
037import microsoft.exchange.webservices.data.misc.id.AlternateIdBase;
038
039import javax.xml.stream.XMLStreamException;
040
041import java.util.ArrayList;
042import java.util.List;
043
044/**
045 * Represents a ConvertId request.
046 */
047public final class ConvertIdRequest extends
048    MultiResponseServiceRequest<ConvertIdResponse> {
049
050  /**
051   * The destination format.
052   */
053  private IdFormat destinationFormat = IdFormat.EwsId;
054
055  /**
056   * The ids.
057   */
058  private List<AlternateIdBase> ids = new ArrayList<AlternateIdBase>();
059
060  /**
061   * Initializes a new instance of the class.
062   *
063   * @param service           the service
064   * @param errorHandlingMode the error handling mode
065   * @throws Exception
066   */
067  public ConvertIdRequest(ExchangeService service, ServiceErrorHandling errorHandlingMode)
068      throws Exception {
069    super(service, errorHandlingMode);
070  }
071
072  /**
073   * Initializes a new instance of the class.
074   *
075   * @param service       the service
076   * @param responseIndex the response index
077   * @return the convert id response
078   */
079  @Override
080  protected ConvertIdResponse createServiceResponse(ExchangeService service,
081      int responseIndex) {
082    return new ConvertIdResponse();
083  }
084
085  /**
086   * Gets the name of the response XML element.
087   *
088   * @return XML element name.
089   */
090  @Override
091  protected String getResponseXmlElementName() {
092    return XmlElementNames.ConvertIdResponse;
093  }
094
095  /**
096   * Gets the name of the response message XML element.
097   *
098   * @return XML element name.
099   */
100  @Override
101  protected String getResponseMessageXmlElementName() {
102    return XmlElementNames.ConvertIdResponseMessage;
103  }
104
105  /**
106   * Gets the expected response message count.
107   *
108   * @return Number of expected response messages.
109   */
110  @Override
111  protected int getExpectedResponseMessageCount() {
112    return this.ids.size();
113  }
114
115  /**
116   * Gets the name of the XML element.
117   *
118   * @return XML element name.
119   */
120  @Override public String getXmlElementName() {
121    return XmlElementNames.ConvertId;
122  }
123
124  /**
125   * Validate request.
126   *
127   * @throws Exception the exception
128   */
129  @Override
130  protected void validate() throws Exception {
131    super.validate();
132    EwsUtilities.validateParamCollection(this.ids.iterator(), "Ids");
133  }
134
135  /**
136   * Writes XML elements.
137   *
138   * @param writer the writer
139   * @throws XMLStreamException the XML stream exception
140   * @throws ServiceXmlSerializationException the service xml serialization exception
141   */
142  @Override
143  protected void writeElementsToXml(EwsServiceXmlWriter writer)
144      throws XMLStreamException, ServiceXmlSerializationException {
145    writer.writeAttributeValue(XmlAttributeNames.DestinationFormat,
146        this.destinationFormat);
147    writer.writeStartElement(XmlNamespace.Messages,
148        XmlElementNames.SourceIds);
149    for (AlternateIdBase alternateId : this.ids) {
150      alternateId.writeToXml(writer);
151    }
152
153    writer.writeEndElement(); // SourceIds
154  }
155
156  /**
157   * Gets the request version.
158   *
159   * @return Earliest Exchange version in which this request is supported.
160   */
161  @Override
162  protected ExchangeVersion getMinimumRequiredServerVersion() {
163    return ExchangeVersion.Exchange2007_SP1;
164  }
165
166  /**
167   * Gets the destination format.
168   *
169   * @return the destination format
170   */
171  public IdFormat getDestinationFormat() {
172    return this.destinationFormat;
173  }
174
175  /**
176   * Sets the destination format.
177   *
178   * @param destinationFormat the new destination format
179   */
180  public void setDestinationFormat(IdFormat destinationFormat) {
181    this.destinationFormat = destinationFormat;
182  }
183
184  /**
185   * Gets the ids.
186   *
187   * @return the ids
188   */
189  public List<AlternateIdBase> getIds() {
190    return this.ids;
191  }
192}