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.service.response;
025
026/**
027 * Represents the base class for all response that can be sent.
028 *
029 */
030import microsoft.exchange.webservices.data.attribute.EditorBrowsable;
031import microsoft.exchange.webservices.data.core.EwsUtilities;
032import microsoft.exchange.webservices.data.core.PropertySet;
033import microsoft.exchange.webservices.data.core.service.ServiceObject;
034import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
035import microsoft.exchange.webservices.data.core.service.item.Item;
036import microsoft.exchange.webservices.data.core.service.schema.ResponseObjectSchema;
037import microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema;
038import microsoft.exchange.webservices.data.core.enumeration.service.calendar.AffectedTaskOccurrence;
039import microsoft.exchange.webservices.data.core.enumeration.service.DeleteMode;
040import microsoft.exchange.webservices.data.core.enumeration.attribute.EditorBrowsableState;
041import microsoft.exchange.webservices.data.core.enumeration.service.MessageDisposition;
042import microsoft.exchange.webservices.data.core.enumeration.service.SendCancellationsMode;
043import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
044import microsoft.exchange.webservices.data.property.complex.FolderId;
045import microsoft.exchange.webservices.data.property.complex.ItemId;
046
047import java.util.List;
048
049/**
050 * The Class ResponseObject.
051 *
052 * @param <TMessage> the generic type
053 */
054@EditorBrowsable(state = EditorBrowsableState.Never)
055public abstract class ResponseObject<TMessage extends EmailMessage> extends ServiceObject {
056
057  /**
058   * The reference item.
059   */
060  private Item referenceItem;
061
062  /**
063   * Initializes a new instance of the class.
064   *
065   * @param referenceItem the reference item
066   * @throws Exception the exception
067   */
068  protected ResponseObject(Item referenceItem) throws Exception {
069    super(referenceItem.getService());
070    referenceItem.throwIfThisIsNew();
071    this.referenceItem = referenceItem;
072  }
073
074  /**
075   * Internal method to return the schema associated with this type of object.
076   *
077   * @return The schema associated with this type of object.
078   */
079  @Override public ServiceObjectSchema getSchema() {
080    return ResponseObjectSchema.Instance;
081  }
082
083  /**
084   * Loads the specified set of property on the object.
085   *
086   * @param propertySet the property set
087   */
088  @Override
089  protected void internalLoad(PropertySet propertySet) {
090    throw new UnsupportedOperationException();
091  }
092
093  /**
094   * Deletes the object.
095   *
096   * @param deleteMode              the delete mode
097   * @param sendCancellationsMode   the send cancellations mode
098   * @param affectedTaskOccurrences the affected task occurrences
099   */
100  @Override
101  protected void internalDelete(DeleteMode deleteMode,
102      SendCancellationsMode sendCancellationsMode,
103      AffectedTaskOccurrence affectedTaskOccurrences) {
104    throw new UnsupportedOperationException();
105  }
106
107  /**
108   * Create the response object.
109   *
110   * @param destinationFolderId the destination folder id
111   * @param messageDisposition  the message disposition
112   * @return The list of item returned by EWS.
113   * @throws Exception the exception
114   */
115  protected List<Item> internalCreate(FolderId destinationFolderId,
116      MessageDisposition messageDisposition) throws Exception {
117    ((ItemId) this.getPropertyBag().getObjectFromPropertyDefinition(
118        ResponseObjectSchema.ReferenceItemId))
119        .assign(this.referenceItem.getId());
120    return this.getService().internalCreateResponseObject(this,
121        destinationFolderId, messageDisposition);
122  }
123
124  /**
125   * Saves the response in the specified folder. Calling this method results
126   * in a call to EWS.
127   *
128   * @param destinationFolderId the destination folder id
129   * @return A TMessage that represents the response.
130   * @throws Exception the exception
131   */
132  public TMessage save(FolderId destinationFolderId) throws Exception {
133    EwsUtilities.validateParam(destinationFolderId, "destinationFolderId");
134    return (TMessage) this.internalCreate(destinationFolderId,
135        MessageDisposition.SaveOnly).get(0);
136  }
137
138  /**
139   * Saves the response in the specified folder. Calling this method results
140   * in a call to EWS.
141   *
142   * @param destinationFolderName the destination folder name
143   * @return A TMessage that represents the response.
144   * @throws Exception the exception
145   */
146  public TMessage save(WellKnownFolderName destinationFolderName)
147      throws Exception {
148    return (TMessage) this.internalCreate(
149        new FolderId(destinationFolderName),
150        MessageDisposition.SaveOnly).get(0);
151  }
152
153  /**
154   * Saves the response in the Drafts folder. Calling this method results in a
155   * call to EWS.
156   *
157   * @return A TMessage that represents the response.
158   * @throws Exception the exception
159   */
160  public TMessage save() throws Exception {
161    return (TMessage) this
162        .internalCreate(null, MessageDisposition.SaveOnly).get(0);
163  }
164
165  /**
166   * Sends this response without saving a copy. Calling this method results in
167   * a call to EWS.
168   *
169   * @throws Exception the exception
170   */
171  public void send() throws Exception {
172    this.internalCreate(null, MessageDisposition.SendOnly);
173  }
174
175  /**
176   * Sends this response and saves a copy in the specified folder. Calling
177   * this method results in a call to EWS.
178   *
179   * @param destinationFolderId the destination folder id
180   * @throws Exception the exception
181   */
182  public void sendAndSaveCopy(FolderId destinationFolderId) throws Exception {
183    EwsUtilities.validateParam(destinationFolderId, "destinationFolderId");
184    this.internalCreate(destinationFolderId,
185        MessageDisposition.SendAndSaveCopy);
186  }
187
188  /**
189   * Sends this response and saves a copy in the specified folder. Calling
190   * this method results in a call to EWS.
191   *
192   * @param destinationFolderName the destination folder name
193   * @throws Exception the exception
194   */
195  public void sendAndSaveCopy(WellKnownFolderName destinationFolderName)
196      throws Exception {
197    this.internalCreate(new FolderId(destinationFolderName),
198        MessageDisposition.SendAndSaveCopy);
199  }
200
201  /**
202   * Sends this response and saves a copy in the Sent Items folder. Calling
203   * this method results in a call to EWS.
204   *
205   * @throws Exception the exception
206   */
207  public void sendAndSaveCopy() throws Exception {
208    this.internalCreate(null, MessageDisposition.SendAndSaveCopy);
209  }
210
211}