Esempio n. 1
0
        /// <summary>
        /// If needed, creates the attributes passed in
        /// </summary>
        /// <param name="unit"></param>
        /// <param name="attributeMapping">Key = name, Value = IsConfigurable</param>
        /// <param name="attributes"></param>
        /// <param name="vendorID"></param>
        protected void SetupAttributes(IUnitOfWork unit, Dictionary <string, bool> attributeMapping, out List <ProductAttributeMetaData> attributes, int?vendorID)
        {
            int generalAttributegroupID = GetGeneralAttributegroupID(unit);

            var attributeRepo     = unit.Scope.Repository <ProductAttributeMetaData>();
            var attributeNameRepo = unit.Scope.Repository <ProductAttributeName>();

            #region Basic Attributes

            var attributesTmp = (from a in attributeRepo.GetAll()
                                 where attributeMapping.Keys.Contains(a.AttributeCode) &&
                                 a.VendorID == (vendorID.HasValue ? vendorID.Value : VendorID)
                                 select a).ToList();

            var attributesToAdd = from a in attributeMapping
                                  where !attributesTmp.Any(at => at.AttributeCode == a.Key)
                                  select a;

            foreach (var toAdd in attributesToAdd)
            {
                var newAttribute = new ProductAttributeMetaData
                {
                    AttributeCode           = toAdd.Key,
                    IsVisible               = true,
                    VendorID                = (vendorID.HasValue ? vendorID.Value : VendorID),
                    ProductAttributeGroupID = generalAttributegroupID,
                    Index          = 0,
                    IsConfigurable = toAdd.Value,
                    NeedsUpdate    = true,
                    IsSearchable   = attributeMapping.Contains(toAdd) ? true : false,
                    Sign           = String.Empty
                };

                attributeRepo.Add(newAttribute);

                attributesTmp.Add(newAttribute);

                var attNameEng = new ProductAttributeName
                {
                    ProductAttributeMetaData = newAttribute,
                    LanguageID = (int)LanguageTypes.English,
                    Name       = toAdd.Key
                };
                attributeNameRepo.Add(attNameEng);

                var attNameDut = new ProductAttributeName
                {
                    ProductAttributeMetaData = newAttribute,
                    LanguageID = (int)LanguageTypes.Netherlands,
                    Name       = toAdd.Key
                };
                attributeNameRepo.Add(attNameDut);
                unit.Save();
            }


            #endregion Basic Attributes

            attributes = attributesTmp;
        }
Esempio n. 2
0
        protected void SetupAttributes(IUnitOfWork unit, List <AttributeVendorMetaData> attributeMapping, int vendorID, out List <ProductAttributeMetaData> attributes)
        {
            Int32           generalAttributegroupID = GetGeneralAttributegroupID(unit, vendorID);
            List <Language> languages = unit.Scope.Repository <Language>().GetAll().ToList();

            var attributeRepo     = unit.Scope.Repository <ProductAttributeMetaData>();
            var attributeNameRepo = unit.Scope.Repository <ProductAttributeName>();

            List <ProductAttributeMetaData> res = new List <ProductAttributeMetaData>();

            attributeMapping.ForEach(attr =>
            {
                var concAttr = attributeRepo.GetSingle(c => c.AttributeCode.ToLower() == attr.Code.ToLower() && c.VendorID == attr.VendorID);

                if (concAttr == null)
                {
                    concAttr = new ProductAttributeMetaData()
                    {
                        AttributeCode           = attr.Code,
                        IsVisible               = true,
                        VendorID                = attr.VendorID,
                        ProductAttributeGroupID = generalAttributegroupID,
                        Index          = 0,
                        IsConfigurable = attr.Configurable,
                        NeedsUpdate    = true,
                        IsSearchable   = attr.Searchable,
                        Sign           = String.Empty
                    };

                    attributeRepo.Add(concAttr);
                }

                res.Add(concAttr);

                unit.Save();

                languages.ForEach(lang =>
                {
                    var productAttributeName = attributeNameRepo.GetSingle(c => c.LanguageID == lang.LanguageID && c.AttributeID == concAttr.AttributeID);

                    if (productAttributeName == null)
                    {
                        productAttributeName = new ProductAttributeName()
                        {
                            LanguageID = lang.LanguageID,
                            ProductAttributeMetaData = concAttr,
                            Name = attr.Code
                        };

                        attributeNameRepo.Add(productAttributeName);
                    }
                });
            });

            unit.Save();

            attributes = res;
        }
Esempio n. 3
0
        private Boolean TryGetAttributeValue(Int32 productID, ProductAttributeMetaData productAttribute, Language language, out string result)
        {
            result = String.Empty;

            if (_currentProductAttributes.ContainsKey(productID))
            {
                var productAttributeValue = _currentProductAttributes[productID]
                                            .SingleOrDefault(x => x.AttributeID == productAttribute.AttributeID);

                if (productAttributeValue != null)
                {
                    result = productAttributeValue.AttributeValue;

                    return(true);
                }
            }

            return(false);
        }
