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    
024    package microsoft.exchange.webservices.data.property.complex;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
028    import microsoft.exchange.webservices.data.core.EwsUtilities;
029    import microsoft.exchange.webservices.data.core.XmlElementNames;
030    import microsoft.exchange.webservices.data.core.enumeration.property.Importance;
031    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032    import microsoft.exchange.webservices.data.misc.MobilePhone;
033    
034    import java.util.ArrayList;
035    import java.util.Collection;
036    
037    /**
038     * Represents the set of actions available for a rule.
039     */
040    public final class RuleActions extends ComplexProperty {
041    
042      /**
043       * SMS recipient address type.
044       */
045      private static final String MobileType = "MOBILE";
046    
047      /**
048       * The AssignCategories action.
049       */
050      private StringList assignCategories;
051    
052      /**
053       * The CopyToFolder action.
054       */
055      private FolderId copyToFolder;
056    
057      /**
058       * The Delete action.
059       */
060      private boolean delete;
061    
062      /**
063       * The ForwardAsAttachmentToRecipients action.
064       */
065      private EmailAddressCollection forwardAsAttachmentToRecipients;
066    
067      /**
068       * The ForwardToRecipients action.
069       */
070      private EmailAddressCollection forwardToRecipients;
071    
072      /**
073       * The MarkImportance action.
074       */
075      private Importance markImportance;
076    
077      /**
078       * The MarkAsRead action.
079       */
080      private boolean markAsRead;
081    
082      /**
083       * The MoveToFolder action.
084       */
085      private FolderId moveToFolder;
086    
087      /**
088       * The PermanentDelete action.
089       */
090      private boolean permanentDelete;
091    
092      /**
093       * The RedirectToRecipients action.
094       */
095      private EmailAddressCollection redirectToRecipients;
096    
097      /**
098       * The SendSMSAlertToRecipients action.
099       */
100      private Collection<MobilePhone> sendSMSAlertToRecipients;
101    
102      /**
103       * The ServerReplyWithMessage action.
104       */
105      private ItemId serverReplyWithMessage;
106    
107      /**
108       * The StopProcessingRules action.
109       */
110      private boolean stopProcessingRules;
111    
112      /**
113       * Initializes a new instance of the RulePredicates class.
114       */
115      protected RuleActions() {
116        super();
117        this.assignCategories = new StringList();
118        this.forwardAsAttachmentToRecipients =
119            new EmailAddressCollection(XmlElementNames.Address);
120        this.forwardToRecipients =
121            new EmailAddressCollection(XmlElementNames.Address);
122        this.redirectToRecipients =
123            new EmailAddressCollection(XmlElementNames.Address);
124        this.sendSMSAlertToRecipients = new ArrayList<MobilePhone>();
125      }
126    
127      /**
128       * Gets the categories that should be stamped on incoming messages.
129       * To disable stamping incoming messages with categories, set
130       * AssignCategories to null.
131       */
132      public StringList getAssignCategories() {
133    
134        return this.assignCategories;
135    
136      }
137    
138      /**
139       * Gets or sets the Id of the folder incoming messages should be copied to.
140       * To disable copying incoming messages
141       * to a folder, set CopyToFolder to null.
142       */
143      public FolderId getCopyToFolder() {
144        return this.copyToFolder;
145      }
146    
147      public void setCopyToFolder(FolderId value) {
148        if (this.canSetFieldValue(this.copyToFolder, value)) {
149          this.copyToFolder = value;
150          this.changed();
151        }
152      }
153    
154      /**
155       * Gets or sets a value indicating whether incoming messages should be
156       * automatically moved to the Deleted Items folder.
157       */
158      public boolean getDelete() {
159        return this.delete;
160      }
161    
162      public void setDelete(boolean value) {
163        if (this.canSetFieldValue(this.delete, value)) {
164          this.delete = value;
165          this.changed();
166        }
167    
168      }
169    
170      /**
171       * Gets the e-mail addresses to which incoming messages should be
172       * forwarded as attachments. To disable forwarding incoming messages
173       * as attachments, empty the ForwardAsAttachmentToRecipients list.
174       */
175      public EmailAddressCollection getForwardAsAttachmentToRecipients() {
176        return this.forwardAsAttachmentToRecipients;
177      }
178    
179      /**
180       * Gets the e-mail addresses to which
181       * incoming messages should be forwarded.
182       * To disable forwarding incoming messages,
183       * empty the ForwardToRecipients list.
184       */
185      public EmailAddressCollection getForwardToRecipients() {
186        return this.forwardToRecipients;
187    
188      }
189    
190      /**
191       * Gets or sets the importance that should be stamped on incoming
192       * messages. To disable the stamping of incoming messages with an
193       * importance, set MarkImportance to null.
194       */
195      public Importance getMarkImportance() {
196        return this.markImportance;
197      }
198    
199      public void setMarkImportance(Importance value) {
200        if (this.canSetFieldValue(this.markImportance, value)) {
201          this.markImportance = value;
202          this.changed();
203        }
204      }
205    
206      /**
207       * Gets or sets a value indicating whether
208       * incoming messages should be marked as read.
209       */
210      public boolean getMarkAsRead() {
211        return this.markAsRead;
212      }
213    
214      public void setMarkAsRead(boolean value) {
215        if (this.canSetFieldValue(this.markAsRead, value)) {
216          this.markAsRead = value;
217          this.changed();
218        }
219      }
220    
221      /**
222       * Gets or sets the Id of the folder to which incoming messages should be
223       * moved. To disable the moving of incoming messages to a folder, set
224       * CopyToFolder to null.
225       */
226      public FolderId getMoveToFolder() {
227        return this.moveToFolder;
228      }
229    
230      public void setMoveToFolder(FolderId value) {
231        if (this.canSetFieldValue(this.moveToFolder, value)) {
232          this.moveToFolder = value;
233          this.changed();
234        }
235    
236      }
237    
238      /**
239       * Gets or sets a value indicating whether incoming messages should be
240       * permanently deleted. When a message is permanently deleted, it is never
241       * saved into the recipient's mailbox. To delete a message after it has
242       * saved into the recipient's mailbox. To delete a message after it has
243       */
244      public boolean getPermanentDelete() {
245        return this.permanentDelete;
246      }
247    
248      public void setPermanentDelete(boolean value) {
249        if (this.canSetFieldValue(this.permanentDelete, value)) {
250          this.permanentDelete = value;
251          this.changed();
252        }
253      }
254    
255      /**
256       * Gets the e-mail addresses to which incoming messages should be
257       * redirecteded. To disable redirection of incoming messages, empty
258       * the RedirectToRecipients list. Unlike forwarded mail, redirected mail
259       * maintains the original sender and recipients.
260       */
261      public EmailAddressCollection getRedirectToRecipients() {
262        return this.redirectToRecipients;
263    
264      }
265    
266      /**
267       * Gets the phone numbers to which an SMS alert should be sent. To disable
268       * sending SMS alerts for incoming messages, empty the
269       * SendSMSAlertToRecipients list.
270       */
271      public Collection<MobilePhone> getSendSMSAlertToRecipients() {
272        return this.sendSMSAlertToRecipients;
273    
274      }
275    
276      /**
277       * Gets or sets the Id of the template message that should be sent
278       * as a reply to incoming messages. To disable automatic replies, set
279       * ServerReplyWithMessage to null.
280       */
281      public ItemId getServerReplyWithMessage() {
282        return this.serverReplyWithMessage;
283      }
284    
285      public void setServerReplyWithMessage(ItemId value) {
286        if (this.canSetFieldValue(this.serverReplyWithMessage, value)) {
287          this.serverReplyWithMessage = value;
288          this.changed();
289        }
290      }
291    
292      /**
293       * Gets or sets a value indicating whether
294       * subsequent rules should be evaluated.
295       */
296      public boolean getStopProcessingRules() {
297        return this.stopProcessingRules;
298      }
299    
300      public void setStopProcessingRules(boolean value) {
301        if (this.canSetFieldValue(this.stopProcessingRules, value)) {
302          this.stopProcessingRules = value;
303          this.changed();
304        }
305    
306      }
307    
308      /**
309       * Tries to read element from XML.
310       *
311       * @param reader The reader.
312       * @return True if element was read.
313       * @throws Exception
314       */
315      @Override
316      public boolean tryReadElementFromXml(EwsServiceXmlReader
317          reader) throws Exception {
318        if (reader.getLocalName().equals(XmlElementNames.CopyToFolder)) {
319          reader.readStartElement(XmlNamespace.NotSpecified,
320              XmlElementNames.FolderId);
321          this.copyToFolder = new FolderId();
322          this.copyToFolder.loadFromXml(reader, XmlElementNames.FolderId);
323          reader.readEndElement(XmlNamespace.NotSpecified,
324              XmlElementNames.CopyToFolder);
325          return true;
326        } else if (reader.getLocalName().equals(XmlElementNames.AssignCategories)) {
327          this.assignCategories.loadFromXml(reader,
328              reader.getLocalName());
329          return true;
330        } else if (reader.getLocalName().equals(XmlElementNames.Delete)) {
331          this.delete = reader.readElementValue(Boolean.class);
332          return true;
333        } else if (reader.getLocalName().equals(XmlElementNames.ForwardAsAttachmentToRecipients)) {
334          this.forwardAsAttachmentToRecipients.loadFromXml(reader,
335              reader.getLocalName());
336          return true;
337        } else if (reader.getLocalName().equals(XmlElementNames.ForwardToRecipients)) {
338          this.forwardToRecipients.loadFromXml(reader, reader.getLocalName());
339          return true;
340        } else if (reader.getLocalName().equals(XmlElementNames.MarkImportance)) {
341          this.markImportance = reader.readElementValue(Importance.class);
342          return true;
343        } else if (reader.getLocalName().equals(XmlElementNames.MarkAsRead)) {
344          this.markAsRead = reader.readElementValue(Boolean.class);
345          return true;
346        } else if (reader.getLocalName().equals(XmlElementNames.MoveToFolder)) {
347          reader.readStartElement(XmlNamespace.NotSpecified,
348              XmlElementNames.FolderId);
349          this.moveToFolder = new FolderId();
350          this.moveToFolder.loadFromXml(reader, XmlElementNames.FolderId);
351          reader.readEndElement(XmlNamespace.NotSpecified,
352              XmlElementNames.MoveToFolder);
353          return true;
354        } else if (reader.getLocalName().equals(XmlElementNames.PermanentDelete)) {
355          this.permanentDelete = reader.readElementValue(Boolean.class);
356          return true;
357        } else if (reader.getLocalName().equals(XmlElementNames.RedirectToRecipients)) {
358          this.redirectToRecipients.loadFromXml(reader,
359              reader.getLocalName());
360          return true;
361        } else if (reader.getLocalName().equals(XmlElementNames.SendSMSAlertToRecipients)) {
362          EmailAddressCollection smsRecipientCollection =
363              new EmailAddressCollection(XmlElementNames.Address);
364          smsRecipientCollection.loadFromXml(reader, reader.getLocalName());
365          this.sendSMSAlertToRecipients = convertSMSRecipientsFromEmailAddressCollectionToMobilePhoneCollection(
366              smsRecipientCollection);
367          return true;
368        } else if (reader.getLocalName().equals(XmlElementNames.ServerReplyWithMessage)) {
369          this.serverReplyWithMessage = new ItemId();
370          this.serverReplyWithMessage.loadFromXml(reader,
371              reader.getLocalName());
372          return true;
373        } else if (reader.getLocalName().equals(XmlElementNames.StopProcessingRules)) {
374          this.stopProcessingRules = reader.readElementValue(Boolean.class);
375          return true;
376        } else {
377          return false;
378        }
379    
380      }
381    
382      /**
383       * Writes elements to XML.
384       *
385       * @param writer The writer.
386       * @throws Exception
387       */
388      @Override
389      public void writeElementsToXml(EwsServiceXmlWriter writer)
390          throws Exception {
391        if (this.getAssignCategories().getSize() > 0) {
392          this.getAssignCategories().writeToXml(writer,
393              XmlElementNames.AssignCategories);
394        }
395    
396        if (this.getCopyToFolder() != null) {
397          writer.writeStartElement(XmlNamespace.Types,
398              XmlElementNames.CopyToFolder);
399          this.getCopyToFolder().writeToXml(writer);
400          writer.writeEndElement();
401        }
402    
403        if (this.getDelete() != false) {
404          writer.writeElementValue(
405              XmlNamespace.Types,
406              XmlElementNames.Delete,
407              this.getDelete());
408        }
409    
410        if (this.getForwardAsAttachmentToRecipients().getCount() > 0) {
411          this.getForwardAsAttachmentToRecipients().writeToXml(writer,
412              XmlElementNames.ForwardAsAttachmentToRecipients);
413        }
414    
415        if (this.getForwardToRecipients().getCount() > 0) {
416          this.getForwardToRecipients().writeToXml(writer,
417              XmlElementNames.ForwardToRecipients);
418        }
419    
420        if (this.getMarkImportance() != null) {
421          writer.writeElementValue(
422              XmlNamespace.Types,
423              XmlElementNames.MarkImportance,
424              this.getMarkImportance());
425        }
426    
427        if (this.getMarkAsRead() != false) {
428          writer.writeElementValue(
429              XmlNamespace.Types,
430              XmlElementNames.MarkAsRead,
431              this.getMarkAsRead());
432        }
433    
434        if (this.getMoveToFolder() != null) {
435          writer.writeStartElement(XmlNamespace.Types,
436              XmlElementNames.MoveToFolder);
437          this.getMoveToFolder().writeToXml(writer);
438          writer.writeEndElement();
439        }
440    
441        if (this.getPermanentDelete() != false) {
442          writer.writeElementValue(
443              XmlNamespace.Types,
444              XmlElementNames.PermanentDelete,
445              this.getPermanentDelete());
446        }
447    
448        if (this.getRedirectToRecipients().getCount() > 0) {
449          this.getRedirectToRecipients().writeToXml(writer,
450              XmlElementNames.RedirectToRecipients);
451        }
452    
453        if (this.getSendSMSAlertToRecipients().size() > 0) {
454          EmailAddressCollection emailCollection =
455              convertSMSRecipientsFromMobilePhoneCollectionToEmailAddressCollection(
456                  this.getSendSMSAlertToRecipients());
457          emailCollection.writeToXml(writer,
458              XmlElementNames.SendSMSAlertToRecipients);
459        }
460    
461        if (this.getServerReplyWithMessage() != null) {
462          this.getServerReplyWithMessage().writeToXml(writer,
463              XmlElementNames.ServerReplyWithMessage);
464        }
465    
466        if (this.getStopProcessingRules() != false) {
467          writer.writeElementValue(
468              XmlNamespace.Types,
469              XmlElementNames.StopProcessingRules,
470              this.getStopProcessingRules());
471        }
472      }
473    
474      /**
475       * Validates this instance.
476       *
477       * @throws Exception
478       */
479      @Override
480      protected void internalValidate() throws Exception {
481        super.internalValidate();
482        EwsUtilities.validateParam(this.forwardAsAttachmentToRecipients, "ForwardAsAttachmentToRecipients");
483        EwsUtilities.validateParam(this.forwardToRecipients,
484            "ForwardToRecipients");
485        EwsUtilities.validateParam(this.redirectToRecipients,
486            "RedirectToRecipients");
487        for (MobilePhone sendSMSAlertToRecipient : this.sendSMSAlertToRecipients) {
488          EwsUtilities.validateParam(sendSMSAlertToRecipient,
489              "SendSMSAlertToRecipient");
490        }
491      }
492    
493      /**
494       * Convert the SMS recipient list from
495       * EmailAddressCollection type to MobilePhone collection type.
496       *
497       * @return A MobilePhone collection object
498       * containing all SMS recipient in MobilePhone type.
499       */
500      private static Collection<MobilePhone> convertSMSRecipientsFromEmailAddressCollectionToMobilePhoneCollection(
501          EmailAddressCollection emailCollection) {
502        Collection<MobilePhone> mobilePhoneCollection =
503            new ArrayList<MobilePhone>();
504        for (EmailAddress emailAddress : emailCollection) {
505          mobilePhoneCollection.add(new MobilePhone(emailAddress.getName(),
506              emailAddress.getAddress()));
507        }
508    
509        return mobilePhoneCollection;
510      }
511    
512      /**
513       * Convert the SMS recipient list from MobilePhone
514       * collection type to EmailAddressCollection type.
515       *
516       * @return An EmailAddressCollection object
517       * containing recipients with "MOBILE" address type.
518       */
519      private static EmailAddressCollection convertSMSRecipientsFromMobilePhoneCollectionToEmailAddressCollection(
520          Collection<MobilePhone> recipientCollection) {
521        EmailAddressCollection emailCollection =
522            new EmailAddressCollection(XmlElementNames.Address);
523        for (MobilePhone recipient : recipientCollection) {
524          EmailAddress emailAddress = new EmailAddress(
525              recipient.getName(),
526              recipient.getPhoneNumber(),
527              RuleActions.MobileType);
528          emailCollection.add(emailAddress);
529        }
530    
531        return emailCollection;
532      }
533    
534    }
535