Ejemplo n.º 1
0
        private void UpdateBrands()
        {
            using (ConcentratorDataContext concentratorContext = new ConcentratorDataContext())
            {
                var brands = (from b in concentratorContext.BrandVendors
                              where b.VendorID == contentVendorID
                              select b).ToList();


                using (ICEcatDataContext ctx = new ICEcatDataContext(ConfigurationManager.ConnectionStrings["ICEcat"].ConnectionString))
                {
                    var iceCatContentVendorBrands = (from c in ctx.Brands
                                                     select new
                    {
                        ContentVendorBrandID = c.Supplier_ID,
                        ContentVendorBrandCode = c.Supplier_Name
                    }).Distinct().ToList();

                    foreach (var pair in iceCatContentVendorBrands)
                    {
                        var vbrand = brands.FirstOrDefault(vb => vb.VendorBrandCode == pair.ContentVendorBrandID.ToString());

                        if (vbrand == null)
                        {
                            vbrand = new BrandVendor
                            {
                                VendorID        = contentVendorID,
                                VendorBrandCode = pair.ContentVendorBrandID.ToString(),
                                Name            = pair.ContentVendorBrandCode,
                                BrandID         = -1
                            };

                            brands.Add(vbrand);

                            concentratorContext.BrandVendors.InsertOnSubmit(vbrand);
                        }
                    }
                    concentratorContext.SubmitChanges();
                }
            }
        }
Ejemplo n.º 2
0
        private void ProcessCrossSellItems()
        {
            using (ConcentratorDataContext contextConcentrator = new ConcentratorDataContext())
            {
                using (ICEcatDataContext contextIceCat = new ICEcatDataContext())
                {
                    //var productVendorItemNumbers = (from p in contextConcentrator.Products select p.VendorItemNumber).ToList();

                    var relatedProducts = (from rp in contextIceCat.dt_ProductRelateds
                                           select rp).ToList();

                    foreach (var relatedProduct in relatedProducts)
                    {
                        //get identifier to check on with concentrator for the source product. VendorItemNuber ?
                        var productIDICECatLeft = (from ic in contextIceCat.dt_Products
                                                   where ic.Product_ID == relatedProduct.Product_ID
                                                   select ic.Prod_ID).FirstOrDefault();

                        //get identifier to check on with concentrator for the destination product. VendorItemNuber ?
                        var productIDICECatRight = (from ic in contextIceCat.dt_Products
                                                    where ic.Product_ID == relatedProduct.ProductRelated_ID
                                                    select ic.Prod_ID).FirstOrDefault();


                        var product =
                            (from p in contextConcentrator.Products where p.VendorItemNumber == productIDICECatLeft select p).FirstOrDefault();

                        var relatedProductItem = (from p in contextConcentrator.Products where p.VendorItemNumber == productIDICECatRight select p).FirstOrDefault();

                        //if any of the two products does not exist in the concentrator --- continue
                        if (product == null || relatedProductItem == null)
                        {
                            continue;
                        }


                        //grab the related item from the concentrator if any
                        var relatedItem = (from p in contextConcentrator.RelatedProducts
                                           where p.Product.VendorItemNumber == productIDICECatLeft
                                           &&
                                           p.RelatedProduct_Product1.VendorItemNumber == productIDICECatRight
                                           &&
                                           p.VendorID == contentVendorID
                                           select p).FirstOrDefault();

                        //if there is isnt such an item in the related products of the concentrator
                        if (relatedItem == null)
                        {
                            //create related item
                            RelatedProduct rp = new RelatedProduct()
                            {
                                Product = product,
                                RelatedProduct_Product1 = relatedProductItem,
                                VendorID = contentVendorID
                            };

                            contextConcentrator.RelatedProducts.InsertOnSubmit(rp);
                        }
                    }

                    contextConcentrator.SubmitChanges();
                }
            }
        }
        public void Execute(JobExecutionContext context)
        {
            try
            {
                DataMap = context.JobDetail.JobDataMap;
                Running = true;

                DateTime start = DateTime.Now;
                log.InfoFormat("Starting plugin: {0}", Name);

                Concentrator.Objects.Web.Client.Login(Concentrator.Objects.Web.ConcentratorPrincipal.SystemPrincipal);

                using (var unit = GetUnitOfWork())
                {
                    var _repoConnectors = unit.Scope.Repository <Connector>();

                    //TODO : Preload options

                    //  var options = new DataLoadOptions();
                    //  options.LoadWith<Connector>(x => x.ConnectorSystem);
                    //  options.LoadWith<Connector>(x => x.Settings);
                    //  options.LoadWith<Connector>(x => x.ContentProducts);
                    //  options.LoadWith<Connector>(x => x.PreferredConnectorVendors);
                    //  options.LoadWith<Connector>(x => x.ConnectorLanguages);
                    //  options.LoadWith<ConnectorLanguage>(x => x.Language);
                    //  options.LoadWith<Vendor>(v => v.VendorSettings);
                    //  ctx.LoadOptions = options;

                    // filter connectors based on the optional attribute on Plugin level
                    var att = GetType().GetCustomAttributes(typeof(ConnectorSystemAttribute), true);

                    if (att.Length > 0)
                    {
                        var attributevalue = ((ConnectorSystemAttribute)att[0]).ConnectorSystemID;
                        _connectors = _repoConnectors.GetAll(c => c.ConnectorSystemID.HasValue && c.ConnectorSystemID.Value == attributevalue && c.IsActive).ToList();
                    }
                    else
                    {
                        _connectors = _repoConnectors.GetAll(x => x.IsActive).ToList();
                    }

                    _connectorSchedules = ctx.ConnectorSchedules.Where(x => x.Plugin == Name).ToList();

                    foreach (var c in _connectors)
                    {
                        var s = _connectorSchedules.FirstOrDefault(x => x.ConnectorID == c.ConnectorID);
                        if (s == null)
                        {
                            s = new ConnectorSchedule()
                            {
                                ConnectorID             = c.ConnectorID,
                                Plugin                  = Name,
                                ConnectorScheduleStatus = ConnectorScheduleStatus.Disabled
                            };
                            ctx.ConnectorSchedules.InsertOnSubmit(s);
                            _connectorSchedules.Add(s);
                        }
                    }

                    _vendors = ctx.Vendors.ToList();


                    Process();

                    _connectorSchedules.Where(x => x.ConnectorScheduleStatus != ConnectorScheduleStatus.WaitForNextRun).ToList().ForEach(x => x.ConnectorScheduleStatus = ConnectorScheduleStatus.Disabled);
                    ctx.SubmitChanges();
                    TimeSpan ts   = DateTime.Now.Subtract(start);
                    string   time = ts.ToString().Substring(0, 8);

                    log.InfoFormat("Finished plugin: {0}, duration : {1}, next run : {2}", Name, time, context.NextFireTimeUtc);
                }
            }
            catch (Exception ex)
            {
                log.FatalFormat("Error executing {0} plugin : {1}\n{2}", this.Name, ex.Message, ex.StackTrace);
            }
            catch
            {
                log.Fatal("Unknown error plugin");
            }
        }
