/// <summary> /// Synchronize RFS with Nop's categories /// Sync E-Commerce first (access to everyone) /// Then Sync Restricted access /// </summary> /// <note>RFS to excludes are to add in appconfig file</note> /// <param name="niveau"></param> /// <returns></returns> private bool Sync(RFS.TYPE niveau) { //Get Rayons From Mercator using (SqlConnection connection = new SqlConnection(dataSettings.DataConnectionString)) { IEnumerable <Category> categories = new List <Category>(); //map mercator's ids with nop commerce's existing ids IDictionary <string, int> mapMIdNopId = GetMapIdNopId(); categories = GetRFSAsCategories(connection, mapMIdNopId, niveau); //ID Mercator (url) + id category //rayonID => urlRecordID //rayonID + "SLUG" => catID //var existingUrlRecordsSlugAndCatId = GetExistingUrlRecords(categories); List <int> categoriesToKeep = new List <int>(); //ADD/EDIT RAYONS foreach (Category category in categories) { try { //Format to JSON JObject json = ParserJSon.ParseRFSToNopCategory(category); int catId = 0; //ADD CATEGORY TO NOP if (mapMIdNopId.ContainsKey(category.MercatorId)) { try { catId = mapMIdNopId[category.MercatorId]; //Send via PATCH/PUT (existing ones) WebService.Patch(urlBuilder.Id(catId).BuildQuery(), json.ToString()); //Activate url (in case it was deactivated) JObject urlActivate = new JObject(); urlActivate.Add("IsActive", true); JObject urls = JObject.Parse(WebService.Get(new UrlBuilder("UrlRecord").And().FilterEq("EntityId", catId).FilterEq("EntityName", ENTITY).BuildQuery())); var urlsValues = urls["value"].ToArray(); if (urlsValues.Count() > 0) { WebService.Patch(new UrlBuilder("UrlRecord").Id((int)urlsValues[0]["Id"]).BuildQuery(), urlActivate.ToString()); } } catch (Exception e) { Program.log("Exception lors de la mise à jour de la categorie : " + category.Name.TrimEnd()); Program.log("NopId de la catégorie :" + catId); Program.log(e.Message); Program.log(e.StackTrace); } } else { //Send via POST (new ones) string result = WebService.Post(ENTITY, json.ToString()); JObject newCat = JObject.Parse(result); catId = (int)newCat["Id"]; //ADD URL RECORD JObject urlJson = ParserJSon.GetUrlRecordJson(ENTITY, catId, UrlsSyncer.BuildCategoryUrl(category)); WebService.Post(WebApiEntities.URL_RECORD, urlJson.ToString()); //ADD GENERIC ATTRIBUTE MERCATOR ID JObject genericAttributeJSon = ParserJSon.GetGenericAttributeJSon(catId, ENTITY, KEY_MERCATORID, category.MercatorId); WebService.Post(WebApiEntities.GENERIC_ATTRIBUTE, genericAttributeJSon.ToString()); Console.WriteLine(category.Name + " inserted"); } //Store cat to keep categoriesToKeep.Add(catId); } catch (Exception e) { Program.log(String.Format("Error syncing category : {0}.", category.Name)); Program.log(e.Message); Program.log(e.StackTrace); } } //Delete categories that aren't in Mercator List <int> existings = new List <int>(); List <JToken> values = niveau == RFS.TYPE.RAYON ? JObject.Parse(WebService.Get(urlBuilder.And().FilterEq("Published", true).FilterEq("ParentCategoryId", 0).BuildQuery()))["value"].ToList() : (niveau == RFS.TYPE.FAMILLE ? GetNopFamilles() : GetNopSousFamilles()); foreach (JToken v in values) { if (!(bool)v["Deleted"]) { existings.Add((int)v["Id"]); } } var toDelete = existings.Except(categoriesToKeep); toDelete = toDelete.ToList(); JObject deletedJson = new JObject(); deletedJson.Add("Deleted", true); JObject urlDeactivated = new JObject(); urlDeactivated.Add("IsActive", false); foreach (int i in toDelete) { //Deactivate the deleted category and delete corresponding urlRecord //Instead of published : false => Delete : false ou vraiment la supprimer ? //WebService.Patch(wsCategoryMapping + "(" + s + ")", "{\"Published\":false}"); JObject urls = JObject.Parse(WebService.Get(new UrlBuilder("UrlRecord").And().FilterEq("EntityId", i).FilterEq("EntityName", "Category").BuildQuery())); var urlsValues = urls["value"].ToArray(); //WebService.Patch("UrlRecord("+urlsValues[0]["Id"].ToString()+")","{\"IsActive\":false}"); if (urlsValues.Count() > 0) { Program.log(i + " deleted"); //deleteResult = WebService.Delete(String.Format(WebServiceUrls.URL_RECORD_ID, (int)urlsValues[0]["Id"])); WebService.Patch(new UrlBuilder("UrlRecord").Id((int)urlsValues[0]["Id"]).BuildQuery(), urlDeactivated.ToString()); } //deleteResult = WebService.Delete(String.Format(WebServiceUrls.CATEGORY_ID, i)); WebService.Patch(urlBuilder.Id(i).BuildQuery(), deletedJson.ToString()); } } return(true); }