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

microsoft.exchange.webservices.data.notification.GetStreamingEventsResults 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.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;

/**
 * Represents a collection of notification events.
 */
public final class GetStreamingEventsResults {

  /**
   * Structure to track a subscription and its associated notification events.
   */
  protected static class NotificationGroup {
    /**
     * Subscription Id
     */
    protected String subscriptionId;

    /**
     * Events in the response associated with the subscription id.
     */
    protected Collection events;
  }


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

  /**
   * Initializes a new instance of the {@link GetStreamingEventsResults}
   * class.
   */
  public GetStreamingEventsResults() {
  }

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

    do {
      NotificationGroup notifications = new NotificationGroup();
      notifications.subscriptionId = reader.readElementValue(
          XmlNamespace.Types,
          XmlElementNames.SubscriptionId);
      notifications.events = new ArrayList();

      synchronized (this) {
        this.events.add(notifications);
      }

      do {
        reader.read();

        if (reader.isStartElement()) {
          String eventElementName = reader.getLocalName();
          EventType eventType;
          if (GetEventsResults.getXmlElementNameToEventTypeMap().containsKey(eventElementName)) {
            eventType = GetEventsResults.getXmlElementNameToEventTypeMap().
                get(eventElementName);
            if (eventType == EventType.Status) {
              // We don't need to return status events
              reader.readEndElementIfNecessary(XmlNamespace.Types,
                  eventElementName);
            } else {
              this.loadNotificationEventFromXml(
                  reader,
                  eventElementName,
                  eventType,
                  notifications);
            }
          } else {
            reader.skipCurrentElement();
          }
        }
      }
      while (!reader.isEndElement(XmlNamespace.Messages,
          XmlElementNames.Notification));

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

  /**
   * Loads a notification event from XML.
   *
   * @param reader           The reader.
   * @param eventElementName Name of the event XML element.
   * @param eventType        Type of the event.
   * @param notifications    Collection of notification
   * @throws Exception
   */
  private void loadNotificationEventFromXml(
      EwsServiceXmlReader reader,
      String eventElementName,
      EventType eventType,
      NotificationGroup notifications) throws Exception {
    Date timestamp = reader.readElementValue(Date.class, XmlNamespace.Types,
        XmlElementNames.TimeStamp);

    NotificationEvent notificationEvent;

    reader.read();

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

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

  /**
   * Gets the notification collection.
   *
   * @return The notification collection.
   */
  protected Collection getNotifications() {
    return this.events;
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy