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.property.complex.FolderId;
030import microsoft.exchange.webservices.data.property.complex.ItemId;
031
032import java.util.Date;
033
034/**
035 * Represents an event that applies to an item.
036 */
037public final class ItemEvent extends NotificationEvent {
038
039  /**
040   * Id of the item this event applies to.
041   */
042  private ItemId itemId;
043
044  /**
045   * Id of the item that moved or copied. This is only meaningful when
046   * EventType is equal to either EventType.Moved or EventType.Copied. For all
047   * other event types, it's null.
048   */
049  private ItemId oldItemId;
050
051  /**
052   * Initializes a new instance.
053   *
054   * @param eventType the event type
055   * @param timestamp the timestamp
056   */
057  protected ItemEvent(EventType eventType, Date timestamp) {
058    super(eventType, timestamp);
059  }
060
061  /**
062   * Initializes a new instance.
063   *
064   * @param eventType the event type
065   * @param itemId of the item that this event refers to
066   * @param parentFolderId of the item this event refers to
067   */
068  public ItemEvent(EventType eventType, ItemId itemId, FolderId parentFolderId) {
069    super(eventType, null);
070    this.itemId = itemId;
071    setParentFolderId(parentFolderId);
072  }
073
074  /**
075   * Load from XML.
076   *
077   * @param reader the reader
078   * @throws Exception the exception
079   */
080  protected void internalLoadFromXml(EwsServiceXmlReader reader)
081      throws Exception {
082    super.internalLoadFromXml(reader);
083
084    this.itemId = new ItemId();
085    this.itemId.loadFromXml(reader, reader.getLocalName());
086
087    reader.read();
088
089    setParentFolderId(new FolderId());
090
091    getParentFolderId().loadFromXml(reader, XmlElementNames.ParentFolderId);
092
093    EventType eventType = getEventType();
094    switch (eventType) {
095      case Moved:
096      case Copied:
097        reader.read();
098
099        this.oldItemId = new ItemId();
100        this.oldItemId.loadFromXml(reader, reader.getLocalName());
101
102        reader.read();
103
104        setOldParentFolderId(new FolderId());
105        getOldParentFolderId().loadFromXml(reader, reader.getLocalName());
106        break;
107
108      default:
109        break;
110    }
111  }
112
113  /**
114   * Gets the Id of the item this event applies to.
115   *
116   * @return itemId
117   */
118  public ItemId getItemId() {
119    return itemId;
120  }
121
122  /**
123   * Gets the Id of the item that was moved or copied. OldItemId is only
124   * meaningful when EventType is equal to either EventType.Moved or
125   * EventType.Copied. For all other event types, OldItemId is null.
126   *
127   * @return the old item id
128   */
129  public ItemId getOldItemId() {
130    return oldItemId;
131  }
132
133}