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
026import microsoft.exchange.webservices.data.attribute.ServiceObjectDefinition;
027import microsoft.exchange.webservices.data.core.EwsUtilities;
028import microsoft.exchange.webservices.data.core.PropertySet;
029import microsoft.exchange.webservices.data.core.XmlElementNames;
030import microsoft.exchange.webservices.data.core.service.ServiceObject;
031import microsoft.exchange.webservices.data.core.service.item.Item;
032import microsoft.exchange.webservices.data.core.service.item.PostItem;
033import microsoft.exchange.webservices.data.core.service.schema.EmailMessageSchema;
034import microsoft.exchange.webservices.data.core.service.schema.ItemSchema;
035import microsoft.exchange.webservices.data.core.service.schema.PostReplySchema;
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.misc.ExchangeVersion;
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.core.exception.misc.InvalidOperationException;
045import microsoft.exchange.webservices.data.property.complex.FolderId;
046import microsoft.exchange.webservices.data.property.complex.ItemId;
047import microsoft.exchange.webservices.data.property.complex.MessageBody;
048
049import java.util.List;
050
051/**
052 * Represents a reply to a post item.
053 */
054@ServiceObjectDefinition(xmlElementName = XmlElementNames.PostReplyItem, returnedByServer = false)
055public final class PostReply 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  public PostReply(Item referenceItem) throws Exception {
069    super(referenceItem.getService());
070    referenceItem.throwIfThisIsNew();
071
072    this.referenceItem = referenceItem;
073  }
074
075  /**
076   * Internal method to return the schema associated with this type of object.
077   *
078   * @return The schema associated with this type of object.
079   */
080  @Override
081  public ServiceObjectSchema getSchema() {
082    return PostReplySchema.Instance;
083  }
084
085  /**
086   * Gets the minimum required server version.
087   *
088   * @return Earliest Exchange version in which this service object type is
089   * supported.
090   */
091  @Override public ExchangeVersion getMinimumRequiredServerVersion() {
092    return ExchangeVersion.Exchange2007_SP1;
093  }
094
095  /**
096   * Create a PostItem response.
097   *
098   * @param parentFolderId     the parent folder id
099   * @param messageDisposition the message disposition
100   * @return Created PostItem.
101   * @throws Exception the exception
102   */
103  protected PostItem internalCreate(FolderId parentFolderId,
104      MessageDisposition messageDisposition) throws Exception {
105    ((ItemId) this
106        .getObjectFromPropertyDefinition(
107            ResponseObjectSchema.ReferenceItemId))
108        .assign(this.referenceItem.getId());
109
110    List<Item> items = this.getService().internalCreateResponseObject(this,
111        parentFolderId, messageDisposition);
112
113    PostItem postItem = EwsUtilities.findFirstItemOfType(PostItem.class,
114        items);
115
116    // This should never happen. If it does, we have a bug.
117    EwsUtilities
118        .ewsAssert(postItem != null, "PostReply.InternalCreate",
119                   "postItem is null. The CreateItem call did" + " not return the expected PostItem.");
120
121    return postItem;
122  }
123
124  /**
125   * Loads the specified set of property on the object.
126   *
127   * @param propertySet the property set
128   * @throws InvalidOperationException the invalid operation exception
129   */
130  @Override
131  protected void internalLoad(PropertySet propertySet)
132      throws InvalidOperationException {
133    throw new InvalidOperationException("Loading this type of object is not supported.");
134  }
135
136  /**
137   * Deletes the object.
138   *
139   * @param deleteMode              the delete mode
140   * @param sendCancellationsMode   the send cancellations mode
141   * @param affectedTaskOccurrences the affected task occurrences
142   * @throws InvalidOperationException the invalid operation exception
143   */
144  @Override
145  protected void internalDelete(DeleteMode deleteMode,
146      SendCancellationsMode sendCancellationsMode,
147      AffectedTaskOccurrence affectedTaskOccurrences)
148      throws InvalidOperationException {
149    throw new InvalidOperationException("Deleting this type of object isn't authorized.");
150  }
151
152  /**
153   * Saves the post reply in the same folder as the original post item.
154   * Calling this method results in a call to EWS.
155   *
156   * @return A PostItem representing the posted reply
157   * @throws Exception the exception
158   */
159  public PostItem save() throws Exception {
160    return this.internalCreate(null, null);
161  }
162
163  /**
164   * Saves the post reply in the same folder as the original post item.
165   * Calling this method results in a call to EWS.
166   *
167   * @param destinationFolderId the destination folder id
168   * @return A PostItem representing the posted reply
169   * @throws Exception the exception
170   */
171  public PostItem save(FolderId destinationFolderId) throws Exception {
172    EwsUtilities.validateParam(destinationFolderId, "destinationFolderId");
173    return this.internalCreate(destinationFolderId, null);
174  }
175
176  /**
177   * Saves the post reply in a specified folder. Calling this method results
178   * in a call to EWS.
179   *
180   * @param destinationFolderName the destination folder name
181   * @return A PostItem representing the posted reply.
182   * @throws Exception the exception
183   */
184  public PostItem save(WellKnownFolderName destinationFolderName)
185      throws Exception {
186    return this.internalCreate(new FolderId(destinationFolderName), null);
187  }
188
189  /**
190   * Gets the subject of the post reply.
191   *
192   * @return the subject
193   * @throws Exception the exception
194   */
195  public String getSubject() throws Exception {
196    return (String) this
197        .getObjectFromPropertyDefinition(EmailMessageSchema.Subject);
198  }
199
200  /**
201   * Sets the subject.
202   *
203   * @param value the new subject
204   * @throws Exception the exception
205   */
206  public void setSubject(String value) throws Exception {
207    this.getPropertyBag().setObjectFromPropertyDefinition(
208        EmailMessageSchema.Subject, value);
209  }
210
211  /**
212   * Gets the body of the post reply.
213   *
214   * @return the body
215   * @throws Exception the exception
216   */
217  public MessageBody getBody() throws Exception {
218    return (MessageBody) this
219        .getObjectFromPropertyDefinition(ItemSchema.Body);
220  }
221
222  /**
223   * Sets the body.
224   *
225   * @param value the new body
226   * @throws Exception the exception
227   */
228  public void setBody(MessageBody value) throws Exception {
229    this.getPropertyBag().setObjectFromPropertyDefinition(ItemSchema.Body,
230        value);
231  }
232
233  /**
234   * Gets the body prefix that should be prepended to the original
235   * post item's body.
236   *
237   * @return the body prefix
238   * @throws Exception the exception
239   */
240  public MessageBody getBodyPrefix() throws Exception {
241    return (MessageBody) this
242        .getObjectFromPropertyDefinition(
243            ResponseObjectSchema.BodyPrefix);
244  }
245
246  /**
247   * Sets the body prefix.
248   *
249   * @param value the new body prefix
250   * @throws Exception the exception
251   */
252  public void setBodyPrefix(MessageBody value) throws Exception {
253    this.getPropertyBag().setObjectFromPropertyDefinition(
254        ResponseObjectSchema.BodyPrefix, value);
255  }
256
257}