예제 #1
0
        private static void HandleRequiredValueError(Action <ImportProgressInfo> progressCallback, ImportProgressInfo importProgress, ICsvPriceImportReporter reporter, CsvContext context, ImportErrorsContext errorsContext)
        {
            var fieldName          = context.Reader.HeaderRecord[context.Reader.CurrentIndex];
            var requiredFields     = CsvPriceImportHelper.GetImportPriceRequiredValueColumns();
            var missedValueColumns = new List <string>();

            for (var i = 0; i < context.Reader.HeaderRecord.Length; i++)
            {
                if (requiredFields.Contains(context.Reader.HeaderRecord[i], StringComparer.InvariantCultureIgnoreCase) && context.Parser.Record[i].IsNullOrEmpty())
                {
                    missedValueColumns.Add(context.Reader.HeaderRecord[i]);
                }
            }

            var importError = new ImportError {
                Error = $"The required value in column {fieldName} is missing.", RawRow = context.Parser.RawRecord
            };

            if (missedValueColumns.Count > 1)
            {
                importError.Error = $"The required values in columns: {string.Join(", ", missedValueColumns)} - are missing.";
            }

            reporter.Write(importError);

            errorsContext.ErrorsRows.Add(context.Parser.Row);
            HandleError(progressCallback, importProgress);
        }
        private static void ValidateRequiredColumns(Stream stream, CsvConfiguration csvConfiguration, List <ImportDataValidationError> errorsList)
        {
            var notCompatibleErrors = new[]
            {
                ModuleConstants.ValidationErrors.FileNotExisted,
                ModuleConstants.ValidationErrors.ExceedingFileMaxSize,
                ModuleConstants.ValidationErrors.WrongDelimiter,
                ModuleConstants.ValidationErrors.NoData,
            };

            if (errorsList.Any(x => notCompatibleErrors.Contains(x.ErrorCode)))
            {
                return;
            }

            stream.Seek(0, SeekOrigin.Begin);
            var streamReader = new StreamReader(stream);
            var csvReader    = new CsvReader(streamReader, csvConfiguration);

            csvReader.Read();
            csvReader.ReadHeader();

            var existedColumns = csvReader.Context.Reader.HeaderRecord;

            var requiredColumns = CsvPriceImportHelper.GetImportPriceRequiredColumns();

            var missedColumns = requiredColumns.Except(existedColumns).ToArray();

            if (missedColumns.Length > 0)
            {
                var error = new ImportDataValidationError()
                {
                    ErrorCode = ModuleConstants.ValidationErrors.MissingRequiredColumns
                };
                error.Properties.Add(nameof(missedColumns), string.Join(", ", missedColumns));
                errorsList.Add(error);
            }
        }