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.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028import microsoft.exchange.webservices.data.core.EwsUtilities;
029import microsoft.exchange.webservices.data.core.ExchangeService;
030import microsoft.exchange.webservices.data.core.XmlElementNames;
031import microsoft.exchange.webservices.data.core.response.UpdateInboxRulesResponse;
032import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
033import microsoft.exchange.webservices.data.core.enumeration.service.ServiceResult;
034import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
035import microsoft.exchange.webservices.data.core.exception.service.remote.UpdateInboxRulesException;
036import microsoft.exchange.webservices.data.property.complex.RuleOperation;
037
038/**
039 * Represents a UpdateInboxRulesRequest request.
040 */
041public final class UpdateInboxRulesRequest extends SimpleServiceRequestBase<UpdateInboxRulesResponse> {
042  /**
043   * The smtp address of the mailbox from which to get the inbox rules.
044   */
045  private String mailboxSmtpAddress;
046
047  /**
048   * Remove OutlookRuleBlob or not.
049   */
050  private boolean removeOutlookRuleBlob;
051
052  /**
053   * InboxRule operation collection.
054   */
055  private Iterable<RuleOperation> inboxRuleOperations;
056
057  /**
058   * Initializes a new instance of the
059   * <see cref="UpdateInboxRulesRequest"/> class.
060   *
061   * @param service The service.
062   */
063  public UpdateInboxRulesRequest(ExchangeService service)
064      throws Exception {
065    super(service);
066  }
067
068  /**
069   * Gets the name of the XML element.
070   *
071   * @return XML element name.
072   */
073  @Override public String getXmlElementName() {
074    return XmlElementNames.UpdateInboxRules;
075  }
076
077  /**
078   * Writes XML elements.
079   *
080   * @param writer The writer.
081   */
082  @Override
083  protected void writeElementsToXml(EwsServiceXmlWriter writer)
084      throws Exception {
085    if (!(mailboxSmtpAddress == null || mailboxSmtpAddress.isEmpty())) {
086      writer.writeElementValue(
087          XmlNamespace.Messages,
088          XmlElementNames.MailboxSmtpAddress,
089          this.mailboxSmtpAddress);
090    }
091
092    writer.writeElementValue(
093        XmlNamespace.Messages,
094        XmlElementNames.RemoveOutlookRuleBlob,
095        this.removeOutlookRuleBlob);
096    writer.writeStartElement(XmlNamespace.Messages,
097        XmlElementNames.Operations);
098    for (RuleOperation operation : this.inboxRuleOperations) {
099      operation.writeToXml(writer, operation.getXmlElementName());
100    }
101    writer.writeEndElement();
102  }
103
104  /**
105   * Gets the name of the response XML element.
106   *
107   * @return XML element name.
108   */
109  @Override
110  protected String getResponseXmlElementName() {
111    return XmlElementNames.UpdateInboxRulesResponse;
112  }
113
114  /**
115   * {@inheritDoc}
116   */
117  @Override
118  protected UpdateInboxRulesResponse parseResponse(EwsServiceXmlReader reader)
119      throws Exception {
120    UpdateInboxRulesResponse response = new UpdateInboxRulesResponse();
121    response.loadFromXml(reader, XmlElementNames.UpdateInboxRulesResponse);
122    return response;
123  }
124
125  /**
126   * Gets the request version.
127   *
128   * @return Earliest Exchange version in which this request is supported.
129   */
130  @Override
131  protected ExchangeVersion getMinimumRequiredServerVersion() {
132    return ExchangeVersion.Exchange2010_SP1;
133  }
134
135  /**
136   * Validate request.
137   */
138  @Override
139  protected void validate() throws Exception {
140    if (this.inboxRuleOperations == null) {
141      throw new IllegalArgumentException(
142          "RuleOperations cannot be null." + "Operations");
143    }
144
145    int operationCount = 0;
146    for (RuleOperation operation : this.inboxRuleOperations) {
147      EwsUtilities.validateParam(operation, "RuleOperation");
148      operationCount++;
149    }
150
151    if (operationCount == 0) {
152      throw new IllegalArgumentException(
153          "RuleOperations cannot be empty." + "Operations");
154    }
155
156    this.getService().validate();
157  }
158
159  /**
160   * Executes this request.
161   *
162   * @return Service response.
163   * @throws Exception on error
164   */
165  public UpdateInboxRulesResponse execute() throws Exception {
166    UpdateInboxRulesResponse serviceResponse = internalExecute();
167    if (serviceResponse.getResult() == ServiceResult.Error) {
168      throw new UpdateInboxRulesException(serviceResponse,
169          this.inboxRuleOperations);
170    }
171    return serviceResponse;
172  }
173
174  /**
175   * Gets the address of the mailbox in which to update the inbox rules.
176   */
177  protected String getMailboxSmtpAddress() {
178    return this.mailboxSmtpAddress;
179  }
180
181  /**
182   * Sets the address of the mailbox in which to update the inbox rules.
183   */
184  public void setMailboxSmtpAddress(String value) {
185    this.mailboxSmtpAddress = value;
186  }
187
188  /**
189   * Gets a value indicating whether or not to
190   * remove OutlookRuleBlob from the rule collection.
191   */
192  protected boolean getRemoveOutlookRuleBlob() {
193    return this.removeOutlookRuleBlob;
194  }
195
196  /**
197   * Sets a value indicating whether or not to
198   * remove OutlookRuleBlob from the rule collection.
199   */
200  public void setRemoveOutlookRuleBlob(boolean value) {
201    this.removeOutlookRuleBlob = value;
202  }
203
204
205  /**
206   * Gets the RuleOperation collection.
207   */
208  protected Iterable<RuleOperation> getInboxRuleOperations() {
209    return this.inboxRuleOperations;
210  }
211
212  /**
213   * Sets the RuleOperation collection.
214   */
215  public void setInboxRuleOperations(Iterable<RuleOperation> value) {
216    this.inboxRuleOperations = value;
217  }
218
219}