Пример #1
0
        protected override void Process()
        {
            log.Info("Selector import cycle started...");
            try
            {
                foreach (Connector connector in base.Connectors.Where(c => ((ConnectorType)c.ConnectorType).Has(ConnectorType.WebAssortment) && c.Selectors))
                {
                    DateTime start = DateTime.Now;
                    AssortmentServiceSoapClient contentClient = new AssortmentServiceSoapClient();
                    SelectorServiceSoapClient   serviceClient = new SelectorServiceSoapClient();

                    using (
                        WebsiteDataContext context =
                            new WebsiteDataContext(
                                ConfigurationManager.ConnectionStrings[connector.Connection].ConnectionString))
                    {
                        log.InfoFormat("Start selector import for connector {0}", connector.Name);

                        var selectorIDs = (from s in context.Selectors select s.SelectorID).ToList();

                        foreach (int selectorID in selectorIDs)
                        {
                            var products = XDocument.Parse(contentClient.GetSelectorProducts(connector.ConnectorID, selectorID));

                            Processor processor = new Processor(products, log, connector);

                            log.Debug("Start import brands");
                            processor.ImportBrands();
                            log.Debug("Finished import brands");
                            log.Debug("Start import productgroups");
                            processor.ImportProductGroups(false);
                            log.Debug("Finished import productgroups");
                            log.Debug("Start import Products");
                            processor.ImportSelectorProducts();
                            log.Debug("Finished import Products");

                            log.Debug("Start import Attributes");
                            processor.processSelectorAttributes(connector);
                            log.Debug("Finished import Attributes");

                            log.Debug("Start import images");
                            ProcessProductImages(connector, products);
                            log.Debug("Finish import images");

                            //processor.CleanUpProducts();
                            //
                        }

                        try
                        {
                            log.InfoFormat("Starting import of related products");
                            ImportRelatedProducts(context, serviceClient, contentClient, connector);
                            log.InfoFormat("Finished import of related products");
                        }
                        catch (Exception e)
                        {
                            log.Error("Importing of related products for connector " + connector.Name + "failed ", e);
                        }

                        try
                        {
                            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress, TimeSpan.FromMinutes(5)))
                            {
                                context.SubmitChanges();
                                scope.Complete();
                            }

                            log.AuditSuccess("Finished selector export for connector " + connector.Name, "Selector export");
                        }
                        catch (Exception e)
                        {
                            log.AuditFatal("There was an error inserting the selectors", e);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                log.Fatal("Selector import cycle failed", e);
            }
        }
Пример #2
0
        public void ImportRelatedProducts(WebsiteDataContext context, SelectorServiceSoapClient client, Concentrator.Web.ServiceClient.AssortmentService.AssortmentServiceSoapClient contentClient, Connector connector)
        {
            var       selectorIDs = (from s in context.Selectors select s.SelectorID).ToList();
            Processor processor   = new Processor(new XDocument(), log, connector);

            foreach (int selectorID in selectorIDs)
            {
                var       selectorProducts   = client.GetAllSelectorProducts(selectorID, connector.ConnectorID);
                XDocument comparableProducts = XDocument.Parse(selectorProducts);
                int       counter            = 0;
                int       total = comparableProducts.Root.Elements("RelatedProduct").Count();

                List <Product> products = (from p in context.Products
                                           select p).ToList();

                int totalRelatedproducts = total;

                foreach (var rProduct in comparableProducts.Root.Elements("RelatedProduct"))
                {
                    if (counter == 50)
                    {
                        base.log.InfoFormat("Still need to process {0} of {1}; {2} done;", total, totalRelatedproducts, totalRelatedproducts - total);
                        counter = 0;
                    }
                    total--;
                    counter++;
                    int concentratorProductID = int.Parse(rProduct.Attribute("ProductID").Value);

                    Product product = null;
                    //try
                    //{
                    product = (from p in products
                               where p.ConcentratorProductID.HasValue && (p.ConcentratorProductID.Value == concentratorProductID)
                               select p).FirstOrDefault();

                    if (product == null)
                    {
                        continue;
                    }

                    foreach (var comparableProduct in rProduct.Elements("ComparableProducts").Elements("ComparableProduct"))
                    {
                        int relatedProductID = int.Parse(comparableProduct.Attribute("ProductID").Value);
                        try
                        {
                            Product relatedProduct = (from p in products
                                                      where p.ConcentratorProductID.HasValue && (p.ConcentratorProductID.Value == relatedProductID)
                                                      select p).FirstOrDefault();

                            if (relatedProduct == null)
                            {
                                continue;
                            }

                            //insert related product
                            RelatedProduct relatedProd = (from rp in context.RelatedProducts
                                                          where
                                                          rp.ProductID == product.ProductID
                                                          &&
                                                          rp.RelatedProductID == relatedProduct.ProductID
                                                          &&
                                                          rp.SelectorID == selectorID
                                                          select rp).FirstOrDefault();

                            if (relatedProd == null)
                            {
                                context.RelatedProducts.InsertOnSubmit(new RelatedProduct
                                {
                                    ProductID        = product.ProductID,
                                    RelatedProductID = relatedProduct.ProductID,
                                    SelectorID       = selectorID
                                });
                            }
                            context.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            log.ErrorFormat("Error process product {0} error {1}", relatedProductID, ex.StackTrace);
                        }
                    }
                }

                #region Cleanup

                var itemNumbers = comparableProducts.Root.Elements("RelatedProduct").ToDictionary(x => x.Attribute("ProductID").Value, x => x);


                List <RelatedProduct> unused = (from c in context.RelatedProducts
                                                select c).ToList();

                foreach (var p in itemNumbers)
                {
                    foreach (var v in p.Value.Elements("ComparableProducts").Elements("ComparableProduct"))
                    {
                        unused.RemoveAll(c => (c.Product.ConcentratorProductID.HasValue && c.Product.ConcentratorProductID.Value.ToString() == p.Key) && (c.Product1.ConcentratorProductID.HasValue && c.Product1.ConcentratorProductID.Value.ToString() == v.Attribute("ProductID").Value));
                    }
                }

                #endregion
                if (unused != null && unused.Count > 0)
                {
                    context.RelatedProducts.DeleteAllOnSubmit(unused);

                    context.SubmitChanges();
                }
            }
        }