Exemplo n.º 1
0
        /// <summary>
        /// Performs one time initialization of the intake source(s), e.g. open input file(s).
        /// This method is only called when the InitErrorOccurred property is accessed for the first time.
        /// </summary>
        /// <returns>true if initialization successful; false if not, e.g. file not found</returns>
        private bool InitIntake()
        {
            var skipRepeatedHeader  = _config.InputDataKind.CanHaveHeaderRow() && _config.HeadersInFirstInputRow && _config.InputHeadersRepeated;
            var x12SegmentDelimiter = _config.InputDataKind == KindOfTextData.X12 ? _config.DefaultX12SegmentDelimiter ?? "~" : null; //note that default delimiter (~) is unlikely to be used as it get overwritten by pos 106 of ISA segment

            // Note that in order for the exceptions to be caught in the try blocks below, CreateLineFeeder (or IntakeInitializer as applicable) processing
            // must be eagerly evaluated (no deferred execution).
            if (_intakeFromReader)
            {
                try
                {
                    //In case both IntakeReaders and InputFileNames are present, then IntakeReaders wins
                    _intakeReader = _config.IntakeReaders == null?LineFeederCreator.CreateLineFeeder(_config.InputFileNames.ToListOfStrings('|'), _config.InputDataKind, _config.AsyncIntake, skipRepeatedHeader, x12SegmentDelimiter, _config.XmlJsonIntakeSettings)
                                        : LineFeederCreator.CreateLineFeeder(_config.IntakeReaders(), _config.InputDataKind, _config.AsyncIntake, skipRepeatedHeader, x12SegmentDelimiter, _config.XmlJsonIntakeSettings);
                }
                catch (Exception ex)
                {
                    //something went wrong with input file(s) or text reader(s), there is not much we can do other than to report it
                    var errMsg = _config.IntakeReaders == null ? $"Attempt to access input file(s) '{_config.InputFileNames}' failed."
                                                          : "Attempt to open text reader(s) failed.";
                    _config.Logger.LogFatal(errMsg, ex);
                    return(false);
                }
                return(true);
            }
            else //intake from IntakeSupplier (or IntakeSupplierAsync) function
            {
                string errMsg;
                try
                {
                    errMsg = _config.IntakeInitializer(_globalCache);
                }
                catch (Exception ex)
                {
                    _config.Logger.LogFatal("Intake initializer threw exception.", ex);
                    return(false);
                }
                if (errMsg != null)
                {
                    _config.Logger.LogFatal("Intake initializer failed:\r\n" + errMsg);
                    return(false);
                }
                return(true);
            }
        }