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.misc;
025    
026    import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027    import microsoft.exchange.webservices.data.core.EwsUtilities;
028    import microsoft.exchange.webservices.data.core.XmlElementNames;
029    import microsoft.exchange.webservices.data.core.enumeration.misc.ConversationActionType;
030    import microsoft.exchange.webservices.data.core.enumeration.service.DeleteMode;
031    import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
032    import microsoft.exchange.webservices.data.core.exception.misc.ArgumentException;
033    import microsoft.exchange.webservices.data.property.complex.ConversationId;
034    import microsoft.exchange.webservices.data.property.complex.StringList;
035    import org.apache.commons.logging.Log;
036    import org.apache.commons.logging.LogFactory;
037    
038    import java.util.Date;
039    
040    /**
041     * ConversationAction class that represents
042     * ConversationActionType in the request XML.
043     * This class really is meant for representing
044     * single ConversationAction that needs to
045     * be taken on a conversation.
046     */
047    public class ConversationAction {
048    
049      private static final Log LOG = LogFactory.getLog(ConversationAction.class);
050    
051      private ConversationActionType action;
052      private ConversationId conversationId;
053      private boolean processRightAway;
054    
055      private boolean enableAlwaysDelete;
056      private StringList categories;
057      private FolderIdWrapper moveFolderId;
058      private FolderIdWrapper contextFolderId;
059      private DeleteMode deleteType;
060      private Boolean isRead;
061      private Date conversationLastSyncTime;
062    
063      /**
064       * Gets conversation action
065       *
066       * @return action
067       */
068      protected ConversationActionType getAction() {
069        return this.action;
070      }
071    
072      /**
073       * Sets conversation action
074       */
075      public void setAction(ConversationActionType value) {
076        this.action = value;
077      }
078    
079      /**
080       * Gets conversation id
081       *
082       * @return conversationId
083       */
084      protected ConversationId getConversationId() {
085        return this.conversationId;
086      }
087    
088      /**
089       * Sets conversation id
090       */
091      public void setConversationId(ConversationId value) {
092        this.conversationId = value;
093      }
094    
095      /**
096       * Gets ProcessRightAway
097       *
098       * @return processRightAway
099       */
100      protected boolean getProcessRightAway() {
101        return this.processRightAway;
102      }
103    
104      /**
105       * Sets ProcessRightAway
106       */
107      public void setProcessRightAway(boolean value) {
108        this.processRightAway = value;
109      }
110    
111    
112      /**
113       * Gets conversation categories for Always Categorize action
114       *
115       * @return categories
116       */
117      protected StringList getCategories() {
118        return this.categories;
119      }
120    
121      /**
122       * Sets conversation categories for Always Categorize actions
123       */
124      public void setCategories(StringList value) {
125        this.categories = value;
126      }
127    
128      /**
129       * Gets Enable Always Delete value for Always Delete action
130       *
131       * @return enableAlwaysDelete
132       */
133      protected boolean getEnableAlwaysDelete() {
134        return this.enableAlwaysDelete;
135      }
136    
137      /**
138       * Sets Enable Always Delete value for Always Delete action
139       */
140      public void setEnableAlwaysDelete(boolean value) {
141        this.enableAlwaysDelete = value;
142      }
143    
144      /**
145       * IsRead
146       *
147       * @return isRead
148       */
149      protected Boolean getIsRead() {
150        return this.isRead;
151      }
152    
153      /**
154       * IsRead
155       */
156      public void setIsRead(Boolean value) {
157        this.isRead = value;
158      }
159    
160      /**
161       * DeleteType
162       *
163       * @return deleteType
164       */
165      protected DeleteMode getDeleteType() {
166        return this.deleteType;
167      }
168    
169      /**
170       * DeleteType
171       */
172      public void setDeleteType(DeleteMode value) {
173        this.deleteType = value;
174      }
175    
176      /**
177       * ConversationLastSyncTime is used in one
178       * time action to determine the item
179       * on which to take the action.
180       *
181       * @return conversationLastSyncTime
182       */
183      protected Date getConversationLastSyncTime() {
184        return this.conversationLastSyncTime;
185      }
186    
187      /**
188       * ConversationLastSyncTime is used in
189       * one time action to determine the item
190       * on which to take the action.
191       */
192      public void setConversationLastSyncTime(Date value) {
193        this.conversationLastSyncTime = value;
194      }
195    
196      /**
197       * Gets folder id ContextFolder
198       *
199       * @return contextFolderId
200       */
201      protected FolderIdWrapper getContextFolderId() {
202        return this.contextFolderId;
203      }
204    
205      /**
206       * Sets folder id ContextFolder
207       */
208      public void setContextFolderId(FolderIdWrapper value) {
209        this.contextFolderId = value;
210      }
211    
212      /**
213       * Gets folder id for Move action
214       *
215       * @return moveFolderId
216       */
217      protected FolderIdWrapper getDestinationFolderId() {
218        return this.moveFolderId;
219      }
220    
221      /**
222       * Sets folder id for Move action
223       */
224      public void setDestinationFolderId(FolderIdWrapper value) {
225        this.moveFolderId = value;
226      }
227    
228      /**
229       * Gets the name of the XML element.
230       *
231       * @return XML element name.
232       */
233      protected String getXmlElementName() {
234        return XmlElementNames.ApplyConversationAction;
235      }
236    
237      /**
238       * Validate request.
239       *
240       * @throws Exception
241       */
242      public void validate() throws Exception {
243        EwsUtilities.validateParam(this.conversationId, "conversationId");
244      }
245    
246      /**
247       * Writes XML elements.
248       *
249       * @param writer The writer.
250       * @throws Exception
251       */
252      public void writeElementsToXml(EwsServiceXmlWriter writer)
253          throws Exception {
254        writer.writeStartElement(
255            XmlNamespace.Types,
256            XmlElementNames.ConversationAction);
257        try {
258          String actionValue = null;
259          if (this.getAction() == ConversationActionType.AlwaysCategorize) {
260            actionValue = XmlElementNames.AlwaysCategorize;
261          } else if (this.getAction() == ConversationActionType.AlwaysDelete) {
262            actionValue = XmlElementNames.AlwaysDelete;
263          } else if (this.getAction() == ConversationActionType.AlwaysMove) {
264            actionValue = XmlElementNames.AlwaysMove;
265          } else if (this.getAction() == ConversationActionType.Delete) {
266            actionValue = XmlElementNames.Delete;
267          } else if (this.getAction() == ConversationActionType.Copy) {
268            actionValue = XmlElementNames.Copy;
269          } else if (this.getAction() == ConversationActionType.Move) {
270            actionValue = XmlElementNames.Move;
271          } else if (this.getAction() == ConversationActionType.SetReadState) {
272            actionValue = XmlElementNames.SetReadState;
273          } else {
274            throw new ArgumentException("ConversationAction");
275          }
276    
277          // Emit the action element
278          writer.writeElementValue(
279              XmlNamespace.Types,
280              XmlElementNames.Action,
281              actionValue);
282    
283          // Emit the conversation id element
284          this.getConversationId().writeToXml(
285              writer,
286              XmlNamespace.Types,
287              XmlElementNames.ConversationId);
288    
289          if (this.getAction() == ConversationActionType.AlwaysCategorize ||
290              this.getAction() == ConversationActionType.AlwaysDelete ||
291              this.getAction() == ConversationActionType.AlwaysMove) {
292            // Emit the ProcessRightAway element
293            writer.writeElementValue(
294                XmlNamespace.Types,
295                XmlElementNames.ProcessRightAway,
296                EwsUtilities.boolToXSBool(this.getProcessRightAway()));
297          }
298    
299          if (this.getAction() == ConversationActionType.AlwaysCategorize) {
300            // Emit the categories element
301            if (this.getCategories() != null && this.getCategories().getSize() > 0) {
302              this.getCategories().writeToXml(
303                  writer,
304                  XmlNamespace.Types,
305                  XmlElementNames.Categories);
306            }
307          } else if (this.getAction() == ConversationActionType.AlwaysDelete) {
308            // Emit the EnableAlwaysDelete element
309            writer.writeElementValue(
310                XmlNamespace.Types,
311                XmlElementNames.EnableAlwaysDelete,
312                EwsUtilities.boolToXSBool(this.
313                    getEnableAlwaysDelete()));
314          } else if (this.getAction() == ConversationActionType.AlwaysMove) {
315            // Emit the Move Folder Id
316            if (this.getDestinationFolderId() != null) {
317              writer.writeStartElement(XmlNamespace.Types,
318                  XmlElementNames.DestinationFolderId);
319              this.getDestinationFolderId().writeToXml(writer);
320              writer.writeEndElement();
321            }
322          } else {
323            if (this.getContextFolderId() != null) {
324              writer.writeStartElement(
325                  XmlNamespace.Types,
326                  XmlElementNames.ContextFolderId);
327    
328              this.getContextFolderId().writeToXml(writer);
329    
330              writer.writeEndElement();
331            }
332    
333            if (this.getConversationLastSyncTime() != null) {
334              writer.writeElementValue(
335                  XmlNamespace.Types,
336                  XmlElementNames.ConversationLastSyncTime,
337                  this.getConversationLastSyncTime());
338            }
339    
340            if (this.getAction() == ConversationActionType.Copy) {
341              EwsUtilities.ewsAssert(this.getDestinationFolderId() != null,
342                "ApplyconversationActionRequest",
343                "DestinationFolderId should be set when performing copy action");
344    
345              writer.writeStartElement(
346                  XmlNamespace.Types,
347                  XmlElementNames.DestinationFolderId);
348              this.getDestinationFolderId().writeToXml(writer);
349              writer.writeEndElement();
350            } else if (this.getAction() == ConversationActionType.Move) {
351              EwsUtilities.ewsAssert(this.getDestinationFolderId() != null,
352                "ApplyconversationActionRequest",
353                "DestinationFolderId should be set when performing move action");
354    
355              writer.writeStartElement(
356                  XmlNamespace.Types,
357                  XmlElementNames.DestinationFolderId);
358              this.getDestinationFolderId().writeToXml(writer);
359              writer.writeEndElement();
360            } else if (this.getAction() == ConversationActionType.Delete) {
361              EwsUtilities.ewsAssert(this.getDeleteType() != null,
362                "ApplyconversationActionRequest",
363                "DeleteType should be specified when deleting a conversation.");
364    
365              writer.writeElementValue(
366                  XmlNamespace.Types,
367                  XmlElementNames.DeleteType,
368                  this.getDeleteType());
369            } else if (this.getAction() == ConversationActionType.SetReadState) {
370              EwsUtilities.ewsAssert(this.getIsRead() != null,
371                "ApplyconversationActionRequest",
372                "IsRead should be specified when marking/unmarking a conversation as read.");
373    
374              writer.writeElementValue(
375                  XmlNamespace.Types,
376                  XmlElementNames.IsRead,
377                  this.getIsRead());
378            }
379          }
380        } catch (Exception e) {
381          LOG.error(e);
382        } finally {
383          writer.writeEndElement();
384        }
385      }
386    
387    }