/// <summary> /// Constructs the Csv to Object reader instance. Most of the parameters are optional parameters /// with default value. Override the default value with the custom values /// </summary> /// <param name="mapper">instance of Csv File to Domain object mapper <see cref="CsvToObjectMapper{T}"/></param> /// <param name="fileContent">Content of .csv/txt file</param> /// <param name="headerPresentInFirstRow">Does this csv/text file has header row in first line?[default=true]</param> /// <param name="mustMatchExpectedHeader">Should this csv file headers match with the header provided in mapper?[default=true]</param> /// <param name="ignoreEmptyFile">should empty file be ignored and not marked as error?[default=true]</param> /// <param name="ignoreColumnCountMismatch">Should ignore the additional columns if present and not report error?[default=true]</param> /// <param name="ignoreDataConversionError">Should ignore the rows failing because of schema/data conversion issues?[default=true]</param> public CsvToObjectReader( CsvToObjectMapper <T> mapper, string fileContent, bool headerPresentInFirstRow = true, bool mustMatchExpectedHeader = true, bool ignoreEmptyFile = true, bool ignoreColumnCountMismatch = true, bool ignoreDataConversionError = true ) { /* * Initialize all the initial configuration settings */ this._csvContentLines = fileContent?.Split( new[] { Environment.NewLine }, StringSplitOptions.None ); this._mustMatchExpectedHeader = mustMatchExpectedHeader; this._mapper = mapper; this._ignoreDataConversionError = ignoreDataConversionError; this._headerPresentInFirstRow = headerPresentInFirstRow; this._ignoreEmptyFile = ignoreEmptyFile; this._ignoreColumnCountMismatch = ignoreColumnCountMismatch; _mustMatchExpectedHeader = headerPresentInFirstRow != false && _mustMatchExpectedHeader; }
private bool MandatoryParameterCheck(IList <List <T> > groupedObjects, ICsvToObjectMapper <T> mapper, string targetFolderPath, IFileService fileService) { if (groupedObjects == null) { LogAndThrowError($"Constructor parameter groupedObjects cannot be blank", ErrorCodes.ParameterNull); } if (mapper == null) { LogAndThrowError($"Constructor parameter mapper cannot be blank", ErrorCodes.ParameterNull); } if (_fileNames.Count != groupedObjects?.Count) { LogAndThrowError($"Number of file names supplied doesn't match with expected count. Supplied count:{_fileNames.Count}, Expected count:{_groupedObjects.Count}", ErrorCodes.FileNameListCountNotMatches); } if (!fileService.DirectoryExists(targetFolderPath)) { try { fileService.CreateDirectory(targetFolderPath); //Create if folder doesn't exists fileService.WriteAllText(targetFolderPath + @"\TestFileCreate.txt", "sample Content"); //Check if file can be created (to check if writable) fileService.DeleteFile(targetFolderPath + @"\TestFileCreate.txt"); } catch (Exception ex) { LogAndThrowError($"Cannot create folder/file in the given path :{targetFolderPath}", ErrorCodes.CannotWriteFileOrDirectory); _logger.LogError($"{ex.Message}\n{ex.StackTrace}"); } } return(true); }
private bool MandatoryParameterCheck(IList <List <T> > groupedObjects, ICsvToObjectMapper <T> mapper, string targetFolderPath, IFileService fileService) { if (groupedObjects == null) { throw new ArgumentNullException($"Constructor parameter groupedObjects cannot be blank"); } if (mapper == null) { throw new ArgumentNullException($"Constructor parameter mapper cannot be blank"); } if (!fileService.DirectoryExists(targetFolderPath)) { fileService.CreateDirectory(targetFolderPath); } return(true); }
/// <summary> /// Constructor /// </summary> /// <param name="groupedObjects">List of Lists to serialize to csv File</param> /// <param name="mapper">Object to .csv Mapper</param> /// <param name="reflectionHelper">Helper class to reflect the object to retrieve metadata properties</param> /// <param name="fileService"></param> /// <param name="targetFolderPath">Folder path where the .csv files are expected to persist</param> /// <param name="fileNames">Optional field which specifies the list of file names, File Name to CSV will be matched based on Index. /// If this parameter is not specified, it will use system timestamp as file name e.g. 21-11-2018 22:00:22.883 /// </param> public ObjectToCsvWriter(IList <List <T> > groupedObjects, ICsvToObjectMapper <T> mapper, IReflectionHelper <T> reflectionHelper, IFileService fileService, string targetFolderPath, IList <string> fileNames = null) { _groupedObjects = groupedObjects; _mapper = mapper; _reflectionHelper = reflectionHelper; _fileService = fileService; _targetFolderPath = targetFolderPath; _fileNames = fileNames; MandatoryParameterCheck(groupedObjects, mapper, targetFolderPath, _fileService); ExtractHeaderFromMapperFromMapper(); }
/// <summary> /// Constructs the Csv to Object reader instance. Most of the parameters are optional parameters /// with default value. Override the default value with the custom values /// </summary> /// <param name="pathToCsv">Complete file path to the .csv/txt file</param> /// <param name="mapper">instance of Csv File to Domain object mapper <see cref="CsvToObjectMapper{T}"/></param> /// <param name="fileService">FileService for all file related operations<see cref="IFileService"/></param> /// <param name="loggerFactory">Logger factory</param> /// <param name="ignoreEmptyFile">should empty file be ignored and not marked as error?[default=true]</param> /// <param name="ignoreColumnCountMismatch">Should ignore the additional columns if present and not report error?[default=true]</param> /// <param name="ignoreDataConversionError">Should ignore the rows failing because of schema/data conversion issues?[default=true]</param> public CsvToObjectReader( string pathToCsv, IFileService fileService, ICsvToObjectMapper <T> mapper, ILoggerFactory loggerFactory, bool ignoreEmptyFile = true, bool ignoreColumnCountMismatch = true, bool ignoreDataConversionError = true ) { /* * Initialize all the initial configuration settings */ this._pathToCsv = pathToCsv; this._fileService = fileService; this._mapper = mapper; this._ignoreDataConversionError = ignoreDataConversionError; this._ignoreEmptyFile = ignoreEmptyFile; this._ignoreColumnCountMismatch = ignoreColumnCountMismatch; this._logger = loggerFactory.CreateLogger <CsvToObjectReader <T> >(); }
/// <summary> /// Constructs the Csv to Object reader instance. Most of the parameters are optional parameters /// with default value. Override the default value with the custom values /// </summary> /// <param name="fileContentLines">Content of .csv/txt file in a string array</param> /// <param name="mapper">instance of Csv File to Domain object mapper <see cref="CsvToObjectMapper{T}"/></param> /// <param name="loggerFactory"></param> /// <param name="headerPresentInFirstRow">Does this csv/text file has header row in first line?[default=true]</param> /// <param name="mustMatchExpectedHeader">Should this csv file headers match with the header provided in mapper?[default=true]</param> /// <param name="ignoreEmptyFile">should empty file be ignored and not marked as error?[default=true]</param> /// <param name="ignoreColumnCountMismatch">Should ignore the additional columns if present and not report error?[default=true]</param> /// <param name="ignoreDataConversionError">Should ignore the rows failing because of schema/data conversion issues?[default=true]</param> public CsvToObjectReader( string[] fileContentLines, CsvToObjectMapper <T> mapper, ILoggerFactory loggerFactory, bool headerPresentInFirstRow = true, bool mustMatchExpectedHeader = true, bool ignoreEmptyFile = true, bool ignoreColumnCountMismatch = true, bool ignoreDataConversionError = true ) { /* * Initialize all the initial configuration settings */ this._csvContentLines = fileContentLines; this._mapper = mapper; this._ignoreDataConversionError = ignoreDataConversionError; this._ignoreEmptyFile = ignoreEmptyFile; this._ignoreColumnCountMismatch = ignoreColumnCountMismatch; this._logger = loggerFactory.CreateLogger <CsvToObjectReader <T> >(); }
/// <summary> /// Constructs the Csv to Object reader instance. Most of the parameters are optional parameters /// with default value. Override the default value with the custom values /// </summary> /// <param name="fileContentLines">Content of .csv/txt file in a string array</param> /// <param name="mapper">instance of Csv File to Domain object mapper <see cref="CsvToObjectMapper{T}"/></param> /// <param name="headerPresentInFirstRow">Does this csv/text file has header row in first line?[default=true]</param> /// <param name="mustMatchExpectedHeader">Should this csv file headers match with the header provided in mapper?[default=true]</param> /// <param name="ignoreEmptyFile">should empty file be ignored and not marked as error?[default=true]</param> /// <param name="ignoreColumnCountMismatch">Should ignore the additional columns if present and not report error?[default=true]</param> /// <param name="ignoreDataConversionError">Should ignore the rows failing because of schema/data conversion issues?[default=true]</param> public CsvToObjectReader( string[] fileContentLines, CsvToObjectMapper <T> mapper, bool headerPresentInFirstRow = true, bool mustMatchExpectedHeader = true, bool ignoreEmptyFile = true, bool ignoreColumnCountMismatch = true, bool ignoreDataConversionError = true ) { /* * Initialize all the initial configuration settings */ this._csvContentLines = fileContentLines; this._mustMatchExpectedHeader = mustMatchExpectedHeader; this._mapper = mapper; this._ignoreDataConversionError = ignoreDataConversionError; this._headerPresentInFirstRow = headerPresentInFirstRow; this._ignoreEmptyFile = ignoreEmptyFile; this._ignoreColumnCountMismatch = ignoreColumnCountMismatch; _mustMatchExpectedHeader = headerPresentInFirstRow != false && _mustMatchExpectedHeader; }
/// <summary> /// Constructs the Csv to Object reader instance. Most of the parameters are optional parameters /// with default value. Override the default value with the custom values /// </summary> /// <param name="mapper">instance of Csv File to Domain object mapper <see cref="CsvToObjectMapper{T}"/></param> /// <param name="loggerFactory"></param> /// <param name="fileContent">Content of .csv/txt file</param> /// <param name="ignoreEmptyFile">should empty file be ignored and not marked as error?[default=true]</param> /// <param name="ignoreColumnCountMismatch">Should ignore the additional columns if present and not report error?[default=true]</param> /// <param name="ignoreDataConversionError">Should ignore the rows failing because of schema/data conversion issues?[default=true]</param> public CsvToObjectReader( CsvToObjectMapper <T> mapper, ILoggerFactory loggerFactory, string fileContent, bool ignoreEmptyFile = true, bool ignoreColumnCountMismatch = true, bool ignoreDataConversionError = true ) { /* * Initialize all the initial configuration settings */ this._csvContentLines = fileContent?.Split( new[] { Environment.NewLine }, StringSplitOptions.None ); this._mapper = mapper; this._ignoreDataConversionError = ignoreDataConversionError; this._ignoreEmptyFile = ignoreEmptyFile; this._ignoreColumnCountMismatch = ignoreColumnCountMismatch; this._logger = loggerFactory.CreateLogger <CsvToObjectReader <T> >(); }
/// <summary> /// Constructs the Csv to Object reader instance. Most of the parameters are optional parameters /// with default value. Override the default value with the custom values /// </summary> /// <param name="pathToCsv">Complete file path to the .csv/txt file</param> /// <param name="mapper">instance of Csv File to Domain object mapper <see cref="CsvToObjectMapper{T}"/></param> /// <param name="fileService">FileService for all file related operations<see cref="IFileService"/></param> /// <param name="headerPresentInFirstRow">Does this csv/text file has header row in first line?[default=true]</param> /// <param name="mustMatchExpectedHeader">Should this csv file headers match with the header provided in mapper?[default=true]</param> /// <param name="ignoreEmptyFile">should empty file be ignored and not marked as error?[default=true]</param> /// <param name="ignoreColumnCountMismatch">Should ignore the additional columns if present and not report error?[default=true]</param> /// <param name="ignoreDataConversionError">Should ignore the rows failing because of schema/data conversion issues?[default=true]</param> public CsvToObjectReader( string pathToCsv, IFileService fileService, ICsvToObjectMapper <T> mapper, bool headerPresentInFirstRow = true, bool mustMatchExpectedHeader = true, bool ignoreEmptyFile = true, bool ignoreColumnCountMismatch = true, bool ignoreDataConversionError = true ) { /* * Initialize all the initial configuration settings */ this._pathToCsv = pathToCsv; this._fileService = fileService; this._mustMatchExpectedHeader = mustMatchExpectedHeader; this._mapper = mapper; this._ignoreDataConversionError = ignoreDataConversionError; this._headerPresentInFirstRow = headerPresentInFirstRow; this._ignoreEmptyFile = ignoreEmptyFile; this._ignoreColumnCountMismatch = ignoreColumnCountMismatch; _mustMatchExpectedHeader = headerPresentInFirstRow != false && _mustMatchExpectedHeader; }