Beispiel #1
0
        static ApiResponse <NetmeraContent> SearchNetmeraContent(NetmeraService service)
        {
            service.setMax(1);

            try
            {
                var list = service.search();

                if (list.Any())
                {
                    return(new ApiResponse <NetmeraContent>()
                    {
                        Data = list[0], IsSuccess = true
                    });
                }

                return(new ApiResponse <NetmeraContent>()
                {
                    IsSuccess = false, Message = "Object was not found"
                });
            }
            catch (Exception tpsEx)
            {
                Console.WriteLine("Search error: " + tpsEx.Message);
                return(new ApiResponse <NetmeraContent>()
                {
                    IsSuccess = false, Message = "Object was not found"
                });
            }
        }
        async Task <ApiResponse <NetmeraContent> > SearchNetmeraContent(NetmeraService service)
        {
            TaskCompletionSource <ApiResponse <NetmeraContent> > _task = new TaskCompletionSource <ApiResponse <NetmeraContent> >();

            service.setMax(1);
            service.search((list, exception) =>
            {
                if (exception != null)
                {
                    _task.SetResult(new ApiResponse <NetmeraContent>()
                    {
                        IsSuccess = false, Message = "Error while processing request. Try again", Exception = exception
                    });
                }
                else
                {
                    if (list.Any())
                    {
                        _task.SetResult(new ApiResponse <NetmeraContent>()
                        {
                            Data = list[0], IsSuccess = true
                        });
                    }
                    else
                    {
                        _task.SetResult(new ApiResponse <NetmeraContent>()
                        {
                            IsSuccess = false, Message = "Object was not found"
                        });
                    }
                }
            });

            return(await _task.Task);
        }
        async Task <ApiResponse <List <T> > > SearchObjects <T>(NetmeraService service)
        {
            TaskCompletionSource <ApiResponse <List <T> > > _task = new TaskCompletionSource <ApiResponse <List <T> > >();

            service.setMax(5000);

            service.search((list, exception) =>
            {
                if (exception != null)
                {
                    _task.SetResult(new ApiResponse <List <T> >()
                    {
                        IsSuccess = false, Message = "Error while processing request. Try again", Exception = exception
                    });
                }
                else
                {
                    var objects = new List <T>();

                    foreach (var netmeraContent in list)
                    {
                        var o = JsonConvert.DeserializeObject <T>(netmeraContent.data.ToString());

                        objects.Add(o);
                    }

                    _task.SetResult(new ApiResponse <List <T> >()
                    {
                        Data = objects, IsSuccess = true
                    });
                }
            });

            return(await _task.Task);
        }
        protected override BaseReply Execute(ImportSurveyorDataRequest request)
        {
            NetmeraClient.init(ImportRequestHandler.KEY);

            _options = DbContext.Options.ToList();

            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                var reply = new BaseReply();


                if (string.IsNullOrEmpty(request.NetmeraId))
                {
                    var netmeraService = new NetmeraService("Surveyors");
                    netmeraService.setMax(100);

                    var surveyors = netmeraService.search();

                    foreach (var s in surveyors)
                    {
                        var o = JsonConvert.DeserializeObject <SurveyorOld>(s.data.ToString());


                        ImportSurveyoyr(o.id.ToString(), connection);
                    }
                }
                else
                {
                    ImportSurveyoyr(request.NetmeraId, connection);
                }



                return(reply);
            }
        }
