/// <summary> /// Converts Excell Row to POCO object. This is index based, depends on order of cells ion the file and properties in /// object /// </summary> /// <param name="source"></param> /// <param name="rowIndex"></param> /// /// <param name="header"></param> /// <returns></returns> public static ProductInputRow Convert(IRow source, int rowIndex, IRow header) { if (source == null) return null; var inputRow = new ProductInputRow(); List<PropertyInfo> props = inputRow.GetProperties(); const int specificationAtrributesIdexStart = 32; int specificationAtrributesIdexEnd = header.LastCellNum - 4; //process product props ProcessProductBase(source, specificationAtrributesIdexStart, props, inputRow); //process product specification attributes props ProcessSpecificationAttributes(source, inputRow, specificationAtrributesIdexStart, specificationAtrributesIdexEnd, header); inputRow.RowIndex = rowIndex; return inputRow; }
private static void ProcessProductBase(IRow source, int specificationAtrributesIdexStart, List<PropertyInfo> props, ProductInputRow inputRow) { for (int i = 0; i < specificationAtrributesIdexStart; i++) { ICell cell = source.GetCell(i, MissingCellPolicy.CREATE_NULL_AS_BLANK); cell.SetCellType(CellType.String); string cellValue = cell.StringCellValue.Trim(); PropertyInfo prop = props.First(x => x.Name == InputRowMap[i]); prop.SetValue(inputRow, cellValue, null); } }
private static void ProcessSpecificationAttributes(IRow source, ProductInputRow inputRow, int specificationAtrributesIdexStart, int specificationAtrributesIdexEnd, IRow header) { if (!inputRow.HasSpecificationAttributes.InvariantEquals("yes")) return; for (int i = specificationAtrributesIdexStart; i <= specificationAtrributesIdexEnd; i += 2) { ICell attrNameCell = header.GetCell(i, MissingCellPolicy.CREATE_NULL_AS_BLANK); attrNameCell.SetCellType(CellType.String); string attrName = attrNameCell.StringCellValue.Trim(); //var mapAttr = InputRowMap.FirstOrDefault(p => p.Value == attrName); //if (!attrName.InvariantEquals(mapAttr.Value)) continue; ICell attrCell = source.GetCell(i, MissingCellPolicy.CREATE_NULL_AS_BLANK); attrCell.SetCellType(CellType.String); string attrValue = attrCell.StringCellValue.Trim(); if (attrValue.InvariantEquals("n/a")) continue; ICell settingsCell = source.GetCell(i + 1, MissingCellPolicy.CREATE_NULL_AS_BLANK); settingsCell.SetCellType(CellType.String); string[] settingsValue = settingsCell.StringCellValue.Trim().Split('|'); foreach (string attrVal in attrValue.Split('|')) { inputRow.ProductSpecificationAttributeInput.Add(new SpecificationAttributeInput { Name = attrName.Trim(), IncludeInFilter = settingsValue[0], ShowOnProductPage = settingsValue[1], Value = attrVal.Trim() }); } } }
private void ProcessInputRow(ProductInputRow inputRow, UserContext userContext, ref int failedCount) { //todo add debug log inputRow.Processed = true; try { Product resolvedProduct = _productResolver.Resolve(new ProductResolveInput(userContext) {ProductInputRow = inputRow}); inputRow.UploadedProductId = resolvedProduct.Id.ToString(); inputRow.UploadStatus = "Data processing completed"; inputRow.UploadMessage = string.Empty; } catch (Exception e) { _logger.Error("Unable to process input row. " + userContext, e); inputRow.UploadStatus = "Failed to process data"; inputRow.UploadMessage = "Unable to process input row."; failedCount++; } }