private void UpgradeTypeIdHash <T>()
        {
            hash = typeof(T).FullName.GetHashCode();
            var tvers = this.siaqodb.Query <SyncProviderTypeVersion>().Where <SyncProviderTypeVersion>(x => x.TypeNameHash == hash).FirstOrDefault();

            if (tvers == null)
            {
                SyncProviderTypeVersion vers = new SyncProviderTypeVersion();
                vers.Version      = 12;
                vers.TypeNameHash = hash;
                siaqodb.StoreObject(vers);


                IList <T> objs = siaqodb.LoadAll <T>();
                foreach (T obj in objs)
                {
                    SiaqodbOfflineEntity en = obj as SiaqodbOfflineEntity;
                    if (en != null)
                    {
                        if (en._idMeta2 != null && en._idMeta2 != string.Empty)
                        {
                            if (en._idMetaHash == 0)
                            {
                                en._idMetaHash = en._idMeta2.GetHashCode();
                                siaqodb.StoreObject(obj);
                            }
                        }
                    }
                }
            }
        }
        internal void SaveEntityByPK(SiaqodbOfflineEntity en)
        {
            List <string> primaryKeys = new List <string>();

            PropertyInfo[] pi = en.GetType().GetProperties(flags);
            foreach (PropertyInfo p in pi)
            {
#if SILVERLIGHT
                Type ty = typeof(Microsoft.Synchronization.ClientServices.KeyAttribute);
#else
                Type ty = typeof(KeyAttribute);
#endif
                object[] pk = p.GetCustomAttributes(ty, false);

                if (pk.Length > 0)
                {
                    primaryKeys.Add(Sqo.Utilities.ExternalMetaHelper.GetBackingField(p));
                }
            }

            if (primaryKeys.Count > 0)
            {
                if (en.IsTombstone)
                {
                    siaqodb.DeleteObjectByBase(en, primaryKeys.ToArray());
                }
                else
                {
                    bool updated = siaqodb.UpdateObjectByBase(en, primaryKeys.ToArray());
                }
            }
        }
Beispiel #3
0
        public new void StoreObject(object obj, ITransaction transaction)
        {
            lock (_locker)
            {
                SiaqodbOfflineEntity entity = obj as SiaqodbOfflineEntity;
                if (entity == null)
                {
                    throw new Exception("Entity should be SiaqodbOfflineEntity type");
                }
                entity.IsDirty = true;

                base.StoreObject(obj, transaction);
            }
        }
 internal void SaveEntity(SiaqodbOfflineEntity en)
 {
     if (en.IsTombstone)
     {
         siaqodb.DeleteObjectByBase("_idMetaHash", en);
     }
     else
     {
         bool updated = siaqodb.UpdateObjectByBase("_idMetaHash", en);
         if (!updated) //insert
         {
             siaqodb.StoreObjectBase(en);
         }
     }
 }
        private bool EntitiesEqualByPK(SiaqodbOfflineEntity a, SiaqodbOfflineEntity b)
        {
            if (a.GetType() == b.GetType())//has same type, eq: Customer
            {
                List <PropertyInfo> piPK = new List <PropertyInfo>();
                PropertyInfo[]      pi   = a.GetType().GetProperties(flags);
                foreach (PropertyInfo p in pi)
                {
#if SILVERLIGHT
                    Type ty = typeof(Microsoft.Synchronization.ClientServices.KeyAttribute);
#else
                    Type ty = typeof(KeyAttribute);
#endif
                    object[] pk = p.GetCustomAttributes(ty, false);

                    if (pk.Length > 0)
                    {
                        piPK.Add(p);
                    }
                }

                if (piPK.Count > 0)
                {
                    foreach (PropertyInfo pk in piPK)
                    {
                        var valA = pk.GetValue(a);
                        var valB = pk.GetValue(b);
                        if (valA == null || valB == null)
                        {
                            if (valA != valB)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            int vomp = ((IComparable)valA).CompareTo((IComparable)valB);
                            if (vomp != 0)
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
            }
            return(false);
        }
Beispiel #6
0
        public new void Delete(object obj)
        {
            lock (_locker)
            {
                SiaqodbOfflineEntity entity = obj as SiaqodbOfflineEntity;
                if (entity == null)
                {
                    throw new Exception("Entity should be SqoOfflineEntity type");
                }
                entity.IsDirty     = true;
                entity.IsTombstone = true;
                base.StoreObject(obj);

                base.Delete(obj);
            }
        }
Beispiel #7
0
 public new bool UpdateObjectBy(object obj, ITransaction transaction, params string[] fieldNames)
 {
     lock (_locker)
     {
         SiaqodbOfflineEntity entity = obj as SiaqodbOfflineEntity;
         if (entity == null)
         {
             throw new Exception("Entity should be SqoOfflineEntity type");
         }
         entity.IsDirty = true;
         bool updated = base.UpdateObjectBy(obj, transaction, fieldNames);
         if (!updated)
         {
             entity.IsDirty = false;
         }
         return(updated);
     }
 }
Beispiel #8
0
 public new bool UpdateObjectBy(string fieldName, object obj)
 {
     lock (_locker)
     {
         SiaqodbOfflineEntity entity = obj as SiaqodbOfflineEntity;
         if (entity == null)
         {
             throw new Exception("Entity should be SqoOfflineEntity type");
         }
         entity.IsDirty = true;
         bool updated = base.UpdateObjectBy(fieldName, obj);
         if (!updated)
         {
             entity.IsDirty = false;
         }
         return(updated);
     }
 }
Beispiel #9
0
        public new bool DeleteObjectBy(string fieldName, object obj)
        {
            lock (_locker)
            {
                SiaqodbOfflineEntity entity = obj as SiaqodbOfflineEntity;
                if (entity == null)
                {
                    throw new Exception("Entity should be SqoOfflineEntity type");
                }
                entity.IsDirty     = true;
                entity.IsTombstone = true;

                base.StoreObject(obj);

                bool deleted = base.DeleteObjectBy(fieldName, obj);
                if (!deleted)
                {
                    entity.IsDirty     = false;
                    entity.IsTombstone = false;
                    base.StoreObject(obj);
                }
                return(deleted);
            }
        }
        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);
        }