Ejemplo n.º 4
0
        public void ImportOrder(string order, int connectorID)
        {
            try
            {
                XDocument document = XDocument.Parse(order);
                SpiderPrincipal.Login("SYSTEM", "SYS");
                using (ConcentratorDataContext context = new ConcentratorDataContext())
                {
                    var header   = document.Element("WebOrderRequest").Element("WebOrderHeader");
                    var details  = document.Element("WebOrderRequest").Element("WebOrderDetails").Elements("WebOrderDetail");
                    var customer = document.Element("WebOrderRequest").Element("WebCustomer");
                    var ord      = new
                    {
                        OrderHeader = new
                        {
                            isDropShipment =
                                header.Element("CustomerOverride").Element("Dropshipment").Try(
                                    c => bool.Parse(c.Value), false),
                            CustomerOrderReference = header.Element("CustomerOrderReference").Value,
                            EdiVersion             = header.Element("EdiVersion").Value,
                            BSKIdentifier          = header.Element("BSKIdentifier").Value,
                            WebSiteOrderNumber     = header.Element("WebSiteOrderNumber").Value,
                            PaymentTermsCode       = header.Element("PaymentTermsCode").Value,
                            PaymentInstrument      = header.Element("PaymentInstrument").Value,
                            BackOrdersAllowed      =
                                header.Try(c => bool.Parse(c.Element("BackOrdersAllowed").Value), false),
                            RouteCode     = header.Try(c => c.Element("RouteCode").Value, string.Empty),
                            EANIdentifier =
                                header.Element("ShipToCustomer").Try(
                                    c => c.Element("EanIdentifier").Value, string.Empty),
                            NonDropDetails = new
                            {
                                ShipToShopID =
                                    header.Try(c => c.Element("ShipToShopID").Value,
                                               string.Empty)
                            }
                        },
                        Customer = new
                        {
                            Name         = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("Name").Value, string.Empty),
                            AddressLine1 = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("AddressLine1").Value, string.Empty),
                            AddressLine2 = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("AddressLine2").Value, string.Empty),
                            AddressLine3 = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("AddressLine3").Value, string.Empty),
                            PostCode     = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("ZipCode").Value, string.Empty),
                            City         = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("City").Value, string.Empty),
                            Country      = header.Try(dc => dc.Element("CustomerOverride").Element("OrderAddress").Element("Country").Value, string.Empty),
                        },
                        OrderLines = (from o in details
                                      let custReference = o.Element("CustomerReference")
                                                          let product = o.Element("ProductIdentifier")
                                                                        select new
                        {
                            CustomerReference = custReference == null ? null : new
                            {
                                CustomerOrder = custReference.Try(c => c.Element("CustomerOrder").Value, string.Empty),
                                CustomerOrderLine = custReference.Try(c => c.Element("CustomerOrderLine").Value, string.Empty)
                            },
                            Product = product == null ? null : new
                            {
                                ID = product.Element("ProductNumber").Value,
                                EAN = product.Try(c => c.Element("EANIdentifier").Value, string.Empty),
                                ManufacturerItemID = product.Try(c => c.Element("ManufacturerItemID").Value, string.Empty),
                                Qty = product.Try(c => int.Parse(c.Element("Quantity").Value), 1),
                                VendorItemNumber = product.Try(c => c.Element("VendorItemNumber").Value, string.Empty),
                                WarehouseCode = product.Try(c => c.Element("WareHouseCode").Value, string.Empty)
                            }
                        }).ToList(),
                        CustomerContactInfo = customer == null ? null : new
                        {
                            Email = customer.Try(c => c.Element("CustomerContact").Element("Name").Value, string.Empty)
                        }
                    };


                    #region Dropshipment/Non-Drop

                    Customer customerE = (from c in context.Customers
                                          where c.EANIdentifier == ord.OrderHeader.EANIdentifier
                                          select c).FirstOrDefault();

                    if (customerE == null)
                    {
                        customerE = new Customer
                        {
                            EANIdentifier = ord.OrderHeader.EANIdentifier
                        };
                        context.Customers.InsertOnSubmit(customerE);
                    }
                    if (ord.Customer != null)
                    {
                        customerE.CustomerAddressLine1 = ord.Customer.AddressLine1;
                        customerE.CustomerAddressLine2 = ord.Customer.AddressLine2;
                        customerE.CustomerAddressLine3 = ord.Customer.AddressLine3;
                        customerE.City          = ord.Customer.City;
                        customerE.Country       = ord.Customer.Country;
                        customerE.PostCode      = ord.Customer.PostCode;
                        customerE.CustomerName  = ord.Customer.Name;
                        customerE.CustomerEmail = ord.Try(c => c.CustomerContactInfo.Email, string.Empty);
                    }

                    Spider.Objects.Orders.Order newOrd = new Objects.Orders.Order()
                    {
                        Customer               = customerE,
                        Document               = order,
                        ConnectorID            = connectorID,
                        IsDropShipment         = ord.OrderHeader.isDropShipment,
                        ReceivedDate           = DateTime.Now,
                        BackOrdersAllowed      = ord.OrderHeader.BackOrdersAllowed,
                        CustomerOrderReference = ord.OrderHeader.CustomerOrderReference,
                        EdiVersion             = ord.OrderHeader.EdiVersion,
                        BSKIdentifier          = ord.OrderHeader.BSKIdentifier,
                        WebSiteOrderNumber     = ord.OrderHeader.WebSiteOrderNumber,
                        PaymentInstrument      = ord.OrderHeader.PaymentInstrument,
                        PaymentTermsCode       = ord.OrderHeader.PaymentTermsCode,
                        RouteCode              = ord.OrderHeader.RouteCode,
                    };


                    #endregion


                    context.Orders.InsertOnSubmit(newOrd);

                    #region Order Lines
                    foreach (var line in ord.OrderLines)
                    {
                        int?productid = null;
                        var product   = (from p in context.Products where p.ProductID == int.Parse(line.Product.ID) select p).FirstOrDefault();

                        if (product != null)
                        {
                            productid = product.ProductID;
                        }

                        OrderLine ol = new OrderLine
                        {
                            CustomerOrderLineNr = line.CustomerReference.CustomerOrderLine,
                            CustomerOrderNr     = line.CustomerReference.CustomerOrder,
                            Order     = newOrd,
                            Quantity  = line.Product.Qty,
                            ProductID = productid
                        };

                        context.OrderLines.InsertOnSubmit(ol);
                    }
                    #endregion


                    context.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error inserting order", e);
            }
        }
