示例#1
0
        public static void Update(string ObjectName, ISyncSource SourceUtils, ISyncStore StoreUtils)
        {
            SyncObject sourceObject = new SyncObject();

            if (!SourceUtils.GetObject(ObjectName, sourceObject))
            {
                throw new NullReferenceException("目标数据源无此对象:" + ObjectName);
            }
            SyncObject storeObject = new SyncObject();

            if (!StoreUtils.GetObject(ObjectName, storeObject))
            {
                storeObject.ObjectName    = ObjectName;
                storeObject.ObjectVersion = "";
            }

            if (sourceObject.ObjectVersion == storeObject.ObjectVersion)
            {
                //比对版本,一致则退出
                return;
            }
            //加载本地数据信息
            List <SyncDataOP> storeDataList = new List <SyncDataOP>();

            StoreUtils.GetObjectDatas(ObjectName, storeDataList);

            List <SyncData> sourceDataList = new List <SyncData>();

            SourceUtils.GetDataList(ObjectName, sourceDataList);

            //遍历数据源索引
            foreach (var item in sourceDataList)
            {
                //本地仓储无数据则条件索引
                if (!storeDataList.Exists((SyncDataOP s) => s.Key == item.Key))
                {
                    SyncDataOP dop = new SyncDataOP();
                    dop.Key     = item.Key;
                    dop.Json    = item.Json;
                    dop.Version = item.Version;
                    dop.OP      = 1;
                    storeDataList.Add(dop);
                }
                else
                {
                    //已有不同则更新
                    if (storeDataList.Find((SyncDataOP s) => s.Key == item.Key).Version != item.Version)
                    {
                        storeDataList.Find((SyncDataOP s) => s.Key == item.Key).Version = item.Version;
                        storeDataList.Find((SyncDataOP s) => s.Key == item.Key).Json    = item.Json;
                        storeDataList.Find((SyncDataOP s) => s.Key == item.Key).OP      = 2;
                    }
                }
            }
            //判断本地仓储是否有删除的
            foreach (var item in storeDataList)
            {
                if (!sourceDataList.Exists((SyncData s) => s.Key == item.Key))
                {
                    item.OP = 3;
                }
            }

            //保存本地存储索引信息
            foreach (var item in storeDataList)
            {
                SyncData d = new SyncData();
                d.Key     = item.Key;
                d.Json    = item.Json;
                d.Version = item.Version;
                if (item.OP == 0)
                {
                    continue;
                }
                if (item.OP == 1)
                {
                    StoreUtils.AddData(storeObject.ObjectName, d);
                }
                if (item.OP == 2)
                {
                    StoreUtils.UpdateData(storeObject.ObjectName, d);
                }
                if (item.OP == 3)
                {
                    StoreUtils.DeleteData(storeObject.ObjectName, d);
                }
                StoreUtils.AddUserIndex(storeObject, d, item.OP);
            }
            //保存本地对象版本信息
            if (storeObject.ObjectVersion == "")
            {
                storeObject.ObjectVersion = sourceObject.ObjectVersion;
                StoreUtils.AddObject(storeObject);
            }
            else
            {
                storeObject.ObjectVersion = sourceObject.ObjectVersion;
                StoreUtils.UpdateObject(storeObject);
            }
        }
 public ReflectionQueryRepository(IWebApiConfiguration webApiConfiguration, ISyncStore syncStore, string collectionName) : base(webApiConfiguration, collectionName)
 {
     this.syncStore = syncStore;
 }
示例#3
0
 public ReflectionStore(IWebApiConfiguration webApiConfiguration, ISyncStore syncStore)
 {
     this.webApiConfiguration = webApiConfiguration;
     this.query   = new ReflectionQueryRepository(webApiConfiguration, syncStore, webApiConfiguration.MongoReflectionCollection);
     this.command = new ReflectionCommandRepository(webApiConfiguration, webApiConfiguration.MongoReflectionCollection);
 }
示例#4
0
 public EventHandler(ISvcRepositoryStore repositoryStore, IMirrorStore mirrorStore, ISyncStore syncStore)
 {
     this.repositoryStore = repositoryStore;
     this.mirrorStore     = mirrorStore;
     this.syncStore       = syncStore;
 }
