Пример #1
0
        private (List <WildlifeReportCsvDto> untypedRows, string headers) ParseRowsUnTyped(Dictionary <string, List <string> > errors)
        {
            MethodLogger.LogEntry(_logger, _enableMethodLog, _methodLogHeader);

            using TextReader textReader = new StreamReader(new MemoryStream(_submission.DigitalRepresentation), Encoding.UTF8);
            using var csv = new CsvReader(textReader, CultureInfo.InvariantCulture);

            CsvHelperUtils.Config(errors, csv, false);
            csv.Configuration.RegisterClassMap <WildlifeReportCsvDtoMap>();

            var rows = GetRecords(csv);

            return(rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
        }
Пример #2
0
        private (decimal rowNum, List <RockfallReportTyped> rows) ParseRowsTyped(string text, Dictionary <string, List <string> > errors)
        {
            MethodLogger.LogEntry(_logger, _enableMethodLog, _methodLogHeader);

            using var stringReader = new StringReader(text);
            using var csv          = new CsvReader(stringReader, CultureInfo.InvariantCulture);

            CsvHelperUtils.Config(errors, csv, false);
            csv.Configuration.RegisterClassMap <RockfallReportDtoMap>();

            var rows   = new List <RockfallReportTyped>();
            var rowNum = 0M;

            while (csv.Read())
            {
                try
                {
                    var row = csv.GetRecord <RockfallReportTyped>();
                    row.HighwayUnique = row.HighwayUnique.ToTrimAndUppercase();
                    //rockfall requires Y/N fields to be set to Uppercase, see HMCR-643
                    row.HeavyPrecip   = row.HeavyPrecip.ToTrimAndUppercase();
                    row.FreezeThaw    = row.FreezeThaw.ToTrimAndUppercase();
                    row.DitchSnowIce  = row.DitchSnowIce.ToTrimAndUppercase();
                    row.VehicleDamage = row.VehicleDamage.ToTrimAndUppercase();

                    rows.Add(row);
                    rowNum          = (decimal)row.RowNum;
                    row.ServiceArea = _serviceArea.ConvertToServiceAreaNumber(row.ServiceArea);
                }
                catch (CsvHelper.TypeConversion.TypeConverterException ex)
                {
                    _logger.LogError(ex.ToString());
                    rowNum = GetRowNum(csv.Context.RawRecord);
                    LogRowParseException(rowNum, ex.ToString(), csv.Context);
                    errors.AddItem("Parse Error", $"Exception while parsing the text [{ex.Text}]");
                    return(rowNum, null);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.ToString());
                    rowNum = GetRowNum(csv.Context.RawRecord);
                    LogRowParseException(rowNum, ex.ToString(), csv.Context);
                    errors.AddItem("Parse Error", $"Exception while parsing");
                    return(rowNum, null);
                }
            }

            return(0, rows);
        }
        private (decimal rowNum, List <RockfallReportTyped> rows) ParseRowsTyped(string text, Dictionary <string, List <string> > errors)
        {
            MethodLogger.LogEntry(_logger, _enableMethodLog, _methodLogHeader);

            using var stringReader = new StringReader(text);
            using var csv          = new CsvReader(stringReader, CultureInfo.InvariantCulture);

            CsvHelperUtils.Config(errors, csv, false);
            csv.Configuration.RegisterClassMap <RockfallReportDtoMap>();

            var rows   = new List <RockfallReportTyped>();
            var rowNum = 0M;

            while (csv.Read())
            {
                try
                {
                    var row = csv.GetRecord <RockfallReportTyped>();
                    rows.Add(row);
                    rowNum          = (decimal)row.RowNum;
                    row.ServiceArea = _serviceArea.ConvertToServiceAreaNumber(row.ServiceArea);
                }
                catch (CsvHelper.TypeConversion.TypeConverterException ex)
                {
                    _logger.LogError(ex.ToString());
                    rowNum = GetRowNum(csv.Context.RawRecord);
                    LogRowParseException(rowNum, ex.ToString(), csv.Context);
                    errors.AddItem("Parse Error", $"Exception while parsing the text [{ex.Text}]");
                    return(rowNum, null);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.ToString());
                    rowNum = GetRowNum(csv.Context.RawRecord);
                    LogRowParseException(rowNum, ex.ToString(), csv.Context);
                    errors.AddItem("Parse Error", $"Exception while parsing");
                    return(rowNum, null);
                }
            }

            return(0, rows);
        }
        private (List <RockfallReportCsvDto> untypedRows, string headers) ParseRowsUnTyped(Dictionary <string, List <string> > errors)
        {
            MethodLogger.LogEntry(_logger, _enableMethodLog, _methodLogHeader);

            using TextReader textReader = new StreamReader(new MemoryStream(_submission.DigitalRepresentation), Encoding.UTF8);
            using var csv = new CsvReader(textReader, CultureInfo.InvariantCulture);

            CsvHelperUtils.Config(errors, csv, false);
            csv.Configuration.RegisterClassMap <RockfallReportCsvDtoMap>();

            var rows = csv.GetRecords <RockfallReportCsvDto>().ToList();

            for (var i = 0; i < rows.Count; i++)
            {
                rows[i].RowNum      = i + 2;
                rows[i].ServiceArea = _serviceArea.ConvertToServiceAreaString(rows[i].ServiceArea);
            }

            return(rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", ""));
        }
Пример #5
0
        protected override async Task <bool> ParseRowsAsync(SubmissionObjectCreateDto submission, TextReader textReader, Dictionary <string, List <string> > errors)
        {
            using var csv = new CsvReader(textReader, CultureInfo.InvariantCulture);

            CsvHelperUtils.Config(errors, csv, false);
            csv.Configuration.RegisterClassMap <WildlifeRptInitCsvDtoMap>();

            var serviceAreastrings = ConvertServiceAreaToStrings(submission.ServiceAreaNumber);
            var serviceArea        = await _saService.GetServiceAreaByServiceAreaNumberAsyc(submission.ServiceAreaNumber);

            var headerValidated = false;
            var rows            = new List <WildlifeRptInitCsvDto>();

            while (csv.Read())
            {
                WildlifeRptInitCsvDto row = null;

                try
                {
                    row = csv.GetRecord <WildlifeRptInitCsvDto>();

                    if (!headerValidated)
                    {
                        if (!CheckCommonMandatoryFields(csv.Context.HeaderRecord, WildlifeReportHeaders.MandatoryFields, errors))
                        {
                            return(false);
                        }
                        else
                        {
                            headerValidated = true;
                        }
                    }

                    row.ServiceArea = serviceArea.ConvertToServiceAreaString(row.ServiceArea);
                    rows.Add(row);
                }
                catch (TypeConverterException ex)
                {
                    errors.AddItem(ex.MemberMapData.Member.Name, ex.Message);
                    break;
                }
                catch (CsvHelper.MissingFieldException)
                {
                    break; //handled in CsvHelperUtils
                }
                catch (CsvHelper.ReaderException ex)
                {
                    _logger.LogWarning(ex.Message);
                    errors.AddItem("Report Type", "Please make sure the report type selected is correct.");
                    return(false);
                }
                catch (CsvHelperException ex)
                {
                    _logger.LogInformation(ex.ToString());
                    throw;
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.ToString());
                    throw;
                }

                if (!serviceAreastrings.Contains(row.ServiceArea))
                {
                    errors.AddItem("ServiceArea", $"The file contains service area which is not {serviceAreastrings[0]}.");
                    return(false);
                }

                var line = csv.Context.RawRecord.RemoveLineBreak();

                submission.SubmissionRows.Add(new SubmissionRowDto
                {
                    RecordNumber = null,
                    RowValue     = line,
                    RowHash      = line.GetSha256Hash(),
                    RowStatusId  = _statusService.RowReceived,
                    EndDate      = row.AccidentDate ?? Constants.MinDate,
                    RowNum       = csv.Context.Row
                });
            }

            if (errors.Count == 0)
            {
                Validate(rows, Entities.WildlifeReportInit, errors);
            }

            return(errors.Count == 0);
        }