/// <summary> /// Writing a delta deleted link. /// </summary> /// <param name="deltaDeletedLink">The delta deleted link to write.</param> public abstract void WriteDeltaDeletedLink(ODataDeltaDeletedLink deltaDeletedLink);
/// <summary> /// Asynchronously writing a delta deleted link. /// </summary> /// <param name="deltaDeletedLink">The delta deleted link to write.</param> /// <returns>A task instance that represents the asynchronous write operation.</returns> public abstract Task WriteDeltaDeletedLinkAsync(ODataDeltaDeletedLink deltaDeletedLink);
/// <summary> /// Generate deleted entry and deleted link items /// </summary> /// <param name="parentId">Parent Id</param> /// <param name="relationShip">Navigation property name</param> private void GenerateDeltaItemsFromDeletedEntities(string parentId, string relationShip) { foreach (DeltaSnapshotEntry entry in this.DeltaSnapshot.Entries) { // Get all top level entities which do not contained in new results if (entry.ParentId == parentId && entry.RelationShip == relationShip) { // Verify if the entry is deleted now, since some of them may dispare because $filter var verifyresult = new QueryContext(this.ServiceRootUri, new Uri(entry.Id), this.DataSource.Model); object queryResults = verifyresult.ResolveQuery(this.DataSource); if (queryResults == null) { var deletedEntry = new ODataDeltaDeletedEntry(entry.Id, DeltaDeletedEntryReason.Deleted); deletedEntry.SetSerializationInfo(new ODataDeltaSerializationInfo { NavigationSourceName = (verifyresult.QueryPath.LastSegment as KeySegment).NavigationSource.Name, }); this.DeltaItems.Add(deletedEntry); if (!string.IsNullOrEmpty(parentId)) { var deltaDeletedLink = new ODataDeltaDeletedLink(new Uri(parentId), new Uri(entry.Id), relationShip); deltaDeletedLink.SetSerializationInfo(new ODataDeltaSerializationInfo { NavigationSourceName = (verifyresult.QueryPath.LastSegment as KeySegment).NavigationSource.Name, }); this.DeltaItems.Add(deltaDeletedLink); } } else { var deletedEntry = new ODataDeltaDeletedEntry(entry.Id, DeltaDeletedEntryReason.Changed); deletedEntry.SetSerializationInfo(new ODataDeltaSerializationInfo { NavigationSourceName = (verifyresult.QueryPath.LastSegment as KeySegment).NavigationSource.Name, }); this.DeltaItems.Add(deletedEntry); } } } }
public override void Process(IODataRequestMessage requestMessage, IODataResponseMessage responseMessage) { var token = HttpUtility.ParseQueryString(RequestUri.Query).Get("$token"); if (string.IsNullOrEmpty(token)) { deltaToken = "common"; } else { deltaToken = token; } if (deltaToken == "common") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Customers?$expand=Orders"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var entitySet = this.DataSource.Model.FindDeclaredEntitySet("Customers"); var entityType = entitySet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(entitySet, entityType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), Properties = new[] {new ODataProperty {Name = "FirstName", Value = "GGGG"}} }; var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(8)"), "Orders"); var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(7)"), "Orders"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Orders(100)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", Properties = new[] { new ODataProperty {Name = "OrderID", Value = 100}, new ODataProperty {Name = "OrderDate", Value = new DateTimeOffset(DateTime.Now)} } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", NavigationSourceKind = EdmNavigationSourceKind.EntitySet, NavigationSourceName = "Orders" }); var deletedEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(2)").AbsoluteUri, DeltaDeletedEntryReason.Deleted); deltaFeed.DeltaLink = new Uri(ServiceConstants.ServiceBaseUri, "$delta?$token=common"); deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedEntry); deltaWriter.WriteEnd(); } } else if (deltaToken == "containment") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments?$expand=BillingStatements"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var accountsSet = this.DataSource.Model.FindDeclaredEntitySet("Accounts"); var accountType = accountsSet.EntityType(); var myPisNav = accountType.FindProperty("MyPaymentInstruments") as IEdmNavigationProperty; var piSet = accountsSet.FindNavigationTarget(myPisNav); var piType = piSet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(piSet as IEdmContainedEntitySet, piType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)"), Properties = new[] { new ODataProperty { Name = "FriendlyName", Value = "GGGG" } } }; var deletedEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901001)").AbsoluteUri, DeltaDeletedEntryReason.Deleted); deletedEntry.SetSerializationInfo(new ODataDeltaSerializationInfo() { NavigationSourceName = "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements" }); var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)"), new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901001)"), "BillingStatements"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901005)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Statement", Properties = new[] { new ODataProperty { Name = "TransactionType", Value = "OnlinePurchase" }, new ODataProperty { Name = "TransactionDescription", Value = "unknown purchase" }, new ODataProperty { Name = "Amount", Value = 32.1 } } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Statement", NavigationSourceKind = EdmNavigationSourceKind.ContainedEntitySet, NavigationSourceName = "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements" }); var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)"), new Uri(ServiceConstants.ServiceBaseUri, "Accounts(103)/MyPaymentInstruments(103901)/BillingStatements(103901005)"), "BillingStatements"); deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedEntry); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteEnd(); } } else if (deltaToken == "derived") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "People?$expand=Microsoft.Test.OData.Services.ODataWCFService.Customer/Orders"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var peopleSet = this.DataSource.Model.FindDeclaredEntitySet("People"); var personType = peopleSet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(peopleSet, personType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "People(1)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Customer", Properties = new[] { new ODataProperty { Name = "City", Value = "GGGG" } } }; var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, "People(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(7)"), "Orders"); var deletedEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, "People(2)").AbsoluteUri, DeltaDeletedEntryReason.Changed); var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, "People(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(8)"), "Orders"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Orders(100)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", Properties = new[] { new ODataProperty {Name = "OrderID", Value = 100}, new ODataProperty {Name = "OrderDate", Value = new DateTimeOffset(DateTime.Now)} } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", NavigationSourceKind = EdmNavigationSourceKind.EntitySet, NavigationSourceName = "Orders" }); deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedEntry); deltaWriter.WriteEnd(); } } else if (deltaToken == "expanded") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Customers?$expand=Orders"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var customerSet = this.DataSource.Model.FindDeclaredEntitySet("Customers"); var orderSet = this.DataSource.Model.FindDeclaredEntitySet("Orders"); var peopleSet = this.DataSource.Model.FindDeclaredEntitySet("People"); var customerType = customerSet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(customerSet, customerType); // Delta feed and entry var deltaFeed = new ODataDeltaFeed { DeltaLink = new Uri(ServiceConstants.ServiceBaseUri, "$delta?$token=expanded") }; var deltaEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, customerSet.Name + "(1)"), Properties = new[] { new ODataProperty { Name = "FirstName", Value = "GGGG" } } }; // Expanded feed var navigationLink = new ODataNavigationLink() { Name = "Orders", IsCollection = true, }; var expandedFeed = new ODataFeed(); var expandedEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, orderSet.Name + "(8)"), Properties = new[] { new ODataProperty { Name = "OrderDate", Value = new DateTimeOffset(2011, 3, 4, 16, 03, 57, TimeSpan.FromHours(-8)) }, new ODataProperty { Name = "OrderID", Value = 8 }, new ODataProperty { Name = "OrderShelfLifes", Value = new ODataCollectionValue { Items = new[] { new TimeSpan(1) } } }, new ODataProperty { Name = "ShelfLife", Value = new TimeSpan(1) }, new ODataProperty { Name = "ShipDate", Value = new Date(2014, 8, 12) }, new ODataProperty { Name = "ShipTime", Value = new TimeOfDay(6, 5, 30, 0) }, } }; // Expanded entry var navigationLinkSingle = new ODataNavigationLink() { Name = "Parent", IsCollection = false, }; var expandedEntrySingle = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, peopleSet.Name + "(2)"), Properties = new[] { new ODataProperty { Name = "FirstName", Value = "Jill" }, new ODataProperty { Name = "LastName", Value = "Jones" }, new ODataProperty { Name = "Numbers", Value = new ODataCollectionValue() }, new ODataProperty { Name = "Emails", Value = new ODataCollectionValue() }, new ODataProperty { Name = "PersonID", Value = 2 }, new ODataProperty { Name = "Home", Value = GeographyPoint.Create(15.0, 161.8) }, } }; // Delta feed and entry deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry); // Expanded feed deltaWriter.WriteStart(navigationLink); deltaWriter.WriteStart(expandedFeed); deltaWriter.WriteStart(expandedEntry); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); // Expanded entry deltaWriter.WriteStart(navigationLinkSingle); deltaWriter.WriteStart(expandedEntrySingle); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); // Delta feed and entry deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); } } else if (deltaToken == "projection") { originalUri = new Uri(ServiceConstants.ServiceBaseUri, "Customers?$select=PersonID,FirstName,LastName&$expand=Orders($select=OrderID,OrderDate)"); using (var messageWriter = this.CreateMessageWriter(responseMessage)) { var entitySet = this.DataSource.Model.FindDeclaredEntitySet("Customers"); var entityType = entitySet.EntityType(); ODataDeltaWriter deltaWriter = messageWriter.CreateODataDeltaWriter(entitySet, entityType); var deltaFeed = new ODataDeltaFeed(); var deltaEntry1 = new ODataEntry { Properties = new[] { new ODataProperty { Name = "PersonID", Value = 1 }, new ODataProperty { Name = "FirstName", Value = "FFFF" }, new ODataProperty { Name = "LastName", Value = "LLLL" }, new ODataProperty { Name = "City", Value = "Beijing" } } }; var deletedLink = new ODataDeltaDeletedLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(8)"), "Orders"); var addedLink = new ODataDeltaLink( new Uri(ServiceConstants.ServiceBaseUri, entitySet.Name + "(1)"), new Uri(ServiceConstants.ServiceBaseUri, "Orders(7)"), "Orders"); var navigationEntry = new ODataEntry { Id = new Uri(ServiceConstants.ServiceBaseUri, "Orders(100)"), TypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", Properties = new[] { new ODataProperty {Name = "OrderID", Value = 100}, new ODataProperty {Name = "OrderDate", Value = new DateTimeOffset(DateTime.Now)} } }; navigationEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = "Microsoft.Test.OData.Services.ODataWCFService.Order", NavigationSourceKind = EdmNavigationSourceKind.EntitySet, NavigationSourceName = "Orders" }); var deletedOrderEntry = new ODataDeltaDeletedEntry( new Uri(ServiceConstants.ServiceBaseUri, "Orders(20)").AbsoluteUri, DeltaDeletedEntryReason.Deleted); deletedOrderEntry.SetSerializationInfo(new ODataDeltaSerializationInfo() { NavigationSourceName = "Orders" }); var deltaEntry2 = new ODataEntry { Properties = new[] { new ODataProperty { Name = "PersonID", Value = 2 }, new ODataProperty { Name = "FirstName", Value = "AAAA" }, } }; deltaWriter.WriteStart(deltaFeed); deltaWriter.WriteStart(deltaEntry1); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedLink(deletedLink); deltaWriter.WriteDeltaLink(addedLink); deltaWriter.WriteStart(navigationEntry); deltaWriter.WriteEnd(); deltaWriter.WriteDeltaDeletedEntry(deletedOrderEntry); deltaWriter.WriteStart(deltaEntry2); deltaWriter.WriteEnd(); deltaWriter.WriteEnd(); } } }
/// <summary> /// Writes the given deltaDeletedLink specified by the parameter graph as a part of an existing OData message using the given /// messageWriter and the writeContext. /// </summary> /// <param name="graph">The object to be written.</param> /// <param name="writer">The <see cref="ODataDeltaWriter" /> to be used for writing.</param> /// <param name="writeContext">The <see cref="ODataSerializerContext"/>.</param> public virtual void WriteDeltaDeletedLink(object graph, ODataDeltaWriter writer, ODataSerializerContext writeContext) { EdmDeltaDeletedLink edmDeltaDeletedLink = graph as EdmDeltaDeletedLink; if (edmDeltaDeletedLink == null) { throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName)); } ODataDeltaDeletedLink deltaDeletedLink = new ODataDeltaDeletedLink( edmDeltaDeletedLink.Source, edmDeltaDeletedLink.Target, edmDeltaDeletedLink.Relationship); if (deltaDeletedLink != null) { writer.WriteDeltaDeletedLink(deltaDeletedLink); } }