public static void DoImport(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;
            // Parse list input parameters
            BackgroundWorkParameters param = (BackgroundWorkParameters)e.Argument;
            string _URL          = param.Url;
            int    categoryId    = param.CategoryId;
            int    portalId      = param.PortalId;
            int    vendorId      = param.VendorId;
            string countryFilter = param.CountryFilter;
            string cityFilter    = param.CityFilter;
            int?   stepImport    = param.StepImport;
            int    feedId        = param.FeedId;

            if (!File.Exists(_URL))
            {
                string xmlFileName = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, "tradedoubler.xml");
                if (File.Exists(xmlFileName))
                {
                    File.Delete(xmlFileName);
                }
                // inside function display progressbar
                Common.SaveFileFromURL(_URL, xmlFileName, 60, bw, e, log);

                // exit if user cancel during saving file or error
                if (e.Cancel || (e.Result != null) && e.Result.ToString().Substring(0, 6).Equals("ERROR:"))
                {
                    return;
                }

                _URL = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, "tradedoubler.xml");
            }

            XmlSchemaSet schemas = new XmlSchemaSet();

            schemas.Add("", "tradedoublerHotels.xsd");
            Form1.activeStep = "Validating input..";
            bw.ReportProgress(0); // start new step of background process
            XDocument xDoc   = XDocument.Load(_URL);
            bool      errors = false;

            xDoc.Validate(schemas, (o, e2) =>
            {
                e.Result = "ERROR:" + e2.Message;
                log.Error(e2.Message);
                errors = true;
            });
            //if (errors)
            //{
            //    e.Cancel = true;
            //    return;
            //}

            // show progress & catch Cancel
            if (bw.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            else if (bw.WorkerReportsProgress)
            {
                bw.ReportProgress(50);
            }

            // read hotels from XML
            // use XmlReader to avoid huge file size dependence
            var xmlProducts =
                from el in Common.StreamRootChildDoc(_URL, "product")
                where (string)el.Element("TDCategoryName") == "Hotels"
                select new ProductView
            {
                Country         = (string)el.Element("fields").Element("country"),
                City            = (string)el.Element("fields").Element("city"),
                ProductNumber   = (string)el.Element("TDProductId"),
                Name            = (string)el.Element("name"),
                Image           = (string)el.Element("imageUrl"),
                UnitCost        = (string)el.Element("price"),
                Description     = (string)el.Element("description"),
                DescriptionHTML = (string)el.Element("description"),
                URL             = (string)el.Element("productUrl"),
                Star            = (string)el.Element("fields").Element("StarRating"),
                CustomerRating  = (string)el.Element("fields").Element("AverageOverallRating"),
                Address         = (string)el.Element("fields").Element("address"),
                PostCode        = (string)el.Element("fields").Element("postalcode"),
                Telephone       = (string)el.Element("fields").Element("telephone"),
                CurrencyCode    = (string)el.Element("currency")
            };

            if (!String.IsNullOrEmpty(countryFilter))
            {
                xmlProducts = xmlProducts.Where(p => p.Country == countryFilter);
            }

            if (!String.IsNullOrEmpty(cityFilter))
            {
                xmlProducts = xmlProducts.Where(p => p.City == cityFilter);
            }

            // show progress & catch Cancel
            if (bw.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            else if (bw.WorkerReportsProgress)
            {
                bw.ReportProgress(100);
            }
            Thread.Sleep(100); // a little bit slow working for visualisation Progress

            // Set step for backgroundWorker
            Form1.activeStep = "Import records..";
            bw.ReportProgress(0); // start new step of background process
            int productCount = xmlProducts.Count();

            try
            {
                int initialStep = 0;
                if (stepImport.HasValue)
                {
                    initialStep = stepImport.Value;
                }

                using (SelectedHotelsEntities db = new SelectedHotelsEntities())
                {
                    int i = 0;
                    foreach (var product in xmlProducts)
                    {
                        if (i < initialStep)
                        {
                            i++;
                            continue;
                        }

                        string productName = product.Name.Replace("&apos;", "'");
#if DEBUG
                        Console.WriteLine(i + " - " + productName); // debug print
#endif

                        Hotel hotel =
                            db.Products.OfType <Hotel>().SingleOrDefault(
                                p => p.Name == productName && p.Number == product.ProductNumber);
                        if (hotel == null)
                        {
                            hotel = new Hotel
                            {
                                Name          = productName,
                                ProductTypeId = (int)Enums.ProductTypeEnum.Hotels,
                                Number        = product.ProductNumber,
                                Description   = product.Description,
                                URL           = product.URL,
                                Image         = product.Image,
                                CreatedByUser = vendorId,
                                CreatedDate   = DateTime.Now,
                                IsDeleted     = false,
                                HotelTypeId   = (int)Enums.HotelTypeEnum.Hotels
                            };

                            if (!String.IsNullOrEmpty(product.UnitCost))
                            {
                                hotel.UnitCost = Convert.ToDecimal(product.UnitCost);
                            }
                            if (!String.IsNullOrEmpty(product.Star))
                            {
                                hotel.Star = decimal.Parse(product.Star);
                            }
                            if (!String.IsNullOrEmpty(product.CustomerRating))
                            {
                                hotel.CustomerRating = decimal.Parse(product.CustomerRating);
                            }
                            if (!String.IsNullOrEmpty(product.Address))
                            {
                                hotel.Address = product.Address;
                            }
                            if (!String.IsNullOrEmpty(product.CurrencyCode))
                            {
                                hotel.CurrencyCode = product.CurrencyCode;
                            }
                            if (!String.IsNullOrEmpty(product.PostCode))
                            {
                                hotel.PostCode = product.PostCode;
                            }
                            hotel.FeedId = feedId;
                            db.Products.Add(hotel);
                            db.SaveChanges();

                            Common.SetLocation(product, db, hotel, log);
                            Common.SetGeoNameId(product, db, hotel, log);

                            Category category = db.Categories.Find(categoryId);
                            if (category != null)
                            {
                                hotel.Categories.Add(category);
                            }
                            db.SaveChanges();

                            i++;
                            Common.UpdateSteps(stepImport: i);
                        }
                        else
                        {
                            if (hotel.Location == null)
                            {
                                Common.SetLocation(product, db, hotel, log);
                            }
                            if (!hotel.GeoNameId.HasValue)
                            {
                                Common.SetGeoNameId(product, db, hotel, log);
                            }

                            // no need to check for null vallue because of previous if
                            decimal?unitCost = null;
                            if (!String.IsNullOrEmpty(product.UnitCost))
                            {
                                unitCost = decimal.Parse(product.UnitCost);
                            }
                            if (hotel.UnitCost != unitCost)
                            {
                                hotel.UnitCost = unitCost;
                            }
                            if (hotel.Description != product.Description && product.Description != product.Name)
                            {
                                hotel.Description = product.Description;
                            }
                            if (hotel.URL != product.URL)
                            {
                                hotel.URL = product.URL;
                            }
                            if (hotel.Image != product.Image)
                            {
                                hotel.Image = product.Image;
                            }
                            decimal?star = null;
                            if (!String.IsNullOrEmpty(product.Star))
                            {
                                star = decimal.Parse(product.Star);
                            }
                            if (hotel.Star != star)
                            {
                                hotel.Star = star;
                            }
                            decimal?customerRating = null;
                            if (!String.IsNullOrEmpty(product.CustomerRating))
                            {
                                customerRating = decimal.Parse(product.CustomerRating);
                            }
                            if (hotel.CustomerRating != customerRating)
                            {
                                hotel.CustomerRating = customerRating;
                            }
                            if (hotel.Address != product.Address)
                            {
                                hotel.Address = product.Address;
                            }
                            if (hotel.PostCode != product.PostCode)
                            {
                                hotel.PostCode = product.PostCode;
                            }
                            if (hotel.CurrencyCode != product.CurrencyCode)
                            {
                                hotel.CurrencyCode = product.CurrencyCode;
                            }
                            db.SaveChanges();

                            db.Database.SqlQuery <string>(@"
                                DELETE FROM Cowrie_HotelViews
                            ");
                            db.Database.SqlQuery <string>(@"
                                INSERT INTO Cowrie_HotelViews
                                SELECT        Extent1.Id, Extent2.Name, Cowrie_GeoNames.Name AS GeoName, 'Hotels' AS HotelTypeName
                                FROM            Cowrie_Hotels AS Extent1 INNER JOIN
                                                         Cowrie_Products AS Extent2 ON Extent1.Id = Extent2.Id INNER JOIN
                                                         Cowrie_GeoNames ON Extent1.GeoNameId = Cowrie_GeoNames.Id
                                WHERE        (Extent1.GeoNameId IS NOT NULL) AND (Extent1.HotelTypeId = 1)
                            ");

                            i++;
                            Common.UpdateSteps(stepImport: i);
                        }

                        if (bw.CancellationPending)
                        {
                            e.Cancel = true;
                            goto Cancelled;
                        }
                        else if (bw.WorkerReportsProgress && i % 100 == 0)
                        {
                            bw.ReportProgress((int)(100 * i / productCount));
                        }
                    }
                }

                if (!e.Cancel)
                {
                    Common.UpdateSteps();
                }
Cancelled:
                ;
            }
            catch (DbEntityValidationException exception)
            {
                foreach (var eve in exception.EntityValidationErrors)
                {
                    log.Error(String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                            eve.Entry.Entity.GetType().Name, eve.Entry.State), exception);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        log.Error(String.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                ve.PropertyName, ve.ErrorMessage), exception);
                    }
                }
                throw;
            }
            catch (Exception ex)
            {
                e.Result = "ERROR:" + ex.Message;
                log.Error("Error error logging", ex);
            }
        }
