/src/lib/EWSJavaAPI_1.2/src/microsoft/exchange/webservices/data/RuleActions.java

https://bitbucket.org/diego_pascotto/my-code-snippets · Java · 509 lines · 300 code · 61 blank · 148 comment · 55 complexity · 5b7c43f238716569546ae07696095833 MD5 · raw file

  1. /**************************************************************************
  2. * copyright file="RuleActions.java" company="Microsoft"
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. *
  5. * Defines the RuleActions class.
  6. **************************************************************************/
  7. package microsoft.exchange.webservices.data;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. /**
  11. * Represents the set of actions available for a rule.
  12. */
  13. public final class RuleActions extends ComplexProperty {
  14. /**
  15. * SMS recipient address type.
  16. */
  17. private static final String MobileType = "MOBILE";
  18. /**
  19. * The AssignCategories action.
  20. */
  21. private StringList assignCategories;
  22. /**
  23. * The CopyToFolder action.
  24. */
  25. private FolderId copyToFolder;
  26. /**
  27. * The Delete action.
  28. */
  29. private boolean delete;
  30. /**
  31. * The ForwardAsAttachmentToRecipients action.
  32. */
  33. private EmailAddressCollection forwardAsAttachmentToRecipients;
  34. /**
  35. * The ForwardToRecipients action.
  36. */
  37. private EmailAddressCollection forwardToRecipients;
  38. /**
  39. * The MarkImportance action.
  40. */
  41. private Importance markImportance;
  42. /**
  43. * The MarkAsRead action.
  44. */
  45. private boolean markAsRead;
  46. /**
  47. * The MoveToFolder action.
  48. */
  49. private FolderId moveToFolder;
  50. /**
  51. * The PermanentDelete action.
  52. */
  53. private boolean permanentDelete;
  54. /**
  55. * The RedirectToRecipients action.
  56. */
  57. private EmailAddressCollection redirectToRecipients;
  58. /**
  59. * The SendSMSAlertToRecipients action.
  60. */
  61. private Collection<MobilePhone> sendSMSAlertToRecipients;
  62. /**
  63. * The ServerReplyWithMessage action.
  64. */
  65. private ItemId serverReplyWithMessage;
  66. /**
  67. * The StopProcessingRules action.
  68. */
  69. private boolean stopProcessingRules;
  70. /**
  71. * Initializes a new instance of the RulePredicates class.
  72. */
  73. protected RuleActions() {
  74. super();
  75. this.assignCategories = new StringList();
  76. this.forwardAsAttachmentToRecipients =
  77. new EmailAddressCollection(XmlElementNames.Address);
  78. this.forwardToRecipients =
  79. new EmailAddressCollection(XmlElementNames.Address);
  80. this.redirectToRecipients =
  81. new EmailAddressCollection(XmlElementNames.Address);
  82. this.sendSMSAlertToRecipients = new ArrayList<MobilePhone>();
  83. }
  84. /**
  85. * Gets the categories that should be stamped on incoming messages.
  86. * To disable stamping incoming messages with categories, set
  87. * AssignCategories to null.
  88. */
  89. public StringList getAssignCategories() {
  90. return this.assignCategories;
  91. }
  92. /**
  93. * Gets or sets the Id of the folder incoming messages should be copied to.
  94. * To disable copying incoming messages
  95. * to a folder, set CopyToFolder to null.
  96. */
  97. public FolderId getCopyToFolder() {
  98. return this.copyToFolder;
  99. }
  100. public void setCopyToFolder(FolderId value) {
  101. if (this.canSetFieldValue(this.copyToFolder, value)) {
  102. this.copyToFolder = value;
  103. this.changed();
  104. }}
  105. /**
  106. * Gets or sets a value indicating whether incoming messages should be
  107. * automatically moved to the Deleted Items folder.
  108. */
  109. public boolean getDelete() {
  110. return this.delete;
  111. }
  112. public void setDelete(boolean value) {
  113. if (this.canSetFieldValue(this.delete, value)) {
  114. this.delete = value;
  115. this.changed();
  116. }
  117. }
  118. /**
  119. * Gets the e-mail addresses to which incoming messages should be
  120. * forwarded as attachments. To disable forwarding incoming messages
  121. * as attachments, empty the ForwardAsAttachmentToRecipients list.
  122. */
  123. public EmailAddressCollection getForwardAsAttachmentToRecipients() {
  124. return this.forwardAsAttachmentToRecipients;
  125. }
  126. /**
  127. * Gets the e-mail addresses to which
  128. * incoming messages should be forwarded.
  129. * To disable forwarding incoming messages,
  130. * empty the ForwardToRecipients list.
  131. */
  132. public EmailAddressCollection getForwardToRecipients() {
  133. return this.forwardToRecipients;
  134. }
  135. /**
  136. * Gets or sets the importance that should be stamped on incoming
  137. * messages. To disable the stamping of incoming messages with an
  138. * importance, set MarkImportance to null.
  139. */
  140. public Importance getMarkImportance(){
  141. return this.markImportance;
  142. }
  143. public void setMarkImportance(Importance value) {
  144. if (this.canSetFieldValue(this.markImportance, value)) {
  145. this.markImportance = value;
  146. this.changed();
  147. }
  148. }
  149. /**
  150. * Gets or sets a value indicating whether
  151. * incoming messages should be marked as read.
  152. */
  153. public boolean getMarkAsRead() {
  154. return this.markAsRead;
  155. }
  156. public void setMarkAsRead(boolean value) {
  157. if (this.canSetFieldValue(this.markAsRead, value)) {
  158. this.markAsRead = value;
  159. this.changed();
  160. }}
  161. /**
  162. * Gets or sets the Id of the folder to which incoming messages should be
  163. * moved. To disable the moving of incoming messages to a folder, set
  164. * CopyToFolder to null.
  165. */
  166. public FolderId getMoveToFolder() {
  167. return this.moveToFolder;
  168. }
  169. public void setMoveToFolder(FolderId value) {
  170. if (this.canSetFieldValue(this.moveToFolder, value)) {
  171. this.moveToFolder = value;
  172. this.changed();
  173. }
  174. }
  175. /**
  176. * Gets or sets a value indicating whether incoming messages should be
  177. * permanently deleted. When a message is permanently deleted, it is never
  178. * saved into the recipient's mailbox. To delete a message after it has
  179. * saved into the recipient's mailbox. To delete a message after it has
  180. */
  181. public boolean getPermanentDelete() {
  182. return this.permanentDelete;
  183. }
  184. public void setPermanentDelete(boolean value) {
  185. if (this.canSetFieldValue(this.permanentDelete, value)) {
  186. this.permanentDelete = value;
  187. this.changed();
  188. }
  189. }
  190. /**
  191. * Gets the e-mail addresses to which incoming messages should be
  192. * redirecteded. To disable redirection of incoming messages, empty
  193. * the RedirectToRecipients list. Unlike forwarded mail, redirected mail
  194. * maintains the original sender and recipients.
  195. */
  196. public EmailAddressCollection getRedirectToRecipients() {
  197. return this.redirectToRecipients;
  198. }
  199. /**
  200. * Gets the phone numbers to which an SMS alert should be sent. To disable
  201. * sending SMS alerts for incoming messages, empty the
  202. * SendSMSAlertToRecipients list.
  203. */
  204. public Collection<MobilePhone> getSendSMSAlertToRecipients() {
  205. return this.sendSMSAlertToRecipients;
  206. }
  207. /**
  208. * Gets or sets the Id of the template message that should be sent
  209. * as a reply to incoming messages. To disable automatic replies, set
  210. * ServerReplyWithMessage to null.
  211. */
  212. public ItemId getServerReplyWithMessage() {
  213. return this.serverReplyWithMessage;
  214. }
  215. public void setServerReplyWithMessage(ItemId value) {
  216. if (this.canSetFieldValue(this.serverReplyWithMessage, value)) {
  217. this.serverReplyWithMessage = value;
  218. this.changed();
  219. }
  220. }
  221. /**
  222. * Gets or sets a value indicating whether
  223. * subsequent rules should be evaluated.
  224. */
  225. public boolean getStopProcessingRules() {
  226. return this.stopProcessingRules;
  227. }
  228. public void setStopProcessingRules(boolean value) {
  229. if (this.canSetFieldValue(this.stopProcessingRules, value)) {
  230. this.stopProcessingRules = value;
  231. this.changed();
  232. }
  233. }
  234. /**
  235. * Tries to read element from XML.
  236. * @param reader The reader.
  237. * @return True if element was read.
  238. * @throws Exception
  239. */
  240. @Override
  241. protected boolean tryReadElementFromXml(EwsServiceXmlReader
  242. reader) throws Exception {
  243. if (reader.getLocalName().equals(XmlElementNames.CopyToFolder)) {
  244. reader.readStartElement(XmlNamespace.NotSpecified,
  245. XmlElementNames.FolderId);
  246. this.copyToFolder = new FolderId();
  247. this.copyToFolder.loadFromXml(reader, XmlElementNames.FolderId);
  248. reader.readEndElement(XmlNamespace.NotSpecified,
  249. XmlElementNames.CopyToFolder);
  250. return true;
  251. }else if(reader.getLocalName().equals(XmlElementNames.AssignCategories)) {
  252. this.assignCategories.loadFromXml(reader,
  253. reader.getLocalName());
  254. return true;
  255. }else if (reader.getLocalName().equals(XmlElementNames.Delete)) {
  256. this.delete = reader.readElementValue(Boolean.class);
  257. return true;
  258. }
  259. else if (reader.getLocalName().equals(XmlElementNames.ForwardAsAttachmentToRecipients)) {
  260. this.forwardAsAttachmentToRecipients.loadFromXml(reader,
  261. reader.getLocalName());
  262. return true;
  263. }
  264. else if (reader.getLocalName().equals(XmlElementNames.ForwardToRecipients)) {
  265. this.forwardToRecipients.loadFromXml(reader, reader.getLocalName());
  266. return true;
  267. }
  268. else if (reader.getLocalName().equals(XmlElementNames.MarkImportance)) {
  269. this.markImportance = reader.readElementValue(Importance.class);
  270. return true;
  271. }
  272. else if (reader.getLocalName().equals(XmlElementNames.MarkAsRead)) {
  273. this.markAsRead = reader.readElementValue(Boolean.class);
  274. return true;
  275. }
  276. else if (reader.getLocalName().equals(XmlElementNames.MoveToFolder)) {
  277. reader.readStartElement(XmlNamespace.NotSpecified,
  278. XmlElementNames.FolderId);
  279. this.moveToFolder = new FolderId();
  280. this.moveToFolder.loadFromXml(reader, XmlElementNames.FolderId);
  281. reader.readEndElement(XmlNamespace.NotSpecified,
  282. XmlElementNames.MoveToFolder);
  283. return true;
  284. }
  285. else if (reader.getLocalName().equals(XmlElementNames.PermanentDelete)) {
  286. this.permanentDelete = reader.readElementValue(Boolean.class);
  287. return true;
  288. }
  289. else if (reader.getLocalName().equals(XmlElementNames.RedirectToRecipients)) {
  290. this.redirectToRecipients.loadFromXml(reader,
  291. reader.getLocalName());
  292. return true;
  293. }
  294. else if (reader.getLocalName().equals(XmlElementNames.SendSMSAlertToRecipients)) {
  295. EmailAddressCollection smsRecipientCollection =
  296. new EmailAddressCollection(XmlElementNames.Address);
  297. smsRecipientCollection.loadFromXml(reader, reader.getLocalName());
  298. this.sendSMSAlertToRecipients = convertSMSRecipientsFromEmailAddressCollectionToMobilePhoneCollection(
  299. smsRecipientCollection);
  300. return true;
  301. }
  302. else if (reader.getLocalName().equals(XmlElementNames.ServerReplyWithMessage)) {
  303. this.serverReplyWithMessage = new ItemId();
  304. this.serverReplyWithMessage.loadFromXml(reader,
  305. reader.getLocalName());
  306. return true;
  307. }
  308. else if (reader.getLocalName().equals(XmlElementNames.StopProcessingRules)) {
  309. this.stopProcessingRules = reader.readElementValue(Boolean.class);
  310. return true;
  311. }
  312. else {
  313. return false;
  314. }
  315. }
  316. /**
  317. * Writes elements to XML.
  318. * @param writer The writer.
  319. * @throws Exception
  320. */
  321. @Override
  322. protected void writeElementsToXml(EwsServiceXmlWriter writer)
  323. throws Exception {
  324. if (this.getAssignCategories().getSize() > 0) {
  325. this.getAssignCategories().writeToXml(writer,
  326. XmlElementNames.AssignCategories);
  327. }
  328. if (this.getCopyToFolder() != null) {
  329. writer.writeStartElement(XmlNamespace.Types,
  330. XmlElementNames.CopyToFolder);
  331. this.getCopyToFolder().writeToXml(writer);
  332. writer.writeEndElement();
  333. }
  334. if (this.getDelete() != false) {
  335. writer.writeElementValue(
  336. XmlNamespace.Types,
  337. XmlElementNames.Delete,
  338. this.getDelete());
  339. }
  340. if (this.getForwardAsAttachmentToRecipients().getCount() > 0) {
  341. this.getForwardAsAttachmentToRecipients().writeToXml(writer,
  342. XmlElementNames.ForwardAsAttachmentToRecipients);
  343. }
  344. if (this.getForwardToRecipients().getCount() > 0) {
  345. this.getForwardToRecipients().writeToXml(writer,
  346. XmlElementNames.ForwardToRecipients);
  347. }
  348. if (this.getMarkImportance()!=null) {
  349. writer.writeElementValue(
  350. XmlNamespace.Types,
  351. XmlElementNames.MarkImportance,
  352. this.getMarkImportance());
  353. }
  354. if (this.getMarkAsRead() != false) {
  355. writer.writeElementValue(
  356. XmlNamespace.Types,
  357. XmlElementNames.MarkAsRead,
  358. this.getMarkAsRead());
  359. }
  360. if (this.getMoveToFolder() != null) {
  361. writer.writeStartElement(XmlNamespace.Types,
  362. XmlElementNames.MoveToFolder);
  363. this.getMoveToFolder().writeToXml(writer);
  364. writer.writeEndElement();
  365. }
  366. if (this.getPermanentDelete() != false) {
  367. writer.writeElementValue(
  368. XmlNamespace.Types,
  369. XmlElementNames.PermanentDelete,
  370. this.getPermanentDelete());
  371. }
  372. if (this.getRedirectToRecipients().getCount() > 0) {
  373. this.getRedirectToRecipients().writeToXml(writer,
  374. XmlElementNames.RedirectToRecipients);
  375. }
  376. if (this.getSendSMSAlertToRecipients().size() > 0) {
  377. EmailAddressCollection emailCollection = convertSMSRecipientsFromMobilePhoneCollectionToEmailAddressCollection(
  378. this.getSendSMSAlertToRecipients());
  379. emailCollection.writeToXml(writer,
  380. XmlElementNames.SendSMSAlertToRecipients);
  381. }
  382. if (this.getServerReplyWithMessage() != null) {
  383. this.getServerReplyWithMessage().writeToXml(writer,
  384. XmlElementNames.ServerReplyWithMessage);
  385. }
  386. if (this.getStopProcessingRules() != false) {
  387. writer.writeElementValue(
  388. XmlNamespace.Types,
  389. XmlElementNames.StopProcessingRules,
  390. this.getStopProcessingRules());
  391. }
  392. }
  393. /**
  394. * Validates this instance.
  395. * @throws Exception
  396. */
  397. @Override
  398. protected void internalValidate() throws Exception {
  399. super.internalValidate();
  400. EwsUtilities.validateParam(this.forwardAsAttachmentToRecipients,
  401. "ForwardAsAttachmentToRecipients");
  402. EwsUtilities.validateParam(this.forwardToRecipients,
  403. "ForwardToRecipients");
  404. EwsUtilities.validateParam(this.redirectToRecipients,
  405. "RedirectToRecipients");
  406. for(MobilePhone sendSMSAlertToRecipient : this.sendSMSAlertToRecipients) {
  407. EwsUtilities.validateParam(sendSMSAlertToRecipient,
  408. "SendSMSAlertToRecipient");
  409. }
  410. }
  411. /**
  412. * Convert the SMS recipient list from
  413. * EmailAddressCollection type to MobilePhone collection type.
  414. * @param Recipient list in EmailAddressCollection type.
  415. * @return A MobilePhone collection object
  416. * containing all SMS recipient in MobilePhone type.
  417. */
  418. private static Collection<MobilePhone> convertSMSRecipientsFromEmailAddressCollectionToMobilePhoneCollection(
  419. EmailAddressCollection emailCollection) {
  420. Collection<MobilePhone> mobilePhoneCollection =
  421. new ArrayList<MobilePhone>();
  422. for(EmailAddress emailAddress : emailCollection) {
  423. mobilePhoneCollection.add(new MobilePhone(emailAddress.getName(),
  424. emailAddress.getAddress()));
  425. }
  426. return mobilePhoneCollection;
  427. }
  428. /**
  429. * Convert the SMS recipient list from MobilePhone
  430. * collection type to EmailAddressCollection type.
  431. * @param Recipient list in a MobilePhone collection type.
  432. * @return An EmailAddressCollection object
  433. * containing recipients with "MOBILE" address type.
  434. */
  435. private static EmailAddressCollection convertSMSRecipientsFromMobilePhoneCollectionToEmailAddressCollection(
  436. Collection<MobilePhone> recipientCollection) {
  437. EmailAddressCollection emailCollection =
  438. new EmailAddressCollection(XmlElementNames.Address);
  439. for(MobilePhone recipient : recipientCollection) {
  440. EmailAddress emailAddress = new EmailAddress(
  441. recipient.getName(),
  442. recipient.getPhoneNumber(),
  443. RuleActions.MobileType);
  444. emailCollection.add(emailAddress);
  445. }
  446. return emailCollection;
  447. }
  448. }