Exemplo n.º 1
0
        public List <int> FindCompatibleProducts(int selectorID, int productID)
        {
            using (ConcentratorDataContext context = new ConcentratorDataContext())
            {
                var selector = (from s in context.Selectors where s.SelectorID == selectorID select s).FirstOrDefault();

                //var products = (from rp in context.RelatedProducts
                //                where rp.ProductID == productID
                //                &&
                //                context.ProductGroupSelectors.Where(c=>c.SelectorID == selectorID).Select(c => c.ProductGroupVendorID).ToList().(
                //                  (from va in context.VendorAssortments where va.VendorID == selector.VendorID select va.VendorProductGroupAssortments.Select( c=> c.ProductGroupVendorID).ToList())
                //                ))
                var allowedVendorProductGroups =
                    (from pg in context.ProductGroupSelectors
                     where pg.SelectorID == selectorID && pg.isAssortment.Try(c => c.Value, false) select pg.ProductGroupVendorID).ToList();

                var products = (from rp in context.RelatedProducts
                                where rp.ProductID == productID &&
                                allowedVendorProductGroups.(
                                    from va in context.VendorAssortments
                                    where
                                    va.VendorID == selector.VendorID
                                    &&
                                    va.ProductID == rp.ProductID
                                    select va.VendorProductGroupAssortments.Select(c => c.ProductGroupVendorID).ToList()
                                    )
                                select rp.RelatedProductID
                                ).ToList();
            }
        }
Exemplo n.º 2
0
 public List <int> GetSelectors()
 {
     using (ConcentratorDataContext context = new ConcentratorDataContext())
     {
         return((from s in context.Selectors select s.SelectorID).ToList());
     }
 }
Exemplo n.º 3
0
 protected override void Process()
 {
     using (ConcentratorDataContext context = new ConcentratorDataContext())
     {
         var mappingList = (from i in context.ProductGroupMappings
                            select i).ToList();
     }
 }
Exemplo n.º 4
0
        private void ImportFeed(ConcentratorDataContext context, XDocument reviewsDoc)
        {
            var products = (from c in context.VendorAssortments
                            select c.CustomItemNumber).Distinct().ToList();

            var reviewSources = (from rs in context.ReviewSources
                                 select rs).ToList();

            products.Sort();

            var rev = (from pr in reviewsDoc.Element("reviewsfeed").Element("products").Elements("product")
                       let root = reviewsDoc.Element("reviewsfeed")
                                  let alid = pr.Element("alid").Value
                                             let reviews = reviewsDoc.Element("reviewsfeed").Element("reviews").Elements("review").Where(c => c.Element("alid").Value == alid)
                                                           select new
            {
                CustomItemNumber = (from r in root.Element("productids").Elements("productid").Where(c => c.Element("alid").Value == alid)
                                    select r.Element("idvalue").Value).FirstOrDefault(),
                ReviewsCollection = (from m in reviews
                                     select new
                {
                    masterLineID = m.Element("masterlineid").Try(c => c.Value, string.Empty),
                    title = m.Try(c => c.Element("testtitle").Value, string.Empty),
                    sourceTestRating = m.Try <XElement, int?>(c => int.Parse(c.Element("sourcetestrating").Value), null),
                    language = m.Try(c => c.Element("language").Value, string.Empty),
                    author = m.Try(c => c.Element("author").Value, string.Empty),
                    date = m.Try <XElement, DateTime?>(c => DateTime.Parse(c.Element("testdate").Value), null),
                    source = (from s in reviewsDoc.Element("reviewsfeed").Element("sources").Elements("source")
                              where s.Element("sourceid").Value == m.Element("sourceid").Value
                              select new
                    {
                        sourceID = s.Element("sourceid").Try <XElement, int>(c => int.Parse(c.Value, 0)),
                        name = s.Element("name").Value,
                        countryCode = s.Try(c => c.Element("countrycode").Value, string.Empty),
                        languageCode = s.Try(c => c.Element("languagecode").Value, string.Empty),
                        sourceURL = s.Try(c => c.Element("sourcewww").Value, string.Empty),
                        sourceLogo = s.Try(c => c.Element("sourcelogo").Value, string.Empty),
                        sourceRank = s.Try <XElement, int?>(c => int.Parse(c.Element("sourcerank").Value), null)
                    }).FirstOrDefault(),
                    advanced = (m.Attribute("issummary") != null && m.Attribute("issummary").Try(c => bool.Parse(c.Value), false)) ?
                               new
                    {
                        ratingImageURL = m.Try(c => c.Element("ratingimage").Value, string.Empty),
                        summary = m.Try(c => c.Element("testsummary").Value, string.Empty),
                        verdict = m.Try(c => c.Element("testverdict").Value, string.Empty),
                        reviewUrl = m.Try(c => c.Element("reviewurl").Value, string.Empty)
                    } : null
                })
            });
        }
