예제 #1
0
        /// <summary>
        /// Takes a line of text and converts into domain object T
        /// </summary>
        /// <param name="lineOfRecordFromCsv">line of text from csv file</param>
        /// <param name="convertedObj">Converted domain object</param>
        /// <returns></returns>
        internal bool ConvertCsvRecordToObject(string lineOfRecordFromCsv, out T convertedObj)
        {
            _csvHeaderAndData.Clear();
            string[] columnData = SplitLineOfTextIntoCsv.Split(lineOfRecordFromCsv);
            for (ushort i = 0; i <= HeaderColumnNamesInCsvFile.Count; i++)
            {
                if (i < HeaderColumnNamesInCsvFile.Count && i < columnData.Length)
                {
                    _csvHeaderAndData.Add(HeaderColumnNamesInCsvFile[i], columnData[i]);
                }
            }

            if (_ignoreColumnCountMismatch == false && columnData.Length != HeaderColumnNamesInCsvFile.Count)
            {
                AddError(ErrorCodes.ColumnCountMismatch, $"Number of columns present in CSV doesn't matches with expected mapper. Count of columns present in CSV={HeaderColumnNamesInCsvFile.Count}, No of columns in mapper:{columnData.Length}  ");
                convertedObj = default(T);
                return(false);
            }

            T domainObj = (T)Activator.CreateInstance(typeof(T));

            PropertyInfo[] properties = domainObj.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                //Get Each property name from list of properties
                if (this._mapper.ObjectToCsvMapping.ContainsKey(property.Name)) //Ignore the object properties if no mapping proeprties passed
                {
                    ICsvToObjectMap <T> map     = this._mapper.ObjectToCsvMapping[property.Name];
                    string csvColumnNameFromMap = map.CsvColumnName;
                    string columnValue          = _csvHeaderAndData[csvColumnNameFromMap];
                    try
                    {
                        property.SetValue(domainObj, Convert.ChangeType(columnValue, property.PropertyType), null);
                    }
                    catch (Exception ex
                           ) //Handle data conversion failure(if csv data is not compatible with object datatype)
                    {
                        _logger.LogError($"{ex.Message}\n{ex.StackTrace}");
                        convertedObj = default(T);
                        this.ExtractFailedRows.Add(lineOfRecordFromCsv);
                        var error =
                            $"Cannot Convert data '{columnValue}' for Column name {csvColumnNameFromMap}, column data type {property.PropertyType.Name} in file {_pathToCsv}";
                        AddError(ErrorCodes.DataConversionError, error
                                 );
                        if (!this._ignoreDataConversionError)
                        {
                            throw new CsvReadWriteException(error, ErrorCodes.DataConversionError);
                        }
                        return(false);
                    }
                }
            }

            convertedObj = domainObj;
            return(true);
        }
        /// <summary>
        /// Runs series of pre validations on the given CSV file before starting extracting data
        /// </summary>
        /// <param name="pathToCsv">Path to .csv/other allowed extension file</param>
        private bool PreExtractValidation(string pathToCsv)
        {
            if (pathToCsv == null && _csvContentLines.Length == 0)
            {
                AddError(ErrorCodes.NullPath, "Please supply the file content or provide the path to the file in constructor argument");
                return(false);
            }
            else if (!string.IsNullOrEmpty(pathToCsv) && !_fileService.FileExists(pathToCsv))
            {
                AddError(ErrorCodes.PathNotExists, $"{pathToCsv}  does not exists");
                return(false);
            }
            else if (!string.IsNullOrEmpty(pathToCsv) && _fileService.PathGetExtension(pathToCsv).ToUpper() != ".CSV")
            {
                AddError(ErrorCodes.InvalidFileExtension, $"Invalid file extension {_fileService.PathGetExtension(pathToCsv)}. Only .csv is supported ");
                return(false);
            }
            try
            {
                if (_csvContentLines.Length == 0)
                {
                    _csvContentLines = _fileService.ReadAllLines(pathToCsv);
                }
            }
            catch (Exception ex)
            {
                AddError(ErrorCodes.CannotReadFile, $"{pathToCsv} Cannot be read\n Exception:{ex.Message}\nStacktrace:{ex.StackTrace}");
                return(false);
            }

            if (
                ((_csvContentLines.Length == 0 ||
                  (_csvContentLines.Length == 1 && string.IsNullOrEmpty(_csvContentLines[0].Trim()))) &&
                 _ignoreDataConversionError == false))
            {
                AddError(ErrorCodes.FileEmpty, "Csv file Content is empty");
                return(false);
            }

            if (_headerPresentInFirstRow)//Read the first row as Header
            {
                this.HeaderColumnNamesInCsvFile = SplitLineOfTextIntoCsv.Split(_csvContentLines[0]);
                return(IsCsvColumnNamesAsExpectedWithoutDuplicateColumnNames());//Throws Exception if csv columns are not as expected
            }
            return(true);
        }
예제 #3
0
        /// <summary>
        /// Runs series of pre validations on the given CSV file before starting extracting data
        /// </summary>
        internal bool PreExtractValidation()
        {
            bool validationFailed = false;

            if (_pathToCsv == null && _csvContentLines.Length == 0)
            {
                AddError(ErrorCodes.NullPath, "Please supply the file content or provide the path to the file in constructor argument");
                validationFailed = true;
            }
            if (!string.IsNullOrEmpty(_pathToCsv) && _fileService == null)
            {
                AddError(ErrorCodes.ParameterNull, $"Constructor parameter 'fileService' cannot be null");
                validationFailed = true;
            }

            if (_mapper == null)
            {
                AddError(ErrorCodes.ParameterNull, $"Constructor parameter '_mapper' cannot be null");
                validationFailed = true;
            }
            if (!string.IsNullOrEmpty(_pathToCsv) && _fileService != null && !_fileService.FileExists(_pathToCsv))
            {
                AddError(ErrorCodes.PathNotExists, $"{_pathToCsv}  does not exists");
                validationFailed = true;
            }
            if (!string.IsNullOrEmpty(_pathToCsv) && _fileService != null && _fileService.PathGetExtension(_pathToCsv).ToUpper() != ".CSV")
            {
                AddError(ErrorCodes.InvalidFileExtension, $"Invalid file extension {_fileService.PathGetExtension(_pathToCsv)}. Only .csv is supported ");
                validationFailed = true;
            }
            if (!validationFailed && _fileService != null && _mapper != null)
            {
                try
                {
                    if (_csvContentLines.Length == 0)
                    {
                        _csvContentLines = _fileService.ReadAllLines(_pathToCsv);
                    }
                }
                catch (Exception ex)
                {
                    AddError(ErrorCodes.CannotReadFile, $"{_pathToCsv} Cannot be read\n Exception:{ex.Message}\nStacktrace:{ex.StackTrace}");
                    return(false);
                }
                if (
                    (_csvContentLines.Length == 0 ||
                     (_csvContentLines.Length == 1 && string.IsNullOrEmpty(_csvContentLines[0].Trim()))))
                {
                    AddError(ErrorCodes.FileEmpty, "Csv file Content is empty");
                    if (!_ignoreEmptyFile)
                    {
                        throw new CsvReadWriteException(ErrorCodes.FileEmpty);
                    }
                    return(false);
                }
                this.HeaderColumnNamesInCsvFile = SplitLineOfTextIntoCsv.Split(_csvContentLines[0]);
                return(IsCsvColumnNamesAsExpectedWithoutDuplicateColumnNames());//Throws Exception if csv columns are not as expected
            }

            return(false);
        }