static void Create <T>(NetmeraContent content, string id) { try { //if ((content.data.ToString().Contains("&"))) //{ // Dictionary<string, string> addressDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(content.data.ToString()); // foreach (KeyValuePair<string, string> keyValuePair in addressDictionary) // { // if (keyValuePair.Value.Contains("&")) // content.add(keyValuePair.Key, keyValuePair.Value.Replace("&", "%26")); // } //} content.create(); var conn2 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " + "Data Source=C:\\NetmeraRMTools\\sqliteInsertLog\\insertlog.accdb"); var cmdInsert = new OleDbCommand("INSERT INTO Log (UPRN,Question_Ref,localID,netmeraPath) VALUES (@UPRN,@Question_Ref,@localID,@netmeraPath)", conn2); cmdInsert.Parameters.AddWithValue("@UPRN", content.getString("UPRN")); cmdInsert.Parameters.AddWithValue("@Question_Ref", content.getString("Question_Ref")); cmdInsert.Parameters.AddWithValue("@localID", content.getString("id")); cmdInsert.Parameters.AddWithValue("@netmeraPath", content.getPath()); conn2.Open(); cmdInsert.ExecuteNonQuery(); conn2.Close(); totalNum = totalNum + 1; } catch (Exception ex) { Console.WriteLine("Create error: " + ex.Message); } }
//public async Task<ApiResponse<List<List<string>>>> BatchUpdate(string table, List<Dictionary<string, string>> entities) //{ // var r = new List<List<string>>(); // var contentJArray = new JArray(); // 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(contentJSon); // } // foreach (IEnumerable<JToken> s in contentJArray.Children().Batch(BULK_QANTITY)) // { // var l = new List<String>(); // var netmeraTable = new NetmeraContent(table); // var bodyJArray = new JArray(); // foreach (JToken sik in s) // { // bodyJArray.Add(sik); // } // paramsJson.Add("content", bodyJArray); // netmeraTable.add("params", paramsJson); // netmeraTable.add("method", "content.updateBulkContentWithoutActionToken"); // netmeraTable.add("st", App.KEY); // var t = new TaskCompletionSource<object>(); // //try // //{ // netmeraTable.bulkUpdate((o, ex) => // { // t.SetResult(null); // paramsJson.RemoveAll(); // }); // //} // //catch (NetmeraException ex) // //{ // // if (ex.getCode().ToString() != "135") // // { MessageBox.Show(ex.getCode().ToString()); } // //} // await t.Task; // } // return new ApiResponse<List<List<string>>> { Data = r }; //} public async Task <ApiResponse <string> > Create <T>(NetmeraContent content, string id) { bool setted = false; TaskCompletionSource <ApiResponse <string> > _task = new TaskCompletionSource <ApiResponse <string> >(); try { content.create((c, ex) => { if (ex != null) { if (setted) { return; } setted = true; BugSense.BugSenseHandler.Instance.LogException(ex, "Entity", typeof(T).Name); _task.SetResult(new ApiResponse <string>() { IsSuccess = false, Message = "Error while processing request. Try again", Exception = ex, Data = id }); } else { if (setted) { return; } setted = true; _task.SetResult(new ApiResponse <string>() { IsSuccess = true, Data = id }); } }); //await Task.Delay(1000); //if (!setted) //{ // setted = true; // _task.SetResult(new ApiResponse<T>() { IsSuccess = true }); //} } catch (Exception ex) { BugSense.BugSenseHandler.Instance.LogException(ex, "Entity", typeof(T).Name); _task.SetResult(new ApiResponse <string>() { IsSuccess = false, Message = "Error while processing request. Try again", Exception = ex, Data = id }); } return(await _task.Task); }
static void ProcessSurvelemes(List <Survelem> survelems) { var requestCount = 10; for (int i = 0; i <= survelems.Count / requestCount; i++) { var items = survelems.Skip(i * requestCount).Take(requestCount).ToList(); var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " + "Data Source=C:\\NetmeraRMTools\\sqliteInsertLog\\checkedLog.accdb"); //conn.Open(); foreach (var survelem in items) { //Thread.Sleep(5); NetmeraContent e; var inNetmera = 1; conn.Open(); var cmdSelect = new OleDbCommand("Select * FROM Log WHERE localID ='" + survelem.id + "'", conn); var reader = cmdSelect.ExecuteReader(); if (!(reader.HasRows)) { e = FindNetmeraContent("Survelem", "id", survelem.id); var cmdInsert = new OleDbCommand("INSERT INTO Log (localID) VALUES (@localID)", conn); cmdInsert.Parameters.AddWithValue("@localID", survelem.id); cmdInsert.ExecuteNonQuery(); if (e == null) { inNetmera = 0; } } conn.Close(); if (inNetmera == 0) { e = new NetmeraContent("Survelem"); } else { //conn.Close(); continue; } if (e != null) { e.add("COMMENT", survelem.COMMENT); e.add("CustomerID", survelem.CustomerID); e.add("CustomerSurveyID", survelem.CustomerSurveyID); e.add("DateOfSurvey", survelem.DateOfSurvey); e.add("OptionID", survelem.OptionID ?? ""); e.add("OptionID2ndry", survelem.OptionID2ndry ?? ""); e.add("Question_Ref", survelem.Question_Ref); e.add("UPRN", survelem.UPRN); e.add("id", survelem.id); e.add("Freetext", survelem.Freetext ?? ""); e.add("BuildingType", survelem.BuildingType ?? ""); e.add("SqN1", survelem.SqN1 ?? ""); e.add("SqN10", survelem.SqN10 ?? ""); e.add("SqN11", survelem.SqN11 ?? ""); e.add("SqN12", survelem.SqN12 ?? ""); e.add("SqN13", survelem.SqN13 ?? ""); e.add("SqN14", survelem.SqN14 ?? ""); e.add("SqN15", survelem.SqN15 ?? ""); e.add("SqN2", survelem.SqN2 ?? ""); e.add("SqN3", survelem.SqN3 ?? ""); e.add("SqN4", survelem.SqN4 ?? ""); e.add("SqN5", survelem.SqN5 ?? ""); e.add("SqN6", survelem.SqN6 ?? ""); e.add("SqN7", survelem.SqN7 ?? ""); e.add("SqN8", survelem.SqN8 ?? ""); e.add("SqN9", survelem.SqN9 ?? ""); e.add("SqT1", survelem.SqT1 ?? ""); e.add("SqT10", survelem.SqT10 ?? ""); e.add("SqT11", survelem.SqT11 ?? ""); e.add("SqT12", survelem.SqT12 ?? ""); e.add("SqT13", survelem.SqT13 ?? ""); e.add("SqT14", survelem.SqT14 ?? ""); e.add("SqT15", survelem.SqT15 ?? ""); e.add("SqT2", survelem.SqT2 ?? ""); e.add("SqT3", survelem.SqT3 ?? ""); e.add("SqT4", survelem.SqT4 ?? ""); e.add("SqT5", survelem.SqT5 ?? ""); e.add("SqT6", survelem.SqT6 ?? ""); e.add("SqT7", survelem.SqT7 ?? ""); e.add("SqT8", survelem.SqT8 ?? ""); e.add("SqT9", survelem.SqT9 ?? ""); } var s = survelem; ProcessItem(s, e); } } }
static async void ProcessItem(Survelem survelem, NetmeraContent e) { Create <Survelem>(e, survelem.id); }
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 }); }
// static async Task ProcessSurvelemes() // { // var requestCount = 10; // var survelems = await new DbService().GetNotSyncedEntities<Survelem>(); // for (int i = 0; i <= survelems.Count / requestCount; i++) // { // TaskCompletionSource<object> res = new TaskCompletionSource<object>(); //; // var items = survelems.Skip(i*requestCount).Take(requestCount).ToList(); // _counter = items.Count(); // Debug.WriteLine("Processign survelems: " + _counter); // if (items.Any()) // { // foreach (var survelem in items) // { // NetmeraContent e = null; // if (survelem.IsCreatedOnClient) // { // e = new NetmeraContent("Survelem"); // } // else // { // e = await new MyNetmeraClient().FindNetmeraContent("Survelem", "id", survelem.id); // } // if (e != null) // { // e.add("COMMENT", survelem.COMMENT); // e.add("CustomerID", survelem.CustomerID); // e.add("CustomerSurveyID", survelem.CustomerSurveyID); // e.add("DateOfSurvey", survelem.DateOfSurvey); // e.add("OptionID", survelem.OptionID ?? ""); // e.add("OptionID2ndry", survelem.OptionID2ndry ?? ""); // e.add("Question_Ref", survelem.Question_Ref); // e.add("UPRN", survelem.UPRN); // e.add("id", survelem.id); // e.add("Freetext", survelem.Freetext ?? ""); // e.add("BuildingType", survelem.BuildingType ?? ""); // e.add("SqN1", survelem.SqN1 ?? ""); // e.add("SqN10", survelem.SqN10 ?? ""); // e.add("SqN11", survelem.SqN11 ?? ""); // e.add("SqN12", survelem.SqN12 ?? ""); // e.add("SqN13", survelem.SqN13 ?? ""); // e.add("SqN14", survelem.SqN14 ?? ""); // e.add("SqN15", survelem.SqN15 ?? ""); // e.add("SqN2", survelem.SqN2 ?? ""); // e.add("SqN3", survelem.SqN3 ?? ""); // e.add("SqN4", survelem.SqN4 ?? ""); // e.add("SqN5", survelem.SqN5 ?? ""); // e.add("SqN6", survelem.SqN6 ?? ""); // e.add("SqN7", survelem.SqN7 ?? ""); // e.add("SqN8", survelem.SqN8 ?? ""); // e.add("SqN9", survelem.SqN9 ?? ""); // e.add("SqT1", survelem.SqT1 ?? ""); // e.add("SqT10", survelem.SqT10 ?? ""); // e.add("SqT11", survelem.SqT11 ?? ""); // e.add("SqT12", survelem.SqT12 ?? ""); // e.add("SqT13", survelem.SqT13 ?? ""); // e.add("SqT14", survelem.SqT14 ?? ""); // e.add("SqT15", survelem.SqT15 ?? ""); // e.add("SqT2", survelem.SqT2 ?? ""); // e.add("SqT3", survelem.SqT3 ?? ""); // e.add("SqT4", survelem.SqT4 ?? ""); // e.add("SqT5", survelem.SqT5 ?? ""); // e.add("SqT6", survelem.SqT6 ?? ""); // e.add("SqT7", survelem.SqT7 ?? ""); // e.add("SqT8", survelem.SqT8 ?? ""); // e.add("SqT9", survelem.SqT9 ?? ""); // } // var s = survelem; // ProcessItem(s, e, res); // } // } // else // { // res.SetResult(null); // } // await res.Task; // } // } static async Task InternalSync() { PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled; SyncStarted(null, EventArgs.Empty); Debug.WriteLine("Sync started"); var addresses = await new DbService().GetNotSyncedEntities <Address>(); foreach (var item in addresses.Where(x => x.SyncStatus == (byte)ESyncStatus.InProcess)) { var content = await new MyNetmeraClient().FindNetmeraContent("Address_List1", "AddressID", item.AddressID); if (content != null) { item.IsCreatedOnClient = false; } } foreach (var a in addresses) { NetmeraContent e = null; if (a.IsCreatedOnClient) { e = new NetmeraContent("Address_List1"); } else { e = await new MyNetmeraClient().FindNetmeraContent("Address_List1", "AddressID", a.AddressID); } if (e != null) { e.add("Address line 1", a.AddressLine1); e.add("Address line 2", a.AddressLine2 ?? ""); e.add("Address line 3", a.AddressLine3 ?? ""); e.add("Address line 4", a.AddressLine4 ?? ""); e.add("Type", a.Type); e.add("PTUpdated", a.PTUpdated); e.add("UPRN", a.UPRN); e.add("FlatNo", a.FlatNo); e.add("Postcode", a.Postcode); e.add("StreetName", a.StreetName); e.add("StreetNo", a.StreetNo); e.add("BlockUPRN", a.BlockUPRN); e.add("BuildingName", a.BuildingName); e.add("CustomerSurveyID", a.CustomerSurveyID); e.add("CustomerID", a.CustomerID); e.add("Surveyor", a.Surveyor ?? ""); e.add("AddressID", a.AddressID); e.add("AllowCopyFrom", a.AllowCopyFrom); e.add("Bedrooms", a.Bedrooms ?? ""); e.add("Complete", a.Complete); e.add("CopiedFrom", a.CopiedFrom ?? ""); e.add("CopyTo", a.CopyTo); e.add("DateSurveyed", a.DateSurveyed ?? ""); e.add("Floor", a.Floor ?? ""); e.add("FullAddress", a.FullAddress ?? ""); e.add("LeaseHolderAddress", a.LeaseHolderAddress ?? ""); e.add("Multipliers", a.Multipliers ?? ""); e.add("QuestionGrp", a.QuestionGrp ?? ""); e.add("SAPBand", a.SAPBand ?? ""); e.add("SAPRating", a.SAPRating ?? ""); e.add("Submit", a.Submit ?? ""); e.add("Submitted", a.Submitted ?? ""); e.add("Visited", a.Visited ?? ""); } await new DbService().Save(a, ESyncStatus.InProcess, null, true); var r = a.IsCreatedOnClient ? await new MyNetmeraClient().Create <Address>(e, a.AddressID) : await new MyNetmeraClient().Update <Address>(e, a.AddressID); a.IsCreatedOnClient = false; if (r.IsSuccess) { await new DbService().Save(a, ESyncStatus.Success, null, true); } else { await new DbService().Save(a, ESyncStatus.Error, r.Exception, true); } } await ProcessSurvelemesWithBatches(); var medias = await new DbService().GetNotSyncedEntities <RichMedia>(); foreach (var item in medias.Where(x => x.SyncStatus == (byte)ESyncStatus.InProcess)) { var content = await new MyNetmeraClient().FindNetmeraContent("RichMedia", "ID", item.ID); if (content != null) { item.IsCreatedOnClient = false; } } foreach (var media in medias) { NetmeraContent e = null; if (media.IsCreatedOnClient) { e = new NetmeraContent("RichMedia"); } else { e = await new MyNetmeraClient().FindNetmeraContent("RichMedia", "ID", media.ID); } if (e != null) { e.add("Comments", media.Comments); e.add("CustomerID", media.CustomerID); e.add("CustomerSurveyID", media.CustomerSurveyID); e.add("FileName", media.FileName); e.add("ID", media.ID); e.add("Option_ID", media.Option_ID ?? ""); e.add("Question_Ref", media.Question_Ref); e.add("UPRN", media.UPRN); using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) { using (var file = iso.OpenFile(media.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { var data = new byte[file.Length]; await file.ReadAsync(data, 0, data.Length); var path = await UploadToCloud(data); e.add("Photo", path); } } } await new DbService().Save(media, ESyncStatus.InProcess, null, true); var r = media.IsCreatedOnClient ? await new MyNetmeraClient().Create <RichMedia>(e, media.ID) : await new MyNetmeraClient().Update <RichMedia>(e, media.ID); media.IsCreatedOnClient = false; if (r.IsSuccess) { await new DbService().Save(media, ESyncStatus.Success, null, true); } else { await new DbService().Save(media, ESyncStatus.Error, r.Exception, true); } } PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Enabled; Debug.WriteLine("Sync finished"); }