Esempio n. 1
0
        /// <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();
        }