示例#5
0
        public SyncModule(IAuthenticationProvider authenticationProvider, ISyncStore store, IReflectionStore reflectionStore, ILogStore log)
            : base(authenticationProvider, "/sync")
        {
            this.Post["/"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <SynchronizationDto>();

                    var sync = store.Add(dto);
                    return(this.Response.AsJson(sync));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    DynamicDictionary           q       = this.Request.Query;
                    Dictionary <string, string> filters = q.ToDictionary().ToDictionary(t => t.Key, t => t.Value.ToString());

                    var result = store.GetAll(filters);

                    return(this.Response.AsJson(result));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    var    dto  = store.GetById(id);

                    return(this.Response.AsJson(dto));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}/reflections"] = parameters =>
            {
                try
                {
                    var    user   = this.CurrentUser;
                    string syncId = parameters.id;
                    var    sync   = store.GetById(syncId);
                    int    take;
                    var    reflections = Int32.TryParse(this.Request.Query["take"].ToString(), out take)
                                              ? reflectionStore.GetBySynchronization(sync, take)
                                              : reflectionStore.GetBySynchronization(sync);

                    return(this.Response.AsJson(reflections));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Put["/{id}/status"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <UpdateStatusDto>();

                    if (dto != null && dto.Status.HasValue)
                    {
                        try
                        {
                            string id = parameters.id;
                            store.UpdateStatus(id, dto.Status.Value);
                            return(HttpStatusCode.OK);
                        }
                        catch (StatusNotChangedException)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase(ReasonPhrases.StatusAlreadyUpdated)
                                   .WithStatusCode(HttpStatusCode.Conflict));
                        }
                    }

                    else
                    {
                        return(this.Negotiate
                               .WithReasonPhrase(ReasonPhrases.UpdateStatusBadRequest)
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Put["{id}/logs"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <SyncLogDto>();

                    if (dto != null && !string.IsNullOrEmpty(dto.Text))
                    {
                        try
                        {
                            string syncId = parameters.id;
                            var    sync   = store.GetById(syncId);
                            log.LogSync(sync, dto.Text);
                            return(HttpStatusCode.OK);
                        }
                        catch (StatusNotChangedException)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase("Log bad request.")
                                   .WithStatusCode(HttpStatusCode.BadRequest));
                        }
                    }
                    else
                    {
                        return(this.Negotiate
                               .WithReasonPhrase("Log bad request.")
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["{id}/logs"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    try
                    {
                        string syncId = parameters.id;
                        var    sync   = store.GetById(syncId);

                        if (sync == null)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase("Sync not found.")
                                   .WithStatusCode(HttpStatusCode.NotFound));
                        }

                        var l = log.Get(sync);

                        if (l == null)
                        {
                            return(this.Negotiate
                                   .WithReasonPhrase("Log not found.")
                                   .WithStatusCode(HttpStatusCode.NotFound));
                        }

                        return(this.Response.AsJson(l));
                    }
                    catch (StatusNotChangedException)
                    {
                        return(this.Negotiate
                               .WithReasonPhrase("Log bad request.")
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };
        }
示例#6
0
        public MirrorModule(IAuthenticationProvider authenticationProvider, IMirrorStore mirrorStore, ISyncStore syncStore, IReflectionStore reflectionStore)
            : base(authenticationProvider, "/mirror")
        {
            this.Get["/"] = parameters =>
            {
                try
                {
                    var userId   = this.CurrentUser.Id;
                    var response = mirrorStore.GetAll();

                    if (response == null)
                    {
                        return(HttpStatusCode.NotFound);
                    }

                    return(this.Response.AsJson(response));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}"] = parameters =>
            {
                try
                {
                    var    user   = this.CurrentUser;
                    string id     = parameters.id;
                    var    mirror = mirrorStore.GetById(id);

                    return(this.Response.AsJson(mirror));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}/reflections"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    int    take;

                    var mirror = mirrorStore.GetById(id);

                    var reflections = Int32.TryParse(this.Request.Query["take"].ToString(), out take)
                                                      ? reflectionStore.GetByMirror(mirror, take)
                                                      : reflectionStore.GetByMirror(mirror);

                    return(this.Response.AsJson(reflections));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Get["/{id}/sync"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    //Thread.Sleep(1000);
                    int take;

                    var mirror = mirrorStore.GetById(id);

                    var reflections = Int32.TryParse(this.Request.Query["take"].ToString(), out take)
                                              ? syncStore.GetByMirror(mirror, take)
                                              : syncStore.GetByMirror(mirror);

                    return(this.Response.AsJson(reflections));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Post["/"] = parameters =>
            {
                try
                {
                    var user = this.CurrentUser;
                    var dto  = this.Bind <MirrorDto>();
                    if (string.IsNullOrEmpty(dto.OwnerId))
                    {
                        dto.OwnerId = user.Id;
                    }

                    var mirror = mirrorStore.Add(dto);

                    return(this.Response.AsJson(mirror));
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Put["/{id}"] = parameters =>
            {
                try
                {
                    var    user = this.CurrentUser;
                    string id   = parameters.id;
                    var    dto  = this.Bind <MirrorDto>();

                    if (string.IsNullOrEmpty(dto.SourceRepositoryId))
                    {
                        dto.SourceRepositoryId = dto.SourceRepository.Id;
                    }

                    if (string.IsNullOrEmpty(dto.TargetRepositoryId))
                    {
                        dto.TargetRepositoryId = dto.TargetRepository.Id;
                    }

                    if (string.IsNullOrEmpty(dto.OwnerId))
                    {
                        dto.OwnerId = dto.Owner.Id;
                    }

                    var mirror = mirrorStore.GetById(id);

                    if (mirror == null)
                    {
                        return(HttpStatusCode.NotFound);
                    }

                    mirrorStore.Update(id, dto);

                    return(HttpStatusCode.OK);
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };

            this.Delete["/{id}"] = parameters =>
            {
                var user = this.CurrentUser;

                string id = parameters.id;

                try
                {
                    mirrorStore.Delete(id);
                    return(HttpStatusCode.OK);
                }
                catch (Exception e)
                {
                    return(this.ResponseFromException(e));
                }
            };
        }