Ejemplo n.º 5
0
        protected override void Process()
        {
            using (ConcentratorDataContext ctx = new ConcentratorDataContext())
            {
                var imagelist = (from i in ctx.ProductImages
                                 where i.ImagePath == null
                                 orderby i.ProductID descending //newest products first
                                 select i).ToList();

                string imageDir = "";

                try
                {
                    int couterProduct = 0;
                    int logCount      = 0;
                    int totalProducts = imagelist.Count;
                    log.DebugFormat("Found {0} images to process", totalProducts);
                    string imageDirectory = GetConfiguration().AppSettings.Settings["FTPImageDirectory"].Value;
                    imageDir = imageDirectory;
                    foreach (var image in imagelist)
                    {
                        couterProduct++;
                        logCount++;
                        if (logCount == 50)
                        {
                            log.DebugFormat("Images Processed : {0}/{1}", couterProduct, totalProducts);
                            logCount = 0;
                        }
                        string fileName = string.Format("{0}_{1}_{2}_{3}", image.ImageID, image.ProductID, image.Sequence, image.VendorID);

                        string imagePath = GetImage(image.ImageUrl, fileName, "Products", imageDirectory);
                        if (!string.IsNullOrEmpty(imagePath))
                        {
                            image.ImagePath = imagePath;
                            ctx.SubmitChanges();
                        }
                    }
                    log.Debug("Finish download image process");
                }
                catch (Exception ex)
                {
                    log.Error("Error processing images", ex);
                }

//        log.InfoFormat("Start Cleanup image directory");
//#region Cleanup
//        imagelist = (from i in ctx.ProductImages
//                         where i.ImagePath != null
//                         orderby i.ProductID descending //newest products first
//                         select i).ToList();

//        try
//        {
//          int couterProduct = 0;
//          int logCount = 0;

//         string imageDirectory = GetConfiguration().AppSettings.Settings["FTPImageDirectory"].Value;

//         var files = Directory.GetFiles(Path.Combine(imageDirectory, "Products"));
//                      int totalProducts = files.Count();
//          log.DebugFormat("Found {0} images to check", totalProducts);


//          foreach(var file in files)
//          {
//            couterProduct++;
//            logCount++;
//            if (logCount == 50)
//            {
//              log.DebugFormat("Images Processed : {0}/{1}", couterProduct, totalProducts);
//              logCount = 0;
//            }
//            if (!imagelist.Any(x => Path.Combine(imageDirectory, x.ImagePath) == file))
//            {
//              try
//              {
//                log.DebugFormat("Try to delete {0}", file);
//                File.Delete(file);
//              }
//              catch (Exception ex)
//              {
//                log.ErrorFormat("Delete file {0} failed: {1}", file, ex.InnerException);
//              }
//            }
//          }
//          log.Debug("Finish download image process");
//        }
//        catch (Exception ex)
//        {
//          log.Error("Error processing images", ex);
//        }
//#endregion
//        log.Info("Finish cleanup images");

                log.Info("Start import brand images");
                #region Brand image
                var brandImages = (from i in ctx.BrandVendors
                                   where i.Logo != null &&
                                   i.BrandID > 0
                                   select i).ToList();

                try
                {
                    int couterProduct = 0;
                    int logCount      = 0;
                    int totalProducts = brandImages.Count;
                    log.DebugFormat("Found {0} brand images to process", totalProducts);
                    string imageDirectory = GetConfiguration().AppSettings.Settings["FTPImageDirectory"].Value;
                    foreach (var image in brandImages)
                    {
                        couterProduct++;
                        logCount++;
                        if (logCount == 50)
                        {
                            log.DebugFormat("Brand images Processed : {0}/{1}", couterProduct, totalProducts);
                            logCount = 0;
                        }
                        string fileName = string.Format("{0}_{1}_{2}", image.BrandID, image.Name, image.VendorID);

                        string imagePath = GetImage(image.Logo, fileName, "Brand", imageDirectory);
                        if (!string.IsNullOrEmpty(imagePath))
                        {
                            image.Brand.ImagePath = imagePath;
                            ctx.SubmitChanges();
                        }
                    }
                    log.Debug("Finish download image process");
                }
                catch (Exception ex)
                {
                    log.Error("Error processing images", ex);
                }
                #endregion
                log.Info("Finish import brand images");

                //log.InfoFormat("Start Cleanup brand image directory");
                //#region Cleanup brands

                //var exBrandImages = (from i in ctx.Brands
                //               where i.ImagePath != null
                //               && i.BrandID > 0
                //               select i).ToList();
                //try
                //{
                //  int couterProduct = 0;
                //  int logCount = 0;

                //  string imageDirectory = GetConfiguration().AppSettings.Settings["FTPImageDirectory"].Value;

                //  var files = Directory.GetFiles(Path.Combine(imageDirectory, "Brand"));
                //  int totalProducts = files.Count();
                //  log.DebugFormat("Found {0} images to check", totalProducts);


                //  foreach (var file in files)
                //  {
                //    couterProduct++;
                //    logCount++;
                //    if (logCount == 50)
                //    {
                //      log.DebugFormat("Images Processed : {0}/{1}", couterProduct, totalProducts);
                //      logCount = 0;
                //    }
                //    if (!exBrandImages.Any(x => Path.Combine(imageDirectory, x.ImagePath) == file))
                //    {
                //      try
                //      {
                //        log.DebugFormat("Try to delete {0}", file);
                //        File.Delete(file);
                //      }
                //      catch (Exception ex)
                //      {
                //        log.ErrorFormat("Delete file {0} failed: {1}", file, ex.InnerException);
                //      }
                //    }
                //  }
                //  log.Debug("Finish download image process");
                //}
                //catch (Exception ex)
                //{
                //  log.Error("Error processing images", ex);
                //}
                //#endregion
                //log.Info("Finish cleanup brand images");

                log.InfoFormat("Start Copy to Media");
                imagelist = (from i in ctx.ProductImages
                             where i.ImagePath != null
                             orderby i.ProductID descending //newest products first
                             select i).ToList();

                var mediaImages = (from m in ctx.ProductMedia
                                   where m.TypeID == 1
                                   select m).ToList();

                try
                {
                    int couterProduct = 0;
                    int logCount      = 0;
                    int totalProducts = imagelist.Count;
                    log.DebugFormat("Found {0} images to copy", totalProducts);
                    foreach (var image in imagelist)
                    {
                        couterProduct++;
                        logCount++;
                        if (logCount == 50)
                        {
                            log.DebugFormat("Images Processed : {0}/{1}", couterProduct, totalProducts);
                            logCount = 0;
                        }

                        var m = mediaImages.Where(x => x.VendorID == image.VendorID &&
                                                  x.Sequence == image.Sequence &&
                                                  x.ProductID == image.ProductID &&
                                                  x.MediaUrl == image.ImageUrl &&
                                                  x.MediaPath == image.ImagePath).FirstOrDefault();

                        if (m != null)
                        {
                            mediaImages.Remove(m);
                        }
                        else
                        {
                            m = new ProductMedia()
                            {
                                MediaPath  = image.ImagePath,
                                Resolution = getRes(Path.Combine(imageDir, image.ImagePath)),
                                Size       = getSize(Path.Combine(imageDir, image.ImagePath)).HasValue ? getSize(Path.Combine(imageDir, image.ImagePath)) : null,
                                MediaUrl   = image.ImageUrl,
                                ProductID  = image.ProductID,
                                Sequence   = image.Sequence,
                                TypeID     = 1,
                                VendorID   = image.VendorID
                            };
                            ctx.ProductMedia.InsertOnSubmit(m);
                            ctx.SubmitChanges();
                        }
                    }

                    ctx.ProductMedia.DeleteAllOnSubmit(mediaImages);
                    ctx.SubmitChanges();
                }
                catch (Exception ex)
                {
                    log.Error("Error processing images", ex);
                }


                log.Info("Finish copy to media");
            }
        }