Exemplo n.º 5
0
        protected override void Process()
        {
            var config = GetConfiguration();

            vendorID_NL = int.Parse(config.AppSettings.Settings["VendorID_NL"].Value);
            vendorID_BE = int.Parse(config.AppSettings.Settings["VendorID_BE"].Value);

            var productXmlFilePath = config.AppSettings.Settings["SennheiserBasePath"].Value + "products.xml";
            var productXmlCatPath  = config.AppSettings.Settings["SennheiserBasePath"].Value + "categories.xml";

            log.InfoFormat("Processing file: {0}", "products.xml");

            var xmlText = File.ReadAllText(productXmlFilePath);

            var doc2 = HttpUtility.HtmlDecode(xmlText);

            XDocument doc = null;
            XDocument cat = null;

            try
            {
                xmlText = xmlText.Replace("<br>", " ");
                xmlText = xmlText.Replace("&M", "&amp;M");

                doc = XDocument.Parse(xmlText);
                cat = XDocument.Load(productXmlCatPath);
            }
            catch (Exception ex)
            {
                log.Error(String.Format("No new files to process or file not available"), ex);
            }

            if (!Running)
            {
                return;
            }

            using (ConcentratorDataContext context = new ConcentratorDataContext())
            {
                bool success = ParseDocument(context, doc, cat);

                if (success)
                {
                    log.DebugFormat("File successfully processed");
                }
            }
            {
                log.DebugFormat("No new files to process");
            }
        }
Exemplo n.º 6
0
 public List <int> FindProduct(int selector, string query)
 {
     using (ConcentratorDataContext context = new ConcentratorDataContext())
     {
         return((from p in context.Products
                 where
                 p.ProductDescription.Any(pd =>
                                          pd.ShortContentDescription.Contains(query) ||
                                          pd.LongContentDescription.Contains(query))
                 ||
                 p.ProductBarcodes.Any(pb =>
                                       pb.Barcode.Contains(query) ||
                                       pb.Barcode == query)
                 select p.ProductID).ToList());
     }
 }
Exemplo n.º 7
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();
                }
            }
        }
Exemplo n.º 8
0
        public void Process(DataSet content, int ImportVendorID, ILog log)
        {
            if (content != null)
            {
                int totalProducts = content.Tables[0].AsEnumerable().Count();
                log.DebugFormat("Start ProcessBSCStock for {0}, {1} productlines", ImportVendorID, totalProducts);
                using (ConcentratorDataContext context = new ConcentratorDataContext())
                {
                    VendorDataParser util = new VendorDataParser(context, ImportVendorID, log);

                    foreach (DataRow p in content.Tables[0].AsEnumerable())
                    {
                        util.LoadProduct(p, log);
                        util.UpdateBSCStockLevels(p, log);
                    }
                }
                log.DebugFormat("Finish ProcessBSCStock for {0}", ImportVendorID);
            }
            else
            {
                log.DebugFormat("Empty dataset for vendor {0} processing assorment failed", ImportVendorID);
            }
        }
Exemplo n.º 9
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();
                }
            }
        }
