private static IPushNotificationSubscriptionItem Create(IMailboxSession session, IXSOFactory xsoFactory, StoreId locationFolderId, string subscriptionId, PushNotificationServerSubscription subscription) { IPushNotificationSubscriptionItem pushNotificationSubscriptionItem = null; try { pushNotificationSubscriptionItem = xsoFactory.CreatePushNotificationSubscriptionItem(session, locationFolderId); subscription.LastSubscriptionUpdate = (DateTime)pushNotificationSubscriptionItem.LastUpdateTimeUTC; pushNotificationSubscriptionItem.SubscriptionId = subscriptionId; pushNotificationSubscriptionItem.SerializedNotificationSubscription = subscription.ToJson(); if (ExTraceGlobals.StorageNotificationSubscriptionTracer.IsTraceEnabled(TraceType.DebugTrace)) { ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceDebug <string, ExDateTime, string>((long)subscription.GetHashCode(), "PushNotificationSubscriptionItem.Create: Created SubscriptionItem on store, Id:{0}, RefTm:{1}, Json:{2}", pushNotificationSubscriptionItem.SubscriptionId, pushNotificationSubscriptionItem.LastUpdateTimeUTC, pushNotificationSubscriptionItem.SerializedNotificationSubscription); } ConflictResolutionResult conflictResolutionResult = pushNotificationSubscriptionItem.Save(SaveMode.FailOnAnyConflict); if (conflictResolutionResult.SaveStatus == SaveResult.IrresolvableConflict) { ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceError <string>((long)pushNotificationSubscriptionItem.GetHashCode(), "PushNotificationSubscriptionItem.Create: Save failed due to conflicts for subscription {0}.", subscriptionId); throw new SaveConflictException(ServerStrings.ExSaveFailedBecauseOfConflicts(pushNotificationSubscriptionItem.SubscriptionId)); } pushNotificationSubscriptionItem.Load(SubscriptionItemEnumeratorBase.PushNotificationSubscriptionItemProperties); } catch { if (pushNotificationSubscriptionItem != null) { pushNotificationSubscriptionItem.Dispose(); } throw; } return(pushNotificationSubscriptionItem); }
public static IPushNotificationSubscriptionItem CreateOrUpdateSubscription(IMailboxSession session, IXSOFactory xsoFactory, IFolder folder, string subscriptionId, PushNotificationServerSubscription subscription) { Util.ThrowOnNullArgument(session, "session"); Util.ThrowOnNullArgument(xsoFactory, "xsoFactory"); Util.ThrowOnNullArgument(folder, "folder"); Util.ThrowOnNullOrEmptyArgument(subscriptionId, "subscriptionId"); Util.ThrowOnNullArgument(subscription, "subscription"); ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceDebug <string, IExchangePrincipal>((long)subscription.GetHashCode(), "PushNotificationSubscriptionItem.CreateOrUpdateSubscription: Searching for Subscription {0} on Mailbox {1}.", subscriptionId, session.MailboxOwner); IStorePropertyBag[] array = PushNotificationSubscriptionItem.GetSubscriptionById(folder, subscriptionId).ToArray <IStorePropertyBag>(); IPushNotificationSubscriptionItem pushNotificationSubscriptionItem = null; try { if (array.Length >= 1) { if (array.Length > 1) { ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceWarning <string, Guid>(0L, "PushNotificationSubscriptionItem.CreateOrUpdateSubscription: AmbiguousSubscription for subscription {0} and user {1}", subscriptionId, session.MailboxGuid); } IStorePropertyBag storePropertyBag = array[0]; VersionedId valueOrDefault = storePropertyBag.GetValueOrDefault <VersionedId>(ItemSchema.Id, null); if (valueOrDefault == null) { ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceError <string>((long)storePropertyBag.GetHashCode(), "PushNotificationSubscriptionItem.CreateOrUpdateSubscription: Cannot resolve the ItemSchema.Id property from the Enumerable.", subscriptionId); throw new CannotResolvePropertyException(ItemSchema.Id.Name); } ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceDebug <VersionedId>((long)storePropertyBag.GetHashCode(), "PushNotificationSubscriptionItem.CreateOrUpdateSubscription: Found one existing subscription with ItemSchema.Id = {0}.", valueOrDefault); pushNotificationSubscriptionItem = xsoFactory.BindToPushNotificationSubscriptionItem(session, valueOrDefault, null); pushNotificationSubscriptionItem.LastUpdateTimeUTC = ExDateTime.UtcNow; subscription.LastSubscriptionUpdate = (DateTime)pushNotificationSubscriptionItem.LastUpdateTimeUTC; pushNotificationSubscriptionItem.SerializedNotificationSubscription = subscription.ToJson(); ConflictResolutionResult conflictResolutionResult = pushNotificationSubscriptionItem.Save(SaveMode.ResolveConflicts); if (conflictResolutionResult.SaveStatus == SaveResult.IrresolvableConflict) { ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceError <string>((long)storePropertyBag.GetHashCode(), "PushNotificationSubscriptionItem.CreateOrUpdateSubscription: Save failed due to conflicts for subscription {0}.", subscriptionId); throw new SaveConflictException(ServerStrings.ExSaveFailedBecauseOfConflicts(subscriptionId)); } pushNotificationSubscriptionItem.Load(SubscriptionItemEnumeratorBase.PushNotificationSubscriptionItemProperties); } else { ExTraceGlobals.StorageNotificationSubscriptionTracer.TraceDebug((long)subscription.GetHashCode(), "PushNotificationSubscriptionItem.CreateOrUpdateSubscription: Cannot resolve given subscription, about to create a new SubscriptionItem."); pushNotificationSubscriptionItem = PushNotificationSubscriptionItem.Create(session, xsoFactory, folder.StoreObjectId, subscriptionId, subscription); } } catch { if (pushNotificationSubscriptionItem != null) { pushNotificationSubscriptionItem.Dispose(); } throw; } return(pushNotificationSubscriptionItem); }