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; 030 031import java.util.ArrayList; 032import java.util.Collection; 033import java.util.Date; 034 035/** 036 * Represents a collection of notification events. 037 */ 038public final class GetStreamingEventsResults { 039 040 /** 041 * Structure to track a subscription and its associated notification events. 042 */ 043 protected static class NotificationGroup { 044 /** 045 * Subscription Id 046 */ 047 protected String subscriptionId; 048 049 /** 050 * Events in the response associated with the subscription id. 051 */ 052 protected Collection<NotificationEvent> events; 053 } 054 055 056 /** 057 * Collection of notification events. 058 */ 059 private Collection<NotificationGroup> events = 060 new ArrayList<NotificationGroup>(); 061 062 /** 063 * Initializes a new instance of the <see cref= 064 * "GetStreamingEventsResults"/> class. 065 */ 066 public GetStreamingEventsResults() { 067 } 068 069 /** 070 * Loads from XML. 071 * 072 * @param reader The reader. 073 * @throws Exception 074 */ 075 public void loadFromXml(EwsServiceXmlReader reader) throws Exception { 076 reader.readStartElement(XmlNamespace.Messages, 077 XmlElementNames.Notification); 078 079 do { 080 NotificationGroup notifications = new NotificationGroup(); 081 notifications.subscriptionId = reader.readElementValue( 082 XmlNamespace.Types, 083 XmlElementNames.SubscriptionId); 084 notifications.events = new ArrayList<NotificationEvent>(); 085 086 synchronized (this) { 087 this.events.add(notifications); 088 } 089 090 do { 091 reader.read(); 092 093 if (reader.isStartElement()) { 094 String eventElementName = reader.getLocalName(); 095 EventType eventType; 096 if (GetEventsResults.getXmlElementNameToEventTypeMap().containsKey(eventElementName)) { 097 eventType = GetEventsResults.getXmlElementNameToEventTypeMap(). 098 get(eventElementName); 099 if (eventType == EventType.Status) { 100 // We don't need to return status events 101 reader.readEndElementIfNecessary(XmlNamespace.Types, 102 eventElementName); 103 } else { 104 this.loadNotificationEventFromXml( 105 reader, 106 eventElementName, 107 eventType, 108 notifications); 109 } 110 } else { 111 reader.skipCurrentElement(); 112 } 113 } 114 } 115 while (!reader.isEndElement(XmlNamespace.Messages, 116 XmlElementNames.Notification)); 117 118 reader.read(); 119 } 120 while (!reader.isEndElement(XmlNamespace.Messages, 121 XmlElementNames.Notifications)); 122 } 123 124 /** 125 * Loads a notification event from XML. 126 * 127 * @param reader The reader. 128 * @param eventElementName Name of the event XML element. 129 * @param eventType Type of the event. 130 * @param notifications Collection of notification 131 * @throws Exception 132 */ 133 private void loadNotificationEventFromXml( 134 EwsServiceXmlReader reader, 135 String eventElementName, 136 EventType eventType, 137 NotificationGroup notifications) throws Exception { 138 Date timestamp = reader.readElementValue(Date.class, XmlNamespace.Types, 139 XmlElementNames.TimeStamp); 140 141 NotificationEvent notificationEvent; 142 143 reader.read(); 144 145 if (reader.getLocalName().equals(XmlElementNames.FolderId)) { 146 notificationEvent = new FolderEvent(eventType, timestamp); 147 } else { 148 notificationEvent = new ItemEvent(eventType, timestamp); 149 } 150 151 notificationEvent.loadFromXml(reader, eventElementName); 152 notifications.events.add(notificationEvent); 153 } 154 155 /** 156 * Gets the notification collection. 157 * 158 * @value The notification collection. 159 */ 160 protected Collection<NotificationGroup> getNotifications() { 161 return this.events; 162 } 163} 164