コード例 #1
0
        private static IRFFormatLoader GetLoader(RFReportParserFormat format, RFReportParserConfig config)
        {
            switch (format)
            {
            case RFReportParserFormat.CSV:
                return(new RIFF.Interfaces.Formats.CSV.CSVLoader(config.Encoding, config.DateFormat, config.SkipRows, config.Separator));

            case RFReportParserFormat.ExcelXLS:
                return(new RIFF.Interfaces.Formats.XLS.XLSLoader(config.Password));

            case RFReportParserFormat.ExcelXLSX:
                return(new RIFF.Interfaces.Formats.XLSX.XLSXLoader(config.Password));

            default:
                throw new RFSystemException(typeof(RFReportParserProcessor), "Unsupported file format for report parser: {0}", format.ToString());
            }
        }
コード例 #2
0
 public static RFRawReport LoadFromFile(string filePath, RFDate?valueDate, RFReportParserConfig config, IRFReportBuilder builder = null)
 {
     using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
     {
         using (var ms = new MemoryStream())
         {
             fs.CopyTo(ms);
             ms.Seek(0, SeekOrigin.Begin);
             return(LoadFromStream(ms, new RFFileTrackedAttributes
             {
                 FileName = Path.GetFileName(filePath),
                 ModifiedDate = File.GetLastWriteTime(filePath),
                 FullPath = filePath,
                 FileSize = new FileInfo(filePath).Length
             },
                                   valueDate, config, builder ?? new RFSimpleReportBuilder()));
         }
     }
 }
コード例 #3
0
        public static RFRawReport LoadFromStream(MemoryStream stream, RFFileTrackedAttributes attributes, RFDate?valueDate, RFReportParserConfig config, IRFReportBuilder builder)
        {
            RFReportParserFormat actualFormat = config.Format;

            if (config.Format == RFReportParserFormat.AutoDetect && config.CustomLoader == null)
            {
                var extension = Path.GetExtension(attributes.FileName).ToLower();
                if (!sExtensionFormats.TryGetValue(extension, out actualFormat))
                {
                    throw new RFSystemException(typeof(RFReportParserProcessor), "Unable to auto-detect file format of file {0}", attributes.FileName);
                }
            }

            var loader = config.CustomLoader ?? GetLoader(actualFormat, config);
            var tables = loader.Load(stream);

            if (tables == null || tables.Count == 0)
            {
                RFStatic.Log.Warning(typeof(RFReportParserProcessor), "No data loaded from file {0}", attributes.FileName);
                return(null);
            }

            var rawReport = new RFRawReportBuilder().BuildReport(tables, builder, config);

            if (rawReport == null)
            {
                RFStatic.Log.Warning(typeof(RFReportParserProcessor), "No data extracted from file {0}", attributes.FileName);
                return(null);
            }

            if (valueDate.HasValue) // override
            {
                rawReport.ValueDate = valueDate.Value;
            }
            else
            {
                rawReport.ValueDate = builder.ExtractValueDate(attributes, rawReport);
            }
            if (rawReport.ValueDate == RFDate.NullDate)
            {
                throw new RFLogicException(typeof(RFReportParserProcessor), "Unable to derive value date for file {0}.", attributes.FileName);
            }
            rawReport.ReportCode = config.ReportCode;
            rawReport.UpdateTime = attributes.ModifiedDate;
            rawReport.PostDeserialize();

            if (config.ValidatorFunc != null)
            {
                var errorMessage = config.ValidatorFunc(rawReport);
                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    if (errorMessage == sIgnoreReport)
                    {
                        return(null);
                    }
                    throw new RFLogicException(typeof(RFReportParserProcessor), "Report validation failed - incorrect file? ({0})", errorMessage);
                }
            }

            if (config.RequiredColumns != null && config.RequiredColumns.Any())
            {
                var cols           = new SortedSet <string>(rawReport.GetFirstSection().Columns);
                var missingColumns = config.RequiredColumns.Where(rc => !cols.Contains(rc));
                if (missingColumns.Any())
                {
                    throw new RFLogicException(typeof(RFReportParserProcessor), "Missing {0} mandatory columns - incorrect file? ({1})", missingColumns.Count(), string.Join(",", missingColumns));
                }
            }
            return(rawReport);
        }
コード例 #4
0
 public RFReportParserProcessor(RFReportParserConfig config, IRFReportBuilder builder)
 {
     _config  = config;
     _builder = builder;
 }
コード例 #5
0
        public RFRawReport BuildReport(List <DataTable> tables, IRFReportBuilder builder, RFReportParserConfig config)
        {
            _report         = new RFRawReport();
            _currentSection = null;
            _parentSection  = null;
            _builder        = builder;

            foreach (var table in tables)
            {
                _parentSection  = table.TableName;
                _currentSection = new RFRawReportSection
                {
                    Name    = String.Format("{0}.{1}", _parentSection, HEADER_SECTION_NAME),
                    Columns = new List <string>()
                };
                _report.Sections.Add(_currentSection);
                bool isFirstRow = true;
                foreach (DataRow row in table.Rows)
                {
                    ProcessRow(row, isFirstRow, config.HasHeaders);
                    isFirstRow = false;
                }
            }

            return(_report);
        }