public void Process() { var config = GetConfiguration().AppSettings.Settings; var ftp = new FtpManager(config["VSNFtpUrl"].Value, "pub3/", config["VSNUser"].Value, config["VSNPassword"].Value, false, true, log); using (var unit = GetUnitOfWork()) { //DataLoadOptions options = new DataLoadOptions(); //options.LoadWith<Product>(p => p.Media); //options.LoadWith<Product>(p => p.ProductDescription); //options.LoadWith<Product>(p => p.ProductBarcodes); //options.LoadWith<Product>(p => p.VendorAssortment); //options.LoadWith<VendorAssortment>(va => va.VendorPrice); //options.LoadWith<VendorAssortment>(va => va.VendorStock); //ctx.LoadOptions = options; ProductGroupSyncer syncer = new ProductGroupSyncer(VendorID, unit.Scope.Repository <ProductGroupVendor>()); ProductStatusVendorMapper statusMapper = new ProductStatusVendorMapper(unit.Scope.Repository <VendorProductStatus>(), VendorID); BrandVendor brandVendor = null; List <ProductAttributeMetaData> attributes; SetupBrandAndAttributes(unit, out attributes, out brandVendor); using (var prodFile = ftp.OpenFile("7ExportProduct.xml")) { using (DataSet ds = new DataSet()) { ds.ReadXml(prodFile.Data); ProcessProductsTable(ds.Tables[0], brandVendor.BrandID, attributes, syncer, statusMapper, null); } } } log.AuditComplete("Finished partial VSN product import", "VSN Product Import"); }
protected void ProcessProductsTable(DataTable table, int vendorBrandID, IEnumerable <ProductAttributeMetaData> productAttributes, ProductGroupSyncer syncer, ProductStatusVendorMapper mapper, Dictionary <int, VendorAssortment> inactiveAss) { var products = (from r in table.Rows.Cast <DataRow>() where !String.IsNullOrEmpty(r.Field <string>("EANNumber")) group r by r.Field <string>("EANNumber") into grp let r = grp.First() select new { ProductID = r.Field <string>("ProductCode"), EAN = r.Field <string>("EANNumber"), EAN_Alt = r.Field <string>("EANNumberAlternative"), Description = r.Field <string>("ProductName"), TagLine = r.Field <string>("TagLine"), Title = r.Field <string>("Title"), ProductionYear = r.Field <int?>("ProductionYear"), RunningTime = r.Field <int?>("RunningTime"), CountryName = r.Field <string>("CountryName"), UsrReleaseDate = r.Field <string>("UsrReleaseDate"), ProductStatusID = r.Field <long?>("ProductStatusID"), ProductStatus = r.Field <string>("ProductStatus").Trim(), SalesPrice = r.Field <decimal?>("SalesPrice"), RetailPrice = ((r.Field <decimal?>("RetailPrice") ?? 0) / (decimal)1.19), ListRetailPrice = r.Field <decimal?>("ListRetailPrice"), ListSalesPrice = r.Field <decimal?>("ListSalesPrice"), InStock = r.Field <int?>("InStock"), GenreID = r.Field <long?>("GenreID"), GenreName = r.Field <string>("GenreName"), ProductGroupCode = r.Field <string>("ProductGroupCode"), ProductGroup = r.Field <string>("ProductGroup"), SubProductGroupCode = r.Field <string>("SubProductGroupCode"), SubProductGroup = r.Field <string>("SubProductGroup"), SubGenreID = r.Field <long?>("SubGenreID"), SubGenreName = r.Field <string>("SubGenreName"), RatingAge = r.Field <string>("RatingAge"), MediaType = r.Field <string>("MediaType"), MediaTypeID = r.Field <long?>("MediaTypeID"), Artist = r.Field <string>("Artist"), MediaDescription = r.Field <string>("MediaDescription") }).ToList(); using (IUnitOfWork unit = GetUnitOfWork()) { //DataLoadOptions options = new DataLoadOptions(); //options.LoadWith<ProductGroupVendor>(x => x.VendorProductGroupAssortments); //options.LoadWith<VendorAssortment>(x => x.VendorPrice); //options.LoadWith<Product>(x => x.ProductBarcodes); //ctx.LoadOptions = options; List <Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorAssortmentItem> assortmentList = new List <Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorAssortmentItem>(); RelatedProductTypes relatedProductTypes = new RelatedProductTypes(unit.Scope.Repository <RelatedProductType>()); var relatedProductType = relatedProductTypes.SyncRelatedProductTypes("CompatibleProducts"); var productGroupVendorRepo = unit.Scope.Repository <ProductGroupVendor>().Include(c => c.VendorAssortments); var assortmentRepo = unit.Scope.Repository <VendorAssortment>().Include(c => c.VendorPrices); var stockRepo = unit.Scope.Repository <VendorStock>(); var productRepo = unit.Scope.Repository <Product>().Include(c => c.ProductBarcodes); var priceRepo = unit.Scope.Repository <VendorPrice>(); var productGroupVendorRecords = productGroupVendorRepo.GetAll(c => c.VendorID == VendorID).ToList(); int step = 1000; int todo = products.Count(); int done = 0; var languages = new[] { (int)LanguageTypes.English, (int)LanguageTypes.Netherlands }; var vendorAssortment = assortmentRepo.GetAll(x => x.VendorID == VendorID).ToDictionary(x => x.ProductID, y => y); var attributeList = productAttributes.ToDictionary(x => x.AttributeCode, y => y.AttributeID); var vendorStock = stockRepo.GetAll(x => x.VendorID == VendorID).ToDictionary(x => x.ProductID, y => y); var prodType = products.First().GetType(); var currentProductGroupVendors = productGroupVendorRepo.GetAll(v => v.VendorID == VendorID).ToList(); log.InfoFormat(todo + " products to be processed"); while (done < todo) { log.DebugFormat("{0} products to be processed", todo - done); var toProcess = products.OrderByDescending(x => x.UsrReleaseDate).Skip(done).Take(step); foreach (var product in toProcess) { try { if (product.ProductGroupCode != "GAMES" && product.ProductGroupCode != "HARDWARE") { continue; } //#if DEBUG // if (product.EAN != "8714025503775") // continue; //#endif //log.DebugFormat("{0} products to be processed in batch", toProcess.Count()); var assortment = new Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorAssortmentItem { #region BrandVendor BrandVendors = new List <VendorAssortmentBulk.VendorImportBrand>() { new VendorAssortmentBulk.VendorImportBrand() { VendorID = VendorID, VendorBrandCode = BrandVendorCode, ParentBrandCode = null, Name = product.GenreName } }, #endregion #region GeneralProductInfo VendorProduct = new VendorAssortmentBulk.VendorProduct { VendorItemNumber = product.EAN, CustomItemNumber = product.ProductID, ShortDescription = product.Description.Length > 150 ? product.Description.Substring(0, 150) : product.Description, LongDescription = product.Description, LineType = "S", LedgerClass = null, ProductDesk = null, ExtendedCatalog = null, VendorID = VendorID, DefaultVendorID = DefaultVendorID, VendorBrandCode = BrandVendorCode, Barcode = product.EAN, VendorProductGroupCode1 = product.ProductGroupCode, VendorProductGroupCodeName1 = product.ProductGroup, VendorProductGroupCode2 = product.SubProductGroupCode, VendorProductGroupCodeName2 = product.SubProductGroup, VendorProductGroupCode3 = product.GenreName //VendorProductGroupCodeName3 = product.SubProductGroup }, #endregion #region RelatedProducts RelatedProducts = new List <VendorAssortmentBulk.VendorImportRelatedProduct>() { new Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorImportRelatedProduct { VendorID = VendorID, DefaultVendorID = DefaultVendorID, CustomItemNumber = product.ProductID, RelatedProductType = relatedProductType.Type, RelatedCustomItemNumber = product.EAN } }, #endregion #region Attributes VendorImportAttributeValues = (from attr in AttributeMapping let prop = product.Equals(attr)//d.Field<object>(attr) let attributeID = attributeList.ContainsKey(attr) ? attributeList[attr] : -1 let value = prop.ToString() where !string.IsNullOrEmpty(value) select new Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorImportAttributeValue { VendorID = VendorID, DefaultVendorID = DefaultVendorID, CustomItemNumber = product.ProductID, AttributeID = attributeID, Value = value, LanguageID = "1", AttributeCode = attr, }).ToList(), #endregion #region Prices VendorImportPrices = new List <Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorImportPrice>() { new Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorImportPrice() { VendorID = VendorID, DefaultVendorID = DefaultVendorID, CustomItemNumber = product.ProductID, Price = decimal.Round(product.RetailPrice, 4).ToString("0.00", CultureInfo.InvariantCulture), CostPrice = decimal.Round(product.SalesPrice.Value, 4).ToString("0.00", CultureInfo.InvariantCulture), TaxRate = "19", //TODO: Calculate this! MinimumQuantity = 1, CommercialStatus = product.ProductStatus } }, #endregion #region Stock VendorImportStocks = new List <Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorImportStock>() { new Concentrator.Objects.Vendors.Bulk.VendorAssortmentBulk.VendorImportStock() { VendorID = VendorID, DefaultVendorID = DefaultVendorID, CustomItemNumber = product.ProductID, QuantityOnHand = 0, StockType = "Assortment", StockStatus = product.ProductStatus } }, #endregion }; assortmentList.Add(assortment); } catch (Exception ex) { log.WarnFormat("Batch update failed with message : {0}", ex.Message); } } done += toProcess.Count(); } #region delete unused foreach (var vProdVendor in currentProductGroupVendors) { if (vProdVendor.ProductGroupID == -1) { //productGroupVendorRepo.Delete(vProdVendor); } } #endregion // Creates a new instance of VendorAssortmentBulk(Passes in the AssortmentList defined above, vendorID and DefaultVendorID) using (var vendorAssortmentBulk = new VendorAssortmentBulk(assortmentList, VendorID, VendorID)) { vendorAssortmentBulk.Init(unit.Context); vendorAssortmentBulk.Sync(unit.Context); } log.AuditInfo("Products processing finished. Processed " + done + " products"); } }
public void Process() { var config = GetConfiguration().AppSettings.Settings; var ftp = new FtpManager(config["VSNFtpUrl"].Value, "pub3/", config["VSNUser"].Value, config["VSNPassword"].Value, false, true, log); using (var unit = GetUnitOfWork()) { //DataLoadOptions options = new DataLoadOptions(); //options.LoadWith<Product>(p => p.ProductImages); //options.LoadWith<Product>(p => p.ProductDescription); //options.LoadWith<Product>(p => p.ProductBarcodes); //options.LoadWith<Product>(p => p.VendorAssortment); //options.LoadWith<VendorAssortment>(va => va.VendorPrice); //options.LoadWith<VendorAssortment>(va => va.VendorStock); //options.LoadWith<ProductAttributeMetaData>(p => p.ProductAttributeValues); //options.LoadWith<ProductGroupVendor>(x=>x.VendorProductGroupAssortments); //options.LoadWith<VendorAssortment>(x => x.VendorPrice); //options.LoadWith<VendorAssortment>(va => va.VendorStock); //ctx.LoadOptions = options; ProductStatusVendorMapper mapper = new ProductStatusVendorMapper(unit.Scope.Repository <VendorProductStatus>(), VendorID); ProductGroupSyncer syncer = new ProductGroupSyncer(VendorID, unit.Scope.Repository <ProductGroupVendor>()); BrandVendor brandVendor = null; List <ProductAttributeMetaData> attributes; SetupBrandAndAttributes(unit, out attributes, out brandVendor); //#if DEBUG // if (File.Exists("ExportProduct.xml")) // { // DataSet ds2 = new DataSet(); // ds2.ReadXml("ExportProduct.xml"); // ProcessProductsTable(ds2.Tables[0], brandVendor.BrandID, attributes, syncer, mapper); // } //#endif var inactiveAss = unit.Scope.Repository <VendorAssortment>().GetAll(x => x.VendorID == VendorID).ToDictionary(x => x.VendorAssortmentID, y => y); using (var prodFile = ftp.OpenFile("XMLExportProduct.zip")) { using (var zipProc = new ZipProcessor(prodFile.Data)) { foreach (var file in zipProc) { using (file) { #if DEBUG using (System.IO.FileStream fs = new FileStream(file.FileName, FileMode.OpenOrCreate)) { file.Data.WriteTo(fs); fs.Close(); } #endif using (DataSet ds = new DataSet()) { ds.ReadXml(file.Data); ProcessProductsTable(ds.Tables[0], brandVendor.BrandID, attributes, syncer, mapper, inactiveAss); } } } } } inactiveAss.Values.ForEach((x, idx) => { x.IsActive = false; }); unit.Save(); } log.AuditComplete("Finished full VSN product import", "VSN Product Import"); }