public async Task <long> Count <T>(NetmeraService service)
        {
            var task = new TaskCompletionSource <long>();

            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.setMax(1);
            service.count((c, e) =>
            {
                if (e != null)
                {
                    task.SetResult(-1);
                }
                else
                {
                    task.SetResult(c);
                }
            });

            return(await task.Task);
        }
Beispiel #2
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);
        }