private void ProcessProductImages(Connector connector)
        {
            using (WebsiteDataContext context = new WebsiteDataContext(connector.ConnectionString))
            {
                var productImages = (from r in images.Root.Elements("Products").Elements("ProductMedia")
                                     select new
                {
                    ConcentratorProductID = int.Parse(r.Attribute("ProductID").Value),
                    Sequence = r.Attribute("Sequence").Value
                }).ToList().OrderByDescending(x => x.ConcentratorProductID);

                var websiteProducts = (from p in context.Products
                                       where p.ConcentratorProductID.HasValue
                                       select p).ToList();

                var ImageDB = (from i in context.ImageStores
                               where i.ImageType == ImageType.ProductImage.ToString()
                               select i).ToList();

                var unusedImages = ImageDB;

                var brands = (from b in context.Brands
                              select b).ToList();

                List <ImageStore> existingProducts = new List <ImageStore>();

                foreach (var prod in productImages)
                {
                    var websiteProduct = (from p in websiteProducts
                                          where p.ConcentratorProductID.Value == prod.ConcentratorProductID
                                          select p).OrderByDescending(x => x.IsVisible).FirstOrDefault();

                    if (websiteProduct != null)
                    {
                        var product = (from p in ImageDB
                                       where p.ConcentratorProductID == prod.ConcentratorProductID &&
                                       p.Sequence == int.Parse(prod.Sequence)
                                       select p).FirstOrDefault();

                        if (product != null)
                        {
                            existingProducts.Add(product);
                        }
                    }
                }

                int  counter     = productImages.Count();
                int  showMessage = 0;
                bool succes      = true;
                //string imageDirectory = GetConfiguration().AppSettings.Settings["ImageDirectory"].Value;
                foreach (var r in images.Root.Elements("Products").Elements("ProductMedia").OrderByDescending(x => x.Attribute("ProductID").Value))
                {
                    var websiteProduct = (from p in websiteProducts
                                          where p.ConcentratorProductID.Value == int.Parse(r.Attribute("ProductID").Value)
                                          select p).OrderByDescending(x => x.IsVisible).FirstOrDefault();

                    try
                    {
                        if (!string.IsNullOrEmpty(r.Value) && websiteProduct != null)
                        {
                            //string url = GetImage(r.Value, ImageType.ProductImage, r.Attribute("ProductID").Value, int.Parse(r.Attribute("Sequence").Value), imageDirectory);
                            string url = r.Value;

                            if (!string.IsNullOrEmpty(url))
                            {
                                ImageStore productImage = existingProducts.Where(x => x.ConcentratorProductID.Value == int.Parse(r.Attribute("ProductID").Value) && x.Sequence == int.Parse(r.Attribute("Sequence").Value)).FirstOrDefault();

                                if (productImage != null)
                                {
                                    if (productImage.ImageUrl != url ||
                                        productImage.BrandID != int.Parse(r.Attribute("BrandID").Value) ||
                                        productImage.ManufacturerID != r.Attribute("ManufacturerID").Value ||
                                        productImage.Sequence != int.Parse(r.Attribute("Sequence").Value) ||
                                        productImage.ConcentratorProductID != int.Parse(r.Attribute("ProductID").Value)
                                        )
                                    {
                                        productImage.ImageUrl              = url;
                                        productImage.BrandID               = int.Parse(r.Attribute("BrandID").Value);
                                        productImage.ManufacturerID        = r.Attribute("ManufacturerID").Value;
                                        productImage.Sequence              = int.Parse(r.Attribute("Sequence").Value);
                                        productImage.ConcentratorProductID = int.Parse(r.Attribute("ProductID").Value);
                                        productImage.LastModificationTime  = DateTime.Now;
                                    }
                                    unusedImages.Remove(productImage);
                                }
                                else
                                {
                                    productImage = new ImageStore()
                                    {
                                        ImageUrl              = url,
                                        BrandID               = int.Parse(r.Attribute("BrandID").Value),
                                        CustomerProductID     = websiteProduct.ProductID.ToString(),
                                        ManufacturerID        = r.Attribute("ManufacturerID").Value,
                                        ImageType             = ImageType.ProductImage.ToString(),
                                        Sequence              = int.Parse(r.Attribute("Sequence").Value),
                                        ConcentratorProductID = int.Parse(r.Attribute("ProductID").Value),
                                        LastModificationTime  = DateTime.Now
                                    };
                                    context.ImageStores.InsertOnSubmit(productImage);
                                }
                            }
                            counter--;
                            showMessage++;
                            if (showMessage == 500)
                            {
                                log.InfoFormat("{0} images to process", counter);
                                showMessage = 0;
                            }
                        }
                        context.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        succes = false;
                        log.Error(ex);
                    }
                }

                try
                {
                    if (succes)
                    {
                        if (Processor.TableExists(context, "AuctionProducts"))
                        {
                            var auctionImages = (from a in context.AuctionProducts
                                                 join i in context.ImageStores on a.Product.ConcentratorProductID equals i.ConcentratorProductID
                                                 select i).ToList();
                            unusedImages = unusedImages.Except(auctionImages).ToList();
                        }

                        if (Processor.TableExists(context, "RelatedProducts"))
                        {
                            var relatedproducts = (from r in context.RelatedProducts
                                                   join i in context.ImageStores on r.Product.ConcentratorProductID equals i.ConcentratorProductID
                                                   select i).ToList();

                            unusedImages = unusedImages.Except(relatedproducts).ToList();
                        }

                        log.DebugFormat("Try To delete {0} product images", unusedImages.Count);

                        context.ImageStores.DeleteAllOnSubmit(unusedImages);
                        context.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    log.Error("Error delete unused images", ex);
                }
            }
        }
Esempio n. 2
0
        private void ProcessProductImages(Connector connector)
        {
            using (WebsiteDataContext context = new WebsiteDataContext(connector.ConnectionString))
            {
                var productImages = (from r in images.Root.Elements("Products").Elements("ProductImage")
                                     select new
                {
                    ConcentratorProductID = int.Parse(r.Attribute("ProductID").Value),
                    Sequence = r.Attribute("Sequence").Value
                }).ToList().OrderByDescending(x => x.ConcentratorProductID);

                var websiteProducts = (from p in context.Products
                                       where p.ConcentratorProductID.HasValue
                                       select p).ToList();

                var ImageDB = (from i in context.ImageStores
                               where i.ImageType == ImageType.ProductImage.ToString()
                               select i).ToList();

                List <ImageStore> existingProducts = new List <ImageStore>();

                foreach (var prod in productImages)
                {
                    var websiteProduct = (from p in websiteProducts
                                          where p.ConcentratorProductID.Value == prod.ConcentratorProductID
                                          select p).FirstOrDefault();

                    if (websiteProduct != null)
                    {
                        var product = (from p in ImageDB
                                       where p.CustomerProductID == websiteProduct.ProductID.ToString() &&
                                       p.Sequence == int.Parse(prod.Sequence)
                                       select p).FirstOrDefault();

                        if (product != null)
                        {
                            existingProducts.Add(product);
                        }
                    }
                }

                int    counter        = productImages.Count();
                int    showMessage    = 0;
                string imageDirectory = GetConfiguration().AppSettings.Settings["ImageDirectory"].Value;
                foreach (var r in images.Root.Elements("Products").Elements("ProductImage").OrderByDescending(x => x.Attribute("ProductID").Value))
                {
                    var websiteProduct = (from p in websiteProducts
                                          where p.ConcentratorProductID.Value == int.Parse(r.Attribute("ProductID").Value)
                                          select p).FirstOrDefault();

                    try
                    {
                        if (!string.IsNullOrEmpty(r.Value) && websiteProduct != null)
                        {
                            string url = GetImage(r.Value, ImageType.ProductImage, r.Attribute("ProductID").Value, int.Parse(r.Attribute("Sequence").Value), imageDirectory);

                            if (!string.IsNullOrEmpty(url))
                            {
                                ImageStore productImage = existingProducts.Where(x => x.Sequence == int.Parse(r.Attribute("Sequence").Value) &&
                                                                                 x.CustomerProductID == websiteProduct.ProductID.ToString()).FirstOrDefault();

                                if (productImage != null)
                                {
                                    productImage.ImageUrl              = url;
                                    productImage.BrandID               = int.Parse(r.Attribute("BrandID").Value);
                                    productImage.ManufacturerID        = r.Attribute("ManufacturerID").Value;
                                    productImage.Sequence              = int.Parse(r.Attribute("Sequence").Value);
                                    productImage.ConcentratorProductID = int.Parse(r.Attribute("ProductID").Value);
                                }
                                else
                                {
                                    productImage = new ImageStore()
                                    {
                                        ImageUrl              = url,
                                        BrandID               = int.Parse(r.Attribute("BrandID").Value),
                                        CustomerProductID     = websiteProduct.ProductID.ToString(),
                                        ManufacturerID        = r.Attribute("ManufacturerID").Value,
                                        ImageType             = ImageType.ProductImage.ToString(),
                                        Sequence              = int.Parse(r.Attribute("Sequence").Value),
                                        ConcentratorProductID = int.Parse(r.Attribute("ProductID").Value)
                                    };
                                    context.ImageStores.InsertOnSubmit(productImage);
                                }
                            }
                            counter--;
                            showMessage++;
                            if (showMessage == 500)
                            {
                                log.InfoFormat("{0} images to process", counter);
                                showMessage = 0;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                    }
                    context.SubmitChanges();
                }
                // context.SubmitChanges();
            }
        }
        private void ProcessBrandImages(Connector connector)
        {
            using (WebsiteDataContext context = new WebsiteDataContext(connector.ConnectionString))
            {
                var brandImages = (from r in images.Root.Elements("Brands").Elements("BrandMedia")
                                   select int.Parse(r.Attribute("BrandID").Value)).Distinct().ToArray();

                var brands = (from b in context.Brands
                              select b).ToList();


                var existingProducts = (from c in context.ImageStores
                                        where c.ImageType == ImageType.BrandImage.ToString()
                                        select c).ToList();

                var unusedImages = existingProducts;

                string imageDirectory = GetConfiguration().AppSettings.Settings["ImageDirectory"].Value;
                foreach (var r in images.Root.Elements("Brands").Elements("BrandMedia"))
                {
                    //log.DebugFormat("Get BrandImage for {0} url: {1}", r.Attribute("BrandVendorCode").Value, r.Value);
                    try
                    {
                        if (!string.IsNullOrEmpty(r.Value))
                        {
                            //string url = GetImage(r.Value, ImageType.BrandImage, r.Attribute("BrandID").Value, null, imageDirectory);
                            string url = r.Value;

                            if (!string.IsNullOrEmpty(url))
                            {
                                ImageStore brandImage = existingProducts.FirstOrDefault(x => x.BrandCode == r.Attribute("BrandID").Value);
                                if (brandImage != null)
                                {
                                    if (
                                        brandImage.ImageUrl != url ||
                                        brandImage.BrandCode != r.Attribute("BrandID").Value ||
                                        brandImage.BrandID != brands.Where(x => x.BrandCode == r.Attribute("BrandID").Value).Select(x => x.BrandID).FirstOrDefault()
                                        )
                                    {
                                        brandImage.ImageUrl             = url;
                                        brandImage.BrandCode            = r.Attribute("BrandID").Value;
                                        brandImage.BrandID              = brands.Where(x => x.BrandCode == r.Attribute("BrandID").Value).Select(x => x.BrandID).FirstOrDefault();
                                        brandImage.LastModificationTime = DateTime.Now;
                                    }
                                    unusedImages.Remove(brandImage);
                                }
                                else
                                {
                                    brandImage = new ImageStore()
                                    {
                                        BrandID              = brands.Where(x => x.BrandCode == r.Attribute("BrandID").Value).Select(x => x.BrandID).FirstOrDefault(),
                                        BrandCode            = r.Attribute("BrandID").Value,
                                        ImageUrl             = url,
                                        ImageType            = ImageType.BrandImage.ToString(),
                                        LastModificationTime = DateTime.Now
                                    };
                                    context.ImageStores.InsertOnSubmit(brandImage);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex);
                    }
                }
                context.SubmitChanges();

                //try
                //{
                //  log.DebugFormat("Try To delete {0} brand images", unusedImages.Count);
                //  context.ImageStores.DeleteAllOnSubmit(unusedImages);
                //  context.SubmitChanges();
                //}
                //catch (Exception ex)
                //{
                //  log.Error("Error delete unused images", ex);
                //}
            }
        }