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.item;
025
026import microsoft.exchange.webservices.data.attribute.Attachable;
027import microsoft.exchange.webservices.data.attribute.ServiceObjectDefinition;
028import microsoft.exchange.webservices.data.core.ExchangeService;
029import microsoft.exchange.webservices.data.core.PropertySet;
030import microsoft.exchange.webservices.data.core.XmlElementNames;
031import microsoft.exchange.webservices.data.core.service.response.PostReply;
032import microsoft.exchange.webservices.data.core.service.response.ResponseMessage;
033import microsoft.exchange.webservices.data.core.service.schema.EmailMessageSchema;
034import microsoft.exchange.webservices.data.core.service.schema.PostItemSchema;
035import microsoft.exchange.webservices.data.core.service.schema.ServiceObjectSchema;
036import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
037import microsoft.exchange.webservices.data.core.enumeration.service.ResponseMessageType;
038import microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException;
039import microsoft.exchange.webservices.data.property.complex.EmailAddress;
040import microsoft.exchange.webservices.data.property.complex.ItemAttachment;
041import microsoft.exchange.webservices.data.property.complex.ItemId;
042import microsoft.exchange.webservices.data.property.complex.MessageBody;
043
044import java.util.Arrays;
045import java.util.Date;
046
047/**
048 * Represents a post item. Properties available on post item are defined in the
049 * PostItemSchema class.
050 */
051@Attachable
052@ServiceObjectDefinition(xmlElementName = XmlElementNames.PostItem)
053public final class PostItem extends Item {
054
055  /**
056   * Initializes an unsaved local instance of PostItem.To bind to an existing
057   * post item, use PostItem.Bind() instead.
058   *
059   * @param service the service
060   * @throws Exception the exception
061   */
062  public PostItem(ExchangeService service) throws Exception {
063    super(service);
064  }
065
066  /**
067   * Initializes a new instance of the class.
068   *
069   * @param parentAttachment the parent attachment
070   * @throws Exception the exception
071   */
072  public PostItem(ItemAttachment parentAttachment) throws Exception {
073    super(parentAttachment);
074  }
075
076  /**
077   * Binds to an existing post item and loads the specified set of property.
078   * Calling this method results in a call to EWS.
079   *
080   * @param service     the service
081   * @param id          the id
082   * @param propertySet the property set
083   * @return An PostItem instance representing the post item corresponding to
084   * the specified Id.
085   * @throws Exception the exception
086   */
087  public static PostItem bind(ExchangeService service, ItemId id,
088      PropertySet propertySet) throws Exception {
089    return service.bindToItem(PostItem.class, id, propertySet);
090  }
091
092  /**
093   * Binds to an existing post item and loads its first class property.
094   * calling this method results in a call to EWS.
095   *
096   * @param service the service
097   * @param id      the id
098   * @return An PostItem instance representing the post item corresponding to
099   * the specified Id.
100   * @throws Exception the exception
101   */
102  public static PostItem bind(ExchangeService service, ItemId id)
103      throws Exception {
104    return PostItem
105        .bind(service, id, PropertySet.getFirstClassProperties());
106  }
107
108  /**
109   * Internal method to return the schema associated with this type of object.
110   *
111   * @return The schema associated with this type of object.
112   */
113  @Override public ServiceObjectSchema getSchema() {
114    return PostItemSchema.Instance;
115  }
116
117  /**
118   * Internal method to return the schema associated with this type of object.
119   *
120   * @return The schema associated with this type of object.
121   */
122  @Override public ExchangeVersion getMinimumRequiredServerVersion() {
123    return ExchangeVersion.Exchange2007_SP1;
124  }
125
126  /**
127   * Creates a post reply to this post item.
128   *
129   * @return A PostReply that can be modified and saved.
130   * @throws Exception the exception
131   */
132  public PostReply createPostReply() throws Exception {
133    this.throwIfThisIsNew();
134    return new PostReply(this);
135  }
136
137  /**
138   * Posts a reply to this post item. Calling this method results in a call to
139   * EWS.
140   *
141   * @param bodyPrefix the body prefix
142   * @throws Exception the exception
143   */
144  public void postReply(MessageBody bodyPrefix) throws Exception {
145    PostReply postReply = this.createPostReply();
146    postReply.setBodyPrefix(bodyPrefix);
147    postReply.save();
148  }
149
150  /**
151   * Creates a e-mail reply response to the post item.
152   *
153   * @param replyAll the reply all
154   * @return A ResponseMessage representing the e-mail reply response that can
155   * subsequently be modified and sent.
156   * @throws Exception the exception
157   */
158  public ResponseMessage createReply(boolean replyAll) throws Exception {
159    this.throwIfThisIsNew();
160    return new ResponseMessage(this,
161        replyAll ? ResponseMessageType.ReplyAll :
162            ResponseMessageType.Reply);
163  }
164
165  /**
166   * Replies to the post item. Calling this method results in a call to EWS.
167   *
168   * @param bodyPrefix the body prefix
169   * @param replyAll   the reply all
170   * @throws Exception the exception
171   */
172  public void reply(MessageBody bodyPrefix, boolean replyAll)
173      throws Exception {
174    ResponseMessage responseMessage = this.createReply(replyAll);
175    responseMessage.setBodyPrefix(bodyPrefix);
176    responseMessage.sendAndSaveCopy();
177  }
178
179  /**
180   * Creates a forward response to the post item.
181   *
182   * @return A ResponseMessage representing the forward response that can
183   * subsequently be modified and sent.
184   * @throws Exception the exception
185   */
186  public ResponseMessage createForward() throws Exception {
187    this.throwIfThisIsNew();
188    return new ResponseMessage(this, ResponseMessageType.Forward);
189  }
190
191  /**
192   * Forwards the post item. Calling this method results in a call to EWS.
193   *
194   * @param bodyPrefix   the body prefix
195   * @param toRecipients the to recipients
196   * @throws Exception the exception
197   */
198  public void forward(MessageBody bodyPrefix, EmailAddress... toRecipients)
199      throws Exception {
200    forward(bodyPrefix, Arrays.asList(toRecipients));
201  }
202
203  /**
204   * Forwards the post item. Calling this method results in a call to EWS.
205   *
206   * @param bodyPrefix   the body prefix
207   * @param toRecipients the to recipients
208   * @throws Exception the exception
209   */
210  public void forward(MessageBody bodyPrefix,
211      Iterable<EmailAddress> toRecipients) throws Exception {
212    ResponseMessage responseMessage = this.createForward();
213    responseMessage.setBodyPrefix(bodyPrefix);
214    responseMessage.getToRecipients()
215        .addEmailRange(toRecipients.iterator());
216
217    responseMessage.sendAndSaveCopy();
218  }
219
220  // Properties
221
222  /**
223   * Gets the conversation index of the post item.
224   *
225   * @return the conversation index
226   * @throws ServiceLocalException the service local exception
227   */
228  public byte[] getConversationIndex() throws ServiceLocalException {
229    return getPropertyBag().getObjectFromPropertyDefinition(
230        EmailMessageSchema.ConversationIndex);
231  }
232
233  /**
234   * Gets the conversation topic of the post item.
235   *
236   * @return the conversation topic
237   * @throws ServiceLocalException the service local exception
238   */
239  public String getConversationTopic() throws ServiceLocalException {
240    return getPropertyBag().getObjectFromPropertyDefinition(
241        EmailMessageSchema.ConversationTopic);
242  }
243
244  /**
245   * Gets the "on behalf" poster of the post item.
246   *
247   * @return the from
248   * @throws ServiceLocalException the service local exception
249   */
250  public EmailAddress getFrom() throws ServiceLocalException {
251    return getPropertyBag().getObjectFromPropertyDefinition(
252        EmailMessageSchema.From);
253  }
254
255  /**
256   * Sets the from.
257   *
258   * @param value the new from
259   * @throws Exception the exception
260   */
261  public void setFrom(EmailAddress value) throws Exception {
262    this.getPropertyBag().setObjectFromPropertyDefinition(
263        EmailMessageSchema.From, value);
264  }
265
266  /**
267   * Gets the Internet message Id of the post item.
268   *
269   * @return the internet message id
270   * @throws ServiceLocalException the service local exception
271   */
272  public String getInternetMessageId() throws ServiceLocalException {
273    return getPropertyBag().getObjectFromPropertyDefinition(
274        EmailMessageSchema.InternetMessageId);
275  }
276
277  /**
278   * Gets a value indicating whether the post item is read.
279   *
280   * @return the checks if is read
281   * @throws ServiceLocalException the service local exception
282   */
283  public Boolean getIsRead() throws ServiceLocalException {
284    return getPropertyBag().getObjectFromPropertyDefinition(
285        EmailMessageSchema.IsRead);
286  }
287
288  /**
289   * Sets the checks if is read.
290   *
291   * @param value the new checks if is read
292   * @throws Exception the exception
293   */
294  public void setIsRead(Boolean value) throws Exception {
295    this.getPropertyBag().setObjectFromPropertyDefinition(
296        EmailMessageSchema.IsRead, value);
297  }
298
299  /**
300   * Gets the the date and time when the post item was posted.
301   *
302   * @return the posted time
303   * @throws ServiceLocalException the service local exception
304   */
305  public Date getPostedTime() throws ServiceLocalException {
306    return getPropertyBag().getObjectFromPropertyDefinition(
307        PostItemSchema.PostedTime);
308  }
309
310  /**
311   * Gets the references of the post item.
312   *
313   * @return the references
314   * @throws ServiceLocalException the service local exception
315   */
316  public String getReferences() throws ServiceLocalException {
317    return getPropertyBag().getObjectFromPropertyDefinition(
318        EmailMessageSchema.References);
319  }
320
321  /**
322   * Sets the checks if is read.
323   *
324   * @param value the new checks if is read
325   * @throws Exception the exception
326   */
327  public void setIsRead(String value) throws Exception {
328    this.getPropertyBag().setObjectFromPropertyDefinition(
329        EmailMessageSchema.References, value);
330  }
331
332  /**
333   * Gets the sender (poster) of the post item.
334   *
335   * @return the sender
336   * @throws ServiceLocalException the service local exception
337   */
338  public EmailAddress getSender() throws ServiceLocalException {
339    return getPropertyBag().getObjectFromPropertyDefinition(
340        EmailMessageSchema.Sender);
341  }
342
343  /**
344   * Sets the sender.
345   *
346   * @param value the new sender
347   * @throws Exception the exception
348   */
349  public void setSender(EmailAddress value) throws Exception {
350    this.getPropertyBag().setObjectFromPropertyDefinition(
351        EmailMessageSchema.Sender, value);
352  }
353
354}