示例#2
0
        public static void DoImport(object sender, DoWorkEventArgs e)               //(string _URL, out string message)        // public static bool
        {
            //bool rc = false;
            //message = String.Empty;
            BackgroundWorker bw = sender as BackgroundWorker;
            // Parse list input parameters
            BackgroundWorkParameters param = (BackgroundWorkParameters)e.Argument;
            string _URL                 = param.Url;
            int    categoryId           = param.CategoryId;
            int    portalId             = param.PortalId;
            int    vendorId             = param.VendorId;
            string advancedCategoryRoot = param.AdvancedCategoryRoot;
            string countryFilter        = param.CountryFilter;
            string cityFilter           = param.CityFilter;
            int    feedId               = param.FeedId;

            if (!File.Exists(_URL))
            {
                string xmlFileName = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, "tradedoubler.xml");
                if (File.Exists(xmlFileName))
                {
                    File.Delete(xmlFileName);
                }
                // inside function display progressbar
                SaveFileFromURL(_URL, xmlFileName, 60, bw, e);

                // exit if user cancel during saving file or error
                if (e.Cancel || (e.Result != null) && e.Result.ToString().Substring(0, 6).Equals("ERROR:"))
                {
                    return;
                }

                _URL = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, "tradedoubler.xml");
            }

#if VALIDATE
            XmlSchemaSet schemas = new XmlSchemaSet();
            schemas.Add("", "tradedoubler.xsd");
            Form1.activeStep = "Validating inpout..";
            bw.ReportProgress(0); // start new step of background process
            XDocument xDoc   = XDocument.Load(_URL);
            bool      errors = false;
            xDoc.Validate(schemas, (o, e2) =>
            {
                e.Result = "ERROR:" + e2.Message;
                log.Error(e2.Message);
                errors = true;
            });
            if (errors)
            {
                e.Cancel = true;
                return;
            }
