Ejemplo n.º 1
0
 protected virtual void OnConflictOccur(ConflictsEventArgs args)
 {
     if (this.ConflictOccur != null)
     {
         this.ConflictOccur(this, args);
     }
 }
Ejemplo n.º 2
0
        public override void OnChangeSetUploaded(Guid state, ChangeSetResponse response)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            if (response.Error != null)
            {
                throw response.Error;
            }
            this.OnSyncProgress(new SyncProgressEventArgs("Upload finished,mark local entities as uploaded..."));

            siaqodb.StartBulkInsert(CacheController.ControllerBehavior.KnownTypes.ToArray());
            try
            {
                if (null != response.UpdatedItems && 0 != response.UpdatedItems.Count)
                {
                    List <string> IdsWithError = new List <string>();
                    foreach (Conflict cf in response.Conflicts)
                    {
                        if (cf is SyncError)
                        {
                            IdsWithError.Add(cf.LiveEntity.ServiceMetadata.Id);
                        }
                    }

                    foreach (var item in response.UpdatedItems)
                    {
                        if (IdsWithError.Contains(item.ServiceMetadata.Id))
                        {
                            continue;
                        }
                        var offlineEntity = (SiaqodbOfflineEntity)item;
                        offlineEntity.IsDirty     = false;
                        offlineEntity.IsTombstone = false;
                        this.SaveEntityByPK(offlineEntity);
                    }
                }

                if (response.Conflicts != null && response.Conflicts.Count > 0)
                {
                    ConflictsEventArgs ceArgs = new ConflictsEventArgs(response.Conflicts);
                    this.OnConflictOccur(ceArgs);
                    if (!ceArgs.CancelResolvingConflicts)
                    {
                        foreach (var conflict in response.Conflicts)
                        {
                            if (conflict is SyncError)
                            {
                                continue;
                            }
                            var offlineEntity = (SiaqodbOfflineEntity)conflict.LiveEntity;
                            offlineEntity.IsDirty     = false;
                            offlineEntity.IsTombstone = false;
                            this.SaveEntity(offlineEntity);
                        }
                    }
                }

                ICollection <IOfflineEntity> changesJustUploaded = this.currentChanges[state];
                foreach (IOfflineEntity enI in changesJustUploaded)
                {
                    SiaqodbOfflineEntity en = enI as SiaqodbOfflineEntity;
                    //check if we did not updated above
                    if (null != response.UpdatedItems && 0 != response.UpdatedItems.Count)
                    {
                        bool existsUpdated = false;
                        foreach (var item in response.UpdatedItems)
                        {
                            var offlineEntity = (SiaqodbOfflineEntity)item;
                            if (EntitiesEqualByPK(offlineEntity, en))
                            {
                                existsUpdated = true;
                            }
                        }
                        if (existsUpdated)
                        {
                            continue;
                        }
                    }
                    if (response.Conflicts != null && response.Conflicts.Count > 0)
                    {
                        bool existsUpdated = false;
                        foreach (var conflict in response.Conflicts)
                        {
                            var offlineEntity = (SiaqodbOfflineEntity)conflict.LiveEntity;
                            if (EntitiesEqualByPK(offlineEntity, en))
                            {
                                existsUpdated = true;
                            }
                        }
                        if (existsUpdated)
                        {
                            continue;
                        }
                    }

                    if (en.IsTombstone)
                    {
                        en.IsDirty     = false;
                        en.IsTombstone = false;
                        //reset flags first
                        siaqodb.StoreObjectBase(en);

                        siaqodb.DeleteBase(en);
                    }
                    else
                    {
                        en.IsDirty = false;
                        siaqodb.StoreObjectBase(en);
                    }
                }
            }
            finally
            {
                siaqodb.EndBulkInsert(CacheController.ControllerBehavior.KnownTypes.ToArray());
                siaqodb.Flush();
            }

            this.SaveAnchor(response.ServerBlob);

            this.OnSyncProgress(new SyncProgressEventArgs("Downloading changes from server..."));
            currentChanges.Remove(state);
        }