Exemplo n.º 10
0
        private void ProcessProductInformation(bool update, log4net.ILog log)
        {
            log.Debug("Start process product Information");
            using (var concentratorContext = new ConcentratorDataContext())
            {
                var vendorContent = (from p in concentratorContext.Products
                                     join cv in concentratorContext.BrandVendors on p.BrandID equals cv.BrandID
                                     where cv.VendorID == contentVendorID && cv.BrandID > 0
                                     select new
                {
                    ProductID = p.ProductID,
                    VendorItemNumber = p.VendorItemNumber.Trim(),
                    BrandID = p.BrandID,
                    IceCatSupplierID = int.Parse(cv.VendorBrandCode),
                    ShortDescription = string.Empty,
                    LongDescription = string.Empty,
                    p.CreationTime
                }).Distinct().OrderBy(x => x.ProductID).ToList();


                if (update)
                {
                    vendorContent = vendorContent.Where(x => x.CreationTime > DateTime.Now.AddDays(-1)).Select(x => x).ToList();
                }


                using (ICEcatDataContext ctx = new ICEcatDataContext(ConfigurationManager.ConnectionStrings["ICEcat"].ConnectionString))
                {
                    //ctx.Log = new DebuggerWriter();

                    List <ProductDescription> newAddedProducts = new List <ProductDescription>();
                    int counter = 0;
                    int logstep = 0;


                    //List<ManualResetEvent> manualEvents = new List<ManualResetEvent>();

                    //ThreadPool.SetMaxThreads(40, 40);

                    //List<BatchResult> _batchResults = new List<BatchResult>();
                    int totalNumberOfProductsToProcess = vendorContent.Count();

                    int processed = 0;

                    for (int i = vendorContent.Count - 1; i >= 0; i--)
                    {
                        StateInfo info = new StateInfo
                        {
                            VendorItemNumber = vendorContent[i].VendorItemNumber,
                            IceCatSupplierID = vendorContent[i].IceCatSupplierID,
                            BrandID          = vendorContent[i].BrandID,
                            ProductID        = vendorContent[i].ProductID
                        };

//#if DEBUG
//            if (info.ProductID != 2335976)
//              continue;
//#endif

                        Callback(info, log);

                        processed++;
                        if (processed >= 1000)
                        {
                            //int numberOfProducts = manualEvents.Count;
                            //log.Info("=================================================");
                            //log.InfoFormat("Start processing icecat product information , batch of {0} products.", numberOfProducts);
                            //WaitHandle.WaitAll(manualEvents.ToArray());
                            //double executionTime = (DateTime.Now - startTime).TotalSeconds;
                            //log.InfoFormat("Processed {0} products in : {1} seconds", numberOfProducts, executionTime);
                            //_batchResults.Add(new BatchResult(numberOfProducts, executionTime));

                            log.InfoFormat("Processed {0} of {1} total records", vendorContent.Count - i, vendorContent.Count);

                            //log.InfoFormat("Still need to process {0} of {1}; {2} done; Estimated completiontime: {3}; Average product time : {4}s",
                            //    vendorContent.Count, totalNumberOfProductsToProcess, totalNumberOfProductsToProcess - vendorContent.Count, DateTime.Now.AddSeconds(secondsNeeded), averageSecondsPerProduct);
                            log.Info("=================================================");
                            processed = 0;
                            DateTime startTime = DateTime.Now;
                        }
                    }
                }
            }
            log.Debug("Finish process product Information");
        }
Exemplo n.º 11
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);
            }
        }
Exemplo n.º 12
0
 /// <summary>
 /// Is called when the object is being updated in the database
 /// </summary>
 /// <param name="ledgerEntry">The ledger object that will be saved</param>
 /// <returns>Return true if you want the ledger entry to be written to the database. Returning false would prevent this from happening. (base.LedgerOnUpdate() will return false)</returns>
 internal virtual bool LedgerOnUpdate(ContentLedger ledgerEntry, CurrentValueRecord currentValues, DbDataRecord originalValues, ConcentratorDataContext dataContext)
 {
     return(false);
 }
Exemplo n.º 13
0
 /// <summary>
 /// Is called when the object is being inserted into the database. (base.LedgerOnInsert() will return false)
 /// </summary>
 /// <param name="ledgerEntry">The ledger object that will be saved</param>
 /// <returns>Return true if you want the ledger entry to be written to the database. Returning false would prevent this from happening.</returns>
 internal virtual bool LedgerOnInsert(ContentLedger ledgerEntry, ConcentratorDataContext dataContext)
 {
     return(false);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Is called when the object is being deleted from the database. (base.LedgerOnDelete() will return false)
 /// </summary>
 /// <param name="ledgerEntry">The ledger object that will be saved</param>
 /// <returns>Return true if you want the ledger entry to be written to the database. Returning false would prevent this from happening.</returns>
 internal virtual bool LedgerOnDelete(ContentLedger ledgerEntry, ConcentratorDataContext dataContext, LedgerObjectBase originalObject)
 {
     return(false);
 }
Exemplo n.º 15
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");
            }
        }
Exemplo n.º 16
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);
        }