#endif

            // Set step for backgroundWorker
            Form1.activeStep = "Import records..";
            bw.ReportProgress(0);           // start new step of background process

            var xmlProducts =
                from el in StreamRootChildDoc(_URL)
                select new
            {
                Category         = (string)el.Element("TDCategoryName"),
                MerchantCategory = (string)el.Element("merchantCategoryName"),
                ProductNumber    = (string)el.Element("TDProductId"),
                Name             = (string)el.Element("name"),
                Image            = (string)el.Element("imageUrl"),
                UnitCost         = (decimal)el.Element("price"),
                CurrencyCode     = (string)el.Element("currency"),
                Description      = (string)el.Element("description"),
                DescriptionHTML  = (string)el.Element("description"),
                URL                  = (string)el.Element("productUrl"),
                Country              = (string)el.Element("fields").Element("country"),
                City                 = (string)el.Element("fields").Element("city"),
                Weight               = (string)el.Element("weight"),
                Size                 = (string)el.Element("size"),
                Brand                = (string)el.Element("brand"),
                Model                = (string)el.Element("model"),
                Manufacturer         = (string)el.Element("manufacturer"),
                Colours              = (string)el.Element("fields").Element("Colours"),
                Department           = (string)el.Element("fields").Element("Department"),
                Gender               = (string)el.Element("fields").Element("Gender"),
                Image1               = (string)el.Element("fields").Element("Image1_Large"),
                Image2               = (string)el.Element("fields").Element("Image2_Large"),
                Image3               = (string)el.Element("fields").Element("Image3_Large"),
                Image4               = (string)el.Element("fields").Element("Image4_Large"),
                Style                = (string)el.Element("fields").Element("Product_Style"),
                CustomerRating       = (string)el.Element("fields").Element("Product_Rating"),
                MerchantCategoryName = (string)el.Element("fields").Element("MerchantCategoryName")
            };

            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
            {
                RegionInfo ri = null;
                try
                {
                    ri = new RegionInfo(ci.Name);
                }
                catch
                {
                    // If a RegionInfo object could not be created we don't want to use the CultureInfo
                    // for the country list.
                    continue;
                }
                if (ri.EnglishName == countryFilter)
                {
                    countryFilter = ri.TwoLetterISORegionName;
                    break;
                }
            }
            if (!String.IsNullOrEmpty(countryFilter))
            {
                xmlProducts = xmlProducts.Where(p => p.Country == countryFilter);
            }

            if (!String.IsNullOrEmpty(cityFilter))
            {
                xmlProducts = xmlProducts.Where(p => p.City == cityFilter);
            }

            long countProducts  = xmlProducts.Count();
            long currentProduct = 0;

            try
            {
                using (SelectedHotelsEntities db = new SelectedHotelsEntities())
                {
                    var category = db.Categories.Find(categoryId);

                    foreach (var xmlProduct in xmlProducts)
                    {
                        string productName = xmlProduct.Name.Replace("&apos;", "'");
#if DEBUG
                        Console.WriteLine(productName); // debug print
#endif
                        Category subCategory = db.Categories.SingleOrDefault(
                            c =>
                            c.Name == xmlProduct.Category &&
                            c.ParentId == categoryId);
                        if (subCategory == null)
                        {
                            subCategory           = new Category();
                            subCategory.Name      = xmlProduct.Category;
                            subCategory.ParentId  = categoryId;
                            subCategory.IsDeleted = false;
                            db.Categories.Add(subCategory);
                            db.SaveChanges();
                        }

                        MerchantCategory merchantCategory = null;
                        if (!String.IsNullOrEmpty(xmlProduct.MerchantCategoryName))
                        {
                            string[] merchantCategoryNames = xmlProduct.MerchantCategoryName.Split(new[] { " > " },
                                                                                                   StringSplitOptions.None);
                            string           childCategoryName  = merchantCategoryNames[1];
                            string           parentCategoryName = merchantCategoryNames[0];
                            MerchantCategory parentCategory     =
                                db.MerchantCategories.SingleOrDefault(
                                    mc => mc.Name == parentCategoryName && mc.ParentId == null);
                            if (parentCategory == null)
                            {
                                parentCategory      = new MerchantCategory();
                                parentCategory.Name = parentCategoryName;
                                db.MerchantCategories.Add(parentCategory);
                                db.SaveChanges();
                            }
                            merchantCategory =
                                db.MerchantCategories.SingleOrDefault(
                                    mc => mc.Name == childCategoryName && mc.ParentId == parentCategory.Id);
                            if (merchantCategory == null)
                            {
                                merchantCategory          = new MerchantCategory();
                                merchantCategory.Name     = childCategoryName;
                                merchantCategory.ParentId = parentCategory.Id;
                                db.MerchantCategories.Add(merchantCategory);
                                db.SaveChanges();
                            }
                        }

                        Brand brand = null;
                        if (!String.IsNullOrEmpty(xmlProduct.Brand))
                        {
                            brand =
                                db.Brands.SingleOrDefault(b => b.Name == xmlProduct.Brand);
                            if (brand == null)
                            {
                                brand      = new Brand();
                                brand.Name = xmlProduct.Brand;
                                db.Brands.Add(brand);
                                db.SaveChanges();
                            }
                        }

                        Colour colour = null;
                        if (!String.IsNullOrEmpty(xmlProduct.Colours))
                        {
                            colour =
                                db.Colours.SingleOrDefault(c => c.Name == xmlProduct.Colours);
                            if (colour == null)
                            {
                                colour      = new Colour();
                                colour.Name = xmlProduct.Colours;
                                db.Colours.Add(colour);
                                db.SaveChanges();
                            }
                        }

                        Gender gender = db.Genders.SingleOrDefault(g => g.Name == xmlProduct.Gender);

                        if (category.Name == "Clothes")
                        {
                            Cloth product =
                                db.Products.OfType <Cloth>().SingleOrDefault(
                                    p =>
                                    p.Name == productName &&
                                    p.Number == xmlProduct.ProductNumber);
                            if (product == null)
                            {
                                product = new Cloth
                                {
                                    Name             = xmlProduct.Name.Replace("&apos;", "'"),
                                    ProductTypeId    = (int)Enums.ProductTypeEnum.Clothes,
                                    Number           = xmlProduct.ProductNumber,
                                    UnitCost         = xmlProduct.UnitCost,
                                    CurrencyCode     = xmlProduct.CurrencyCode,
                                    Description      = xmlProduct.Description,
                                    URL              = xmlProduct.URL,
                                    Image            = xmlProduct.Image,
                                    MerchantCategory = merchantCategory,
                                    CreatedByUser    = vendorId,
                                    CreatedDate      = DateTime.Now,
                                    IsDeleted        = false
                                };

                                product.Categories.Add(subCategory);
                                product.Brand  = brand;
                                product.Colour = colour;
                                product.Gender = gender;
                                if (xmlProduct.CustomerRating != null)
                                {
                                    MatchCollection matches = Regex.Matches(xmlProduct.CustomerRating,
                                                                            @"([0-9]*\.?[0-9]*)(.*)");
                                    if (matches != null)
                                    {
                                        product.CustomerRating = decimal.Parse(matches[0].Groups[1].Value);
                                    }
                                }
                                product.FeedId = feedId;
                                db.Products.Add(product);
                                db.SaveChanges();

                                List <string> imageURLList = new List <string>
                                {
                                    xmlProduct.Image1,
                                    xmlProduct.Image2,
                                    xmlProduct.Image3,
                                    xmlProduct.Image4
                                };
                                foreach (var imageURL in imageURLList)
                                {
                                    if (!String.IsNullOrEmpty(imageURL))
                                    {
                                        ProductImage productImage = new ProductImage {
                                            URL = imageURL
                                        };
                                        product.ProductImages.Add(productImage);
                                    }
                                }
                                db.SaveChanges();

                                if (!String.IsNullOrEmpty(xmlProduct.Style))
                                {
                                    List <string> styleList = new List <string>();
                                    if (xmlProduct.Style.Contains(","))
                                    {
                                        foreach (var style in xmlProduct.Style.Split(','))
                                        {
                                            if (!String.IsNullOrEmpty(style))
                                            {
                                                styleList.Add(style);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        styleList.Add(xmlProduct.Style);
                                    }

                                    foreach (var styleName in styleList)
                                    {
                                        Style style = db.Styles.SingleOrDefault(s => s.Name == styleName);
                                        if (style == null)
                                        {
                                            style = new Style {
                                                Name = styleName
                                            };
                                        }
                                        product.Styles.Add(style);
                                    }
                                }

                                if (!String.IsNullOrEmpty(xmlProduct.Department))
                                {
                                    List <string> departmentList = new List <string>();
                                    if (xmlProduct.Department.Contains(","))
                                    {
                                        foreach (var department in xmlProduct.Department.Split(','))
                                        {
                                            if (!String.IsNullOrEmpty(department))
                                            {
                                                departmentList.Add(department);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        departmentList.Add(xmlProduct.Department);
                                    }

                                    foreach (var departmentName in departmentList)
                                    {
                                        Department department = db.Departments.SingleOrDefault(d => d.Name == departmentName);
                                        if (department == null)
                                        {
                                            department = new Department {
                                                Name = departmentName
                                            };
                                        }
                                        product.Departments.Add(department);
                                    }
                                }

                                List <string> clothSizeList = new List <string>();
                                if (xmlProduct.Size.Contains(","))
                                {
                                    foreach (var size in xmlProduct.Size.Split(','))
                                    {
                                        if (!String.IsNullOrEmpty(size))
                                        {
                                            clothSizeList.Add(size);
                                        }
                                    }
                                }
                                else
                                {
                                    if (!String.IsNullOrEmpty(xmlProduct.Size))
                                    {
                                        clothSizeList.Add(xmlProduct.Size);
                                    }
                                }

                                clothSizeList = clothSizeList.Distinct().ToList();
                                foreach (var sizeName in clothSizeList)
                                {
                                    if (!String.IsNullOrEmpty(sizeName))
                                    {
                                        Size size = db.Sizes.SingleOrDefault(s => s.Name == sizeName);
                                        if (size == null)
                                        {
                                            size = new Size {
                                                Name = sizeName
                                            };
                                        }
                                        product.Sizes.Add(size);
                                    }
                                }
                                db.SaveChanges();
                            }
                            else
                            {
                                product.UnitCost         = xmlProduct.UnitCost;
                                product.CurrencyCode     = xmlProduct.CurrencyCode;
                                product.Description      = xmlProduct.Description;
                                product.URL              = xmlProduct.URL;
                                product.Image            = xmlProduct.Image;
                                product.MerchantCategory = merchantCategory;
                                product.Colour           = colour;
                                product.ProductTypeId    = (int)Enums.ProductTypeEnum.Clothes;
                                product.Brand            = brand;
                                product.Gender           = gender;
                                if (xmlProduct.CustomerRating != null)
                                {
                                    MatchCollection matches = Regex.Matches(xmlProduct.CustomerRating,
                                                                            @"([0-9]*\.?[0-9]*)(.*)");
                                    if (matches != null)
                                    {
                                        product.CustomerRating = decimal.Parse(matches[0].Groups[1].Value);
                                    }
                                }
                                db.SaveChanges();

                                if (!product.Categories.Contains(subCategory))
                                {
                                    product.Categories.Add(subCategory);
                                }
                                var productCategoriesToRemove = product.Categories.Where(pc => pc.Name != subCategory.Name);
                                foreach (Category productCategoryToRemove in productCategoriesToRemove.ToList())
                                {
                                    product.Categories.Remove(productCategoryToRemove);
                                }
                                db.SaveChanges();

                                List <string> imageURLList = new List <string>
                                {
                                    xmlProduct.Image1,
                                    xmlProduct.Image2,
                                    xmlProduct.Image3,
                                    xmlProduct.Image4
                                };
                                foreach (var imageURL in imageURLList)
                                {
                                    if (!String.IsNullOrEmpty(imageURL))
                                    {
                                        ProductImage productImage =
                                            product.ProductImages.FirstOrDefault(pi => pi.URL == imageURL);
                                        if (productImage == null)
                                        {
                                            productImage = new ProductImage {
                                                URL = imageURL
                                            };
                                            product.ProductImages.Add(productImage);
                                        }
                                    }
                                }
                                db.SaveChanges();

                                var productImagesToRemove = db.ProductImages.Where(pi => pi.ProductId == product.Id &&
                                                                                   imageURLList.All(
                                                                                       xe => xe != pi.URL));
                                if (productImagesToRemove.Any())
                                {
                                    db.ProductImages.RemoveRange(productImagesToRemove);
                                }
                                db.SaveChanges();

                                List <string> styleList = new List <string>();
                                if (!String.IsNullOrEmpty(xmlProduct.Style))
                                {
                                    if (xmlProduct.Style.Contains(","))
                                    {
                                        foreach (var style in xmlProduct.Style.Split(','))
                                        {
                                            if (!String.IsNullOrEmpty(style))
                                            {
                                                styleList.Add(style);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        styleList.Add(xmlProduct.Style);
                                    }
                                }

                                List <string> oldStyleList = product.Styles.Select(s => s.Name).ToList();
                                if (!styleList.OrderBy(s => s).SequenceEqual(oldStyleList.OrderBy(s => s)))
                                {
                                    foreach (var styleName in styleList)
                                    {
                                        if (!String.IsNullOrEmpty(styleName))
                                        {
                                            Style style = db.Styles.SingleOrDefault(s => s.Name == styleName);
                                            if (style == null)
                                            {
                                                style = new Style {
                                                    Name = styleName
                                                };
                                            }
                                            product.Styles.Add(style);
                                        }
                                    }
                                    db.SaveChanges();

                                    var stylesToRemove = product.Styles.Where(s => styleList.All(sl => sl != s.Name));
                                    if (stylesToRemove.Any())
                                    {
                                        foreach (var styleToRemove in stylesToRemove.ToList())
                                        {
                                            product.Styles.Remove(styleToRemove);
                                        }
                                        db.SaveChanges();
                                    }
                                }

                                List <string> departmentList = new List <string>();
                                if (!String.IsNullOrEmpty(xmlProduct.Department))
                                {
                                    if (xmlProduct.Department.Contains(","))
                                    {
                                        foreach (var department in xmlProduct.Department.Split(','))
                                        {
                                            if (!String.IsNullOrEmpty(department))
                                            {
                                                departmentList.Add(department);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        departmentList.Add(xmlProduct.Department);
                                    }
                                }

                                List <string> oldDepartmentList = product.Departments.Select(d => d.Name).ToList();
                                if (!departmentList.OrderBy(s => s).SequenceEqual(oldDepartmentList.OrderBy(s => s)))
                                {
                                    foreach (var departmentName in departmentList)
                                    {
                                        if (!String.IsNullOrEmpty(departmentName))
                                        {
                                            Department department = db.Departments.SingleOrDefault(d => d.Name == departmentName);
                                            if (department == null)
                                            {
                                                department = new Department {
                                                    Name = departmentName
                                                };
                                            }
                                            product.Departments.Add(department);
                                        }
                                    }
                                    db.SaveChanges();

                                    var departmentsToRemove = product.Departments.Where(d => departmentList.All(dl => dl != d.Name));
                                    if (departmentsToRemove.Any())
                                    {
                                        foreach (var departmentToRemove in departmentsToRemove.ToList())
                                        {
                                            product.Departments.Remove(departmentToRemove);
                                        }
                                        db.SaveChanges();
                                    }
                                }

                                List <string> clothSizeList = new List <string>();
                                if (xmlProduct.Size.Contains(","))
                                {
                                    foreach (var size in xmlProduct.Size.Split(','))
                                    {
                                        if (!String.IsNullOrEmpty(size))
                                        {
                                            clothSizeList.Add(size);
                                        }
                                    }
                                }
                                else
                                {
                                    if (!String.IsNullOrEmpty(xmlProduct.Size))
                                    {
                                        clothSizeList.Add(xmlProduct.Size);
                                    }
                                }

                                clothSizeList = clothSizeList.Distinct().ToList();
                                List <string> oldClothSizeList = product.Sizes.Select(s => s.Name).ToList();
                                if (!clothSizeList.OrderBy(s => s).SequenceEqual(oldClothSizeList.OrderBy(s => s)))
                                {
                                    foreach (var sizeName in clothSizeList)
                                    {
                                        if (!String.IsNullOrEmpty(sizeName))
                                        {
                                            Size size = db.Sizes.SingleOrDefault(s => s.Name == sizeName);
                                            if (size == null)
                                            {
                                                size = new Size {
                                                    Name = sizeName
                                                };
                                            }
                                            product.Sizes.Add(size);
                                        }
                                    }
                                    db.SaveChanges();

                                    var sizesToRemove = product.Sizes.Where(s => clothSizeList.All(sl => sl != s.Name));
                                    if (sizesToRemove.Any())
                                    {
                                        foreach (var sizeToRemove in sizesToRemove.ToList())
                                        {
                                            product.Sizes.Remove(sizeToRemove);
                                        }
                                        db.SaveChanges();
                                    }
                                }
                            }

                            db.Database.SqlQuery <string>(@"
                                DELETE FROM Cowrie_ClothViews
                            ");
                            db.Database.SqlQuery <string>(@"
                                INSERT INTO Cowrie_ClothViews
                                SELECT        Extent1.Id, Extent2.Name, Cowrie_Brands.Name AS BrandName
                                FROM            Cowrie_Clothes AS Extent1 INNER JOIN
                                                         Cowrie_Products AS Extent2 ON Extent1.Id = Extent2.Id INNER JOIN
                                                         Cowrie_Brands ON Extent1.BrandId = Cowrie_Brands.Id
                            ");
                        }
#if HOMEANDGARDENS
                        else if (categoryId == (int)Enums.ProductTypeEnum.HomeAndGardens)
                        {
                            HomeAndGarden product =
                                db.Products.OfType <HomeAndGarden>().SingleOrDefault(
                                    p =>
                                    p.Name == productName &&
                                    p.Number == xmlProduct.ProductNumber);
                            if (product == null)
                            {
                                product = new HomeAndGarden
                                {
                                    Name          = xmlProduct.Name.Replace("&apos;", "'"),
                                    ProductTypeId = (int)Enums.ProductTypeEnum.HomeAndGardens,
                                    Number        = xmlProduct.ProductNumber,
                                    UnitCost      = xmlProduct.UnitCost,
                                    Description   = xmlProduct.Description,
                                    URL           = xmlProduct.URL,
                                    Image         = xmlProduct.Image,
                                    CreatedByUser = vendorId,
                                    Weight        = xmlProduct.Weight,
                                    Size          = xmlProduct.Size,
                                    Brand         = xmlProduct.Brand,
                                    Model         = xmlProduct.Model,
                                    Manufacturer  = xmlProduct.Manufacturer,
                                };

                                product.Categories.Add(subCategory);
                                db.Products.Add(product);
                                db.SaveChanges();
                            }
                            else
                            {
                                product.UnitCost    = xmlProduct.UnitCost;
                                product.Description = xmlProduct.Description;
                                product.URL         = xmlProduct.URL;
                                product.Categories.Add(subCategory);
                                db.SaveChanges();
                            }

                            StringBuilder sb = new StringBuilder();
                            sb.AppendLine("DELETE");
                            sb.AppendLine("FROM         Cowrie_ProductImages");
                            sb.AppendLine("WHERE     ProductID = @ProductID");
                            SqlCommand commandDelete =
                                new SqlCommand(
                                    sb.ToString(),
                                    destinationConnection);
                            commandDelete.Parameters.Add("@ProductID", SqlDbType.Int);
                            commandDelete.Parameters["@ProductID"].Value = product.Id;
                            commandDelete.ExecuteNonQuery();

                            bool isChanged = false;
                            if (xmlProduct.Image1 != null)
                            {
                                ProductImage productImage = new ProductImage();
                                productImage.URL = xmlProduct.Image1;
                                product.ProductImages.Add(productImage);
                                isChanged = true;
                            }
                            if (xmlProduct.Image2 != null)
                            {
                                ProductImage productImage = new ProductImage();
                                productImage.URL = xmlProduct.Image2;
                                product.ProductImages.Add(productImage);
                                isChanged = true;
                            }
                            if (xmlProduct.Image3 != null)
                            {
                                ProductImage productImage = new ProductImage();
                                productImage.URL = xmlProduct.Image3;
                                product.ProductImages.Add(productImage);
                                isChanged = true;
                            }
                            if (xmlProduct.Image4 != null)
                            {
                                ProductImage productImage = new ProductImage();
                                productImage.URL = xmlProduct.Image4;
                                product.ProductImages.Add(productImage);
                                isChanged = true;
                            }
                            if (isChanged)
                            {
                                db.SaveChanges();
                            }
                        }
#endif
                        currentProduct++;
                        if (bw.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }
                        else if (bw.WorkerReportsProgress && currentProduct % 100 == 0)
                        {
                            bw.ReportProgress((int)(100 * currentProduct / countProducts));
                        }
                    }
                }
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName,
                                               validationError.ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                e.Result = "ERROR:" + ex.Message;
                //message = ex.Message;
                log.Error("Error error logging", ex);
            }
        }
        public static void DoImport(object sender, DoWorkEventArgs e)               //(string _URL, out string message)        // public static bool
        {
            //bool rc = false;
            //message = String.Empty;
            BackgroundWorker bw = sender as BackgroundWorker;
            // Parse list input parameters
            BackgroundWorkParameters param = (BackgroundWorkParameters)e.Argument;
            string _URL                 = param.Url;
            int    categoryId           = param.CategoryId;
            int    portalId             = param.PortalId;
            int    vendorId             = param.VendorId;
            string advancedCategoryRoot = param.AdvancedCategoryRoot;
            string countryFilter        = param.CountryFilter;
            string cityFilter           = param.CityFilter;
            int    feedId               = param.FeedId;

            string xmlFileName = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, "webgainsproducts.xml");

            if (File.Exists(xmlFileName))
            {
                File.Delete(xmlFileName);
            }
            // inside function display progressbar
            SaveFileFromURL(_URL, xmlFileName, 60, bw, e);

            // exit if user cancel during saving file or error
            if (e.Cancel || (e.Result != null) && e.Result.ToString().Substring(0, 6).Equals("ERROR:"))
            {
                return;
            }

            _URL = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, "webgainsproducts.xml");

            XmlSchemaSet schemas = new XmlSchemaSet();

            schemas.Add("", "webgainsproducts.xsd");
            Form1.activeStep = "Validating inpout..";
            bw.ReportProgress(0);  // start new step of background process
            XDocument xDoc   = XDocument.Load(_URL);
            bool      errors = false;

            xDoc.Validate(schemas, (o, e2) =>
            {
                e.Result = "ERROR:" + e2.Message;
                log.Error(e2.Message);
                errors = true;
            });
            if (errors)
            {
                e.Cancel = true;
                return;
            }

            // Set step for backgroundWorker
            Form1.activeStep = "Import records..";
            bw.ReportProgress(0);           // start new step of background process

            var products =
                from el in StreamRootChildDoc(_URL)
                select new
            {
                Category        = (string)el.Element("categories").Element("category"),
                CategoryPath    = (string)el.Element("categories").Element("category").Attribute("path"),
                ProductNumber   = (string)el.Element("product_id"),
                Name            = (string)el.Element("product_name"),
                Image           = (string)el.Element("image_url"),
                UnitCost        = (decimal)el.Element("price"),
                Description     = (string)el.Element("description"),
                DescriptionHTML = (string)el.Element("description"),
                URL             = (string)el.Element("deeplink")
            };


            long countProducts  = products.Count();
            long currentProduct = 0;

            try
            {
                using (SelectedHotelsEntities db = new SelectedHotelsEntities())
                {
                    foreach (var product in products)
                    {
                        Console.WriteLine(product.Name.Replace("&apos;", "'")); // debug print

                        Category category;
                        if (!String.IsNullOrEmpty(product.CategoryPath))
                        {
                            if (product.CategoryPath.Contains(" > "))
                            {
                                string[] categoryNames = product.CategoryPath.Split(new[] { " > " },
                                                                                    StringSplitOptions.
                                                                                    RemoveEmptyEntries);
                                string rootCategoryName = categoryNames[0];

                                category =
                                    db.Categories.SingleOrDefault(
                                        c => c.Name == rootCategoryName && c.ParentId == null);
                                if (category == null)
                                {
                                    category = new Category
                                    {
                                        IsDeleted = false,
                                        Name      = rootCategoryName
                                    };
                                    db.Categories.Add(category);
                                    db.SaveChanges();
                                }
                                Category parentCategory = category;

                                string subCategoryName = categoryNames[1];
                                category =
                                    db.Categories.SingleOrDefault(
                                        c => c.Name == subCategoryName && c.ParentId == parentCategory.Id);
                                if (category == null)
                                {
                                    category = new Category
                                    {
                                        IsDeleted      = false,
                                        Name           = subCategoryName,
                                        ParentCategory = parentCategory
                                    };
                                    db.Categories.Add(category);
                                    db.SaveChanges();
                                }
                            }
                            else
                            {
                                category =
                                    db.Categories.SingleOrDefault(
                                        c => c.Name == product.CategoryPath && c.ParentId == null);
                                if (category == null)
                                {
                                    category = new Category
                                    {
                                        IsDeleted = false,
                                        Name      = product.CategoryPath
                                    };
                                    db.Categories.Add(category);
                                    db.SaveChanges();
                                }
                            }
                        }
                        else
                        {
                            category =
                                db.Categories.SingleOrDefault(
                                    c => c.Name == product.Category && c.ParentId == null);
                            if (category == null)
                            {
                                category = new Category
                                {
                                    IsDeleted = false,
                                    Name      = product.Category
                                };
                                db.Categories.Add(category);
                                db.SaveChanges();
                            }
                        }

                        var product2 = db.Products.SingleOrDefault(p => p.Name == product.Name && p.Number == product.ProductNumber);
                        if (product2 == null)
                        {
                            product2 = new Product
                            {
                                Name          = product.Name.Replace("&apos;", "'"),
                                Number        = product.ProductNumber,
                                UnitCost      = product.UnitCost,
                                Description   = product.Description,
                                URL           = product.URL,
                                Image         = product.Image,
                                CreatedByUser = vendorId,
                                IsDeleted     = false
                            };

                            product2.FeedId = feedId;
                            db.Products.Add(product2);
                            db.SaveChanges();
                        }
                        else
                        {
                            product2.UnitCost    = product.UnitCost;
                            product2.Description = product.Description;
                            product2.URL         = product.URL;
                            product2.Image       = product.Image;
                            db.SaveChanges();
                        }

                        // add product to advanced categories
                        if (!product2.Categories.Contains(category))
                        {
                            product2.Categories.Add(category);
                            db.SaveChanges();
                        }

                        currentProduct++;
                        if (bw.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }
                        else if (bw.WorkerReportsProgress && currentProduct % 100 == 0)
                        {
                            bw.ReportProgress((int)(100 * currentProduct / countProducts));
                        }
                    }
                    // rc = true;
                }
            }
            catch (Exception ex)
            {
                e.Result = "ERROR:" + ex.Message;
                //message = ex.Message;
                log.Error("Error error logging", ex);
            }
            //return rc;
        }
示例#4
0
        private void StartWorkerProcess(int?stepImport = null, int?stepAddToCategories = null, int?stepAddImages = null)
        {
            string keyDownload;

            foreach (DataGridViewRow selectedRow in dataGridView1.SelectedRows)
            {
                var selectedFeed = selectedRow.DataBoundItem as Feed;
                keyDownload = selectedFeed.Name;
                if (!bgw.Keys.Contains(keyDownload))
                {
                    context = new SelectedHotelsEntities();
                    feed    = context.Feeds.SingleOrDefault(f => f.Id == selectedFeed.Id);
                    bgProcesses.Add(keyDownload, feed);
                    // set parameters for background process here if lists of parameters are the same for different rows in Feeds
                    // if it is necessary send different parameter`s list to background operation, assign parameters in block
                    // 'switch' as well as set 'BackGroundWorkerDelegateWork' for each row in Feeds
                    BackgroundWorkParameters bgParams = new BackgroundWorkParameters();
                    bgParams.Url = selectedFeed.URL;
                    if (!String.IsNullOrEmpty(selectedFeed.Category))
                    {
                        using (SelectedHotelsEntities db = new SelectedHotelsEntities())
                        {
                            var category = db.Categories.SingleOrDefault(c => c.Name == selectedFeed.Category);
                            if (category != null)
                            {
                                bgParams.CategoryId = category.Id;
                            }
                            else
                            {
                                category      = new Category();
                                category.Name = selectedFeed.Category;
                                db.Categories.Add(category);
                                db.SaveChanges();
                                bgParams.CategoryId = category.Id;
                            }
                        }
                    }
                    bgParams.PortalId             = selectedFeed.PortalId;
                    bgParams.VendorId             = selectedFeed.VendorId;
                    bgParams.AdvancedCategoryRoot = selectedFeed.AdvancedCategoryRoot;
                    bgParams.CountryFilter        = selectedFeed.CountryFilter;
                    bgParams.CityFilter           = selectedFeed.CityFilter;
                    bgParams.StepImport           = stepImport;
                    bgParams.StepAddToCategories  = stepAddToCategories;
                    bgParams.StepAddImages        = stepAddImages;
                    bgParams.FeedId = selectedFeed.Id;

                    switch (keyDownload)
                    {
                    case "Laterooms":
                    case "Laterooms (filtered)":
                        workD = ImportLaterooms.DoImport;
                        break;

                    case "Trade Doubler":
                    case "Home and garden":
                    case "Clothes":
                        workD = ImportTradeDoublerProducts.DoImport;
                        break;

                    case "Trade Doubler hotels.com EMEA2":
                    case "Trade Doubler hotels.com EMEA1":
                    case "Trade Doubler expedia":
                    case "Trade Doubler Hotels":
                        workD = ImportTradeDoublerHotels.DoImport;
                        break;

                    case "Productserve":
#if ImportProductserve
                        workD = ImportProductserve.DoImport;
#endif
                        break;

                    case "Webgains":
                        workD = ImportWebgainsProducts.DoImport;
                        break;

                    case "Excel":
                        workD = ImportExcelHotels.DoImport;
                        break;
                    }
                    progressD = backgroundWorkerProgressChanged;
                    completeD = backgroundWorkerRunWorkerCompleted;
                    bgw.Add(keyDownload, AddBackGroundWorker(workD, progressD, completeD));
                    bgProgress.Add(keyDownload, 0);
                    activeStep = "Start download";
                    bgStep.Add(keyDownload, activeStep);
                    bgw[keyDownload].RunWorkerAsync(bgParams);      // selectedFeed.URL
                }
                activeKey = keyDownload;
                // display status
                ShiftStatusStripToActiveBackgroudWorker(bgw.Count, activeKey, activeStep, bgw[activeKey].IsBusy, bgProgress[activeKey]);
            }
        }
示例#5
0
        public static void DoImport(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;
            // Parse list input parameters
            BackgroundWorkParameters param = (BackgroundWorkParameters)e.Argument;
            string _URL          = param.Url;
            int    categoryId    = param.CategoryId;
            int    portalId      = param.PortalId;
            int    vendorId      = param.VendorId;
            string countryFilter = param.CountryFilter;
            string cityFilter    = param.CityFilter;
            int?   stepImport    = param.StepImport;
            int    feedId        = param.FeedId;

            var excel     = new ExcelQueryFactory(_URL);
            var xlsHotels = from h in excel.Worksheet <HotelView>("Query")
                            where h.Name != String.Empty
                            select h;

            // Set step for backgroundWorker
            Form1.activeStep = "Import records..";
            bw.ReportProgress(0); // start new step of background process
            int productCount = xlsHotels.Count();

            try
            {
                int initialStep = 0;
                if (stepImport.HasValue)
                {
                    initialStep = stepImport.Value;
                }

                using (SelectedHotelsEntities db = new SelectedHotelsEntities())
                {
                    int i = 0;
                    foreach (HotelView hotelView in xlsHotels)
                    {
                        if (i < initialStep)
                        {
                            i++;
                            continue;
                        }

#if DEBUG
                        Console.WriteLine(i + " - " + hotelView.Name); // debug print
#endif

                        // create new product record
                        Hotel hotel =
                            db.Products.SingleOrDefault(
                                p => p.ProductTypeId == (int)Enums.ProductTypeEnum.Hotels && p.Categories.Any(c => c.Id == categoryId) && p.Name == hotelView.Name && p.Number == hotelView.Number) as Hotel;
                        if (hotel == null)
                        {
                            hotel = new Hotel();
                            hotel.ProductTypeId  = (int)Enums.ProductTypeEnum.Hotels;
                            hotel.Name           = hotelView.Name;
                            hotel.Number         = hotelView.Number;
                            hotel.UnitCost       = hotelView.UnitCost;
                            hotel.Description    = hotelView.Description;
                            hotel.URL            = hotelView.URL.Replace("[[PARTNERID]]", "2248").Trim(' ');
                            hotel.Image          = hotelView.Image;
                            hotel.Star           = hotelView.Star;
                            hotel.CustomerRating = hotelView.CustomerRating;
                            hotel.Rooms          = hotelView.Rooms;
                            hotel.Address        = hotelView.Address;
                            hotel.PostCode       = hotelView.PostCode;
                            hotel.CurrencyCode   = hotelView.CurrencyCode;
                            hotel.Location       = DbGeography.FromText(String.Format("POINT({0} {1})", hotelView.Lon, hotelView.Lat));
                            hotel.HotelTypeId    = hotelView.HotelTypeId;
                            hotel.CreatedByUser  = vendorId;
                            hotel.CreatedDate    = DateTime.Now;
                            hotel.IsDeleted      = false;

                            Category category = db.Categories.Find(categoryId);
                            if (category != null)
                            {
                                hotel.Categories.Add(category);
                            }

                            hotel.FeedId = feedId;
                            db.Products.Add(hotel);

                            db.SaveChanges();

                            i++;
                            //Common.UpdateSteps(stepImport: i);
                        }
                        else
                        {
                            // no need to check for null vallue because of previous if
                            bool    isChanged = false;
                            decimal?unitCost  = hotelView.UnitCost;
                            if (hotel.UnitCost != unitCost)
                            {
                                hotel.UnitCost = unitCost;
                                isChanged      = true;
                            }
                            if (hotel.Description != hotelView.Description)
                            {
                                hotel.Description = hotelView.Description;
                                isChanged         = true;
                            }
                            if (hotel.URL != hotelView.URL.Replace("[[PARTNERID]]", "2248").Trim(' '))
                            {
                                hotel.URL = hotelView.URL.Replace("[[PARTNERID]]", "2248").Trim(' ');
                                isChanged = true;
                            }
                            if (hotel.Image != (string)hotelView.Image)
                            {
                                hotel.Image = (string)hotelView.Image;
                                isChanged   = true;
                            }
                            decimal?star = hotelView.Star;
                            if (hotel.Star != star)
                            {
                                hotel.Star = star;
                                isChanged  = true;
                            }
                            decimal?customerRating = hotelView.CustomerRating;
                            if (hotel.CustomerRating != customerRating)
                            {
                                hotel.CustomerRating = customerRating;
                                isChanged            = true;
                            }
                            int?rooms = hotelView.Rooms;
                            if (hotel.Rooms != rooms)
                            {
                                hotel.Rooms = rooms;
                                isChanged   = true;
                            }
                            if (hotel.Address != hotelView.Address)
                            {
                                hotel.Address = hotelView.Address;
                                isChanged     = true;
                            }
                            if (hotel.PostCode != hotelView.PostCode)
                            {
                                hotel.PostCode = hotelView.PostCode;
                                isChanged      = true;
                            }
                            if (hotel.CurrencyCode != hotelView.CurrencyCode)
                            {
                                hotel.CurrencyCode = hotelView.CurrencyCode;
                                isChanged          = true;
                            }
                            DbGeography location = DbGeography.FromText(String.Format("POINT({0} {1})", hotelView.Lon, hotelView.Lat));
                            if (hotel.Location != location)
                            {
                                hotel.Location = DbGeography.FromText(String.Format("POINT({0} {1})", hotelView.Lon, hotelView.Lat));
                                isChanged      = true;
                            }
                            int hotelTypeId = hotelView.HotelTypeId;
                            if (hotel.HotelTypeId != hotelTypeId)
                            {
                                hotel.HotelTypeId = hotelTypeId;
                            }

#if UPDATELOCATION
                            int?     parentId   = null;
                            int?     locationId = hotel.LocationId;
                            Location country    =
                                db.Locations.SingleOrDefault(
                                    c =>
                                    c.Name == product.Country &&
                                    c.ParentId == null);
                            if (country == null)
                            {
                                country = new Location
                                {
                                    Name      = product.Country,
                                    IsDeleted = false
                                };
                                db.Locations.Add(country);
                                db.SaveChanges();
                            }
                            if (country != null)
                            {
                                hotel.Location           = country;
                                hotel.LocationId         = country.Id;
                                hotel.Location.IsDeleted = false;
                                parentId = country.Id;
                            }
                            Location county =
                                db.Locations.SingleOrDefault(
                                    c =>
                                    c.Name == product.County &&
                                    c.ParentId == parentId);
                            if (county == null)
                            {
                                county = new Location
                                {
                                    Name      = product.County,
                                    ParentId  = parentId,
                                    IsDeleted = false
                                };
                                db.Locations.Add(county);
                                db.SaveChanges();
                            }
                            if (county != null)
                            {
                                hotel.Location           = county;
                                hotel.LocationId         = county.Id;
                                hotel.Location.IsDeleted = false;
                                parentId = county.Id;
                            }
                            Location city =
                                db.Locations.SingleOrDefault(
                                    c =>
                                    c.Name == product.City &&
                                    c.ParentId == parentId);
                            if (city == null)
                            {
                                city = new Location
                                {
                                    Name      = product.City,
                                    ParentId  = parentId,
                                    IsDeleted = false
                                };
                                db.Locations.Add(city);
                                db.SaveChanges();
                            }
                            if (city != null)
                            {
                                hotel.Location           = city;
                                hotel.LocationId         = city.Id;
                                hotel.Location.IsDeleted = false;
                            }

                            if (hotel.LocationId != locationId)
                            {
                                isChanged = true;
                            }
#endif

                            if (isChanged)
                            {
                                db.SaveChanges();
                            }

                            i++;
                            //Common.UpdateSteps(stepImport: i);
                        }

                        if (bw.CancellationPending)
                        {
                            e.Cancel = true;
                            goto Cancelled;
                        }
                        else if (bw.WorkerReportsProgress && i % 100 == 0)
                        {
                            bw.ReportProgress((int)(100 * i / productCount));
                        }
                    }
                }

                if (!e.Cancel)
                {
                    Common.UpdateSteps();
                }
Cancelled:
                ;
            }
            catch (DbEntityValidationException exception)
            {
                foreach (var eve in exception.EntityValidationErrors)
                {
                    log.Error(String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                            eve.Entry.Entity.GetType().Name, eve.Entry.State), exception);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        log.Error(String.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                ve.PropertyName, ve.ErrorMessage), exception);
                    }
                }
                throw;
            }
            catch (Exception ex)
            {
                e.Result = "ERROR:" + ex.Message;
                log.Error("Error error logging", ex);
            }
        }
示例#6
0
        public static void DoImport(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;
            // Parse list input parameters
            BackgroundWorkParameters param = (BackgroundWorkParameters)e.Argument;
            string _URL          = param.Url;
            int    categoryId    = param.CategoryId;
            int    portalId      = param.PortalId;
            int    vendorId      = param.VendorId;
            string countryFilter = param.CountryFilter;
            string cityFilter    = param.CityFilter;
            int?   stepImport    = param.StepImport;
            int    feedId        = param.FeedId;

            if (!File.Exists(_URL))
            {
                // unzip file to temp folder if needed
                if (_URL.EndsWith(".zip"))
                {
                    string zipFileName = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath,
                                                       "Hotels_Standard.zip");
                    // inside this function show progressBar for step: LoadFile
                    if (File.Exists(zipFileName))
                    {
                        File.Delete(zipFileName);
                    }
                    Common.SaveFileFromURL(_URL, zipFileName, 60, bw, e, log);

                    // if user cancel during saving file or ERROR
                    if (e.Cancel || (e.Result != null) && e.Result.ToString().Substring(0, 6).Equals("ERROR:"))
                    {
                        return;
                    }
                    // Set step for backgroundWorker
                    Form1.activeStep = "Extract file..";
                    bw.ReportProgress(0); // start new step of background process

                    using (ZipFile zip1 = ZipFile.Read(zipFileName))
                    {
                        foreach (ZipEntry zipEntry in zip1)
                        {
                            zipEntry.Extract(Properties.Settings.Default.TempPath,
                                             ExtractExistingFileAction.OverwriteSilently);
                        }
                    }
                    _URL = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, "Hotels_Standard.xml");
                }
            }

            XmlSchemaSet schemas = new XmlSchemaSet();

            schemas.Add("", "Hotels_Standard.xsd");
            Form1.activeStep = "Validating input..";
            bw.ReportProgress(0); // start new step of background process
            XDocument xDoc   = XDocument.Load(_URL);
            bool      errors = false;

            xDoc.Validate(schemas, (o, e2) =>
            {
                e.Result = "ERROR:" + e2.Message;
                log.Error(e2.Message);
                errors = true;
            });
            if (errors)
            {
                e.Cancel = true;
                return;
            }

            // show progress & catch Cancel
            if (bw.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            else if (bw.WorkerReportsProgress)
            {
                bw.ReportProgress(50);
            }

            // read hotels from XML
            // use XmlReader to avoid huge file size dependence
            var xmlProducts =
                from el in Common.StreamRootChildDoc(_URL, "hotel")
                select new ProductView
            {
                Country         = (string)el.Element("hotel_country"),
                County          = (string)el.Element("hotel_county"),
                City            = (string)el.Element("hotel_city"),
                ProductNumber   = (string)el.Element("hotel_ref"),
                Name            = (string)el.Element("hotel_name"),
                Images          = el.Element("images"),
                UnitCost        = (string)el.Element("PricesFrom"),
                Description     = (string)el.Element("hotel_description"),
                DescriptionHTML = (string)el.Element("alternate_description"),
                URL             = (string)el.Element("hotel_link"),
                Star            = (string)el.Element("hotel_star"),
                CustomerRating  = (string)el.Element("customerrating"),
                Rooms           = (string)el.Element("hotel_total_rooms"),
                Address         = (string)el.Element("hotel_address"),
                PostCode        = (string)el.Element("hotel_pcode"),
                CurrencyCode    = (string)el.Element("CurrencyCode"),
                Lat             = (string)el.Element("geo_code").Element("lat"),
                Long            = (string)el.Element("geo_code").Element("long")
            };

            if (!String.IsNullOrEmpty(countryFilter))
            {
                xmlProducts = xmlProducts.Where(p => p.Country == countryFilter);
            }

            if (!String.IsNullOrEmpty(cityFilter))
            {
                xmlProducts = xmlProducts.Where(p => p.City == cityFilter);
            }

            // show progress & catch Cancel
            if (bw.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            else if (bw.WorkerReportsProgress)
            {
                bw.ReportProgress(100);
            }
            Thread.Sleep(100); // a little bit slow working for visualisation Progress

            // Set step for backgroundWorker
            Form1.activeStep = "Import records..";
            bw.ReportProgress(0); // start new step of background process
            int productCount = xmlProducts.Count();

            try
            {
                int initialStep = 0;
                if (stepImport.HasValue)
                {
                    initialStep = stepImport.Value;
                }

                using (SelectedHotelsEntities db = new SelectedHotelsEntities())
                {
                    int i = 0;
                    foreach (ProductView product in xmlProducts)
                    {
                        if (i < initialStep)
                        {
                            i++;
                            continue;
                        }

#if DEBUG
                        Console.WriteLine(i + " - " + product.Name); // debug print
#endif
                        // create new product record
                        Hotel hotel =
                            db.Products.SingleOrDefault(
                                p => p.ProductTypeId == (int)Enums.ProductTypeEnum.Hotels && p.Categories.Any(c => c.Id == categoryId) && p.Name == product.Name && p.Number == product.ProductNumber) as Hotel;
                        if (hotel == null)
                        {
                            hotel = new Hotel();
                            hotel.ProductTypeId = (int)Enums.ProductTypeEnum.Hotels;
                            hotel.Name          = product.Name;
                            hotel.Number        = product.ProductNumber;
                            if (!String.IsNullOrEmpty(product.UnitCost))
                            {
                                hotel.UnitCost = Convert.ToDecimal(product.UnitCost);
                            }
                            hotel.Description = product.Description;
                            hotel.URL         = product.URL.Replace("[[PARTNERID]]", "2248").Trim(' ');
                            hotel.Image       = (string)product.Images.Element("url");
                            int    star    = 0;
                            string strStar = new string(product.Star.TakeWhile(char.IsDigit).ToArray());
                            if (strStar.Length > 0)
                            {
                                star       = int.Parse(strStar);
                                hotel.Star = star;
                            }
                            if (!String.IsNullOrEmpty(product.CustomerRating))
                            {
                                hotel.CustomerRating = int.Parse(product.CustomerRating);
                            }
                            if (!String.IsNullOrEmpty(product.Rooms))
                            {
                                hotel.Rooms = int.Parse(product.Rooms);
                            }
                            if (!String.IsNullOrEmpty(product.Address))
                            {
                                hotel.Address = product.Address;
                            }
                            if (!String.IsNullOrEmpty(product.PostCode))
                            {
                                hotel.PostCode = product.PostCode;
                            }
                            if (!String.IsNullOrEmpty(product.CurrencyCode))
                            {
                                hotel.CurrencyCode = product.CurrencyCode;
                            }
                            hotel.CreatedByUser = vendorId;
                            hotel.CreatedDate   = DateTime.Now;
                            hotel.IsDeleted     = false;
                            hotel.HotelTypeId   = (int)Enums.HotelTypeEnum.Hotels;
                            hotel.FeedId        = feedId;
                            db.Products.Add(hotel);
                            db.SaveChanges();

                            Common.SetLocation(product, db, hotel, log);
                            Common.SetGeoNameId(product, db, hotel, log);

                            Category category = db.Categories.Find(categoryId);
                            if (category != null)
                            {
                                hotel.Categories.Add(category);
                            }
                            db.SaveChanges();

                            var imageURLs = product.Images.Elements("url").Where(xe => xe.Value.EndsWith(".jpg") || xe.Value.EndsWith(".png")).Select(xe => xe.Value);
                            IEnumerable <string> imageURLList = imageURLs as IList <string> ?? imageURLs.ToList();
                            foreach (var imageURL in imageURLList)
                            {
                                ProductImage productImage = new ProductImage
                                {
                                    URL = imageURL
                                };
                                hotel.ProductImages.Add(productImage);
                            }
                            db.SaveChanges();

                            i++;
                            Common.UpdateSteps(stepImport: i);
                        }
                        else
                        {
                            if (hotel.Location == null)
                            {
                                Common.SetLocation(product, db, hotel, log);
                            }
                            if (!hotel.GeoNameId.HasValue)
                            {
                                Common.SetGeoNameId(product, db, hotel, log);
                            }

                            decimal?unitCost = null;
                            if (!String.IsNullOrEmpty(product.UnitCost))
                            {
                                unitCost = decimal.Parse(product.UnitCost);
                            }
                            if (hotel.UnitCost != unitCost)
                            {
                                hotel.UnitCost = unitCost;
                            }
                            if (hotel.Description != product.Description)
                            {
                                hotel.Description = product.Description;
                            }
                            if (hotel.URL != product.URL.Replace("[[PARTNERID]]", "2248").Trim(' '))
                            {
                                hotel.URL = product.URL.Replace("[[PARTNERID]]", "2248").Trim(' ');
                            }
                            if (hotel.Image != (string)product.Images.Element("url"))
                            {
                                hotel.Image = (string)product.Images.Element("url");
                            }
                            int?   star    = null;
                            string strStar = new string(product.Star.TakeWhile(char.IsDigit).ToArray());
                            if (strStar.Length > 0)
                            {
                                star = int.Parse(strStar);
                            }
                            if (hotel.Star != star)
                            {
                                hotel.Star = star;
                            }
                            int?customerRating = null;
                            if (!String.IsNullOrEmpty(product.CustomerRating))
                            {
                                customerRating = int.Parse(product.CustomerRating);
                            }
                            if (hotel.CustomerRating != customerRating)
                            {
                                hotel.CustomerRating = customerRating;
                            }
                            int?rooms = null;
                            if (!String.IsNullOrEmpty(product.Rooms))
                            {
                                rooms = int.Parse(product.Rooms);
                            }
                            if (hotel.Rooms != rooms)
                            {
                                hotel.Rooms = rooms;
                            }
                            if (hotel.Address != product.Address)
                            {
                                hotel.Address = product.Address;
                            }
                            if (hotel.PostCode != product.PostCode)
                            {
                                hotel.PostCode = product.PostCode;
                            }
                            if (hotel.CurrencyCode != product.CurrencyCode)
                            {
                                hotel.CurrencyCode = product.CurrencyCode;
                            }
                            db.SaveChanges();

                            var imageURLs = product.Images.Elements("url").Where(xe => xe.Value.EndsWith(".jpg") || xe.Value.EndsWith(".png")).Select(xe => xe.Value);
                            IEnumerable <string> imageURLList = imageURLs as IList <string> ?? imageURLs.ToList();
                            foreach (var imageURL in imageURLList)
                            {
                                ProductImage productImage =
                                    hotel.ProductImages.SingleOrDefault(pi => pi.URL == imageURL);
                                if (productImage == null)
                                {
                                    productImage = new ProductImage {
                                        URL = imageURL
                                    };
                                    hotel.ProductImages.Add(productImage);
                                }
                            }
                            db.SaveChanges();

                            var productImagesToRemove = db.ProductImages.Where(pi => pi.ProductId == hotel.Id &&
                                                                               imageURLList.All(xe => xe != pi.URL));
                            if (productImagesToRemove.Any())
                            {
                                db.ProductImages.RemoveRange(productImagesToRemove);
                            }
                            db.SaveChanges();

                            i++;
                            Common.UpdateSteps(stepImport: i);
                        }

                        if (bw.CancellationPending)
                        {
                            e.Cancel = true;
                            goto Cancelled;
                        }
                        else if (bw.WorkerReportsProgress && i % 100 == 0)
                        {
                            bw.ReportProgress((int)(100 * i / productCount));
                        }
                    }
                }

                if (!e.Cancel)
                {
                    Common.UpdateSteps();
                }
Cancelled:
                ;
            }
            catch (DbEntityValidationException exception)
            {
                foreach (var eve in exception.EntityValidationErrors)
                {
                    log.Error(String.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                            eve.Entry.Entity.GetType().Name, eve.Entry.State), exception);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        log.Error(String.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                ve.PropertyName, ve.ErrorMessage), exception);
                    }
                }
                throw;
            }
            catch (Exception ex)
            {
                e.Result = "ERROR:" + ex.Message;
                log.Error("Error error logging", ex);
            }
        }
示例#7
0
        public static void DoImport(object sender, DoWorkEventArgs e)               // static bool    (string _URL, out string message)
        {
            //bool rc = false;
            //message = String.Empty;
            BackgroundWorker bw = sender as BackgroundWorker;
            // Parse list input parameters
            BackgroundWorkParameters param = (BackgroundWorkParameters)e.Argument;
            string _URL                 = param.Url;
            int    categoryId           = param.CategoryId;
            int    portalId             = param.PortalId;
            int    vendorId             = param.VendorId;
            string advancedCategoryRoot = param.AdvancedCategoryRoot;
            string countryFilter        = param.CountryFilter;
            string cityFilter           = param.CityFilter;

            string zipFileName = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath,
                                               "datafeed.zip");

            // inside this function show progressBar for step: LoadFile
            if (File.Exists(zipFileName))
            {
                File.Delete(zipFileName);
            }
            SaveFileFromURL(_URL, zipFileName, 60, bw, e);

            // if user cancel during saving file or ERROR
            if (e.Cancel || (e.Result != null) && e.Result.ToString().Substring(0, 6).Equals("ERROR:"))
            {
                return;
            }
            // Set step for backgroundWorker
            Form1.activeStep = "Extract file..";
            bw.ReportProgress(0);           // start new step of background process

            using (ZipFile zip1 = ZipFile.Read(zipFileName))
            {
                foreach (ZipEntry zipEntry in zip1)
                {
                    zipEntry.Extract(Properties.Settings.Default.TempPath,
                                     ExtractExistingFileAction.OverwriteSilently);
                    _URL = String.Format("{0}\\{1}", Properties.Settings.Default.TempPath, zipEntry.FileName);
                }
            }

            XmlSchemaSet schemas = new XmlSchemaSet();

            schemas.Add("", "datafeed.xsd");
            Form1.activeStep = "Validating inpout..";
            bw.ReportProgress(0); // start new step of background process
            XDocument xDoc   = XDocument.Load(_URL);
            bool      errors = false;

            xDoc.Validate(schemas, (o, e2) =>
            {
                e.Result = "ERROR:" + e2.Message;
                log.Error(e2.Message);
                errors = true;
            });
            if (errors)
            {
                e.Cancel = true;
                return;
            }

            // show progress & catch Cancel
            if (bw.CancellationPending)
            {
                e.Cancel = true; return;
            }
            else if (bw.WorkerReportsProgress)
            {
                bw.ReportProgress(50);
            }

            // read hotels from XML
            // use XmlReader to avoid huge file size dependence
            var products =
                from el in StreamRootChildDoc(_URL)
                select new
            {
                ProductNumber   = (string)el.Attribute("id"),
                Name            = (string)el.Element("text").Element("name"),
                Description     = (string)el.Element("text").Element("desc"),
                DescriptionHTML = (string)el.Element("text").Element("desc"),
                UnitCost        = (decimal)el.Element("price").Element("buynow"),
                Category        = (string)el.Element("cat").Element("mCat"),
                Category2       = (string)el.Element("cat").Element("awCat"),
                URL             = (string)el.Element("uri").Element("mLink"),
                Image           = (string)el.Element("uri").Element("mImage"),
            };

            // show progress & catch Cancel
            if (bw.CancellationPending)
            {
                e.Cancel = true; return;
            }
            else if (bw.WorkerReportsProgress)
            {
                bw.ReportProgress(100);
            }
            Thread.Sleep(100); // a little bit slow working for visualisation Progress

            // Set step for backgroundWorker
            Form1.activeStep = "Import records..";
            bw.ReportProgress(0);          // start new step of background process
            long countProducts  = products.Count();
            long currentProduct = 0;

            try
            {
                using (SelectedHotelsEntities db = new SelectedHotelsEntities())
                {
                    foreach (var product in products)
                    {
                        Console.WriteLine(product.Name); // debug print

                        int?parentID       = null;
                        int?catRootID      = null;
                        int?catCategoryID  = null;
                        int?catCategory2ID = null;
                        int level          = 0;
                        int maxOrder       = 0;

                        if (!String.IsNullOrEmpty(advancedCategoryRoot))
                        {
                            var advCatRoot =
                                db.AdvCats.SingleOrDefault(
                                    ac => ac.PortalID == portalId && ac.AdvCatName == advancedCategoryRoot && ac.Level == level);
                            if (advCatRoot == null)
                            {
                                if (db.AdvCats.Count() > 0)
                                {
                                    maxOrder = db.AdvCats.Max(ac => ac.AdvCatOrder);
                                }
                                advCatRoot = new AdvCat
                                {
                                    AdvCatOrder    = maxOrder + 1,
                                    PortalID       = portalId,
                                    AdvCatName     = advancedCategoryRoot,
                                    IsVisible      = true,
                                    DisableLink    = false,
                                    Url            = String.Empty,
                                    Title          = String.Empty,
                                    Description    = String.Empty,
                                    KeyWords       = String.Empty,
                                    IsDeleted      = false,
                                    IconFile       = String.Empty,
                                    Level          = level,
                                    AdvCatImportID = String.Empty
                                };
                                db.AdvCats.Add(advCatRoot);
                                db.SaveChanges();

                                Common.AddAdvCatDefaultPermissions(db, advCatRoot.AdvCatID);
                            }
                            parentID  = advCatRoot.AdvCatID;
                            catRootID = advCatRoot.AdvCatID;
                            level++;
                        }

                        if (!String.IsNullOrEmpty(product.Category))
                        {
                            var advCatCategory =
                                db.AdvCats.SingleOrDefault(
                                    ac => ac.PortalID == portalId && ac.AdvCatName == product.Category && ac.Level == level);
                            if (advCatCategory == null)
                            {
                                if (db.AdvCats.Count() > 0)
                                {
                                    maxOrder = db.AdvCats.Max(ac => ac.AdvCatOrder);
                                }
                                advCatCategory = new AdvCat
                                {
                                    AdvCatOrder    = maxOrder + 1,
                                    PortalID       = portalId,
                                    AdvCatName     = product.Category,
                                    IsVisible      = true,
                                    DisableLink    = false,
                                    Url            = String.Empty,
                                    Title          = String.Empty,
                                    Description    = String.Empty,
                                    KeyWords       = String.Empty,
                                    IsDeleted      = false,
                                    IconFile       = String.Empty,
                                    Level          = level,
                                    AdvCatImportID = String.Empty
                                };
                                db.AdvCats.Add(advCatCategory);
                                db.SaveChanges();

                                Common.AddAdvCatDefaultPermissions(db, advCatCategory.AdvCatID);
                            }
                            parentID      = advCatCategory.AdvCatID;
                            catCategoryID = advCatCategory.AdvCatID;
                            level++;
                        }

                        if (!String.IsNullOrEmpty(product.Category2))
                        {
                            var advCatCategory2 =
                                db.AdvCats.SingleOrDefault(
                                    ac => ac.PortalID == portalId && ac.AdvCatName == product.Category2 && ac.Level == level);
                            if (advCatCategory2 == null)
                            {
                                if (db.AdvCats.Count() > 0)
                                {
                                    maxOrder = db.AdvCats.Max(ac => ac.AdvCatOrder);
                                }
                                advCatCategory2 = new AdvCat
                                {
                                    AdvCatOrder    = maxOrder + 1,
                                    PortalID       = portalId,
                                    AdvCatName     = product.Category2,
                                    IsVisible      = true,
                                    ParentId       = parentID.Value,
                                    DisableLink    = false,
                                    Url            = String.Empty,
                                    Title          = String.Empty,
                                    Description    = String.Empty,
                                    KeyWords       = String.Empty,
                                    IsDeleted      = false,
                                    IconFile       = String.Empty,
                                    Level          = level,
                                    AdvCatImportID = String.Empty
                                };
                                db.AdvCats.Add(advCatCategory2);
                                db.SaveChanges();

                                Common.AddAdvCatDefaultPermissions(db, advCatCategory2.AdvCatID);
                            }
                            parentID       = advCatCategory2.AdvCatID;
                            catCategory2ID = advCatCategory2.AdvCatID;
                            level++;
                        }

                        var product2 = db.Products.SingleOrDefault(p => p.CategoryID == categoryId && p.ProductNumber == product.ProductNumber);
                        if (product2 == null)
                        {
                            product2 = new Product
                            {
                                CategoryID      = categoryId,
                                Category2ID     = 0,
                                Category3       = String.Empty,
                                ProductName     = product.Name.Replace("&apos;", "'"),
                                ProductNumber   = product.ProductNumber,
                                UnitCost        = product.UnitCost,
                                UnitCost2       = product.UnitCost,
                                UnitCost3       = product.UnitCost,
                                UnitCost4       = product.UnitCost,
                                UnitCost5       = product.UnitCost,
                                UnitCost6       = product.UnitCost,
                                Description     = product.Description,
                                DescriptionHTML = product.DescriptionHTML,
                                URL             = product.URL,
                                ProductCost     = product.UnitCost,
                                ProductImage    = product.Image,
                                CreatedByUser   = vendorId,
                                DateCreated     = DateTime.Now
                            };

                            product2.ProductImage = product.Image;
                            product2.OrderQuant   = "0";

                            db.Products.Add(product2);
                            db.SaveChanges();
                        }
                        else
                        {
                            bool isChanged = false;
                            if (product2.CategoryID != categoryId)
                            {
                                product2.CategoryID = categoryId;
                                isChanged           = true;
                            }
                            if (product2.Category2ID != 0)
                            {
                                product2.Category2ID = 0;
                                isChanged            = true;
                            }
                            if (product2.ProductName != product.Name.Replace("&apos;", "'"))
                            {
                                product2.ProductName = product.Name.Replace("&apos;", "'");
                                isChanged            = true;
                            }
                            if (product2.ProductNumber != product.ProductNumber)
                            {
                                product2.ProductNumber = product.ProductNumber;
                                isChanged = true;
                            }
                            if (product2.UnitCost != product.UnitCost)
                            {
                                product2.UnitCost = product.UnitCost;
                                isChanged         = true;
                            }
                            if (product2.UnitCost2 != product.UnitCost)
                            {
                                product2.UnitCost2 = product.UnitCost;
                                isChanged          = true;
                            }
                            if (product2.UnitCost3 != product.UnitCost)
                            {
                                product2.UnitCost3 = product.UnitCost;
                                isChanged          = true;
                            }
                            if (product2.UnitCost4 != product.UnitCost)
                            {
                                product2.UnitCost4 = product.UnitCost;
                                isChanged          = true;
                            }
                            if (product2.UnitCost5 != product.UnitCost)
                            {
                                product2.UnitCost5 = product.UnitCost;
                                isChanged          = true;
                            }
                            if (product2.UnitCost6 != product.UnitCost)
                            {
                                product2.UnitCost6 = product.UnitCost;
                                isChanged          = true;
                            }
                            if (product2.Description != product.Description)
                            {
                                product2.Description = product.Description;
                                isChanged            = true;
                            }
                            if (product2.DescriptionHTML != product.DescriptionHTML)
                            {
                                product2.DescriptionHTML = product.DescriptionHTML;
                                isChanged = true;
                            }
                            if (product2.URL != product.URL)
                            {
                                product2.URL = product.URL;
                                isChanged    = true;
                            }
                            if (product2.ProductCost != product.UnitCost)
                            {
                                product2.ProductCost = product.UnitCost;
                                isChanged            = true;
                            }
                            if (product2.ProductImage != product.Image)
                            {
                                product2.ProductImage = product.Image;
                                isChanged             = true;
                            }
                            if (product2.OrderQuant != "0")
                            {
                                product2.OrderQuant = "0";
                                isChanged           = true;
                            }

                            if (isChanged)
                            {
                                db.SaveChanges();
                            }
                        }

                        // add product to advanced categories
                        if (catRootID.HasValue && db.AdvCatProducts.SingleOrDefault(act => act.AdvCatID == catRootID.Value && act.ProductID == product2.ProductID) == null)
                        {
                            AdvCatProduct advCatProduct = new AdvCatProduct
                            {
                                AdvCatID  = catRootID.Value,
                                ProductID = product2.ProductID,
                                AddAdvCatToProductDisplay = false
                            };
                            db.AdvCatProducts.Add(advCatProduct);
                            db.SaveChanges();
                        }
                        if (catCategoryID.HasValue && db.AdvCatProducts.SingleOrDefault(act => act.AdvCatID == catCategoryID.Value && act.ProductID == product2.ProductID) == null)
                        {
                            AdvCatProduct advCatProduct = new AdvCatProduct
                            {
                                AdvCatID  = catCategoryID.Value,
                                ProductID = product2.ProductID,
                                AddAdvCatToProductDisplay = false
                            };
                            db.AdvCatProducts.Add(advCatProduct);
                            db.SaveChanges();
                        }
                        if (catCategory2ID.HasValue && db.AdvCatProducts.SingleOrDefault(act => act.AdvCatID == catCategory2ID.Value && act.ProductID == product2.ProductID) == null)
                        {
                            AdvCatProduct advCatProduct = new AdvCatProduct
                            {
                                AdvCatID  = catCategory2ID.Value,
                                ProductID = product2.ProductID,
                                AddAdvCatToProductDisplay = false
                            };
                            db.AdvCatProducts.Add(advCatProduct);
                            db.SaveChanges();
                        }

                        currentProduct++;
                        if (bw.CancellationPending)
                        {
                            e.Cancel = true;
                            break;
                        }
                        else if (bw.WorkerReportsProgress && currentProduct % 100 == 0)
                        {
                            bw.ReportProgress((int)(100 * currentProduct / countProducts));
                        }
                    }
                    //    rc = true;
                }
            }
            catch (Exception ex)
            {
                e.Result = "ERROR:" + ex.Message;
                //message = ex.Message;
                log.Error("Error error logging", ex);
            }
            //return rc;
        }