Ejemplo n.º 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());
            }
        }
Ejemplo n.º 2
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);
        }