public override bool Execute() { _generatedFile = downloadFileFromFtp(); // there should be an error if the downloaded file path is null and let's continue if (_generatedFile == null) { Logger.LogWarning(LogEntryType.FileInventoryTaskService, string.Format("The vendor product file for vendor ID {0} was not successfully downloaded! No file found at {1}!", _task.VendorId, _task.FtpFileFullPath)); return(false); } try { // get the vendor info first var vendor = _service.GetVendorById(_task.VendorId); // let's create file manager var fileManager = FileManagerFactory.CreateFileManager(new FileInfo(_generatedFile), _task, FileType.Csv); fileManager.ReadFile(_task.HasHeader ? 1 : 0); // read the first vendor product record from the file var vendorProduct = fileManager.GetNextVendorProduct(); var totalRecords = fileManager.TotalRecords; while (vendorProduct != null) { var percentage = (((double)fileManager.CurrentRowIndex) / totalRecords) * 100.00; Console.WriteLine("{0:#0.00}% Processing vendor product \'{1}\' - {2}", percentage, _task.VendorId, vendorProduct.SupplierSKU); // get first the vendor product SKU var eisSupplierSKU = _service.GetVendorProductSKU(vendorProduct); vendorProduct.EisSupplierSKU = eisSupplierSKU; // continue if it is not to create new item and its eisSupplierSKU is NULL or it has invalid data if ((!_task.IsAddNewItem && string.IsNullOrEmpty(eisSupplierSKU)) || vendorProduct.HasInvalidData) { Console.WriteLine(string.Format("{1:#0.00}% No vendor product updated!", vendorProduct.EisSupplierSKU, percentage)); // read and parsed the next product from the file vendorProduct = fileManager.GetNextVendorProduct(); continue; } var isToUpdate = true; if (string.IsNullOrEmpty(eisSupplierSKU)) { // get the start SKU code for this vendor var startSkuCode = _service.GetVendorStartSku(vendorProduct.VendorId); vendorProduct.EisSupplierSKU = string.Format("{0}{1}", startSkuCode, vendorProduct.SupplierSKU.Trim()); isToUpdate = false; } // let's override its quantity if its vendor is configured as IsAlwasyInStock if (vendor.IsAlwaysInStock && vendorProduct.IsQuantitySet) { vendorProduct.Quantity = vendor.AlwaysQuantity ?? 0; } // do insert or update vendor product vendorProduct.IsAutoLinkToEisSKU = _task.IsCreateEisSKUAndLink || _task.IsLinkEisSKU; vendorProduct.IsAutoLinkToEisSKUSet = _task.IsCreateEisSKUAndLink || _task.IsLinkEisSKU; _service.DoUpadateOrInsertVendorProduct(vendorProduct, isToUpdate, Constants.APP_NAME); _updatedEisSupplierSKUs.Add(vendorProduct.EisSupplierSKU); // check first if we want to auto-link and create new EIS product if it doesn't exist if (_task.IsCreateEisSKUAndLink && !string.IsNullOrEmpty(vendorProduct.UPC)) { _service.AddLinkAndCreateEisProductIfNoMatchWithUPC(vendorProduct); } // check if there's a need to auto-link this vendor product with EIS product if (_task.IsLinkEisSKU && !_task.IsCreateEisSKUAndLink && !string.IsNullOrEmpty(vendorProduct.UPC)) { _service.UpdateEisProductLinks(vendorProduct.EisSupplierSKU, vendorProduct.UPC, vendorProduct.MinPack); } // read and parsed the next product from the file vendorProduct = fileManager.GetNextVendorProduct(); } Logger.LogInfo(LogEntryType.FileInventoryTaskService, "Successfully parsed the vendor product inventory file. Total records: " + fileManager.TotalRecords); return(true); } catch (Exception ex) { Logger.LogError(LogEntryType.FileInventoryTaskService, "Error in parsing the downloaded vendor product inventory file! Message: " + EisHelper.GetExceptionMessage(ex), ex.StackTrace); return(false); } }
protected override void bw_DoWork(object sender, DoWorkEventArgs e) { if (_isWorkerExecuted) { return; } // set the flag the this bw_DoWork has already called _isWorkerExecuted = true; var totalProcessed = 0; var affectedRows = 0; // parsed the vendor product data from the file var vendorProducts = new List <VendorProduct>(); var message = CsvFileDataParser.ParseVendorProductFile(_systemJob.Parameters, vendorProducts, _systemJob.HasHeader); // get the total items and update the system job status var totalItems = vendorProducts.Count; setTotalItemsProcessed(totalItems); _logger.LogInfo(LogEntryType.VendorProductFileUploadWorker, string.Format("Uploading {0} products initiated by {1}", totalItems, _systemJob.SubmittedBy)); try { // iterate and do insert or update vendor product data foreach (var vendorProduct in vendorProducts) { totalProcessed++; var percentage = (((double)totalProcessed) / totalItems) * 100.00; // get first the vendor product SKU var eisSupplierSKU = _service.GetVendorProductSKU(vendorProduct); vendorProduct.EisSupplierSKU = eisSupplierSKU; // continue if it is not to create new item and its eisSupplierSKU is NULL if (!_systemJob.IsAddNewItem && string.IsNullOrEmpty(eisSupplierSKU)) { Console.WriteLine(string.Format("{1:#0.00}% No vendor product updated!", vendorProduct.EisSupplierSKU, percentage)); continue; } var isToUpdate = true; if (string.IsNullOrEmpty(eisSupplierSKU)) { // get the start SKU code for this vendor var startSkuCode = _service.GetVendorStartSku(vendorProduct.VendorId); vendorProduct.EisSupplierSKU = string.Format("{0}{1}", startSkuCode, vendorProduct.SupplierSKU.Trim()); isToUpdate = false; } // do insert or update vendor product affectedRows += _service.DoUpadateOrInsertVendorProduct(vendorProduct, isToUpdate, _systemJob.SubmittedBy); var uploadResultType = UploadResultType.NoChanges; var newEisSKU = string.Empty; // the IsAutoLinkToEisSKU field from the vendor product is the highest precedendens over on the HasPostAction_2 and HasPostAction_1 if (vendorProduct.IsAutoLinkToEisSKUSet) { if (vendorProduct.IsAutoLinkToEisSKU) { // update the current link of this vendor product uploadResultType = _service.UpdateEisProductLinks(vendorProduct.EisSupplierSKU, vendorProduct.UPC, vendorProduct.MinPack); } else { // delete the existing product links if there's any _service.DeleteOldVendorProductLinks(vendorProduct.EisSupplierSKU, new List <string>()); uploadResultType = UploadResultType.DeletedLink; } } else { // check first if we want to auto-link and create new EIS product if it doesn't exist if (_systemJob.HasPostAction_2 && !string.IsNullOrEmpty(vendorProduct.UPC)) { newEisSKU = _service.AddLinkAndCreateEisProductIfNoMatchWithUPC(vendorProduct); uploadResultType = string.IsNullOrEmpty(newEisSKU) ? UploadResultType.UpdatedItem : UploadResultType.NewItemCreated; } // check if there's a need to auto-link this vendor product with EIS product if (_systemJob.HasPostAction_1 && !_systemJob.HasPostAction_2 && !string.IsNullOrEmpty(vendorProduct.UPC)) { uploadResultType = _service.UpdateEisProductLinks(vendorProduct.EisSupplierSKU, vendorProduct.UPC, vendorProduct.MinPack); } } // add the EisSupplierSKU for the upload result tracking addUploadFileResult(vendorProduct.EisSupplierSKU, newEisSKU, uploadResultType); _bw.ReportProgress(affectedRows); Console.WriteLine(string.Format("{1:#0.00}% Updating vendor products EisSupplierSKU: {0}", vendorProduct.EisSupplierSKU, percentage)); } } catch (DbEntityValidationException ex) { _hasError = true; _logger.LogError(LogEntryType.VendorProductFileUploadWorker, EisHelper.ParseDbEntityValidationException(ex), ex.StackTrace); _jobRepository.UpdateSystemJobStatus(_systemJob.Id, JobStatus.Failed); } catch (Exception ex) { _hasError = true; _logger.LogError(LogEntryType.VendorProductFileUploadWorker, string.Format("Error in uploading vendor products. <br/> Error message: {0}", EisHelper.GetExceptionMessage(ex)), ex.StackTrace); _jobRepository.UpdateSystemJobStatus(_systemJob.Id, JobStatus.Failed); } _logger.LogInfo(LogEntryType.VendorProductFileUploadWorker, string.Format("{0} vendor products have been successfully uploaded by {1}", totalProcessed, _systemJob.SubmittedBy)); }