/// <summary> /// Writing a delta link. /// </summary> /// <param name="deltaLink">The delta link to write.</param> public abstract void WriteDeltaLink(ODataDeltaLink deltaLink);
/// <summary> /// Asynchronously writing a delta link. /// </summary> /// <param name="deltaLink">The delta link to write.</param> /// <returns>A task instance that represents the asynchronous write operation.</returns> public abstract Task WriteDeltaLinkAsync(ODataDeltaLink deltaLink);
/// <summary> /// Handle single entry, which from first level and $expand /// </summary> /// <param name="entry">Entry</param> /// <param name="entitySet">EntitySet</param> /// <param name="targetVersion">Target Version</param> /// <param name="selectExpandClause">Select and Expand Caluse</param> /// <param name="parentId">ParentId, string.Empty for first level</param> /// <param name="relationShip">Navigation property name, string.Empty for first level</param> private void GenerateDeltaItemFromEntry(object entry, IEdmNavigationSource entitySet, ODataVersion targetVersion, SelectExpandClause selectExpandClause, string parentId, string relationShip) { var deltaEntry = ODataObjectModelConverter.ConvertToODataEntry(entry, entitySet, targetVersion); // Verify entry if need to be written DateTime lastestUpdated = ((ClrObject)entry).UpdatedTime; if (lastestUpdated > DeltaSnapshot.TimeStamp) { var lastSegmentOfDeltaEntry = new ODataUriParser(this.DataSource.Model, ServiceConstants.ServiceBaseUri, deltaEntry.Id).ParsePath().LastSegment as KeySegment; deltaEntry.SetSerializationInfo(new ODataFeedAndEntrySerializationInfo { NavigationSourceEntityTypeName = lastSegmentOfDeltaEntry.EdmType.ToString(), NavigationSourceKind = lastSegmentOfDeltaEntry.NavigationSource.NavigationSourceKind(), NavigationSourceName = lastSegmentOfDeltaEntry.NavigationSource.Name, }); this.DeltaItems.Add(deltaEntry); } // Verify if need added link bool ifDeleted = DeltaSnapshot.Entries.Remove(DeltaSnapshot.Entries.FirstOrDefault(p => p.Id == deltaEntry.Id.AbsoluteUri && p.ParentId == parentId && p.RelationShip == relationShip)); if ((!string.IsNullOrEmpty(parentId)) && ifDeleted == false) { var deltaLink = new ODataDeltaLink(new Uri(parentId), deltaEntry.Id, relationShip); var lastSegmentOfDeltaEntry = new ODataUriParser(this.DataSource.Model, ServiceConstants.ServiceBaseUri, deltaEntry.Id).ParsePath().LastSegment as KeySegment; deltaLink.SetSerializationInfo(new ODataDeltaSerializationInfo { NavigationSourceName = lastSegmentOfDeltaEntry.NavigationSource.Name, }); this.DeltaItems.Add(deltaLink); } // Handle $expand var expandedNavigationItems = selectExpandClause == null ? null : selectExpandClause.SelectedItems.OfType<ExpandedNavigationSelectItem>(); GenerateDeltaItemsFromExpand(entry, entitySet, targetVersion, expandedNavigationItems, deltaEntry.Id.AbsoluteUri); }
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 deltaLink 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 WriteDeltaLink(object graph, ODataDeltaWriter writer, ODataSerializerContext writeContext) { EdmDeltaLink edmDeltaLink = graph as EdmDeltaLink; if (edmDeltaLink == null) { throw new SerializationException(Error.Format(SRResources.CannotWriteType, GetType().Name, graph.GetType().FullName)); } ODataDeltaLink deltaLink = new ODataDeltaLink( edmDeltaLink.Source, edmDeltaLink.Target, edmDeltaLink.Relationship); if (deltaLink != null) { writer.WriteDeltaLink(deltaLink); } }