All Downloads are FREE. Search and download functionalities are using the official Maven repository.

microsoft.exchange.webservices.data.notification.GetEventsResults Maven / Gradle / Ivy

/*
 * The MIT License
 * Copyright (c) 2012 Microsoft Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package microsoft.exchange.webservices.data.notification;

import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
import microsoft.exchange.webservices.data.core.ILazyMember;
import microsoft.exchange.webservices.data.core.LazyMember;
import microsoft.exchange.webservices.data.core.XmlElementNames;
import microsoft.exchange.webservices.data.core.enumeration.notification.EventType;
import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Represents a collection of notification events.
 */
public final class GetEventsResults {
  /**
   * Watermark in event.
   */
  private String newWatermark;

  /**
   * Subscription id.
   */
  private String subscriptionId;

  /**
   * Previous watermark.
   */
  private String previousWatermark;

  /**
   * True if more events available for this subscription.
   */
  private boolean moreEventsAvailable;

  /**
   * Collection of notification events.
   */
  private Collection events =
      new ArrayList();

  /**
   * Map XML element name to notification event type. If you add a new
   * notification event type, you'll need to add a new entry to the Map here.
   */
  private static LazyMember>
      xmlElementNameToEventTypeMap =
      new LazyMember>(
          new ILazyMember>() {
            @Override
            public Map createInstance() {
              Map result =
                  new HashMap();
              result.put(XmlElementNames.CopiedEvent, EventType.Copied);
              result.put(XmlElementNames.CreatedEvent, EventType.Created);
              result.put(XmlElementNames.DeletedEvent, EventType.Deleted);
              result.put(XmlElementNames.ModifiedEvent,
                  EventType.Modified);
              result.put(XmlElementNames.MovedEvent, EventType.Moved);
              result.put(XmlElementNames.NewMailEvent, EventType.NewMail);
              result.put(XmlElementNames.StatusEvent, EventType.Status);
              result.put(XmlElementNames.FreeBusyChangedEvent,
                  EventType.FreeBusyChanged);
              return result;
            }
          });

  /**
   * Gets the XML element name to event type mapping.
   *
   * @return The XML element name to event type mapping.
   */
  protected static Map getXmlElementNameToEventTypeMap() {
    return GetEventsResults.xmlElementNameToEventTypeMap.getMember();
  }

  /**
   * Initializes a new instance.
   */
  public GetEventsResults() {
  }

  /**
   * Loads from XML.
   *
   * @param reader the reader
   * @throws Exception the exception
   */
  public void loadFromXml(EwsServiceXmlReader reader) throws Exception {
    reader.readStartElement(XmlNamespace.Messages,
        XmlElementNames.Notification);

    this.subscriptionId = reader.readElementValue(XmlNamespace.Types,
        XmlElementNames.SubscriptionId);
    this.previousWatermark = reader.readElementValue(XmlNamespace.Types,
        XmlElementNames.PreviousWatermark);
    this.moreEventsAvailable = reader.readElementValue(Boolean.class,
        XmlNamespace.Types, XmlElementNames.MoreEvents);

    do {
      reader.read();

      if (reader.isStartElement()) {
        String eventElementName = reader.getLocalName();
        EventType eventType;

        if (xmlElementNameToEventTypeMap.getMember().containsKey(
            eventElementName)) {
          eventType = xmlElementNameToEventTypeMap.getMember().get(
              eventElementName);
          this.newWatermark = reader.readElementValue(
              XmlNamespace.Types, XmlElementNames.Watermark);
          if (eventType == EventType.Status) {
            // We don't need to return status events
            reader.readEndElementIfNecessary(XmlNamespace.Types,
                eventElementName);
          } else {
            this.loadNotificationEventFromXml(reader,
                eventElementName, eventType);
          }
        } else {
          reader.skipCurrentElement();
        }

      }

    } while (!reader.isEndElement(XmlNamespace.Messages,
        XmlElementNames.Notification));
  }

  /**
   * Loads a notification event from XML.
   *
   * @param reader           the reader
   * @param eventElementName the event element name
   * @param eventType        the event type
   * @throws Exception the exception
   */
  private void loadNotificationEventFromXml(EwsServiceXmlReader reader,
      String eventElementName, EventType eventType) throws Exception {
    Date date = reader.readElementValue(Date.class, XmlNamespace.Types,
        XmlElementNames.TimeStamp);

    NotificationEvent notificationEvent;

    reader.read();

    if (reader.getLocalName().equals(XmlElementNames.FolderId)) {
      notificationEvent = new FolderEvent(eventType, date);
    } else {
      notificationEvent = new ItemEvent(eventType, date);
    }

    notificationEvent.loadFromXml(reader, eventElementName);
    this.events.add(notificationEvent);
  }

  /**
   * Gets the Id of the subscription the collection is associated with.
   *
   * @return the subscription id
   */
  public String getSubscriptionId() {
    return subscriptionId;
  }

  /**
   * Gets the subscription's previous watermark.
   *
   * @return the previous watermark
   */
  public String getPreviousWatermark() {
    return previousWatermark;
  }

  /**
   * Gets the subscription's new watermark.
   *
   * @return the new watermark
   */
  public String getNewWatermark() {
    return newWatermark;
  }

  /**
   * Gets a value indicating whether more events are available on the Exchange
   * server.
   *
   * @return true, if is more events available
   */
  public boolean isMoreEventsAvailable() {
    return moreEventsAvailable;
  }

  /**
   * Gets the collection of folder events.
   *
   * @return the folder events
   */
  public Iterable getFolderEvents() {
    Collection folderEvents = new ArrayList();
    for (Object event : this.events) {
      if (event instanceof FolderEvent) {
        folderEvents.add((FolderEvent) event);
      }
    }
    return folderEvents;
  }

  /**
   * Gets the collection of item events.
   *
   * @return the item events
   */
  public Iterable getItemEvents() {
    Collection itemEvents = new ArrayList();
    for (Object event : this.events) {
      if (event instanceof ItemEvent) {
        itemEvents.add((ItemEvent) event);
      }
    }
    return itemEvents;
  }

  /**
   * Gets the collection of all events.
   *
   * @return the all events
   */
  public Collection getAllEvents() {
    return this.events;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy