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.notification;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027import microsoft.exchange.webservices.data.core.XmlElementNames;
028import microsoft.exchange.webservices.data.core.enumeration.notification.EventType;
029import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
030import microsoft.exchange.webservices.data.property.complex.FolderId;
031
032import java.util.Date;
033
034/**
035 * Represents an event that applies to a folder.
036 */
037public class FolderEvent extends NotificationEvent {
038
039  /**
040   * The folder id.
041   */
042  private FolderId folderId;
043
044  /**
045   * The old folder id.
046   */
047  private FolderId oldFolderId;
048
049  /**
050   * The new number of unread messages. This is is only meaningful when
051   * EventType is equal to EventType.Modified. For all other event types, it's
052   * null.
053   */
054  private int unreadCount;
055
056  /**
057   * Initializes a new instance.
058   *
059   * @param eventType the event type
060   * @param timestamp the timestamp
061   */
062  protected FolderEvent(EventType eventType, Date timestamp) {
063    super(eventType, timestamp);
064  }
065
066  /**
067   * Load from XML.
068   *
069   * @param reader the reader
070   * @throws Exception the exception
071   */
072  protected void internalLoadFromXml(EwsServiceXmlReader reader)
073      throws Exception {
074    super.internalLoadFromXml(reader);
075
076    this.folderId = new FolderId();
077    this.folderId.loadFromXml(reader, reader.getLocalName());
078
079    reader.read();
080
081    setParentFolderId(new FolderId());
082    getParentFolderId().loadFromXml(reader, XmlElementNames.ParentFolderId);
083
084    switch (getEventType()) {
085      case Moved:
086      case Copied:
087        reader.read();
088
089        this.oldFolderId = new FolderId();
090        this.oldFolderId.loadFromXml(reader, reader.getLocalName());
091
092        reader.read();
093
094        setParentFolderId(new FolderId());
095        getParentFolderId().loadFromXml(reader, reader.getLocalName());
096        break;
097
098      case Modified:
099        reader.read();
100        if (reader.isStartElement()) {
101          reader.ensureCurrentNodeIsStartElement(XmlNamespace.Types,
102              XmlElementNames.UnreadCount);
103          String str = reader.readValue();
104          this.unreadCount = Integer.parseInt(str);
105        }
106        break;
107
108      default:
109        break;
110    }
111  }
112
113  /**
114   * Gets the Id of the folder this event applies to.
115   *
116   * @return folderId
117   */
118  public FolderId getFolderId() {
119    return folderId;
120  }
121
122  /**
123   * gets the Id of the folder that was moved or copied. OldFolderId is only
124   * meaningful when EventType is equal to either EventType.Moved or
125   * EventType.Copied. For all other event types, OldFolderId is null.
126   *
127   * @return oldFolderId
128   */
129  public FolderId getOldFolderId() {
130    return oldFolderId;
131  }
132
133  /**
134   * Gets the new number of unread messages. This is is only meaningful when
135   * EventType is equal to EventType.Modified. For all other event types,
136   * UnreadCount is null.
137   *
138   * @return unreadCount
139   */
140  public int getUnreadCount() {
141    return unreadCount;
142  }
143
144}