/// <summary> /// Send the products to the website by WS /// </summary> /// <param name="products"></param> /// <returns>False if an exception has been caught, true if products have been succesfully synced</returns> private void SendProducts(JArray products, bool publish = true, List <STOCK> stocks = null, SqlConnection connection = null) { JObject container = new JObject(); string addedProductsResult = ""; JObject addedProductsResultO = new JObject(); try { //Add the products to the JSON container.Add("products", products); ParserJSon.checkJson(container.ToString()); //Post everything Console.WriteLine("Envoi des produits"); addedProductsResult = WebService.Post(urlBuilder.BuildQueryFct("AddProducts"), container.ToString()); addedProductsResultO = JObject.Parse(addedProductsResult); Console.WriteLine("Produits synchros"); if (publish) { Console.WriteLine("Ajouts URLS && Generic Attributes"); JArray urlList = new JArray(); JArray genericAttributeList = new JArray(); foreach (var j in addedProductsResultO) { STOCK s = stocks.Where(x => x.S_ID.TrimEnd() == j.Value.ToString()).FirstOrDefault(); string url = UrlsSyncer.BuildProductUrl(s, connection); JObject urlJSon = ParserJSon.GetUrlRecordJson(ENTITY, Int32.Parse(j.Key), url); JObject genericAttribute = ParserJSon.GetGenericAttributeJSon(Int32.Parse(j.Key), ENTITY, KEY_MODIFTAG, s.S_MODIFTAG.ToString()); urlList.Add(urlJSon); genericAttributeList.Add(genericAttribute); } JObject urlContainer = new JObject(); JObject gaContainer = new JObject(); urlContainer.Add("urls", urlList); gaContainer.Add("genericAttributes", genericAttributeList); WebService.Post(new UrlBuilder("UrlRecord").BuildQueryFct("AddUrlRecords"), urlContainer.ToString()); WebService.Post(new UrlBuilder("GenericAttribute").BuildQueryFct("AddGenericAttributes"), gaContainer.ToString()); Console.WriteLine("URLS et GA ajoutés"); } } catch (Exception e) { Program.log("Erreur lors de l'envoi des produits"); Program.log(e.Message); Program.log(e.StackTrace); Program.log("These products have failed syncing : "); foreach (JObject jo in products) { Program.WriteLine(jo["Name"].ToString()); Program.log(jo.ToString(), false); } } products.Clear(); }
/// <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); }