Beispiel #5
0
        protected void SearchObjectsWithPaging <T, V>(NetmeraService service, Func <T, V, bool> action = null, Action <List <V> > saveAction = null)
            where T : Entity, new()
            where V : class
        {
            var max  = -1;
            var page = 1000;

            if (typeof(V) == typeof(AddressOld))
            {
                service.setSortBy("UPRN");
            }
            else if (typeof(V) == typeof(SurvelemOld))
            {
                service.setSortBy("id");
            }
            else if (typeof(V) == typeof(QuestionOld))
            {
                service.setSortBy("Question_Order");
            }
            else
            {
                service.setSortBy("Path");
            }

            service.setSortOrder(NetmeraService.SortOrder.ascending);

            var c           = service.count();
            var loadedCount = 0;
            var pages       = c / page;

            for (int i = 0; i <= pages; i++)
            {
                service.setPage(i);
                service.setMax(page);
                var objects = new List <V>();
                var list    = service.search();
                foreach (var netmeraContent in list)
                {
                    var o = JsonConvert.DeserializeObject <V>(netmeraContent.data.ToString());

                    if (typeof(V) == typeof(AddressOld))
                    {
                        (o as AddressOld).UpdateDate = netmeraContent.getUpdateDate();
                    }

                    objects.Add(o);
                }

                if (saveAction == null)
                {
                    Dictionary <Guid, V> oldData = new Dictionary <Guid, V>();

                    var entities = objects.Select(x =>
                    {
                        var s = new T();

                        var r = action(s, x as V);

                        if (!r)
                        {
                            s.InjectFrom(x);
                        }

                        if (!oldData.ContainsKey(s.Id))
                        {
                            oldData.Add(s.Id, x);
                        }

                        return(s);
                    }).ToList();

                    foreach (var entity in entities)
                    {
                        if (true)
                        {
                            if (DbContext.Set <T>().Any(x => x.NetmeraId == entity.NetmeraId))
                            {
                                continue;
                            }
                        }

                        DbContext.Set <T>().Add(entity);
                    }
                    DbContext.SaveChanges();


                    loadedCount += objects.Count;
                }
                else
                {
                    saveAction(objects);

                    loadedCount += objects.Count;
                }



                if (max != -1)
                {
                    if (loadedCount > max)
                    {
                        break;
                    }
                }
            }
        }
        async Task <ApiResponse <List <T> > > SearchObjectsWithPaging <T>(NetmeraService service)
        {
            object locker = new object();
            TaskCompletionSource <ApiResponse <List <T> > > _task = new TaskCompletionSource <ApiResponse <List <T> > >();
            var page = 100;

            //service.setMax(page);
            if (typeof(T) == typeof(Models.Address))
            {
                service.setSortBy("UPRN");
            }
            else if (typeof(T) == typeof(Models.Survelem))
            {
                service.setSortBy("id");
            }
            else if (typeof(T) == typeof(Models.Question))
            {
                service.setSortBy("Question_Order");
            }
            else
            {
                service.setSortBy("Path");
            }

            service.setSortOrder(NetmeraService.SortOrder.ascending);
            service.count((c, e) =>
            {
                if (e != null)
                {
                    _task.SetResult(new ApiResponse <List <T> >()
                    {
                        IsSuccess = false, Message = "Error while processing request. Try again", Exception = e
                    });
                    return;
                }
                else
                {
                    if (c == 0)
                    {
                        _task.SetResult(new ApiResponse <List <T> >()
                        {
                            Data = new List <T>(), IsSuccess = true
                        });
                        return;
                    }
                    else
                    {
                        bool setResult = false;
                        int completed  = -1;
                        var pages      = c / page;
                        var objects    = new List <T>();
                        for (int i = 0; i <= pages; i++)
                        {
                            service.setPage(i);
                            service.setMax(page);
                            service.search((list, exception) =>
                            {
                                if (exception != null)
                                {
                                    lock (locker)
                                    {
                                        if (!setResult)
                                        {
                                            setResult = true;
                                            _task.SetResult(new ApiResponse <List <T> >()
                                            {
                                                IsSuccess = false,
                                                Message   = "Error while processing request. Try again",
                                                Exception = exception
                                            });
                                        }
                                    }
                                }
                                else
                                {
                                    lock (locker)
                                    {
                                        foreach (var netmeraContent in list)
                                        {
                                            var o = JsonConvert.DeserializeObject <T>(netmeraContent.data.ToString());

                                            objects.Add(o);
                                        }

                                        completed++;

                                        if (completed == pages)
                                        {
                                            setResult = true;
                                            _task.SetResult(new ApiResponse <List <T> >()
                                            {
                                                Data = objects, IsSuccess = true
                                            });
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
            });



            return(await _task.Task);
        }