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.XmlElementNames;
030import microsoft.exchange.webservices.data.core.enumeration.service.error.ServiceErrorHandling;
031import microsoft.exchange.webservices.data.core.response.ServiceResponse;
032import microsoft.exchange.webservices.data.core.response.UpdateFolderResponse;
033import microsoft.exchange.webservices.data.core.service.folder.Folder;
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.ServiceLocalException;
037
038import java.util.ArrayList;
039
040/**
041 * Represents an UpdateFolder request.
042 */
043public final class UpdateFolderRequest extends
044    MultiResponseServiceRequest<ServiceResponse> {
045
046  /**
047   * The folder.
048   */
049  private ArrayList<Folder> folders = new ArrayList<Folder>();
050
051  /**
052   * Initializes a new instance of the UpdateFolderRequest class.
053   *
054   * @param service           The Servcie
055   * @param errorHandlingMode Indicates how errors should be handled.
056   * @throws Exception
057   */
058  public UpdateFolderRequest(ExchangeService service, ServiceErrorHandling errorHandlingMode)
059      throws Exception {
060    super(service, errorHandlingMode);
061  }
062
063  /**
064   * validates request.
065   *
066   * @throws ServiceLocalException the service local exception
067   * @throws Exception             the exception
068   */
069  @Override
070  protected void validate() throws ServiceLocalException, Exception {
071    super.validate();
072    EwsUtilities.validateParamCollection(this.getFolders().iterator(), "Folders");
073    for (int i = 0; i < this.getFolders().size(); i++) {
074      Folder folder = this.getFolders().get(i);
075
076      if ((folder == null) || folder.isNew()) {
077        throw new IllegalArgumentException(String.format("Folders[%d] is either null or does not have an Id.", i));
078      }
079
080      folder.validate();
081    }
082  }
083
084  /**
085   * Creates the service response.
086   *
087   * @param session       The session
088   * @param responseIndex Index of the response.
089   * @return Service response.
090   */
091  @Override
092  protected ServiceResponse createServiceResponse(ExchangeService session,
093      int responseIndex) {
094    return new UpdateFolderResponse(this.getFolders().get(responseIndex));
095  }
096
097  /**
098   * Gets the name of the XML element.
099   *
100   * @return Xml element name.
101   */
102  @Override public String getXmlElementName() {
103    return XmlElementNames.UpdateFolder;
104  }
105
106  /**
107   * Gets the name of the response XML element.
108   *
109   * @return Xml element name.
110   */
111  @Override
112  protected String getResponseXmlElementName() {
113    return XmlElementNames.UpdateFolderResponse;
114  }
115
116  /**
117   * Gets the name of the response message XML element.
118   *
119   * @return Xml element name.
120   */
121  @Override
122  protected String getResponseMessageXmlElementName() {
123    return XmlElementNames.UpdateFolderResponseMessage;
124  }
125
126  /**
127   * Gets the expected response message count.
128   *
129   * @return Number of expected response messages.
130   */
131  @Override
132  protected int getExpectedResponseMessageCount() {
133    return this.getFolders().size();
134  }
135
136  /**
137   * Writes to xml.
138   *
139   * @param writer the writer
140   * @throws Exception the exception
141   */
142  @Override
143  protected void writeElementsToXml(EwsServiceXmlWriter writer)
144      throws Exception {
145    writer.writeStartElement(XmlNamespace.Messages,
146        XmlElementNames.FolderChanges);
147
148    for (Folder folder : this.folders) {
149      folder.writeToXmlForUpdate(writer);
150    }
151
152    writer.writeEndElement();
153  }
154
155  /**
156   * Gets the request version.
157   *
158   * @return Earliest Exchange version in which this request is supported.
159   */
160  @Override
161  protected ExchangeVersion getMinimumRequiredServerVersion() {
162    return ExchangeVersion.Exchange2007_SP1;
163  }
164
165  /**
166   * Gets the folder.
167   *
168   * @return the folder
169   */
170  public ArrayList<Folder> getFolders() {
171    return this.folders;
172  }
173}