// public async Task<List<Customer>> GetAllCustomers()
        // {

        //    // var r1 = await GetObjects<Address>("NetmeraUser");

        //     return await GetObjects<Customer>("Customers");

        //}


        public async Task <ApiResponse <List <Surveyor> > > Login(string login, string password)
        {
            NetmeraService service = new NetmeraService("Surveyors");

            service.whereEqual("Username", login);
            service.whereEqual("Password", Helpers.Sha1(password));


            var surveyors = await SearchObjects <Surveyor>(service);

            if (surveyors.IsSuccess && surveyors.Data.Any())
            {
                return(surveyors);
            }
            else
            {
                if (string.IsNullOrEmpty(surveyors.Message))
                {
                    surveyors.Message = "Wrong username or password";
                }

                surveyors.IsSuccess = false;

                return(surveyors);
            }
        }
Example #2
0
        static NetmeraContent FindNetmeraContent(string table, string idField, object idValue)
        {
            NetmeraService service = new NetmeraService(table);

            service.whereEqual(idField, idValue);

            var items = SearchNetmeraContent(service);

            if (items.IsSuccess)
            {
                return(items.Data);
            }

            return(null);
        }
        void ImportSurveyoyr(string surveyorId, SqlConnection connection)
        {
            var addressService = new NetmeraService("Address_List1");

            addressService.whereEqual("Surveyor", surveyorId);

            var surveyor = DbContext.Surveyors.First(x => x.NetmeraId == surveyorId);


            SearchObjectsWithPaging <Address, AddressOld>(addressService, (db, s) =>
            {
                db.NetmeraId   = s.AddressID;
                db.SurveyorId  = surveyor.Id;
                db.TypeUpdated = s.PTUpdated;
                db.IsCompleted = s.Complete;
                db.UpdateDate  = db.CreateDate;

                var status         = new AddressStatus();
                status.IsCompleted = s.Complete;
                status.AddressId   = db.Id;

                return(false);
            });


            var downloadedAddresses = DbContext.Addresses.Where(x => x.SurveyorId == surveyor.Id).ToList();
            var page = 100;

            for (int i = 0; i < downloadedAddresses.Count; i = i + page)
            {
                var addresses = downloadedAddresses.Skip(i).Take(page);

                var service = new NetmeraService("Survelem");

                service.whereContainedIn("UPRN", addresses.Select(x => x.UPRN).ToList());

                ProcessSurvelems(service, connection);
            }
        }
        //public void GetAddresses()
        //{

        //}

        //public void GetQuestions()
        //{

        //}

        public async Task <List <Customer> > GetCustomers(long userId)
        {
            var service1 = new NetmeraService("Customers");

            service1.whereEqual("Completed", "False");

            var customers = await SearchObjects <Customer>(service1);

            var foundCustomers = new List <Customer>();

            if (customers.IsSuccess)
            {
                foreach (var customer in customers.Data.Where(x => !x.Completed))
                {
                    var ids = customer.Surveyors.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    if (ids.Contains(userId.ToString()))
                    {
                        foundCustomers.Add(customer);
                    }
                }
            }
            return(foundCustomers);
        }
        async Task <ApiResponse <Address> > ExecuteSyncUserData(long userId)
        {
            var surveyTypes = await SearchObjects <SurveyTypes>(new NetmeraService("SurveyTypes"));

            if (surveyTypes.IsSuccess)
            {
                await new DbService().ClearTable <SurveyTypes>();

                foreach (var surveyTypese in surveyTypes.Data.Distinct())
                {
                    surveyTypese.Identity = surveyTypese.ID;
                    await new DbService().Save(surveyTypese);
                }
            }
            else
            {
                return(new ApiResponse <Address>()
                {
                    IsSuccess = false
                });
            }


            var foundCustomers = await GetCustomers(userId);

            foreach (var foundCustomer in foundCustomers)
            {
                foundCustomer.Identity = foundCustomer.CustomerID.ToString();
                await new DbService().Save(foundCustomer);
            }

            NetmeraService service2 = new NetmeraService("Address_List1");

            service2.whereEqual("Surveyor", userId.ToString());
            service2.whereEqual("Complete", false);


            //var addresses = await SearchObjects<Address>(service2);
            var addresses = await SearchObjectsWithPaging <Address>(service2);

            var downloadedAddresses = new List <Address>();

            if (addresses.IsSuccess)
            {
                var insert = new List <Address>();
                foreach (var address in addresses.Data)
                {
                    if (foundCustomers.Any(x => x.CustomerSurveyID.ToString() == address.CustomerSurveyID.ToString()))
                    {
                        address.Identity = address.AddressID;

                        if (string.IsNullOrEmpty(address.FullAddress))
                        {
                            address.FullAddress = "";
                        }

                        if (!insert.Any(x => x.Identity == address.Identity))
                        {
                            insert.Add(address);
                            downloadedAddresses.Add(address);
                        }
                    }
                }

                await new DbService().Insert(insert);
            }
            else
            {
                return(new ApiResponse <Address>()
                {
                    IsSuccess = false
                });
            }


            var r = await DownloadQuestionsAndOptions(foundCustomers);

            if (!r.IsSuccess)
            {
                return(r);
            }
            //var questions = await SearchObjects<Question>(new NetmeraService("Questions"));
            //var questions = await SearchObjectsWithPaging<Question>(new NetmeraService("Questions"));

            //if (questions.IsSuccess)
            //{
            //    var insert = new List<Question>();
            //    foreach (var question in questions.Data)
            //    {
            //        if (foundCustomers.Any(x => x.CustomerSurveyID == question.CustomerSurveyID))
            //        {
            //            question.Identity = question.Question_Ref;
            //            if (!insert.Any(x => x.Identity == question.Identity))
            //                insert.Add(question);

            //        }
            //    }

            //    await new DbService().Insert(insert);
            //}
            //else
            //{
            //    return new ApiResponse<Address>() { IsSuccess = false };
            //}

            //var options = await SearchObjectsWithPaging<Option>(new NetmeraService("Options"));
            ////var options = await SearchObjects<Option>(new NetmeraService("Options"));

            //if (options.IsSuccess)
            //{
            //    var insert = new List<Option>();
            //    foreach (var option in options.Data)
            //    {
            //        if (foundCustomers.Any(x => x.CustomerSurveyID == option.CustomerSurveyID))
            //        {
            //            option.Identity = option.OptionId;
            //            if (!insert.Any(x => x.Identity == option.Identity))
            //                insert.Add(option);


            //        }
            //    }
            //    await new DbService().Insert(insert);
            //}
            //else
            //{
            //    return new ApiResponse<Address>() { IsSuccess = false };
            //}



            var media = await SearchObjects <RichMedia>(new NetmeraService("RichMedia"));

            if (media.IsSuccess)
            {
                var insert = new List <RichMedia>();
                foreach (var option in media.Data)
                {
                    if (foundCustomers.Any(x => x.CustomerSurveyID == option.CustomerSurveyID))
                    {
                        option.Identity = option.ID;
                        if (!insert.Any(x => x.Identity == option.Identity))
                        {
                            insert.Add(option);
                        }
                    }
                }
                await new DbService().Insert(insert);
            }
            else
            {
                return(new ApiResponse <Address>()
                {
                    IsSuccess = false
                });
            }

            var page = 100;

            for (int i = 0; i < downloadedAddresses.Count; i = i + page)
            {
                var survelemSearch = new NetmeraService("Survelem");

                survelemSearch.whereContainedIn("UPRN", downloadedAddresses.Skip(i).Take(page).Select(x => x.UPRN).ToList());

                //var survelems = await SearchObjects<Survelem>(survelemSearch);
                var survelems = await SearchObjectsWithPaging <Survelem>(survelemSearch);

                if (survelems.IsSuccess)
                {
                    var insert = new List <Survelem>();
                    foreach (var option in survelems.Data)
                    {
                        if (foundCustomers.Any(x => x.CustomerSurveyID == option.CustomerSurveyID))
                        {
                            option.Identity = option.id;
                            if (!insert.Any(x => x.Identity == option.Identity))
                            {
                                insert.Add(option);
                            }
                        }
                    }
                    await new DbService().Insert(insert);
                }
                else
                {
                    return(new ApiResponse <Address>()
                    {
                        IsSuccess = false
                    });
                }
            }



            var survelemMaps = await SearchObjects <SurvelemMap>(new NetmeraService("TblSurvelemMap"));

            if (survelemMaps.IsSuccess)
            {
                var insert = new List <SurvelemMap>();
                foreach (var option in survelemMaps.Data)
                {
                    if (foundCustomers.Any(x => x.CustomerSurveyID == option.CustomerSurveyID))
                    {
                        option.Identity = option.SvmMapID;
                        if (!insert.Any(x => x.Identity == option.Identity))
                        {
                            insert.Add(option);
                        }
                    }
                }
                await new DbService().Insert(insert);
            }
            else
            {
                return(new ApiResponse <Address>()
                {
                    IsSuccess = false
                });
            }

            return(new ApiResponse <Address>()
            {
                IsSuccess = true
            });
        }
        public async Task <ApiResponse <bool> > BatchSave(string table,
                                                          List <Dictionary <string, string> > entities,
                                                          List <Survelem> items,
                                                          bool create)
        {
            var paramsJson = new JObject();

            //foreach (var entity in entities)
            //{
            //    var contentJSon = new JObject();
            //    foreach (var e in entity.Keys)
            //    {
            //        contentJSon.Add(new JProperty(e, entity[e]));
            //    }

            //    contentJArray.Add(new Tuple<string, JObject>(entity["id"], contentJSon));
            //}
            bool      error     = false;
            Exception exception = null;
            var       j         = 0;

            foreach (var e in entities.Batch(BULK_QANTITY))
            {
                var ss = ConvertToJObjects(e.ToList());

                var s = ss.ToList();

                var netmeraTable = new NetmeraContent(table);
                var bodyJArray   = new JArray();
                int i            = 0;
                foreach (var sik in s)
                {
                    j++;
                    if (create)
                    {
                        var o = sik.Item2;
                        o.Add("netmera-mobimera:api-content-type", "Survelem");

                        bodyJArray.Add(o);
                    }
                    else
                    {
                        var query      = new JObject();
                        var queryInner = new JObject();
                        queryInner.Add(new JProperty("netmera-mobimera:api-content-type", "Survelem"));
                        queryInner.Add(new JProperty("id", sik.Item1));

                        query.Add(new JProperty("data", sik.Item2));
                        query.Add(new JProperty("query", queryInner));
                        bodyJArray.Add(query);
                        Debug.WriteLine("Survelem added in the batch :" + sik.Item1);
                    }
                    i++;
                }
                if (create)
                {
                    paramsJson.Add("content", bodyJArray);
                    netmeraTable.add("params", paramsJson);
                }
                else
                {
                    netmeraTable.add("content", bodyJArray);
                }

                netmeraTable.add("method", create ? "content.createBulkContentWithoutActionToken" : "content.queryAndUpdateBulkContentWithoutActionToken");
                netmeraTable.add("st", App.KEY);

                var t = new TaskCompletionSource <object>();


                Action <JObject, Exception> a = async(JObject o, Exception ex) =>
                {
                    paramsJson.RemoveAll();

                    foreach (var tuple in s)
                    {
                        var item = items.FirstOrDefault(x => x.id == tuple.Item1);
                        if (item != null)
                        {
                            Debug.WriteLine("Does Survelem exists in Netmera id:" + item.id);
                            NetmeraService service = new NetmeraService("Survelem");
                            service.whereEqual("id", item.id);

                            var survelem = await SearchObjects <Survelem>(service);

                            if (survelem.IsSuccess && survelem.Data.Any())
                            {
                                Debug.WriteLine("Survelem is in Netmera :" + item.id);
                                item.IsCreatedOnClient = false;
                                await
                                new DbService().Save(item, ex == null ? ESyncStatus.Success : ESyncStatus.Error, ex, true);
                            }
                            else
                            {
                                var retryInsert = new NetmeraContent("Survelem");

                                retryInsert.add("COMMENT", item.COMMENT);
                                retryInsert.add("CustomerID", item.CustomerID.ToString());
                                retryInsert.add("CustomerSurveyID", item.CustomerSurveyID);
                                retryInsert.add("OptionID", item.OptionID ?? "");
                                retryInsert.add("OptionID2ndry", item.OptionID2ndry ?? "");
                                retryInsert.add("Question_Ref", item.Question_Ref);
                                retryInsert.add("UPRN", item.UPRN);
                                retryInsert.add("id", item.id);
                                retryInsert.add("Freetext", item.Freetext ?? "");
                                retryInsert.add("BuildingType", item.BuildingType ?? "");
                                retryInsert.add("DateOfSurvey", item.DateOfSurvey);

                                retryInsert.add("SqN1", item.SqN1 ?? "");
                                retryInsert.add("SqN10", item.SqN10 ?? "");
                                retryInsert.add("SqN11", item.SqN11 ?? "");
                                retryInsert.add("SqN12", item.SqN12 ?? "");
                                retryInsert.add("SqN13", item.SqN13 ?? "");
                                retryInsert.add("SqN14", item.SqN14 ?? "");
                                retryInsert.add("SqN15", item.SqN15 ?? "");
                                retryInsert.add("SqN2", item.SqN2 ?? "");
                                retryInsert.add("SqN3", item.SqN3 ?? "");
                                retryInsert.add("SqN4", item.SqN4 ?? "");
                                retryInsert.add("SqN5", item.SqN5 ?? "");
                                retryInsert.add("SqN6", item.SqN6 ?? "");
                                retryInsert.add("SqN7", item.SqN7 ?? "");
                                retryInsert.add("SqN8", item.SqN8 ?? "");
                                retryInsert.add("SqN9", item.SqN9 ?? "");
                                retryInsert.add("SqT1", item.SqT1 ?? "");
                                retryInsert.add("SqT10", item.SqT10 ?? "");
                                retryInsert.add("SqT11", item.SqT11 ?? "");
                                retryInsert.add("SqT12", item.SqT12 ?? "");
                                retryInsert.add("SqT13", item.SqT13 ?? "");
                                retryInsert.add("SqT14", item.SqT14 ?? "");
                                retryInsert.add("SqT15", item.SqT15 ?? "");
                                retryInsert.add("SqT2", item.SqT2 ?? "");
                                retryInsert.add("SqT3", item.SqT3 ?? "");
                                retryInsert.add("SqT4", item.SqT4 ?? "");
                                retryInsert.add("SqT5", item.SqT5 ?? "");
                                retryInsert.add("SqT6", item.SqT6 ?? "");
                                retryInsert.add("SqT7", item.SqT7 ?? "");
                                retryInsert.add("SqT8", item.SqT8 ?? "");
                                retryInsert.add("SqT9", item.SqT9 ?? "");



                                var r = await new MyNetmeraClient().Create <Survelem>(retryInsert, item.id);

                                if (r.IsSuccess)
                                {
                                    await new DbService().Save(item, ESyncStatus.Success, null, true);
                                }
                                else
                                {
                                    await new DbService().Save(item, ESyncStatus.Error, r.Exception, true);
                                }

                                Debug.WriteLine("Survelem Record not found, id is: " + item.id);
                                //item.IsCreatedOnClient = false;
                                //await new DbService().Save(item, ESyncStatus.Error, ex, true);
                            }
                        }
                    }

                    if (ex == null)
                    {
                        /////
                    }
                    else
                    {
                        //error = true;
                        exception = ex;
                        Debug.WriteLine("BatchSave: " + ex.Message);
                    }

                    t.SetResult(null);
                };

                //try
                //{

                if (create)
                {
                    netmeraTable.bulkCreate(async(o, ex) =>
                    {
                        a(o, ex);
                    });
                }
                else
                {
                    netmeraTable.bulkUpdate(async(o, ex) =>
                    {
                        a(o, ex);
                    });
                }


                await t.Task;

                if (error)
                {
                    return(new ApiResponse <bool>()
                    {
                        Data = false, Exception = exception
                    });
                }
            }

            return(new ApiResponse <bool> {
                Data = true
            });
        }