private void ApplyChanges(CloudLibraryContext workingContext, IList<ChangeSet> changes) { foreach (var changeSet in changes.OrderBy(c => c.ChangedObject.ObjectType).ToList()) { switch (changeSet.ChangedObject.ObjectType) { case ObjectType.Library: var sourceLibrary = (ServiceConnector.Models.Rest.Wallbin.Entities.Library)changeSet.ChangedObject; workingContext.Library.ImportCloudData(sourceLibrary); break; case ObjectType.Page: var sourceLibraryPage = (ServiceConnector.Models.Rest.Wallbin.Entities.LibraryPage)changeSet.ChangedObject; var localLibraryPage = workingContext.Library.Pages.FirstOrDefault(page => page.ExtId == sourceLibraryPage.Id); if (localLibraryPage == null) { localLibraryPage = WallbinEntity.CreateEntity<LibraryPage>(); localLibraryPage.Library = workingContext.Library; workingContext.Library.Pages.Add(localLibraryPage); } localLibraryPage.ImportCloudData(sourceLibraryPage); break; } } workingContext.Library.SyncDate = DateTime.Now; workingContext.SaveChanges(); }
private void GetPendingChanges(CloudLibraryContext workingContext) { var chandedEntries = workingContext.ChangeTracker.Entries() .Where(entry => entry.State == EntityState.Modified || entry.State == EntityState.Added || entry.State == EntityState.Deleted) .ToList(); foreach (var entry in chandedEntries) { var wallbinEntity = entry.Entity as WallbinEntity; if (wallbinEntity == null) throw new CloudLibraryException("Undefined entity type"); var changeSet = PendingChanges .FirstOrDefault(existedChangeSet => existedChangeSet.ChangedObject.Id == wallbinEntity.ExtId); if (changeSet == null) { changeSet = new ChangeSet(); switch (entry.State) { case EntityState.Added: changeSet.ChangeType = ChangeType.Add; break; case EntityState.Modified: changeSet.ChangeType = ChangeType.Update; break; case EntityState.Deleted: changeSet.ChangeType = ChangeType.Delete; break; default: throw new CloudLibraryException("Undefined entity change state"); } PendingChanges.Add(changeSet); } else { if (entry.State == EntityState.Deleted) { if (changeSet.ChangeType == ChangeType.Add) PendingChanges.Remove(changeSet); else changeSet.ChangeType = ChangeType.Delete; } } changeSet.ChangedObject = wallbinEntity.ExportCloudData(); } }