Ejemplo n.º 6
0
        private bool ParseDocument(ConcentratorDataContext context, XDocument doc, XDocument categories)
        {
            var brands = (from bv in context.BrandVendors
                          where bv.VendorID == vendorID_NL
                          select bv).ToList();

            var productGroups_NL = (from g in context.ProductGroupVendors
                                    where g.VendorID == vendorID_NL
                                    select g).ToList();

            var productAttributes_NL = (from g in context.ProductAttributeMetaDatas
                                        where g.VendorID == vendorID_NL
                                        select g).ToList();

            var brands_BE = (from bv in context.BrandVendors
                             where bv.VendorID == vendorID_BE
                             select bv).ToList();

            var productGroups_BE = (from g in context.ProductGroupVendors
                                    where g.VendorID == vendorID_BE
                                    select g).ToList();

            var productAttributes_BE = (from g in context.ProductAttributeMetaDatas
                                        where g.VendorID == vendorID_BE
                                        select g).ToList();

            var productAttributeGroups = (from g in context.ProductAttributeGroupMetaDatas select g).ToList();

            int counter = 0;
            int total   = itemProducts.Count();
            int totalNumberOfProductsToProcess = total;

            log.InfoFormat("Start inport {0} products", total);

            var Languages = new List <string>()
            {
                "NL",
                "BE"
            };

            foreach (var language in Languages)
            {
                foreach (var product in itemProducts)
                {
                    int vendorID = (language == "NL" ? vendorID_NL : vendorID_BE);

                    if (counter == 50)
                    {
                        counter = 0;
                        log.InfoFormat("Still need to process {0} of {1}; {2} done;", totalNumberOfProductsToProcess, total, total - totalNumberOfProductsToProcess);
                    }
                    totalNumberOfProductsToProcess--;
                    counter++;

                    try
                    {
                        #region BrandVendor

                        Product prod = null;
                        //check if brandvendor exists in db
                        var brandVendor = language == "NL" ? brands_NL.FirstOrDefault(vb => vb.VendorBrandCode == product.VendorBrandCode + "NL") : brands_BE.FirstOrDefault(vb => vb.VendorBrandCode == product.VendorBrandCode + "BE");

                        if (brandVendor == null) //if brandvendor does not exist
                        {
                            //create new brandVendor
                            brandVendor = new BrandVendor
                            {
                                BrandID         = unmappedID,
                                VendorID        = vendorID,
                                VendorBrandCode = language == "NL" ? product.VendorBrandCode + "NL" : product.VendorBrandCode + "BE",
                                Name            = language == "NL" ? product.VendorBrandCode + "NL" : product.VendorBrandCode + "BE",
                            };
                            context.BrandVendors.InsertOnSubmit(brandVendor);
                            if (language == "NL")
                            {
                                brands_NL.Add(brandVendor);
                            }
                            else
                            {
                                brands_BE.Add(brandVendor);
                            }
                        }

                        //use BrandID to create product and retreive ProductID

                        var BrandID = brandVendor.BrandID;

                        prod = context.Products.FirstOrDefault(p => p.VendorItemNumber == product.CustomItemNr);

                        //if product does not exist (usually true)
                        if (prod == null)
                        {
                            prod = new Product
                            {
                                VendorItemNumber = product.CustomItemNr.ToString(),
                                BrandID          = BrandID,
                                SourceVendorID   = vendorID
                            };
                            context.Products.InsertOnSubmit(prod);
                            //context.SubmitChanges();
                        }

                        #endregion

                        #region ProductImage

                        if (!string.IsNullOrEmpty(product.ImageUrl))
                        {
                            var productImage = context.ProductImages.FirstOrDefault(pi => pi.Product.ProductID == prod.ProductID);

                            //if (productImage == null)
                            //{
                            //  productImage = new ProductImage
                            //  {
                            //    Product = prod,
                            //    VendorID = language == "NL" ? vendorID_NL : vendorID_BE,
                            //    ImageUrl = product.ImageUrl
                            //  };
                            //  context.ProductImages.InsertOnSubmit(productImage);
                            //}

                            if (productImage == null)
                            {
                                productImage = new ProductImage
                                {
                                    Product  = prod,
                                    VendorID = vendorID
                                };
                                context.ProductImages.InsertOnSubmit(productImage);
                            }

                            productImage.ImageUrl = product.ImageUrl;
                        }

                        #endregion

                        #region VendorAssortMent

                        var productID = prod.ProductID;

                        var vendorAssortment = language == "NL" ? prod.VendorAssortment.FirstOrDefault(va => va.VendorID == vendorID_NL) : prod.VendorAssortment.FirstOrDefault(va => va.VendorID == vendorID_BE);

                        //if vendorAssortMent does not exist
                        if (vendorAssortment == null)
                        {
                            //create vendorAssortMent with productID
                            vendorAssortment = new VendorAssortment
                            {
                                Product          = prod,
                                CustomItemNumber = product.CustomItemNr.ToString(),
                                VendorID         = vendorID,
                                ShortDescription =
                                    product.ShortDescription.Length > 150
                                         ? product.ShortDescription.Substring(0, 150)
                                         : product.ShortDescription,
                                LongDescription = ""
                            };
                            context.VendorAssortments.InsertOnSubmit(vendorAssortment);
                            // context.SubmitChanges();
                        }

                        #endregion

                        #region ProductGroupVendor

                        ////create vendorGroup five times, each time with a different VendorProductGroupCode on a different level if not exist
                        int productGroupCount = 1;

                        foreach (var cat in product.ProductCategory.Split('_'))
                        {
                            var productGroupVendor = language == "NL"
                                          ? productGroups_NL.FirstOrDefault(
                                pg => pg.GetType().GetProperty("VendorProductGroupCode" + productGroupCount).GetValue(pg, null) == cat)
                                          : productGroups_BE.FirstOrDefault(
                                pg => pg.GetType().GetProperty("VendorProductGroupCode" + productGroupCount).GetValue(pg, null) == cat);


                            if (productGroupVendor == null)
                            {
                                productGroupVendor = new ProductGroupVendor
                                {
                                    ProductGroupID          = unmappedID,
                                    VendorID                = vendorID,
                                    VendorName              = product.VendorName,
                                    VendorProductGroupCode1 = cat
                                };
                                context.ProductGroupVendors.InsertOnSubmit(productGroupVendor);
                                if (language == "NL")
                                {
                                    productGroups_NL.Add(productGroupVendor);
                                }
                                else
                                {
                                    productGroups_BE.Add(productGroupVendor);
                                }
                            }

                            var vendorProductGroupAssortments = (from c in context.VendorProductGroupAssortments
                                                                 where
                                                                 c.VendorAssortment == vendorAssortment
                                                                 select c).ToList();

                            var vendorProductGroupAssortment1 =
                                vendorProductGroupAssortments.FirstOrDefault(
                                    vPga => vPga.ProductGroupVendor == productGroupVendor);

                            if (vendorProductGroupAssortment1 == null)
                            {
                                vendorProductGroupAssortment1 = new VendorProductGroupAssortment
                                {
                                    VendorAssortment   = vendorAssortment,
                                    ProductGroupVendor = productGroupVendor
                                };
                                context.VendorProductGroupAssortments.InsertOnSubmit(vendorProductGroupAssortment1);
                            }

                            productGroupCount++;
                        }

                        #endregion

                        #region ProductDescription

                        var productDescription =
                            prod.ProductDescription.FirstOrDefault(pd => pd.LanguageID == languageID && pd.VendorID == vendorID);

                        if (productDescription == null)
                        {
                            //create ProductDescription
                            productDescription = new ProductDescription
                            {
                                Product    = prod,
                                LanguageID = languageID,
                                VendorID   = vendorID,
                                ShortContentDescription = product.ShortDescription.Length > 1000
                                                                   ? product.ShortDescription.Substring(0, 1000)
                                                                   : product.ShortDescription,
                                ProductName = product.ProductName.ToString()
                            };

                            context.ProductDescription.InsertOnSubmit(productDescription);
                        }
                        else
                        {
                            productDescription.ProductName             = product.ProductName.ToString();
                            productDescription.ShortContentDescription = product.ShortDescription.Length > 1000
                                                             ? product.ShortDescription.Substring(0, 1000)
                                                             : product.ShortDescription;
                        }

                        #endregion

                        // first insert all the TechnicalData
                        foreach (var content in product.TechnicalData)
                        {
                            #region ProductAttributeGroupMetaData

                            var productAttributeGroupMetaData =
                                productAttributeGroups.FirstOrDefault(c => c.GroupCode == "TechnicalData");
                            //create ProductAttributeGroupMetaData if not exists
                            if (productAttributeGroupMetaData == null)
                            {
                                productAttributeGroupMetaData = new ProductAttributeGroupMetaData
                                {
                                    Index     = 0,
                                    GroupCode = "TechnicalData",
                                    VendorID  = vendorID
                                };
                                context.ProductAttributeGroupMetaDatas.InsertOnSubmit(productAttributeGroupMetaData);
                                productAttributeGroups.Add(productAttributeGroupMetaData);
                            }
                            #endregion
                            //  context.SubmitChanges();

                            #region ProductAttributeGroupName

                            var productAttributeGroupName =
                                productAttributeGroupMetaData.ProductAttributeGroupLabels.FirstOrDefault(c => c.LanguageID == languageID);
                            //create ProductAttributeGroupName if not exists
                            if (productAttributeGroupName == null)
                            {
                                productAttributeGroupName = new ProductAttributeGroupName
                                {
                                    Name = "TechnicalData",
                                    ProductAttributeGroup = productAttributeGroupMetaData,
                                    LanguageID            = languageID
                                };
                                context.ProductAttributeGroupNames.InsertOnSubmit(productAttributeGroupName);
                            }
                            //context.SubmitChanges();

                            #endregion

                            #region ProductAttributeMetaData

                            //create ProductAttributeMetaData as many times that there are entrys in technicaldata

                            var productAttributeMetaData = language == "NL" ? productAttributes_NL.FirstOrDefault(c => c.AttributeCode == content.AttributeCode) : productAttributes_BE.FirstOrDefault(c => c.AttributeCode == content.AttributeCode);
                            //create ProductAttributeMetaData if not exists
                            if (productAttributeMetaData == null)
                            {
                                productAttributeMetaData = new ProductAttributeMetaData
                                {
                                    ProductAttributeGroup = productAttributeGroupMetaData,
                                    AttributeCode         = content.AttributeCode.ToString(),
                                    Index        = 0,
                                    IsVisible    = true,
                                    NeedsUpdate  = true,
                                    VendorID     = vendorID,
                                    IsSearchable = false
                                };
                                context.ProductAttributeMetaDatas.InsertOnSubmit(productAttributeMetaData);
                                if (language == "NL")
                                {
                                    productAttributes_NL.Add(productAttributeMetaData);
                                }
                                else
                                {
                                    productAttributes_BE.Add(productAttributeMetaData);
                                }
                            }

                            // context.SubmitChanges();
                            #endregion

                            #region ProductAttributeName

                            var productAttributeName =
                                productAttributeMetaData.ProductAttributeLabels.FirstOrDefault(c => c.LanguageID == languageID);
                            //create ProductAttributeName if not exists
                            if (productAttributeName == null)
                            {
                                productAttributeName = new ProductAttributeName
                                {
                                    ProductAttribute = productAttributeMetaData,
                                    LanguageID       = languageID,
                                    Name             = content.AttributeCode.ToString()
                                };
                                context.ProductAttributeNames.InsertOnSubmit(productAttributeName);
                            }
                            else
                            {
                                productAttributeName.Name = content.AttributeCode.ToString();
                            }
                            // context.SubmitChanges();
                            #endregion

                            #region ProductAttributeValue

                            var productAttributeValue =
                                productAttributeMetaData.ProductAttributeValues.FirstOrDefault(c => c.ProductID == prod.ProductID);
                            //create ProductAttributeValue
                            if (productAttributeValue == null)
                            {
                                productAttributeValue = new ProductAttributeValue
                                {
                                    ProductAttribute = productAttributeMetaData,
                                    Product          = prod,
                                    Value            = content.Value,
                                    LanguageID       = languageID
                                };
                                context.ProductAttributeValues.InsertOnSubmit(productAttributeValue);
                            }
                            else
                            {
                                productAttributeValue.Value = content.Value;
                            }

                            #endregion
                        }

                        //  insert all other Attributes
                        foreach (var content in product.Attributes)
                        {
                            #region ProductAttributeGroupMetaData

                            var productAttributeGroupMetaData =
                                productAttributeGroups.FirstOrDefault(c => c.GroupCode == "General");
                            //create ProductAttributeGroupMetaData if not exists
                            if (productAttributeGroupMetaData == null)
                            {
                                productAttributeGroupMetaData = new ProductAttributeGroupMetaData
                                {
                                    Index     = 0,
                                    GroupCode = "General",
                                    VendorID  = vendorID
                                };
                                context.ProductAttributeGroupMetaDatas.InsertOnSubmit(productAttributeGroupMetaData);
                                productAttributeGroups.Add(productAttributeGroupMetaData);
                            }
                            #endregion

                            #region ProductAttributeGroupName

                            var productAttributeGroupName =
                                productAttributeGroupMetaData.ProductAttributeGroupLabels.FirstOrDefault(c => c.LanguageID == languageID);
                            //create ProductAttributeGroupName if not exists
                            if (productAttributeGroupName == null)
                            {
                                productAttributeGroupName = new ProductAttributeGroupName
                                {
                                    Name = "General",
                                    ProductAttributeGroup = productAttributeGroupMetaData,
                                    LanguageID            = languageID
                                };
                                context.ProductAttributeGroupNames.InsertOnSubmit(productAttributeGroupName);
                            }

                            #endregion

                            #region ProductAttributeMetaData

                            //create ProductAttributeMetaData as many times that there are entrys in technicaldata

                            var productAttributeMetaData = language == "NL" ?
                                                           productAttributes_NL.FirstOrDefault(c => c.AttributeCode == content.AttributeCode) : productAttributes_BE.FirstOrDefault(c => c.AttributeCode == content.AttributeCode);
                            //create ProductAttributeMetaData if not exists
                            if (productAttributeMetaData == null)
                            {
                                productAttributeMetaData = new ProductAttributeMetaData
                                {
                                    ProductAttributeGroup = productAttributeGroupMetaData,
                                    AttributeCode         = content.AttributeCode,
                                    Index        = 0,
                                    IsVisible    = true,
                                    NeedsUpdate  = true,
                                    VendorID     = vendorID,
                                    IsSearchable = false
                                };
                                context.ProductAttributeMetaDatas.InsertOnSubmit(productAttributeMetaData);
                                if (language == "NL")
                                {
                                    productAttributes_NL.Add(productAttributeMetaData);
                                }
                                else
                                {
                                    productAttributes_BE.Add(productAttributeMetaData);
                                }
                            }

                            #endregion

                            #region ProductAttributeName

                            var productAttributeName =
                                productAttributeMetaData.ProductAttributeLabels.FirstOrDefault(c => c.LanguageID == languageID);
                            //create ProductAttributeName if not exists
                            if (productAttributeName == null)
                            {
                                productAttributeName = new ProductAttributeName
                                {
                                    ProductAttribute = productAttributeMetaData,
                                    LanguageID       = languageID,
                                    Name             = content.AttributeCode
                                };
                                context.ProductAttributeNames.InsertOnSubmit(productAttributeName);
                            }
                            else
                            {
                                productAttributeName.Name = content.AttributeCode;
                            }

                            #endregion

                            #region ProductAttributeValue

                            var productAttributeValue =
                                productAttributeMetaData.ProductAttributeValues.FirstOrDefault(c => c.ProductID == prod.ProductID);
                            //create ProductAttributeValue
                            if (productAttributeValue == null)
                            {
                                productAttributeValue = new ProductAttributeValue
                                {
                                    ProductAttribute = productAttributeMetaData,
                                    Product          = prod,
                                    Value            = content.Value,
                                    LanguageID       = languageID
                                };
                                context.ProductAttributeValues.InsertOnSubmit(productAttributeValue);
                            }
                            else
                            {
                                productAttributeValue.Value = content.Value;
                            }

                            #endregion
                        }

                        context.SubmitChanges();
                        //  insert Guarantee data
                        #region ProductAttributeGroupMetaData

                        var guaranteeProductAttributeGroupMetaData =
                            productAttributeGroups.FirstOrDefault(c => c.GroupCode == "Guarantee");
                        //create ProductAttributeGroupMetaData if not exists
                        if (guaranteeProductAttributeGroupMetaData == null)
                        {
                            guaranteeProductAttributeGroupMetaData = new ProductAttributeGroupMetaData
                            {
                                Index     = 0,
                                GroupCode = "Guarantee",
                                VendorID  = vendorID
                            };
                            context.ProductAttributeGroupMetaDatas.InsertOnSubmit(guaranteeProductAttributeGroupMetaData);
                            productAttributeGroups.Add(guaranteeProductAttributeGroupMetaData);
                        }
                        #endregion
                        //  context.SubmitChanges();

                        #region ProductAttributeGroupName

                        var guaranteeProductAttributeGroupName =
                            guaranteeProductAttributeGroupMetaData.ProductAttributeGroupLabels.FirstOrDefault(c => c.LanguageID == languageID);
                        //create ProductAttributeGroupName if not exists
                        if (guaranteeProductAttributeGroupName == null)
                        {
                            guaranteeProductAttributeGroupName = new ProductAttributeGroupName
                            {
                                Name = "Guarantee",
                                ProductAttributeGroup = guaranteeProductAttributeGroupMetaData,
                                LanguageID            = languageID
                            };
                            context.ProductAttributeGroupNames.InsertOnSubmit(guaranteeProductAttributeGroupName);
                        }
                        //context.SubmitChanges();

                        #endregion

                        #region ProductAttributeMetaData

                        //create ProductAttributeMetaData as many times that there are entrys in technicaldata

                        var guaranteeProductAttributeMetaData = language == "NL" ?
                                                                productAttributes_NL.FirstOrDefault(c => c.AttributeCode == "GuaranteeSupplier") : productAttributes_BE.FirstOrDefault(c => c.AttributeCode == "GuaranteeSupplier");;
                        //create ProductAttributeMetaData if not exists
                        if (guaranteeProductAttributeMetaData == null)
                        {
                            guaranteeProductAttributeMetaData = new ProductAttributeMetaData
                            {
                                ProductAttributeGroup = guaranteeProductAttributeGroupMetaData,
                                AttributeCode         = "GuaranteeSupplier",
                                Index        = 0,
                                IsVisible    = true,
                                NeedsUpdate  = true,
                                VendorID     = vendorID,
                                IsSearchable = false
                            };
                            context.ProductAttributeMetaDatas.InsertOnSubmit(guaranteeProductAttributeMetaData);
                            if (language == "NL")
                            {
                                productAttributes_NL.Add(guaranteeProductAttributeMetaData);
                            }
                            else
                            {
                                productAttributes_BE.Add(guaranteeProductAttributeMetaData);
                            }
                        }

                        // context.SubmitChanges();
                        #endregion

                        #region ProductAttributeName

                        var guaranteeProductAttributeName =
                            guaranteeProductAttributeMetaData.ProductAttributeLabels.FirstOrDefault(c => c.LanguageID == languageID);
                        //create ProductAttributeName if not exists
                        if (guaranteeProductAttributeName == null)
                        {
                            guaranteeProductAttributeName = new ProductAttributeName
                            {
                                ProductAttribute = guaranteeProductAttributeMetaData,
                                LanguageID       = languageID,
                                Name             = "GuaranteeSupplier"
                            };
                            context.ProductAttributeNames.InsertOnSubmit(guaranteeProductAttributeName);
                        }

                        // context.SubmitChanges();
                        #endregion

                        #region ProductAttributeValue

                        var guaranteeProductAttributeValue =
                            guaranteeProductAttributeMetaData.ProductAttributeValues.FirstOrDefault(c => c.ProductID == prod.ProductID);
                        //create ProductAttributeValue
                        if (guaranteeProductAttributeValue == null)
                        {
                            guaranteeProductAttributeValue = new ProductAttributeValue
                            {
                                ProductAttribute = guaranteeProductAttributeMetaData,
                                Product          = prod,
                                Value            = "Sennheiser",
                                LanguageID       = languageID
                            };
                            context.ProductAttributeValues.InsertOnSubmit(guaranteeProductAttributeValue);
                        }

                        #endregion


                        context.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        log.ErrorFormat("product: {0} error: {1}", product.CustomItemNr, ex.StackTrace);
                        return(false);
                    }
                }
            }

            foreach (var product in itemProducts)
            {
                #region Related Products


                var relatedProduct  = (from va in context.VendorAssortments where va.CustomItemNumber == product.CustomItemNr && va.VendorID == vendorID_NL select va).FirstOrDefault().Try <VendorAssortment, int?>(c => c.ProductID, null);
                var relatedProducts = (from rp in context.RelatedProducts
                                       where
                                       rp.ProductID == (from pro in context.Products
                                                        where pro.VendorItemNumber == product.CustomItemNr.ToString() && pro.SourceVendorID == vendorID_NL
                                                        select pro.ProductID).FirstOrDefault() &&
                                       rp.VendorID == vendorID_NL
                                       select rp).ToList();

                foreach (var relatedProd in product.RelatedProducts)
                {
                    if ((from pro in context.Products
                         where pro.VendorItemNumber == relatedProd.RelatedProductID
                         select pro.ProductID).FirstOrDefault() != 0)
                    {
                        if (relatedProduct.HasValue)
                        {
                            RelatedProduct relProd = (from rp in relatedProducts
                                                      where
                                                      rp.ProductID == (from pro in context.Products
                                                                       where pro.VendorItemNumber == product.CustomItemNr.ToString() && pro.SourceVendorID == vendorID_NL
                                                                       select pro.ProductID).FirstOrDefault() &&
                                                      rp.RelatedProductID == (from pro in context.Products
                                                                              where pro.VendorItemNumber == relatedProd.RelatedProductID && pro.SourceVendorID == vendorID_NL
                                                                              select pro.ProductID).FirstOrDefault() &&
                                                      rp.VendorID == vendorID_NL
                                                      select rp).FirstOrDefault();

                            if (relProd == null)
                            {
                                relProd = new RelatedProduct
                                {
                                    ProductID = (from pro in context.Products
                                                 where pro.VendorItemNumber == product.CustomItemNr.ToString() && pro.SourceVendorID == vendorID_NL
                                                 select pro.ProductID).FirstOrDefault(),
                                    RelatedProductID = (from pro in context.Products
                                                        where pro.VendorItemNumber == relatedProd.RelatedProductID && pro.SourceVendorID == vendorID_NL
                                                        select pro.ProductID).FirstOrDefault(),
                                    VendorID = vendorID_NL
                                };
                                context.RelatedProducts.InsertOnSubmit(relProd);
                                relatedProducts.Add(relProd);
                            }
                        }
                    }
                }
            }
            #endregion

            return(true);
        }