Esempio n. 4
0
        //private List<ProductAttributeValue> _productAttributeValues;
        //public List<ProductAttributeValue> ProductAttributeValues
        //{
        //  get
        //  {

        //    if (_productAttributeValues == null)
        //    {
        //      _productAttributeValues = (from b in concentratorContext.ProductAttributeValues
        //                                 select b).ToList();
        //    }
        //    return _productAttributeValues;
        //  }
        //  set { _productAttributeValues = value; }
        //}



        private void ProcessProductAttributes(ICEcatDataContext ctx, string vendorItemNumber, int?supplierID, int productID, log4net.ILog log)
        {
            log.Info(
                string.Format("Started importing product attributes for product with id: {0} and Vendor Item Number : {1}", productID, vendorItemNumber));
            try
            {
                //DataLoadOptions options = new DataLoadOptions();
                //options.LoadWith<ProductAttributeGroupMetaData>(x => x.ProductAttributeGroupLabels);
                //options.LoadWith<ProductAttributeGroupMetaData>(x => x.ProductAttributes);
                //options.LoadWith<ProductAttributeMetaData>(x => x.ProductAttributeLabels);
                ////options.LoadWith<ProductAttributeMetaData>(x=>x.ProductAttributeValues);

                //_concentratorDataContext.LoadOptions = options;



                foreach (int languageKey in importLanguages)
                {
                    var attributes = ctx.Concentrator_GetIceProductAttributebyManufactID(vendorItemNumber, supplierID,
                                                                                         languageKey).ToList();

                    foreach (var at in attributes)
                    {
                        if (!string.IsNullOrEmpty(at.Value))
                        {
                            #region Product Attribute Group MetaData

                            ProductAttributeGroupMetaData attributeGroup = ProductAttributeGroupMetaDatas.FirstOrDefault(
                                x => x.GroupCode == at.AttributeGroupID.ToString() && x.VendorID == contentVendorID);
                            //where pr.GroupCode == at.AttributeGroupID.ToString()

                            if (attributeGroup == null)
                            {
                                attributeGroup = new ProductAttributeGroupMetaData();
                                concentratorContext.ProductAttributeGroupMetaDatas.InsertOnSubmit(attributeGroup);
                                ProductAttributeGroupMetaDatas.Add(attributeGroup);
                            }
                            attributeGroup.Index     = at.AttributeGroupIndex;
                            attributeGroup.GroupCode = at.AttributeGroupID.ToString();
                            attributeGroup.VendorID  = contentVendorID;

                            #endregion

                            #region Product Attribute Group Name


                            ProductAttributeGroupName attributeGroupName = ProductAttributeGroupNames.FirstOrDefault(
                                pag => pag.LanguageID == languageKey &&
                                pag.ProductAttributeGroupID == attributeGroup.ProductAttributeGroupID
                                );


                            if (attributeGroupName == null)
                            {
                                attributeGroupName = new ProductAttributeGroupName
                                {
                                    LanguageID            = languageKey,
                                    ProductAttributeGroup = attributeGroup
                                };
                                concentratorContext.ProductAttributeGroupNames.InsertOnSubmit(attributeGroupName);
                                ProductAttributeGroupNames.Add(attributeGroupName);
                            }
                            attributeGroupName.Name = at.AttributeGroupName;

                            #endregion

                            #region Product Attribute MetaData

                            ProductAttributeMetaData productAttribute =
                                ProductAttributeMetaDatas.FirstOrDefault(x => x.AttributeCode == at.Feature_ID.ToString() && x.ProductAttributeGroupID == attributeGroup.ProductAttributeGroupID && x.VendorID == contentVendorID);

                            if (productAttribute == null)
                            {
                                productAttribute = new ProductAttributeMetaData()
                                {
                                    VendorID              = contentVendorID,
                                    NeedsUpdate           = true,
                                    AttributeCode         = at.Feature_ID.ToString(),
                                    ProductAttributeGroup = attributeGroup
                                };

                                concentratorContext.ProductAttributeMetaDatas.InsertOnSubmit(productAttribute);
                                ProductAttributeMetaDatas.Add(productAttribute);
                            }
                            productAttribute.Sign          = string.IsNullOrEmpty(at.LanguageSign) ? at.Sign : at.LanguageSign;
                            productAttribute.Index         = at.FeatureIndex;
                            productAttribute.AttributeCode = at.Feature_ID.ToString();
                            productAttribute.IsVisible     = at.KeyFeature.HasValue ? at.KeyFeature.Value == 0 : false;
                            productAttribute.IsSearchable  = Convert.ToBoolean(at.IsSearchable.Try(c => c.Value, 0));
                            productAttribute.VendorID      = contentVendorID;

                            #endregion

                            #region Product Attribute Value

                            ProductAttributeValue value =
                                concentratorContext.ProductAttributeValues.FirstOrDefault(x => x.LanguageID == languageKey &&
                                                                                          x.ProductID == productID &&
                                                                                          x.AttributeID == productAttribute.AttributeID
                                                                                          );

                            if (value == null)
                            {
                                value = new ProductAttributeValue
                                {
                                    ProductID        = productID,
                                    ProductAttribute = productAttribute,
                                    LanguageID       = languageKey
                                };
                                concentratorContext.ProductAttributeValues.InsertOnSubmit(value);
                            }

                            if (at.Value.Length > 3000)
                            {
                                log.DebugFormat("ProductAttribute value length > 3000 for product {0}, attritbuteID {1}", productID, productAttribute.AttributeID);
                            }


                            value.Value = at.Value.Cap(3000);

                            #endregion

                            #region Product Attribute Name

                            ProductAttributeName attributeName =
                                ProductAttributeNames.FirstOrDefault(x => x.LanguageID == languageKey &&
                                                                     x.AttributeID == productAttribute.AttributeID);
                            if (attributeName == null)
                            {
                                attributeName = new ProductAttributeName()
                                {
                                    ProductAttribute = productAttribute,
                                    LanguageID       = languageKey
                                };
                                concentratorContext.ProductAttributeNames.InsertOnSubmit(attributeName);
                                ProductAttributeNames.Add(attributeName);
                            }

                            attributeName.Name = at.AttributeName;

                            #endregion

                            concentratorContext.SubmitChanges();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                log.Error(string.Format("Importing Product Attributes failed for product with id: {0} and Vendor Item Number : {1}", productID, vendorItemNumber));
                log.Error("Import failed", e);
            }
        }
Esempio n. 5
0
        private void ParseDocuments(IUnitOfWork unit, XDocument[] products, XDocument cont)
        {
            #region Xml Data

            //products = new XDocument[1];
            //products[0] = XDocument.Load(@"C:\Lenmar\test.xml");

            var itemContent = (from content in cont.Root.Elements("item")
                               let attributes = content.Elements().Where(x => AttributeMapping.Contains(x.Name.LocalName))
                                                where content.Element("LenmarSKU") != null && content.Element("Description") != null && content.Element("Manufacturer") != null
                                                select new
            {
                LenmarSKU = content.Element("LenmarSKU").Value,
                ShortContentDescription = content.Element("Description").Value,
                GroupCode = content.Element("Manufacturer").Value,
                CostPrice = content.Element("BC_euro_cost") != null ? content.Element("BC_euro_cost").Value : "0",
                dynamic = attributes
            }).ToList();
            XNamespace xName = "http://logictec.com/schemas/internaldocuments";


            var itemProducts = (from d in products
                                from itemproduct in d.Elements(xName + "Envelope").Elements("Messages").Elements(xName + "Price")
                                let c = itemContent.Where(x => x.LenmarSKU == itemproduct.Element("SupplierSku").Value).FirstOrDefault()
                                        select new
            {
                VendorBrandCode = itemproduct.Element("MfgName").Value,
                VendorName = itemproduct.Element("MfgName").Value,
                SupplierSKU = itemproduct.Element("SupplierSku").Value,
                CustomItemNr = itemproduct.Element("MfgSKU").Value,
                ShortDescription = itemproduct.Element("ProductName").Value,
                Price = itemproduct.Element("MSRP").Value,
                Status = itemproduct.Element("Active").Value,
                CostPrice = c != null ? c.CostPrice : itemproduct.Element("Price").Value,
                QuantityOnHand = itemproduct.Element("Inventory").Value,
                VendorProductGroupCode1 = itemproduct.Element("Category1").Value,
                VendorProductGroupCode2 = itemproduct.Element("Category2").Value,
                VendorProductGroupCode3 = itemproduct.Element("Category3").Value,
                VendorProductGroupCode4 = itemproduct.Element("Category4").Value,
                VendorProductGroupCode5 = itemproduct.Element("Category5").Value,
                Barcode = itemproduct.Element("UPCCode").Value,
                ShortContentDescription = c != null ? c.ShortContentDescription : string.Empty
            }).ToList();

            #endregion

            var _brandVendorRepo       = unit.Scope.Repository <BrandVendor>();
            var _productRepo           = unit.Scope.Repository <Product>();
            var _assortmentRepo        = unit.Scope.Repository <VendorAssortment>();
            var productGroupVendorRepo = unit.Scope.Repository <ProductGroupVendor>();
            var _prodDescriptionRepo   = unit.Scope.Repository <ProductDescription>();
            var _attrGroupRepo         = unit.Scope.Repository <ProductAttributeGroupMetaData>();
            var _attrGroupName         = unit.Scope.Repository <ProductAttributeGroupName>();
            var _attrRepo      = unit.Scope.Repository <ProductAttributeMetaData>();
            var _attrNameRepo  = unit.Scope.Repository <ProductAttributeName>();
            var _attrValueRepo = unit.Scope.Repository <ProductAttributeValue>();
            var _mediaRepo     = unit.Scope.Repository <ProductMedia>();
            var _priceRepo     = unit.Scope.Repository <VendorPrice>();
            var _stockRepo     = unit.Scope.Repository <VendorStock>();
            var _barcodeRepo   = unit.Scope.Repository <ProductBarcode>();

            var brands                     = _brandVendorRepo.GetAll(bv => bv.VendorID == VendorID).ToList();
            var productGroups              = productGroupVendorRepo.GetAll(g => g.VendorID == VendorID).ToList();
            var productAttributes          = _attrRepo.GetAll(g => g.VendorID == VendorID).ToList();
            var productAttributeGroups     = _attrGroupRepo.GetAll().ToList();
            var currentProductGroupVendors = productGroupVendorRepo.GetAll(g => g.VendorID == VendorID).ToList();

            ProductStatusVendorMapper mapper = new ProductStatusVendorMapper(unit.Scope.Repository <VendorProductStatus>(), VendorID);

            var unusedVendorAssortmentItems = _assortmentRepo.GetAll(x => x.VendorID == VendorID && x.IsActive == true).ToList();

            int counter = 0;
            int total   = itemProducts.Count();
            int totalNumberOfProductsToProcess = total;
            log.InfoFormat("Start import {0} products", total);

            foreach (var product in itemProducts)
            {
                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 = brands.FirstOrDefault(vb => vb.VendorBrandCode == product.VendorBrandCode);

                    if (brandVendor == null) //if brandvendor does not exist
                    {
                        //create new brandVendor
                        brandVendor = new BrandVendor
                        {
                            BrandID         = unmappedID,
                            VendorID        = VendorID,
                            VendorBrandCode = product.VendorBrandCode,
                            Name            = product.VendorBrandCode
                        };
                        _brandVendorRepo.Add(brandVendor);
                        brands.Add(brandVendor);
                    }

                    //use BrandID to create product and retreive ProductID

                    var BrandID = brandVendor.BrandID;

                    var prods = _productRepo.GetAll(p => p.VendorItemNumber == product.SupplierSKU && (p.BrandID == BrandID || p.BrandID < 0)).ToList();
                    prod = prods.OrderByDescending(x => x.BrandID).FirstOrDefault();

                    if (prods.Count() > 1)
                    {
                        try
                        {
                            _stockRepo.Delete(prods.Where(x => x.BrandID < 0).SelectMany(x => x.VendorStocks));
                            unit.Save();
                            _productRepo.Delete(prods.Where(x => x.BrandID < 0));
                            unit.Save();
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    //if product does not exist (usually true)
                    if (prod == null)
                    {
                        prod = new Product
                        {
                            VendorItemNumber = product.SupplierSKU,
                            SourceVendorID   = VendorID
                        };
                        _productRepo.Add(prod);
                    }
                    prod.BrandID = BrandID;
                    unit.Save();

                    #endregion

                    #region VendorAssortMent

                    var productID = prod.ProductID;

                    if (prod.VendorAssortments == null)
                    {
                        prod.VendorAssortments = new List <VendorAssortment>();
                    }
                    var vendorAssortment = prod.VendorAssortments.FirstOrDefault(va => va.VendorID == VendorID);

                    //if vendorAssortMent does not exist
                    if (vendorAssortment == null)
                    {
                        //create vendorAssortMent with productID
                        vendorAssortment = new VendorAssortment
                        {
                            Product          = prod,
                            CustomItemNumber = product.CustomItemNr,
                            VendorID         = VendorID
                        };
                        _assortmentRepo.Add(vendorAssortment);
                    }
                    vendorAssortment.IsActive         = true;
                    vendorAssortment.ShortDescription =
                        product.ShortDescription.Length > 150
                                       ? product.ShortDescription.Substring(0, 150)
                                       : product.ShortDescription;
                    vendorAssortment.LongDescription = "";

                    unusedVendorAssortmentItems.Remove(vendorAssortment);

                    #endregion

                    #region VendorPrice

                    if (vendorAssortment.VendorPrices == null)
                    {
                        vendorAssortment.VendorPrices = new List <VendorPrice>();
                    }
                    var vendorPrice = vendorAssortment.VendorPrices.FirstOrDefault();
                    //create vendorPrice with vendorAssortmentID
                    if (vendorPrice == null)
                    {
                        vendorPrice = new VendorPrice
                        {
                            VendorAssortment = vendorAssortment
                        };
                        _priceRepo.Add(vendorPrice);
                    }

                    vendorPrice.Price                = Decimal.Parse(product.Price) / 119;
                    vendorPrice.TaxRate              = (Decimal)19.00;
                    vendorPrice.CommercialStatus     = product.Status;
                    vendorPrice.MinimumQuantity      = 0;
                    vendorPrice.CostPrice            = Decimal.Parse(product.CostPrice) / 100;
                    vendorPrice.ConcentratorStatusID = mapper.SyncVendorStatus(product.Status, -1);

                    #endregion

                    #region VendorStock

                    //var vendorStock = vendorAssortment.VendorStock.FirstOrDefault();
                    var vendorStock = _stockRepo.GetSingle(c => c.ProductID == vendorAssortment.ProductID && c.VendorID == vendorAssortment.VendorID);
                    //create vendorStock with productID
                    if (vendorStock == null)
                    {
                        vendorStock = new VendorStock
                        {
                            Product = prod
                        };
                        _stockRepo.Add(vendorStock);
                    }

                    vendorStock.StockStatus          = product.Status;
                    vendorStock.QuantityOnHand       = int.Parse(product.QuantityOnHand);
                    vendorStock.VendorID             = VendorID;
                    vendorStock.VendorStockTypeID    = 1;
                    vendorStock.VendorStatus         = product.Status;
                    vendorStock.ConcentratorStatusID = mapper.SyncVendorStatus(product.Status, -1);

                    #endregion

                    #region ProductGroupVendor

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

                    //create vendorGroup five times, each time with a different VendorProductGroupCode on a different level if not exist
                    string vendorProductGroupCode1 = string.IsNullOrEmpty(product.VendorProductGroupCode1) ? "Battery" : product.VendorProductGroupCode1;

                    var productGroupVendor1 =
                        productGroups.FirstOrDefault(pg => pg.VendorProductGroupCode1 == vendorProductGroupCode1);

                    if (productGroupVendor1 == null)
                    {
                        productGroupVendor1 = new ProductGroupVendor
                        {
                            ProductGroupID          = unmappedID,
                            VendorID                = VendorID,
                            VendorName              = product.VendorName,
                            VendorProductGroupCode1 = vendorProductGroupCode1,

                            VendorAssortments = new List <VendorAssortment>()
                        };
                        productGroupVendorRepo.Add(productGroupVendor1);
                        productGroups.Add(productGroupVendor1);
                    }


                    #region sync

                    if (currentProductGroupVendors.Contains(productGroupVendor1))
                    {
                        currentProductGroupVendors.Remove(productGroupVendor1);
                    }
                    #endregion



                    if (!productGroupVendor1.VendorAssortments.Any(c => c == vendorAssortment))
                    {
                        productGroupVendor1.VendorAssortments.Add(vendorAssortment);
                    }

                    if (!string.IsNullOrEmpty(product.VendorProductGroupCode2))
                    {
                        var productGroupVendor2 =
                            productGroups.FirstOrDefault(pg => pg.VendorProductGroupCode2 == product.VendorProductGroupCode2);

                        if (productGroupVendor2 == null)
                        {
                            productGroupVendor2 = new ProductGroupVendor
                            {
                                ProductGroupID          = unmappedID,
                                VendorID                = VendorID,
                                VendorName              = product.VendorName,
                                VendorProductGroupCode2 = product.VendorProductGroupCode2,
                                VendorAssortments       = new List <VendorAssortment>()
                            };

                            productGroupVendorRepo.Add(productGroupVendor2);
                            productGroups.Add(productGroupVendor2);
                        }


                        #region sync

                        if (currentProductGroupVendors.Contains(productGroupVendor2))
                        {
                            currentProductGroupVendors.Remove(productGroupVendor2);
                        }
                        #endregion

                        var vendorProductGroupAssortment2 =
                            productGroupVendor2.VendorAssortments.FirstOrDefault(c => c.VendorAssortmentID == vendorAssortment.VendorAssortmentID);

                        if (vendorProductGroupAssortment2 == null)
                        {
                            productGroupVendor2.VendorAssortments.Add(vendorAssortment);
                        }
                    }

                    if (!string.IsNullOrEmpty(product.VendorProductGroupCode3))
                    {
                        var productGroupVendor3 =
                            productGroups.FirstOrDefault(pg => pg.VendorProductGroupCode3 == product.VendorProductGroupCode3);

                        if (productGroupVendor3 == null)
                        {
                            productGroupVendor3 = new ProductGroupVendor
                            {
                                ProductGroupID          = unmappedID,
                                VendorID                = VendorID,
                                VendorName              = product.VendorName,
                                VendorProductGroupCode3 = product.VendorProductGroupCode3,
                                VendorAssortments       = new List <VendorAssortment>()
                            };

                            productGroupVendorRepo.Add(productGroupVendor3);
                            productGroups.Add(productGroupVendor3);
                        }


                        #region sync

                        if (currentProductGroupVendors.Contains(productGroupVendor3))
                        {
                            currentProductGroupVendors.Remove(productGroupVendor3);
                        }
                        #endregion

                        var vendorProductGroupAssortment3 =
                            productGroupVendor3.VendorAssortments.FirstOrDefault(c => c.VendorAssortmentID == vendorAssortment.VendorAssortmentID);

                        if (vendorProductGroupAssortment3 == null)
                        {
                            productGroupVendor3.VendorAssortments.Add(vendorAssortment);
                        }
                    }

                    if (!string.IsNullOrEmpty(product.VendorProductGroupCode4))
                    {
                        var productGroupVendor4 =
                            productGroups.FirstOrDefault(pg => pg.VendorProductGroupCode4 == product.VendorProductGroupCode4);

                        if (productGroupVendor4 == null)
                        {
                            productGroupVendor4 = new ProductGroupVendor
                            {
                                ProductGroupID          = unmappedID,
                                VendorID                = VendorID,
                                VendorName              = product.VendorName,
                                VendorProductGroupCode4 = product.VendorProductGroupCode4,
                                VendorAssortments       = new List <VendorAssortment>()
                            };

                            productGroupVendorRepo.Add(productGroupVendor4);
                            productGroups.Add(productGroupVendor4);
                        }


                        #region sync

                        if (currentProductGroupVendors.Contains(productGroupVendor4))
                        {
                            currentProductGroupVendors.Remove(productGroupVendor4);
                        }
                        #endregion

                        var vendorProductGroupAssortment4 =
                            productGroupVendor4.VendorAssortments.FirstOrDefault(c => c.VendorAssortmentID == vendorAssortment.VendorAssortmentID);

                        if (vendorProductGroupAssortment4 == null)
                        {
                            productGroupVendor4.VendorAssortments.Add(vendorAssortment);
                        }
                    }

                    if (!string.IsNullOrEmpty(product.VendorProductGroupCode5))
                    {
                        var productGroupVendor5 =
                            productGroups.FirstOrDefault(pg => pg.VendorProductGroupCode5 == product.VendorProductGroupCode5);

                        if (productGroupVendor5 == null)
                        {
                            productGroupVendor5 = new ProductGroupVendor
                            {
                                ProductGroupID          = unmappedID,
                                VendorID                = VendorID,
                                VendorName              = product.VendorName,
                                VendorProductGroupCode5 = product.VendorProductGroupCode5,
                                VendorAssortments       = new List <VendorAssortment>()
                            };

                            productGroupVendorRepo.Add(productGroupVendor5);
                            productGroups.Add(productGroupVendor5);
                        }


                        #region sync

                        if (currentProductGroupVendors.Contains(productGroupVendor5))
                        {
                            currentProductGroupVendors.Remove(productGroupVendor5);
                        }

                        #endregion
                        var vendorProductGroupAssortment5 =
                            productGroupVendor5.VendorAssortments.FirstOrDefault(c => c.VendorAssortmentID == vendorAssortment.VendorAssortmentID);

                        if (vendorProductGroupAssortment5 == null)
                        {
                            productGroupVendor5.VendorAssortments.Add(vendorAssortment);
                        }
                    }

                    if (!string.IsNullOrEmpty(product.VendorBrandCode))
                    {
                        var brandProductGroupvendor =
                            productGroups.FirstOrDefault(pg => pg.BrandCode == product.VendorBrandCode && pg.VendorProductGroupCode1 == null && pg.VendorProductGroupCode2 == null && pg.VendorProductGroupCode3 == null && pg.VendorProductGroupCode4 == null && pg.VendorProductGroupCode5 == null);

                        if (brandProductGroupvendor == null)
                        {
                            brandProductGroupvendor = new ProductGroupVendor
                            {
                                ProductGroupID = unmappedID,
                                VendorID       = VendorID,
                                VendorName     = product.VendorName,
                                BrandCode      = product.VendorBrandCode
                            };

                            productGroupVendorRepo.Add(brandProductGroupvendor);
                            productGroups.Add(brandProductGroupvendor);
                        }


                        #region sync

                        if (currentProductGroupVendors.Contains(brandProductGroupvendor))
                        {
                            currentProductGroupVendors.Remove(brandProductGroupvendor);
                        }

                        #endregion
                        var brandProductGroupAssortment =
                            brandProductGroupvendor.VendorAssortments.FirstOrDefault(c => c.VendorAssortmentID == vendorAssortment.VendorAssortmentID);

                        if (brandProductGroupAssortment == null)
                        {
                            brandProductGroupvendor.VendorAssortments.Add(vendorAssortment);
                        }
                    }

                    #endregion

                    #region ProductBarcode
                    if (prod.ProductBarcodes == null)
                    {
                        prod.ProductBarcodes = new List <ProductBarcode>();
                    }
                    if (!prod.ProductBarcodes.Any(pb => pb.Barcode.Trim() == product.Barcode))
                    {
                        //create ProductBarcode if not exists
                        _barcodeRepo.Add(new ProductBarcode
                        {
                            Product     = prod,
                            Barcode     = product.Barcode,
                            VendorID    = VendorID,
                            BarcodeType = (int)BarcodeTypes.Default
                        });
                    }

                    #endregion

                    #region ProductDescription
                    if (prod.ProductDescriptions == null)
                    {
                        prod.ProductDescriptions = new List <ProductDescription>();
                    }
                    var productDescription =
                        prod.ProductDescriptions.FirstOrDefault(pd => pd.LanguageID == languageID && pd.VendorID == VendorID);

                    if (productDescription == null)
                    {
                        //create ProductDescription
                        productDescription = new ProductDescription
                        {
                            Product    = prod,
                            LanguageID = languageID,
                            VendorID   = VendorID
                        };

                        _prodDescriptionRepo.Add(productDescription);
                    }
                    productDescription.ShortContentDescription = product.ShortContentDescription;

                    #endregion

                    foreach (var content in itemContent.Where(x => x.LenmarSKU == product.SupplierSKU))
                    {
                        #region ProductAttributeGroupMetaData

                        var productAttributeGroupMetaData =
                            productAttributeGroups.FirstOrDefault(c => c.GroupCode == content.GroupCode);
                        //create ProductAttributeGroupMetaData if not exists
                        if (productAttributeGroupMetaData == null)
                        {
                            productAttributeGroupMetaData = new ProductAttributeGroupMetaData
                            {
                                Index     = 0,
                                GroupCode = content.GroupCode,
                                VendorID  = VendorID
                            };
                            _attrGroupRepo.Add(productAttributeGroupMetaData);
                            productAttributeGroups.Add(productAttributeGroupMetaData);
                        }
                        #endregion

                        #region ProductAttributeGroupName
                        if (productAttributeGroupMetaData.ProductAttributeGroupNames == null)
                        {
                            productAttributeGroupMetaData.ProductAttributeGroupNames = new List <ProductAttributeGroupName>();
                        }
                        var productAttributeGroupName =
                            productAttributeGroupMetaData.ProductAttributeGroupNames.FirstOrDefault(c => c.LanguageID == languageID);
                        //create ProductAttributeGroupName if not exists
                        if (productAttributeGroupName == null)
                        {
                            productAttributeGroupName = new ProductAttributeGroupName
                            {
                                Name = "General",
                                ProductAttributeGroupMetaData = productAttributeGroupMetaData,
                                LanguageID = languageID
                            };
                            _attrGroupName.Add(productAttributeGroupName);
                        }

                        #endregion

                        #region ProductAttributeMetaData

                        //create ProductAttributeMetaData as many times that there are entrys in content.dynamic
                        foreach (var element in content.dynamic)
                        {
                            var productAttributeMetaData =
                                productAttributes.FirstOrDefault(c => c.AttributeCode == element.Name.ToString());
                            //create ProductAttributeMetaData if not exists
                            if (productAttributeMetaData == null)
                            {
                                productAttributeMetaData = new ProductAttributeMetaData
                                {
                                    ProductAttributeGroupMetaData = productAttributeGroupMetaData,
                                    AttributeCode = element.Name.ToString(),
                                    Index         = 0,
                                    IsVisible     = true,
                                    NeedsUpdate   = true,
                                    VendorID      = VendorID,
                                    IsSearchable  = false
                                };
                                _attrRepo.Add(productAttributeMetaData);
                                productAttributes.Add(productAttributeMetaData);
                            }

                            #endregion

                            #region ProductAttributeName
                            if (productAttributeMetaData.ProductAttributeNames == null)
                            {
                                productAttributeMetaData.ProductAttributeNames = new List <ProductAttributeName>();
                            }
                            var productAttributeName =
                                productAttributeMetaData.ProductAttributeNames.FirstOrDefault(c => c.LanguageID == languageID);
                            //create ProductAttributeName with generated productAttributeMetaData.AttributeID if not exists
                            if (productAttributeName == null)
                            {
                                productAttributeName = new ProductAttributeName
                                {
                                    ProductAttributeMetaData = productAttributeMetaData,
                                    LanguageID = languageID,
                                    Name       = element.Name.ToString()
                                };
                                _attrNameRepo.Add(productAttributeName);
                            }

                            #endregion

                            #region ProductAttributeValue

                            if (productAttributeMetaData.ProductAttributeValues == null)
                            {
                                productAttributeMetaData.ProductAttributeValues = new List <ProductAttributeValue>();
                            }
                            var productAttributeValue =
                                productAttributeMetaData.ProductAttributeValues.FirstOrDefault(c => c.ProductID == prod.ProductID);
                            //create ProductAttributeValue with generated productAttributeMetaData.AttributeID
                            if (productAttributeValue == null)
                            {
                                productAttributeValue = new ProductAttributeValue
                                {
                                    ProductAttributeMetaData = productAttributeMetaData,
                                    Product    = prod,
                                    Value      = element.Value,
                                    LanguageID = languageID
                                };
                                _attrValueRepo.Add(productAttributeValue);
                            }

                            #endregion
                        }
                    }
                    unit.Save();
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("product: {0} error: {1}", product.SupplierSKU, ex.StackTrace);
                }
            }
            #region delete unused vendorProductGroups
            foreach (var vProdVendor in currentProductGroupVendors)
            {
                if (vProdVendor.ProductGroupID == -1)
                {
                    productGroupVendorRepo.Add(vProdVendor);
                }
            }
            unit.Save();

            unusedVendorAssortmentItems.ForEach(x => x.IsActive = false);
            unit.Save();

            #endregion
        }
        private void DoProcess(List <TrdPartyProductModel> data, List <Attributes> atts, Objects.DataAccess.UnitOfWork.IUnitOfWork unit)
        {
            var config = GetConfiguration();

            var vendorID_NL = vendors.First(x => x.Value == "NL").Key;
            var vendorID_BE = vendors.First(x => x.Value == "BE").Key;

            var repoBrandVendor = unit.Scope.Repository <BrandVendor>();

            var prodRepo = unit.Scope.Repository <Product>().Include(c => c.ProductMedias, c => c.ProductBarcodes, c => c.ProductDescriptions);
            var productAttributeGroups = unit.Scope.Repository <ProductAttributeGroupMetaData>().GetAll(g => g.VendorID == vendorID_NL || g.VendorID == vendorID_BE).ToList();
            var repoAssortment         = unit.Scope.Repository <VendorAssortment>().Include(c => c.VendorPrices);
            var stockRepo                = unit.Scope.Repository <VendorStock>();
            var repoAttributeGroup       = unit.Scope.Repository <ProductAttributeGroupMetaData>();
            var repoAttributeGroupName   = unit.Scope.Repository <ProductAttributeGroupName>();
            var repoAttributeValue       = unit.Scope.Repository <ProductAttributeValue>();
            var repotAttributeName       = unit.Scope.Repository <ProductAttributeName>();
            var repoAttribute            = unit.Scope.Repository <ProductAttributeMetaData>().Include(c => c.ProductAttributeNames, c => c.ProductAttributeValues);
            var repoVendor               = unit.Scope.Repository <ProductGroupVendor>();
            var prodDescriptionRepo      = unit.Scope.Repository <ProductDescription>();
            var priceRepo                = unit.Scope.Repository <VendorPrice>();
            var productGroupLanguageRepo = unit.Scope.Repository <ProductGroupLanguage>();
            var productGroupMappingRepo  = unit.Scope.Repository <ProductGroupMapping>();

            //var products = prodRepo.GetAll(x => x.SourceVendorID == VendorID).ToList();
            var vendorassortments = repoAssortment.GetAll(x => x.VendorID == vendorID_NL || x.VendorID == vendorID_BE).ToList();
            var productAttributes = repoAttribute.GetAll(g => g.VendorID == vendorID_NL || g.VendorID == vendorID_BE).ToList();

            var productGroupVendorRecords = repoVendor.GetAll(pc => pc.VendorID == vendorID_NL || pc.VendorID == vendorID_BE).ToList();
            var brands              = repoBrandVendor.GetAll(bv => bv.VendorID == vendorID_NL || bv.VendorID == vendorID_BE).ToList();
            var brandEnts           = unit.Scope.Repository <Brand>().GetAll();
            var products            = prodRepo.GetAll(x => x.SourceVendorID == vendorID_NL || x.SourceVendorID == vendorID_BE).ToList();
            var vendorStock         = stockRepo.GetAll(x => x.VendorID == vendorID_NL || x.VendorID == vendorID_BE).ToList();
            var productDescriptions = prodDescriptionRepo.GetAll().ToList();

            var productGroupLanguages = productGroupLanguageRepo.GetAll().ToList();
            var prodAttributeGroups   = repoAttributeGroup.GetAll(g => g.VendorID == vendorID_NL || g.VendorID == vendorID_BE).ToList();
            var productGroupMapping   = productGroupMappingRepo.GetAll(x => x.ConnectorID == 2).ToList();

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

            log.InfoFormat("Start import {0} products", total);
            bool firstVendor = true;

            foreach (var VendorID in vendors.Keys)
            {
                ProductStatusVendorMapper mapper = new ProductStatusVendorMapper(unit.Scope.Repository <VendorProductStatus>(), VendorID);
                var vendor = unit.Scope.Repository <Vendor>().GetSingle(x => x.VendorID == VendorID);

                foreach (var product in data)
                {
                    if (string.IsNullOrEmpty(product.Artnr))
                    {
                        continue;
                    }
                    try
                    {
                        if (counter == 100)
                        {
                            counter = 0;
                            log.InfoFormat("Still need to process {0} of {1}; {2} done for {3};", totalNumberOfProductsToProcess, total, total - totalNumberOfProductsToProcess, VendorID);
                        }

                        totalNumberOfProductsToProcess--;
                        counter++;

                        var brand   = brandEnts.FirstOrDefault(c => c.Name.ToLower() == product.BrandName.Trim().ToLower());
                        int brandID = 0;

                        if (brand == null)
                        {
                            var vbrand = brands.FirstOrDefault(vb => vb.VendorBrandCode.Trim() == product.BrandName.Trim());

                            if (vbrand == null)
                            {
                                vbrand = new BrandVendor
                                {
                                    VendorID        = VendorID,
                                    VendorBrandCode = product.BrandName.Trim(),
                                    BrandID         = unmappedID,
                                    Name            = product.BrandName.Trim()
                                };

                                brands.Add(vbrand);

                                repoBrandVendor.Add(vbrand);
                            }
                            brandID = vbrand.BrandID;
                        }
                        else
                        {
                            brandID = brand.BrandID;
                        }

                        var item = products.FirstOrDefault(p => p.VendorItemNumber.Trim() == product.Artnr.Trim() && p.BrandID == brandID);
                        if (item == null)
                        {
                            item = new Product
                            {
                                VendorItemNumber = product.Artnr.Trim(),
                                BrandID          = brandID,
                                SourceVendorID   = VendorID
                            };
                            prodRepo.Add(item);
                            products.Add(item);
                            unit.Save();
                        }

                        if (firstVendor)
                        {
                            var productDescription = productDescriptions.FirstOrDefault(pd => pd.Product.VendorItemNumber == item.VendorItemNumber && pd.LanguageID == 1 && (pd.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && pd.VendorID == vendor.VendorID)));

                            if (productDescription == null)
                            {
                                //create ProductDescription
                                productDescription = new ProductDescription
                                {
                                    Product    = item,
                                    LanguageID = languageID,
                                    VendorID   = VendorID,
                                };

                                prodDescriptionRepo.Add(productDescription);
                                productDescriptions.Add(productDescription);
                            }

                            if (string.IsNullOrEmpty(productDescription.ProductName))
                            {
                                productDescription.ProductName = product.Product;
                                productDescription.ModelName   = product.Product;
                            }

                            if (!string.IsNullOrEmpty(product.ShortDescription))
                            {
                                productDescription.ShortContentDescription = product.ShortDescription.Cap(1000);
                            }

                            if (string.IsNullOrEmpty(productDescription.LongContentDescription))
                            {
                                productDescription.LongContentDescription = product.LongDescription;
                            }

                            if (string.IsNullOrEmpty(productDescription.LongSummaryDescription))
                            {
                                productDescription.LongSummaryDescription = product.LongDescription;
                            }
                        }

                        var assortment = vendorassortments.FirstOrDefault(x => x.Product.VendorItemNumber == item.VendorItemNumber.Trim() && x.VendorID == VendorID);
                        if (assortment == null)
                        {
                            assortment = new VendorAssortment
                            {
                                VendorID         = VendorID,
                                Product          = item,
                                ShortDescription = product.Try(c => c.ShortDescription.Cap(1000), string.Empty),//.ShortDescription // .Length > 150 ? product.ShortDescription.Substring(0, 150) : product.ShortDescription,
                                CustomItemNumber = product.Artnr.Trim(),
                                IsActive         = true,
                                LongDescription  = product.LongDescription
                            };
                            vendorassortments.Add(assortment);
                            repoAssortment.Add(assortment);
                        }

                        var productGroups = new string[4];
                        productGroups[0] = product.BrandName;
                        productGroups[1] = product.Chapter;
                        productGroups[2] = product.Category;
                        productGroups[3] = product.Subcategory;

                        var pgvs = assortment.ProductGroupVendors != null?assortment.ProductGroupVendors.ToList() : new List <ProductGroupVendor>();

                        ProductGroupMapping parentpgm = null;

                        for (int i = 0; i < productGroups.Length; i++)
                        {
                            var groupCode = productGroups[i];

                            if (string.IsNullOrEmpty(groupCode))
                            {
                                continue;
                            }

                            var productGroup = productGroupLanguages.FirstOrDefault(x => x.Name == groupCode.Trim());
                            if (productGroup == null)
                            {
                                var pg = new Concentrator.Objects.Models.Products.ProductGroup()
                                {
                                    Score = 0
                                };

                                unit.Scope.Repository <Concentrator.Objects.Models.Products.ProductGroup>().Add(pg);

                                productGroup = new ProductGroupLanguage()
                                {
                                    ProductGroup = pg,
                                    LanguageID   = 1,
                                    Name         = groupCode.Trim()
                                };

                                productGroupLanguages.Add(productGroup);
                                productGroupLanguageRepo.Add(productGroup);
                                unit.Save();
                            }

                            //var productGroupVendor = productGroupVendorRecords.Where(pg => pg.GetType().GetProperty(string.Format("VendorProductGroupCode{0}", i + 1)).GetValue(pg, null) != null
                            //        && pg.GetType().GetProperty(string.Format("VendorProductGroupCode{0}", i + 1)).GetValue(pg, null).ToString() == groupCode.Trim()).FirstOrDefault();

                            var productGroupVendor = productGroupVendorRecords.FirstOrDefault(x => x.ProductGroupID == productGroup.ProductGroupID);

                            if (productGroupVendor == null)
                            {
                                productGroupVendor = new ProductGroupVendor
                                {
                                    ProductGroupID = productGroup.ProductGroupID,
                                    VendorID       = VendorID,
                                    VendorName     = groupCode.Trim(),
                                };

                                productGroupVendor.GetType().GetProperty(string.Format("VendorProductGroupCode{0}", i + 1)).SetValue(productGroupVendor, groupCode.Trim().Cap(50), null);

                                repoVendor.Add(productGroupVendor);
                                productGroupVendorRecords.Add(productGroupVendor);
                            }

                            if (productGroupVendor.VendorAssortments == null)
                            {
                                productGroupVendor.VendorAssortments = new List <VendorAssortment>();
                            }

                            var assortmentRelation = productGroupVendor.VendorAssortments.Count == 0 ? null : productGroupVendor.VendorAssortments.Where(c => c.Product != null && c.Product.VendorItemNumber == item.VendorItemNumber.Trim() && c.VendorID == VendorID).FirstOrDefault();
                            if (assortmentRelation == null)
                            {
                                productGroupVendor.VendorAssortments.Add(assortment);
                            }
                            else
                            {
                                pgvs.Remove(productGroupVendor);
                            }

                            if (productGroupVendor.ProductGroupID > 0)
                            {
                                ProductGroupMapping pgm = null;

                                if (parentpgm != null)
                                {
                                    pgm = productGroupMapping.FirstOrDefault(x => x.ProductGroupID == productGroupVendor.ProductGroupID && x.ParentProductGroupMappingID == parentpgm.ProductGroupMappingID);
                                }
                                else
                                {
                                    pgm = productGroupMapping.FirstOrDefault(x => x.ProductGroupID == productGroupVendor.ProductGroupID);
                                }

                                if (pgm == null)
                                {
                                    pgm = new ProductGroupMapping()
                                    {
                                        ConnectorID         = 2,
                                        ProductGroupID      = productGroupVendor.ProductGroupID,
                                        FlattenHierarchy    = false,
                                        FilterByParentGroup = parentpgm != null ? true : false,
                                        Depth = i,
                                        Score = 0
                                    };

                                    if (parentpgm != null)
                                    {
                                        pgm.ParentProductGroupMappingID = parentpgm.ProductGroupMappingID;
                                    }

                                    unit.Scope.Repository <ProductGroupMapping>().Add(pgm);
                                    unit.Save();
                                    productGroupMapping.Add(pgm);
                                }
                                parentpgm = pgm;
                            }
                        }

                        pgvs.ForEach(pg =>
                        {
                            productGroupVendorRecords.Remove(pg);
                        });

                        if (firstVendor)
                        {
                            if (!string.IsNullOrEmpty(product.PriceGroup))
                            {
                                atts.Add(new Attributes()
                                {
                                    AttArtnr = product.Artnr.Trim(),
                                    Feature  = "PriceGroup",
                                    Value    = product.PriceGroup
                                });
                            }

                            if (!string.IsNullOrEmpty(product.Features))
                            {
                                atts.Add(new Attributes()
                                {
                                    AttArtnr = product.Artnr.Trim(),
                                    Feature  = "Features",
                                    Value    = product.Features
                                });
                            }

                            if (!string.IsNullOrEmpty(product.NEW))
                            {
                                atts.Add(new Attributes()
                                {
                                    AttArtnr = product.Artnr.Trim(),
                                    Feature  = "New",
                                    Value    = product.NEW
                                });
                            }

                            var attributes = atts.Where(x => x.AttArtnr != null && (x.AttArtnr.Trim() == product.Artnr.Trim())).ToList();

                            if (attributes != null && attributes.Count > 0)
                            {
                                foreach (var att in attributes)
                                {
                                    string AttributeCode        = "General",
                                             AttributeGroupCode = att.Feature,
                                             AttributeValue     = att.Value;

                                    if (!string.IsNullOrEmpty(AttributeGroupCode) && !string.IsNullOrEmpty(AttributeValue))
                                    {
                                        var productAttributeMetadata = productAttributes.FirstOrDefault(c => c.ProductAttributeNames.Any(l => l.Name == AttributeGroupCode) && c.VendorID == VendorID);
                                        if (productAttributeMetadata == null)
                                        {
                                            productAttributeMetadata = new ProductAttributeMetaData
                                            {
                                                AttributeCode = att.Feature,
                                                Index         = 0,
                                                IsVisible     = true,
                                                NeedsUpdate   = true,
                                                VendorID      = VendorID,
                                                IsSearchable  = false
                                            };
                                            productAttributes.Add(productAttributeMetadata);
                                            repoAttribute.Add(productAttributeMetadata);
                                        }

                                        var attributeGroup = productAttributeMetadata.ProductAttributeGroupMetaData;
                                        if (attributeGroup == null)
                                        {
                                            attributeGroup = new ProductAttributeGroupMetaData
                                            {
                                                Index     = 0,
                                                GroupCode = "TechnicalData",
                                                VendorID  = VendorID
                                            };
                                            repoAttributeGroup.Add(attributeGroup);
                                            productAttributeMetadata.ProductAttributeGroupMetaData = attributeGroup;
                                        }
                                        productAttributeMetadata.ProductAttributeGroupID = attributeGroup.ProductAttributeGroupID;

                                        var attributeGroupName = attributeGroup.ProductAttributeGroupNames.FirstOrDefault(c => c.LanguageID == 1);
                                        if (attributeGroupName == null)
                                        {
                                            attributeGroupName = new ProductAttributeGroupName
                                            {
                                                Name       = AttributeCode,
                                                LanguageID = languageID,
                                                ProductAttributeGroupMetaData = attributeGroup
                                            };
                                            repoAttributeGroupName.Add(attributeGroupName);
                                            attributeGroup.ProductAttributeGroupNames.Add(attributeGroupName);
                                        }



                                        var attributeName = productAttributeMetadata.ProductAttributeNames.FirstOrDefault(c => c.LanguageID == 1);
                                        if (attributeName == null)
                                        {
                                            attributeName = new ProductAttributeName
                                            {
                                                ProductAttributeMetaData = productAttributeMetadata,
                                                LanguageID = 1,
                                                Name       = AttributeGroupCode
                                            };
                                            repotAttributeName.Add(attributeName);
                                            productAttributeMetadata.ProductAttributeNames.Add(attributeName);
                                        }

                                        var attributeValue = productAttributeMetadata.ProductAttributeValues.FirstOrDefault(c => c.Value == AttributeValue && c.ProductID == item.ProductID);
                                        if (attributeValue == null && !string.IsNullOrEmpty(AttributeValue))
                                        {
                                            attributeValue = new ProductAttributeValue
                                            {
                                                Value = AttributeValue,
                                                ProductAttributeMetaData = productAttributeMetadata,
                                                LanguageID = 1,
                                                Product    = item
                                            };
                                            productAttributeMetadata.ProductAttributeValues.Add(attributeValue);
                                            repoAttributeValue.Add(attributeValue);
                                        }
                                    }
                                }
                            }
                        }

                        var stock = vendorStock.FirstOrDefault(c => c.Product.VendorItemNumber == item.VendorItemNumber.Trim() && c.VendorID == VendorID);

                        if (stock == null)
                        {
                            stock = new VendorStock
                            {
                                Product           = item,
                                QuantityOnHand    = 0,
                                VendorID          = VendorID,
                                VendorStockTypeID = 1
                            };
                            vendorStock.Add(stock);
                            stockRepo.Add(stock);
                        }



                        if (assortment.VendorPrices == null)
                        {
                            assortment.VendorPrices = new List <VendorPrice>();
                        }


                        var vendorPrice = assortment.VendorPrices.FirstOrDefault();
                        //create vendorPrice with vendorAssortmentID
                        if (vendorPrice == null)
                        {
                            vendorPrice = new VendorPrice
                            {
                                VendorAssortment = assortment,
                                CommercialStatus = "S",
                                MinimumQuantity  = 0
                            };
                            priceRepo.Add(vendorPrice);
                            assortment.VendorPrices.Add(vendorPrice);
                        }

                        decimal taxRate   = 19;
                        decimal price     = 0;
                        decimal costPrice = 0;

                        switch (vendors[VendorID])
                        {
                        case "NL":
                            taxRate = 19;
                            Decimal.TryParse(product.VATExclNL, out costPrice);
                            Decimal.TryParse(product.NLincl, out price);
                            break;

                        case "BE":
                            taxRate = 21;
                            Decimal.TryParse(product.VATExclBE, out costPrice);
                            Decimal.TryParse(product.BEincl, out price);
                            break;
                        }

                        vendorPrice.Price     = price;
                        vendorPrice.CostPrice = costPrice;
                        vendorPrice.TaxRate   = taxRate;

                        if (product.Image != null)
                        {
                            if (item.ProductMedias == null)
                            {
                                item.ProductMedias = new List <ProductMedia>();
                            }
                            if (!item.ProductMedias.Any(pi => pi.VendorID == VendorID && pi.MediaUrl == product.Image))
                            {
                                unit.Scope.Repository <ProductMedia>().Add(new ProductMedia
                                {
                                    VendorID = VendorID,
                                    MediaUrl = product.Image,
                                    TypeID   = 1, // image
                                    Product  = item,
                                    Sequence = 0
                                });
                            }
                        }


                        if (product.EAN != null || product.Barcode != null)
                        {
                            if (item.ProductBarcodes == null)
                            {
                                item.ProductBarcodes = new List <ProductBarcode>();
                            }
                            if (!item.ProductBarcodes.Any(pb => pb.Barcode.Trim() == product.EAN.Trim()))
                            {
                                unit.Scope.Repository <ProductBarcode>().Add(new ProductBarcode
                                {
                                    Product     = item,
                                    Barcode     = product.EAN != null ? product.EAN : product.Barcode != null ? product.Barcode : "",
                                    BarcodeType = (int)BarcodeTypes.Default,
                                    VendorID    = VendorID
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.AuditError("Error import products for Sennheiser 3rdParty", ex);
                    }
                }
                firstVendor = false;
            }
        }
Esempio n. 7
0
        protected void SetupBrandAndAttributes(IUnitOfWork unit, out List <ProductAttributeMetaData> attributes, out BrandVendor brandVendor)
        {
            int generalAttributegroupID = GetGeneralAttributegroupID(unit);

            #region Basic Attributes
            var attributeRepo     = unit.Scope.Repository <ProductAttributeMetaData>();
            var attributeNameRepo = unit.Scope.Repository <ProductAttributeName>();
            var brandVendorRepo   = unit.Scope.Repository <BrandVendor>();

            var attributesTmp = (from a in attributeRepo.GetAll()
                                 where AttributeMapping.Contains(a.AttributeCode) &&
                                 a.VendorID == VendorID
                                 select a).ToList();

            var attributesToAdd = from a in AttributeMapping
                                  where !attributesTmp.Any(at => at.AttributeCode == a)
                                  select a;

            foreach (var toAdd in attributesToAdd)
            {
                var newAttribute = new ProductAttributeMetaData
                {
                    AttributeCode           = toAdd,
                    IsVisible               = true,
                    VendorID                = VendorID,
                    ProductAttributeGroupID = generalAttributegroupID,
                    Index        = 0,
                    NeedsUpdate  = true,
                    IsSearchable = FilterAttributes.Contains(toAdd) ? true : false,
                    Sign         = String.Empty
                };

                attributeRepo.Add(newAttribute);
                attributesTmp.Add(newAttribute);

                var attNameEng = new ProductAttributeName
                {
                    ProductAttributeMetaData = newAttribute,
                    LanguageID = (int)LanguageTypes.English,
                    Name       = toAdd
                };
                attributeNameRepo.Add(attNameEng);

                var attNameDut = new ProductAttributeName
                {
                    ProductAttributeMetaData = newAttribute,
                    LanguageID = (int)LanguageTypes.Netherlands,
                    Name       = toAdd
                };
                attributeNameRepo.Add(attNameDut);
            }
            unit.Save();

            #endregion Basic Attributes

            #region Brand
            brandVendor = unit.Scope.Repository <BrandVendor>().GetSingle(bv => bv.VendorBrandCode == BrandVendorCode);
            if (brandVendor == null)
            {
                var brand = new Brand
                {
                    Name = "VSN"
                };
                unit.Scope.Repository <Brand>().Add(brand);

                brandVendor = new BrandVendor
                {
                    VendorBrandCode = BrandVendorCode,
                    VendorID        = VendorID,
                    Brand           = brand
                };
                brandVendorRepo.Add(brandVendor);

                unit.Save();
            }

            #endregion Brand

            attributes = attributesTmp;
        }
        protected override void Process()
        {
            using (var unit = GetUnitOfWork())
            {
                var productattributeGroup = unit.Scope.Repository <ProductAttributeGroupMetaData>().GetSingle(x => x.VendorID == 1 && x.GroupCode == "Insurance");

                if (productattributeGroup == null)
                {
                    productattributeGroup = new ProductAttributeGroupMetaData()
                    {
                        Index     = 0,
                        GroupCode = "Insurance",
                        VendorID  = 1
                    };

                    unit.Scope.Repository <ProductAttributeGroupMetaData>().Add(productattributeGroup);

                    var productGroupName = new ProductAttributeGroupName()
                    {
                        ProductAttributeGroupMetaData = productattributeGroup,
                        LanguageID = 1,
                        Name       = "Insurance"
                    };
                    unit.Scope.Repository <ProductAttributeGroupName>().Add(productGroupName);

                    var productGroupNameNL = new ProductAttributeGroupName()
                    {
                        ProductAttributeGroupMetaData = productattributeGroup,
                        LanguageID = 2,
                        Name       = "Verzekeringen"
                    };
                    unit.Scope.Repository <ProductAttributeGroupName>().Add(productGroupNameNL);

                    unit.Save();
                }

                var productattribute = unit.Scope.Repository <ProductAttributeMetaData>().GetSingle(x => x.VendorID == 1 && x.AttributeCode == "Risk");

                if (productattribute == null)
                {
                    productattribute = new ProductAttributeMetaData()
                    {
                        ProductAttributeGroupID = productattributeGroup.ProductAttributeGroupID,
                        VendorID      = 1,
                        IsVisible     = false,
                        NeedsUpdate   = true,
                        IsSearchable  = false,
                        CreatedBy     = 1,
                        Mandatory     = false,
                        AttributeCode = "Risk"
                    };
                    unit.Scope.Repository <ProductAttributeMetaData>().Add(productattribute);

                    var productAttributeName = new ProductAttributeName()
                    {
                        AttributeID = productattribute.AttributeID,
                        LanguageID  = 1,
                        Name        = "Risk"
                    };
                    unit.Scope.Repository <ProductAttributeName>().Add(productAttributeName);

                    var productAttributeNameNL = new ProductAttributeName()
                    {
                        AttributeID = productattribute.AttributeID,
                        LanguageID  = 2,
                        Name        = "Risico"
                    };
                    unit.Scope.Repository <ProductAttributeName>().Add(productAttributeNameNL);

                    unit.Save();
                }

                var insurances = GetInsurances(productattribute.AttributeID);

                using (var BASAttributeBulkImport = new BASAttributeBulkImport(insurances))
                {
                    BASAttributeBulkImport.Init(unit.Context);
                    BASAttributeBulkImport.Sync(unit.Context);
                }
            }
        }
Esempio n. 9
0
        protected void ProcessSpecTable(DataTable specTable, IUnitOfWork unit)
        {
            int generalAttributegroupID = GetGeneralAttributegroupID(unit);

            var specs = from r in specTable.Rows.Cast <DataRow>()
                        group r by new { ProductCode = r.Field <string>("ProductCode"), AttributeID = r.Field <long>("ProductPropertyTypeID"), AttributeName = r.Field <string>("ProductPropertyType") } into grp
            let values = from v in grp
                         select v.Field <string>("ProductPropertyDescription")
                         select new
            {
                grp.Key.AttributeName,
                grp.Key.ProductCode,
                grp.Key.AttributeID,
                Value = String.Join(", ", values.ToArray())
            };

            var groupedByProduct = (from r in specs
                                    group r by r.ProductCode
                                    into grouped
                                    select grouped);

            var attributeRepo     = unit.Scope.Repository <ProductAttributeMetaData>();
            var attributeNameRepo = unit.Scope.Repository <ProductAttributeName>();

            int step = 100;
            int todo = groupedByProduct.Count();
            int done = 0;

            log.Info("Starting processing of specs. To process:  " + todo);
            //while (done < todo)
            //{
            //  var productsToProcess = groupedByProduct.Skip(done).Take(step);

            foreach (var row in groupedByProduct)
            {
                var productRow = row;
                log.DebugFormat("Processing product {0}", productRow.Key);
                var product = unit.Scope.Repository <VendorAssortment>().GetSingle(va => va.CustomItemNumber == productRow.Key && va.VendorID == VendorID && va.IsActive == true);
                if (product == null)
                {
                    log.WarnFormat(
                        "Cannot process specs for product with VSN number: {0} because it doesn't exist in Concentrator database",
                        productRow.Key);
                    continue;
                }



                foreach (var spec in productRow)
                {
                    #region Attribute meta data

                    var meta =
                        attributeRepo.GetSingle(
                            pam => pam.AttributeCode == spec.AttributeID.ToString() && pam.VendorID == VendorID);
                    if (meta == null)
                    {
                        meta = new ProductAttributeMetaData
                        {
                            VendorID                = VendorID,
                            AttributeCode           = spec.AttributeID.ToString(),
                            IsVisible               = true,
                            ProductAttributeGroupID = generalAttributegroupID,
                            Index        = 0,
                            NeedsUpdate  = true,
                            IsSearchable = false,
                            Sign         = String.Empty
                        };
                        attributeRepo.Add(meta);
                    }

                    #endregion Attribute meta data

                    #region Attribute name
                    if (meta.ProductAttributeNames == null)
                    {
                        meta.ProductAttributeNames = new List <ProductAttributeName>();
                    }
                    var attName =
                        meta.ProductAttributeNames.FirstOrDefault(pal => pal.LanguageID == (int)LanguageTypes.Netherlands);
                    if (attName == null)
                    {
                        attName = new ProductAttributeName
                        {
                            ProductAttributeMetaData = meta,
                            LanguageID = (int)LanguageTypes.Netherlands
                        };
                        attributeNameRepo.Add(attName);
                    }
                    attName.Name = spec.AttributeName;

                    #endregion


                    #region Attribute value
                    if (meta.ProductAttributeValues == null)
                    {
                        meta.ProductAttributeValues = new List <ProductAttributeValue>();
                    }
                    var attributeValue =
                        meta.ProductAttributeValues.FirstOrDefault(
                            pav => pav.ProductID == product.ProductID && pav.LanguageID == (int)LanguageTypes.Netherlands);
                    if (attributeValue == null)
                    {
                        attributeValue = new ProductAttributeValue
                        {
                            ProductID  = product.ProductID,
                            LanguageID = (int)LanguageTypes.Netherlands,
                            ProductAttributeMetaData = meta
                        };
                        unit.Scope.Repository <ProductAttributeValue>().Add(attributeValue);
                    }
                    attributeValue.Value = spec.Value.Cap(500);

                    #endregion Attribute value
                }
                unit.Save();
            }

            //log.Info("Finished processing specs. Processed: " + done);
            //  }
            //  done += productsToProcess.Count();
            //}
        }
Esempio n. 10
0
 public IEnumerable <ConnectorProductAttributeMapping> Get(Connector connector = null, ProductAttributeMetaData productAttribute = null, Boolean?isFilter = null)
 {
     return(Get(connector != null ? (Int32?)connector.ConnectorID : null, productAttribute != null ? (Int32?)productAttribute.AttributeID : null, isFilter));
 }
Esempio n. 11
0
        private void Import(List <int> Vendors, XDocument productsDoc, XDocument categoriesDoc, IUnitOfWork unit)
        {
            #region Repositories
            var _vendorRepo             = unit.Scope.Repository <Vendor>();
            var _brandRepo              = unit.Scope.Repository <Brand>();
            var _brandVendorRepo        = unit.Scope.Repository <BrandVendor>();
            var _productRepo            = unit.Scope.Repository <Product>();
            var _assortmentRepo         = unit.Scope.Repository <VendorAssortment>();
            var _productGroupVendorRepo = unit.Scope.Repository <ProductGroupVendor>();
            var _prodDescriptionRepo    = unit.Scope.Repository <ProductDescription>();
            var _attrGroupRepo          = unit.Scope.Repository <ProductAttributeGroupMetaData>();
            var _attrGroupName          = unit.Scope.Repository <ProductAttributeGroupName>();
            var _attrRepo               = unit.Scope.Repository <ProductAttributeMetaData>();
            var _attrNameRepo           = unit.Scope.Repository <ProductAttributeName>();
            var _attrValueRepo          = unit.Scope.Repository <ProductAttributeValue>();
            var _mediaRepo              = unit.Scope.Repository <ProductMedia>();
            var _mediaTypeRepo          = unit.Scope.Repository <MediaType>();
            var _priceRepo              = unit.Scope.Repository <VendorPrice>();
            var _stockRepo              = unit.Scope.Repository <VendorStock>();
            var _barcodeRepo            = unit.Scope.Repository <ProductBarcode>();
            var _relatedProductRepo     = unit.Scope.Repository <RelatedProduct>();
            var _relatedProductTypeRepo = unit.Scope.Repository <RelatedProductType>();
            #endregion

            #region Parsing xmls

            //Parse categories
            var Categories = (from cat in categoriesDoc.Element("CATEGORYDATA").Elements("category")
                              select new
            {
                category = cat.Element("Category").Value,
                path = cat.Element("Path").Value
            }
                              );

            //Parse products
            var importProducts = (from productData in productsDoc.Element("PRODUCTDATA").Elements("product")
                                  let td = productData.Element("TechnicalData").Elements()
                                           let c =
                                      Categories.Where(
                                          x =>
                                          x.path ==
                                          "private_" + productData.Elements().Where(p => p.Name == "Path").FirstOrDefault().Value).
                                      FirstOrDefault()
                                      select new SennHeiserItemImport
            {
                CustomItemNumber = productData.Attribute("name").Value.Trim(),
                VendorItemNumber = productData.Attribute("no").Value.Trim(),
                Url = productData.Element("URL") != null ? productData.Element("URL").Value.Trim() : string.Empty,
                ShortContentDescription =
                    System.Web.HttpUtility.UrlDecode(productData.Element("ShortDescription").Value),
                LongContentDescription =
                    System.Web.HttpUtility.UrlDecode(productData.Element("GeneralDescription").Value),
                Attributes = (from items in td
                              group items by items.Attribute("order").Value
                              into g
                              select new SennHeiserAttribute
                {
                    AttributeCode =
                        System.Web.HttpUtility.UrlDecode(
                            td.Where(
                                x =>
                                x.Attribute("order").Value == g.Key &&
                                x.Name == "TechnicalData").FirstOrDefault().Value),
                    AttributeValue =
                        System.Web.HttpUtility.UrlDecode(
                            td.Where(
                                x =>
                                x.Attribute("order").Value == g.Key &&
                                x.Name == "TechnicalData_Content").FirstOrDefault().Value),
                    AttributeGroupCode = "TechnicalData"
                }

                              ).Union(
                    (from elem in productData.Elements()
                     where
                     elem.Name == "KeyFeatures" || elem.Name == "Features" ||
                     elem.Name == "DeliveryIncludes"
                     select new SennHeiserAttribute
                {
                    AttributeCode =
                        elem.Name.LocalName.Length > 150
                           ? elem.Name.LocalName.Substring(0, 150)
                           : elem.Name.LocalName,
                    AttributeValue = System.Web.HttpUtility.UrlDecode(elem.Value),
                    AttributeGroupCode = elem.Name.LocalName
                })).ToList(),
                Variants = (from data in productData.Elements()
                            where data.Name == "Variant"
                            from data1 in data.Elements()
                            where
                            data1.Name == "VariantData"
                            select new ProductData
                {
                    VendorItemNumber = data1.Element("ProductNo").Value,
                    OrderID = int.Parse(data1.Attribute("order").Value),
                    CustomItemNumber = data1.Element("ProductName").Value,
                    Description = data1.Element("Description").Value,
                    IsVariant = true
                }).ToList(),
                Accessoires = (from data in productData.Elements()
                               where data.Name == "Accessories"
                               from data1 in data.Elements()
                               where
                               data1.Name == "AccessoriesData"
                               select new ProductData
                {
                    VendorItemNumber = data1.Element("ProductNo").Value,
                    OrderID = int.Parse(data1.Attribute("order").Value),
                    CustomItemNumber = data1.Element("ProductName").Value,
                    Description = data1.Element("Description").Value,
                    IsVariant = false,
                    Designation = data1.Element("Designation").Value
                }).ToList(),
                ProductCategory = c.Try(q => q.category.Remove(0, "Private_".Length), string.Empty),
                ProductMedia = (from url in productData.Elements()
                                where url.Name == "Image"
                                select new ProductMedia
                {
                    MediaUrl = url.Element("ImageFile").Value,
                    Description = url.Attribute("description").Value
                }).ToList(),
                OtherMedia = (from m in productData.Elements()
                              where m.Name == "Download"
                              select new ProductMedia
                {
                    MediaUrl = m.Element("DownloadFile").Value,
                    Sequence = int.Parse(m.Element("DownloadFile").Attribute("order").Value),
                    Description = m.Attribute("description").Value
                }).ToList()
            }).ToList();


            #endregion

            #region Initialize independent inmemory collections

            var brands = _brandRepo.GetAll().ToList();

            var products = _productRepo.GetAll().ToList();

            var relatedProductTypeVariant = _relatedProductTypeRepo.GetSingle(x => x.Type == "Variant");

            var relatedProductTypeAccessory = _relatedProductTypeRepo.GetSingle(x => x.Type == "Accessory");

            var mediaTypes = _mediaTypeRepo.GetAll().ToList();

            var imageMediaType = _mediaTypeRepo.GetSingle(i => i.Type.ToLower() == "Image");
            if (imageMediaType == null)
            {
                imageMediaType = new MediaType()
                {
                    Type = "Image"
                };

                _mediaTypeRepo.Add(imageMediaType);
            }
            #endregion

            bool firstVendor = true;
            foreach (var vendorID in Vendors)
            {
                var vendor = _vendorRepo.GetSingle(c => c.VendorID == vendorID);

                log.InfoFormat("Starting processing for {0}", vendor.Name);

                #region Initialize vendor dependent inmemory collections

                var brandVendors = _brandVendorRepo.GetAll(b => (b.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && b.VendorID == vendor.ParentVendorID))).ToList();

                var currentVendorProductGroups = _productGroupVendorRepo.GetAll(v => v.VendorID == vendor.VendorID).ToList();

                var productGroupVendors = _productGroupVendorRepo.GetAll(g => (g.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && g.VendorID == vendor.VendorID))).ToList();

                var vendorassortments = _assortmentRepo.GetAll(x => x.VendorID == vendor.VendorID).ToList();

                var medias = _mediaRepo.GetAll().ToList();

                var productDescriptions = _prodDescriptionRepo.GetAll().ToList();

                var vendorStocks = _stockRepo.GetAll(c => c.VendorID == vendorID).ToList();

                #endregion

                log.InfoFormat("About to process {0} products", importProducts.Count);

                int counter = 0;
                int total   = importProducts.Count();
                int totalNumberOfProductsToProcess = total;
                log.InfoFormat("Start import {0} products", total);

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

                    string vendorBrandCode = "Sennheiser";

                    if (!string.IsNullOrEmpty(product.MainCategory))
                    {
                        vendorBrandCode = int.Parse(product.MainCategory.Substring(1, 3)) < 8 ? "Sennheiser" : product.MainCategory.Substring(4).Replace("$'", "").Trim();
                    }

                    int BrandID;
                    #region Brands
                    var brandVendor = brandVendors.FirstOrDefault(c => c.VendorBrandCode == vendorBrandCode.Trim());

                    if (brandVendor == null)
                    {
                        Brand brand = brands.FirstOrDefault(c => c.Name == vendorBrandCode);

                        brandVendor = new BrandVendor()
                        {
                            BrandID         = brand == null ? unmappedID : brand.BrandID,
                            VendorID        = vendorID,
                            VendorBrandCode = vendorBrandCode,
                            Name            = vendorBrandCode
                        };

                        _brandVendorRepo.Add(brandVendor);
                        brandVendors.Add(brandVendor);
                    }

                    BrandID = brandVendor.BrandID;
                    #endregion

                    #region product and assortment

                    var productIsExisting = false;

                    var productEntity = products.FirstOrDefault(c => c.VendorItemNumber == product.VendorItemNumber);

                    if (productEntity == null)
                    {
                        //productIsExisting = false;

                        productEntity = new Product()
                        {
                            VendorItemNumber = product.VendorItemNumber,
                            BrandID          = brandVendor.BrandID,
                            SourceVendorID   = vendorID
                        };
                        _productRepo.Add(productEntity);
                        products.Add(productEntity);
                    }
                    productEntity.BrandID = BrandID;

                    bool assortmentIsExisting = true;
                    var  vendorAssortment     = vendorassortments.FirstOrDefault(c => c.Product.VendorItemNumber == product.VendorItemNumber && c.VendorID == vendorID);

                    if (vendorAssortment == null)
                    {
                        assortmentIsExisting = false;
                        vendorAssortment     = new VendorAssortment()
                        {
                            VendorID         = vendorID,
                            Product          = productEntity,
                            IsActive         = true,
                            CustomItemNumber = product.CustomItemNumber
                        };
                        _assortmentRepo.Add(vendorAssortment);
                        vendorassortments.Add(vendorAssortment);
                    }

                    vendorAssortment.CustomItemNumber = product.CustomItemNumber;
                    vendorAssortment.LongDescription  = string.Empty;
                    vendorAssortment.ShortDescription = string.Empty;
                    #endregion

                    #region Vendor stock --> insert empty stock

                    var vendorStock = vendorStocks.FirstOrDefault(c => c.VendorID == vendorID && c.Product.VendorItemNumber == product.VendorItemNumber);

                    //create vendorPrice with vendorAssortmentID
                    if (vendorStock == null)
                    {
                        vendorStock = new VendorStock
                        {
                            Product           = productEntity,
                            QuantityOnHand    = 0,
                            VendorID          = vendorID,
                            VendorStockTypeID = 1
                        };

                        vendorStocks.Add(vendorStock);
                        _stockRepo.Add(vendorStock);
                    }

                    #endregion

                    if (firstVendor)
                    {
                        #region Product description


                        var productDescription = productDescriptions.FirstOrDefault(pd => pd.Product.VendorItemNumber == product.VendorItemNumber && pd.LanguageID == 1 && (pd.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && pd.VendorID == vendor.VendorID)));

                        if (productDescription == null)
                        {
                            //create ProductDescription
                            productDescription = new ProductDescription
                            {
                                Product    = productEntity,
                                LanguageID = languageID,
                                VendorID   = vendorID,
                            };

                            _prodDescriptionRepo.Add(productDescription);
                            productDescriptions.Add(productDescription);
                        }

                        if (string.IsNullOrEmpty(productDescription.ProductName))
                        {
                            productDescription.ProductName = product.CustomItemNumber.ToString();
                            productDescription.ModelName   = product.CustomItemNumber.ToString();
                        }

                        if (string.IsNullOrEmpty(productDescription.ShortContentDescription))
                        {
                            productDescription.ShortContentDescription = product.ShortContentDescription.Cap(1000);
                        }

                        if (string.IsNullOrEmpty(productDescription.ShortSummaryDescription))
                        {
                            productDescription.ShortSummaryDescription = product.ShortContentDescription.Cap(1000);
                        }

                        if (string.IsNullOrEmpty(productDescription.LongContentDescription))
                        {
                            productDescription.LongContentDescription = product.LongContentDescription;
                        }

                        if (string.IsNullOrEmpty(productDescription.LongSummaryDescription))
                        {
                            productDescription.LongSummaryDescription = product.LongContentDescription;
                        }

                        if (string.IsNullOrEmpty(productDescription.WarrantyInfo))
                        {
                            productDescription.WarrantyInfo = "2 years warranty";
                        }

                        #endregion

                        #region Product images

                        if (product.ProductMedia != null)
                        {
                            int imageSequence = 0;

                            foreach (var image in product.ProductMedia.OrderByDescending(x => x.Description == "ProductImage").OrderByDescending(x => x.Description == "ProductImageNew"))
                            {
                                if (!string.IsNullOrEmpty(image.MediaUrl))
                                {
                                    var productImage = medias.FirstOrDefault(pd => (pd.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && pd.VendorID == vendor.VendorID)) &&
                                                                             pd.MediaUrl == image.MediaUrl && pd.Product == productEntity);

                                    if (productImage == null)
                                    {
                                        //create ProductDescription
                                        productImage = new ProductMedia
                                        {
                                            Product  = productEntity,
                                            MediaUrl = image.MediaUrl,
                                            VendorID = vendorID
                                        };

                                        _mediaRepo.Add(productImage);
                                        medias.Add(productImage);
                                    }

                                    productImage.Sequence    = imageSequence;
                                    productImage.MediaType   = imageMediaType;
                                    productImage.Description = image.Description;
                                    imageSequence++;
                                }
                            }
                        }

                        if (product.OtherMedia != null)
                        {
                            int productMediaSequence = 0;

                            foreach (var media in product.OtherMedia.OrderByDescending(x => x.Description.Contains("Instruction")).OrderByDescending(x => x.Description.Contains("Sheet")))
                            {
                                string fileType = media.MediaUrl.Substring(media.MediaUrl.Length - 3).ToLower();

                                var mediaType = mediaTypes.FirstOrDefault(c => c.Type.ToLower() == fileType);

                                if (mediaType == null)
                                {
                                    mediaType = new MediaType()
                                    {
                                        Type = fileType
                                    };

                                    _mediaTypeRepo.Add(mediaType);
                                    mediaTypes.Add(mediaType);
                                }

                                if (!string.IsNullOrEmpty(media.MediaUrl))
                                {
                                    var productImage = medias.FirstOrDefault(pd => (pd.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && pd.VendorID == vendor.VendorID)) &&
                                                                             pd.MediaUrl == media.MediaUrl);

                                    if (productImage == null)
                                    {
                                        //create ProductDescription
                                        productImage = new ProductMedia
                                        {
                                            Product  = productEntity,
                                            MediaUrl = media.MediaUrl,
                                            VendorID = vendorID
                                        };

                                        _mediaRepo.Add(productImage);
                                        medias.Add(productImage);
                                    }

                                    productImage.Sequence    = productMediaSequence;
                                    productImage.MediaType   = mediaType;
                                    productImage.Description = media.Description;
                                    productMediaSequence++;
                                }
                            }
                        }
                        #endregion

                        if (!productIsExisting)
                        {
                            #region Product attribute

                            //add the mandatory gurantee supplier
                            product.Attributes.Add(new SennHeiserAttribute
                            {
                                AttributeCode      = "Guarantee",
                                AttributeGroupCode = "GuaranteeSupplier",
                                AttributeValue     = "Sennheiser"
                            });

                            product.Attributes.Add(new SennHeiserAttribute
                            {
                                AttributeCode      = "ProductUrl",
                                AttributeGroupCode = "General",
                                AttributeValue     = product.Url
                            });

                            int keyFeatures      = 0;
                            int features         = 0;
                            int DeliveryIncludes = 0;

                            foreach (var content in product.Attributes)
                            {
                                if (content == null)
                                {
                                    continue;
                                }

                                #region ProductAttributeGroupMetaData

                                var productAttributeGroupMetaData = _attrGroupRepo.GetSingle(c => c.GroupCode == content.AttributeGroupCode && (c.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && c.VendorID == vendor.VendorID)));

                                //create ProductAttributeGroupMetaData if not exists
                                if (productAttributeGroupMetaData == null)
                                {
                                    productAttributeGroupMetaData = new ProductAttributeGroupMetaData
                                    {
                                        Index     = 0,
                                        GroupCode = content.AttributeGroupCode,
                                        VendorID  = vendorID
                                    };
                                    _attrGroupRepo.Add(productAttributeGroupMetaData);
                                }
                                #endregion

                                #region ProductAttributeGroupName

                                if (productAttributeGroupMetaData.ProductAttributeGroupNames == null)
                                {
                                    productAttributeGroupMetaData.ProductAttributeGroupNames = new List <ProductAttributeGroupName>();
                                }
                                var productAttributeGroupName = productAttributeGroupMetaData.ProductAttributeGroupNames.FirstOrDefault(c => c.LanguageID == languageID);
                                //create ProductAttributeGroupName if not exists
                                if (productAttributeGroupName == null)
                                {
                                    productAttributeGroupName = new ProductAttributeGroupName
                                    {
                                        Name = content.AttributeGroupCode,
                                        ProductAttributeGroupMetaData = productAttributeGroupMetaData,
                                        LanguageID = languageID
                                    };
                                    _attrGroupName.Add(productAttributeGroupName);
                                }

                                #endregion

                                #region ProductAttributeMetaData

                                //create ProductAttributeMetaData as many times that there are entrys in technicaldata
                                string attributeCode = content.AttributeCode;
                                if (content.AttributeCode == "KeyFeatures")
                                {
                                    if (keyFeatures > 0)
                                    {
                                        attributeCode = attributeCode + keyFeatures.ToString();
                                    }

                                    keyFeatures++;
                                }
                                else if (content.AttributeCode == "Features")
                                {
                                    if (features > 0)
                                    {
                                        attributeCode = attributeCode + features.ToString();
                                    }

                                    features++;
                                }
                                else if (content.AttributeCode == "DeliveryIncludes")
                                {
                                    if (DeliveryIncludes > 0)
                                    {
                                        attributeCode = attributeCode + DeliveryIncludes.ToString();
                                    }

                                    DeliveryIncludes++;
                                }

                                var productAttributeMetaData = _attrRepo.GetSingle(x => x.AttributeCode == attributeCode && (x.VendorID == vendor.VendorID || (vendor.ParentVendorID.HasValue && x.VendorID == vendor.VendorID)));
                                //create ProductAttributeMetaData if not exists
                                if (productAttributeMetaData == null)
                                {
                                    productAttributeMetaData = new ProductAttributeMetaData
                                    {
                                        ProductAttributeGroupMetaData = productAttributeGroupMetaData,
                                        AttributeCode = attributeCode,
                                        Index         = 0,
                                        IsVisible     = true,
                                        NeedsUpdate   = true,
                                        VendorID      = vendorID,
                                        IsSearchable  = false
                                    };
                                    _attrRepo.Add(productAttributeMetaData);
                                }

                                #endregion

                                #region ProductAttributeName
                                if (productAttributeMetaData.ProductAttributeNames == null)
                                {
                                    productAttributeMetaData.ProductAttributeNames = new List <ProductAttributeName>();
                                }
                                var productAttributeName = productAttributeMetaData.ProductAttributeNames.FirstOrDefault(c => c.LanguageID == languageID);
                                //create ProductAttributeName if not exists
                                if (productAttributeName == null)
                                {
                                    productAttributeName = new ProductAttributeName
                                    {
                                        ProductAttributeMetaData = productAttributeMetaData,
                                        LanguageID = languageID
                                    };
                                    _attrNameRepo.Add(productAttributeName);
                                }
                                productAttributeName.Name = productAttributeName.Name.IfNullOrEmpty(content.AttributeCode);


                                #endregion

                                #region ProductAttributeValue

                                if (productAttributeMetaData.ProductAttributeValues == null)
                                {
                                    productAttributeMetaData.ProductAttributeValues = new List <ProductAttributeValue>();
                                }

                                var productAttributeValue =
                                    productAttributeMetaData.ProductAttributeValues.FirstOrDefault(c => c.Product.VendorItemNumber == productEntity.VendorItemNumber && c.LanguageID == languageID);
                                //create ProductAttributeValue
                                if (productAttributeValue == null)
                                {
                                    productAttributeValue = new ProductAttributeValue
                                    {
                                        ProductAttributeMetaData = productAttributeMetaData,
                                        Product    = productEntity,
                                        LanguageID = languageID
                                    };
                                    _attrValueRepo.Add(productAttributeValue);
                                }
                                productAttributeValue.Value = content.AttributeValue;
                                #endregion
                            }

                            #endregion
                        }

                        #region Related products

                        var allRelatedItems = product.Variants;
                        allRelatedItems.AddRange(product.Accessoires);

                        foreach (var relatedProduct in allRelatedItems)
                        {
                            var relatedProductEntity = products.FirstOrDefault(c => c.VendorItemNumber == relatedProduct.VendorItemNumber);

                            if (relatedProductEntity == null)
                            {
                                relatedProductEntity = new Product
                                {
                                    VendorItemNumber = relatedProduct.VendorItemNumber,
                                    BrandID          = BrandID,
                                    SourceVendorID   = vendorID
                                };

                                _productRepo.Add(relatedProductEntity);
                                products.Add(relatedProductEntity);
                                log.DebugFormat("New product inserted: {0}", relatedProduct.VendorItemNumber);
                                unit.Save();
                            }

                            //relatedProductEntity.BrandID = BrandID;

                            var assortment = vendorassortments.FirstOrDefault(c => c.Product.VendorItemNumber == relatedProduct.VendorItemNumber && c.VendorID == vendorID);

                            if (assortment == null)
                            {
                                assortment = new VendorAssortment
                                {
                                    Product          = relatedProductEntity,
                                    CustomItemNumber = relatedProduct.CustomItemNumber,
                                    VendorID         = vendorID,
                                    IsActive         = true
                                };
                                vendorassortments.Add(assortment);
                                _assortmentRepo.Add(assortment);
                            }

                            var vendorStockRelated = vendorStocks.FirstOrDefault(c => c.Product.VendorItemNumber == relatedProduct.VendorItemNumber && c.VendorID == vendorID);
                            if (vendorStockRelated == null)
                            {
                                vendorStockRelated = new VendorStock
                                {
                                    Product           = relatedProductEntity,
                                    QuantityOnHand    = 0,
                                    VendorID          = vendorID,
                                    VendorStockTypeID = 1
                                };

                                vendorStocks.Add(vendorStockRelated);
                                _stockRepo.Add(vendorStockRelated);
                            }

                            var descriptionEntity = productDescriptions.FirstOrDefault(c => c.Product.VendorItemNumber == relatedProduct.VendorItemNumber && c.LanguageID == languageID && c.VendorID == vendorID);
                            if (descriptionEntity == null)
                            {
                                descriptionEntity = new ProductDescription()
                                {
                                    LanguageID              = languageID,
                                    Product                 = relatedProductEntity,
                                    ProductName             = relatedProduct.CustomItemNumber,
                                    ShortContentDescription = relatedProduct.Description,
                                    ModelName               = relatedProduct.Designation,
                                    VendorID                = vendor.VendorID
                                };

                                _prodDescriptionRepo.Add(descriptionEntity);
                                productDescriptions.Add(descriptionEntity);
                            }

                            if (productEntity.RelatedProductsSource == null)
                            {
                                productEntity.RelatedProductsSource = new List <RelatedProduct>();
                            }
                            var relatedProductActual = productEntity.RelatedProductsSource.FirstOrDefault(c => c.RProduct.VendorItemNumber == relatedProduct.VendorItemNumber && c.VendorID == vendorID);
                            if (relatedProductActual == null)
                            {
                                relatedProductActual = new RelatedProduct()
                                {
                                    SourceProduct      = productEntity,
                                    RProduct           = relatedProductEntity,
                                    VendorID           = vendorID,
                                    RelatedProductType = relatedProduct.IsVariant ? relatedProductTypeVariant : relatedProductTypeAccessory
                                };

                                _relatedProductRepo.Add(relatedProductActual);
                                productEntity.RelatedProductsSource.Add(relatedProductActual);
                            }
                        }

                        #endregion
                    }
                    unit.Save();
                }

                //unit.Save();
                log.InfoFormat("Finished processing {0} products for vendor {1}", importProducts.Count, vendor.Name);
                firstVendor = false;
            }
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        private ProductAttributeMetaData GetOrCreateProductAttributeMetaData(string attributeCode, int vendorID, IServiceUnitOfWork unit)
        {
            ProductAttributeMetaData attribute = null;

            var attributes = unit.Service <ProductAttributeMetaData>().GetAll(x => x.AttributeCode == attributeCode);

            if (attributes.Any())
            {
                //get attribute by vendor, create if not exists
                if (ConcentratorSection.Default.Management.ProductBrowser.General.CreateAttributeByVendor)
                {
                    attribute = attributes.FirstOrDefault(x => x.VendorID == vendorID);
                }
                else
                {
                    attribute = attributes.FirstOrDefault();
                }
            }
            else
            {
                throw new Exception(string.Format("Attribute '{0}' does not exist, please create first.", attributeCode));
            }

            if (attribute == null)
            {
                //create and return
                var baseAttribute = attributes.FirstOrDefault();
                attribute = new ProductAttributeMetaData
                {
                    AttributeCode           = baseAttribute.AttributeCode,
                    ProductAttributeGroupID = baseAttribute.ProductAttributeGroupID,
                    FormatString            = baseAttribute.FormatString,
                    DataType              = baseAttribute.DataType,
                    Index                 = baseAttribute.Index,
                    IsVisible             = baseAttribute.IsVisible,
                    NeedsUpdate           = baseAttribute.NeedsUpdate,
                    VendorID              = vendorID,
                    IsSearchable          = baseAttribute.IsSearchable,
                    Sign                  = baseAttribute.Sign,
                    CreatedBy             = baseAttribute.CreatedBy,
                    CreationTime          = DateTime.Now,
                    Mandatory             = baseAttribute.Mandatory,
                    DefaultValue          = baseAttribute.DefaultValue,
                    IsConfigurable        = baseAttribute.IsConfigurable,
                    ConfigurablePosition  = baseAttribute.ConfigurablePosition,
                    HasOption             = baseAttribute.HasOption,
                    IsSlider              = baseAttribute.IsSlider,
                    ProductAttributeNames = new List <ProductAttributeName>()
                };

                unit.Service <ProductAttributeMetaData>().Create(attribute);

                foreach (var attributeName in baseAttribute.ProductAttributeNames)
                {
                    attribute.ProductAttributeNames.Add(new ProductAttributeName
                    {
                        LanguageID = attributeName.LanguageID,
                        Name       = attributeName.Name
                    });
                }

                unit.Save();
            }

            return(attribute);
        }