public async Task Upload() { var fileReader = new StreamReader(_file.FullName); var count = 0; _productfileconfig = await VendorsConfig.GetProductFileConfigByVendorId(_vendor.Id); _imagefileconfig = await VendorsConfig.GetImageFileConfigByVendorId(_vendor.Id); for (var i = 1; i < _vendor.RowAt; ++i) { await fileReader.ReadLineAsync(); } var totalLines = File.ReadAllLines(_file.FullName).Count(); var line = await fileReader.ReadLineAsync(); while (!String.IsNullOrEmpty(line)) { await InsertVendorProduct(line); line = await fileReader.ReadLineAsync(); count++; var percentage = (((double)count + 1) / totalLines - _vendor.RowAt) * 100.00; System.Console.WriteLine("{1:#0.00}% Inserting product for vendor: {0}", _vendor.VendorName, percentage); } System.Console.WriteLine("Done uploading {0} products for {1}", count, _vendor.VendorName); fileReader.Close(); await MoveFile(); }
private async Task InsertProductImages(IList <string> rowValues, long vendorProductId) { foreach (var imageConfig in _imagefileconfig) { var image = new productimage { ImagePath = rowValues[(int)imageConfig.ColumnNo], ImageType = imageConfig.ImageType, VendorProductId = vendorProductId }; await VendorsConfig.AddProductImage(image); } }
private async Task InsertProductImages(long vendorProductId, int row) { foreach (var imageConfig in _imagefileconfig) { var image = new productimage { ImagePath = Convert.ToString((_range.Cells[row, (imageConfig.ColumnNo + 1)] as Range).Value2), ImageType = imageConfig.ImageType, VendorProductId = vendorProductId }; await VendorsConfig.AddProductImage(image); } }
private async Task InsertVendorProducts() { var count = 0; try { for (var rCnt = (int)_vendor.RowAt; rCnt <= _range.Rows.Count; rCnt++) { var SKU = (_productfileconfig.SKU != -1) ? (_range.Cells[rCnt, (_productfileconfig.SKU + 1)] as Range).Value2 : null; var Name = (_productfileconfig.Name != -1) ? (_range.Cells[rCnt, (_productfileconfig.Name + 1)] as Range).Value2 : null; var Description = (_productfileconfig.Description != -1) ? (_range.Cells[rCnt, (_productfileconfig.Description + 1)] as Range).Value2 : null; var ShortDescription = (_productfileconfig.ShortDescription != -1) ? (_range.Cells[rCnt, (_productfileconfig.ShortDescription + 1)] as Range).Value2 : null; var Category = (_productfileconfig.Category != -1) ? (_range.Cells[rCnt, (_productfileconfig.Category + 1)] as Range).Value2 : null; var UPCCode = (_productfileconfig.UPCCode != -1) ? (_range.Cells[rCnt, (_productfileconfig.UPCCode + 1)] as Range).Value2 : null; var SupplierCost = (_productfileconfig.SupplierCost != -1) ? (_range.Cells[rCnt, (_productfileconfig.SupplierCost + 1)] as Range).Value2 : null; var product = new vendorproduct { VendorId = _vendor.Id, SKU = (SKU != null) ? Convert.ToString(SKU) : "", Name = (Name != null) ? Convert.ToString(Name) : "", Description = (Description != null) ? Convert.ToString(Description) : "", ShortDescription = (ShortDescription != null) ? Convert.ToString(ShortDescription) : "", Category = (Category != null) ? Convert.ToString(Category) : "", UPCCode = (UPCCode != null) ? Convert.ToString(UPCCode) : "", SupplierCost = (SupplierCost != null) ? Convert.ToDecimal(SupplierCost) : 0, ResultDate = _date }; var productId = await VendorsConfig.AddVendorProduct(product); await InsertProductImages(productId, rCnt); count++; var percentage = (((double)count + 1) / _range.Rows.Count) * 100.00; System.Console.WriteLine("{1:#0.00}% Inserted product: {0}", product.SKU, percentage); } System.Console.WriteLine("Uploaded {0} products for {1}", count, _vendor.VendorName); } catch (Exception ex) { System.Console.WriteLine(ex.StackTrace); } }
private async Task InsertVendorProduct(string unSplitRowValues) { var rowValues = unSplitRowValues.Split(_delimiter).ToList(); var product = new vendorproduct { VendorId = _vendor.Id, SKU = rowValues[(int)_productfileconfig.SKU], Name = rowValues[(int)_productfileconfig.Name], Description = rowValues[(int)_productfileconfig.Description], ShortDescription = rowValues[(int)_productfileconfig.ShortDescription], Category = rowValues[(int)_productfileconfig.Category], UPCCode = rowValues[(int)_productfileconfig.UPCCode], SupplierCost = Convert.ToDecimal(rowValues[(int)_productfileconfig.SupplierCost]), ResultDate = _date }; var vendorProductId = await VendorsConfig.AddVendorProduct(product); await InsertProductImages(rowValues, vendorProductId); }
public async Task Upload() { // System.Console.WriteLine("Uploading "); _productfileconfig = await VendorsConfig.GetProductFileConfigByVendorId(_vendor.Id); _imagefileconfig = await VendorsConfig.GetImageFileConfigByVendorId(_vendor.Id); var xlApplication = new Application(); xlApplication.DisplayAlerts = false; var xlWorkbook = xlApplication.Workbooks.Open(_file.FullName); var xlWorksheet = (Worksheet)xlWorkbook.Worksheets.Item[1]; _range = xlWorksheet.UsedRange; await InsertVendorProducts(); //Cleanup _range.Clear(); Marshal.FinalReleaseComObject(_range); _range = null; Marshal.FinalReleaseComObject(xlWorksheet); xlWorksheet = null; xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing); Marshal.FinalReleaseComObject(xlWorkbook); xlWorkbook = null; xlApplication.Quit(); Marshal.FinalReleaseComObject(xlApplication); xlApplication = null; GC.Collect(); GC.WaitForPendingFinalizers(); await MoveFile(); }
public async Task StartAsync() { while (_loop) { var errorMessage = string.Empty; try { System.Console.WriteLine("Searching for scheduled vendors to upload files."); foreach (var vendor in await VendorsConfig.GetScheduledVendors()) { var hasError = false; uploadstatu status; if (vendor.uploadstatus.Count > 0) { status = vendor.uploadstatus.First(); await VendorsConfig.DeleteCurrentProductsByVendorId(vendor.Id); } else { status = await VendorsConfig.CreateUploadStatus(vendor.Id); } try { var file = GetUploadedFile(vendor); if (file != null) { IFileReader reader = FileReaderFactory.GetFileReader(file, vendor); await reader.Upload(); } else { await Task.Run(() => Logger.LogDisplay(String.Format("No products were uploaded for {0}", vendor.VendorName), "Info")); } await VendorsConfig.UpdateUploadStatus(status, true); } catch (Exception ex) { System.Console.WriteLine(ex); Logger.LogException(ex); hasError = true; } //When error occured set uploadstatus if (hasError) { await VendorsConfig.UpdateUploadStatus(status, false); await Task.Run(() => Logger.LogError(String.Format("Error uploading {0} products", vendor.VendorName))); } } } catch (Exception ex) { System.Console.WriteLine(ex); errorMessage = ex.Message; } if (@String.IsNullOrEmpty(errorMessage)) { await Task.Run(() => Logger.LogError(errorMessage)); } await Task.Delay(GetInterval()); } }