Ejemplo n.º 1
0
        /// <summary>
        /// Initialize EDIReader with a stream. Verify that stream contains EDI document (ISA segment).
        /// </summary>
        /// <param name="stream"></param>
        /// <returns>true if EDI document otherwise false</returns>
        public bool Initialize(Stream stream, IFatpipeManager fatpipeManager)
        {
            Logger.Info("EDIReader.Initialize", "Start");

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            FPManager = fatpipeManager;

            long orgStartPos = stream.Position;

            Delimiters           delimiters;
            InterchangeTokenizer tokenizer = new InterchangeTokenizer(stream);
            bool isValidEDIDocument        = tokenizer.IsX12Interchange(out delimiters);

            CurrentSegmentNumber     = 0;
            ValidTransactionSetCount = InvalidTransactionSetCount = 0;
            PrevTransactionSetType   = 0;

            DocumentReader = null;
            if (isValidEDIDocument == true)
            {
                EDIDelimiters = delimiters;
                //TODO: Review following logic
                //Read ISA field till component separator - Do not include component separator
                ISASegment = tokenizer.ISARecord.Substring(0, tokenizer.ISARecord.IndexOf((char)EDIDelimiters.ComponentSeperator) + 1);

                //TODO: Suraj: confirm this special case - last value as data element separator
                ISARecordFields = ISASegment.Split((char)EDIDelimiters.FieldSeperator);

                CurrentSegmentNumber = 1;

                this.DocumentReader = new StreamReader(stream, Encoding.UTF8);
                //TODO: Why is seek required here?
                this.DocumentReader.BaseStream.Seek(tokenizer.ISARecordLen + orgStartPos, SeekOrigin.Begin);
                FatpipeDocumentInst = new FatpipeDocument();

                SegmentDelimiter = ((char)EDIDelimiters.SegmentDelimiter).ToString();
            }
            else
            {
                ISASegment = string.Empty;
            }

            LastState = EDIState.ISA;

            sw.Stop();
            Logger.Info("EDIReader.Initialize", "Stop. Elapsed time {0} ms", sw.ElapsedMilliseconds);

            return(isValidEDIDocument);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initialize EDIReader with a stream. Verify that stream contains EDI document (ISA segment).
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fatpipeManager"></param>
        /// <param name="documentPlug"></param>
        /// <returns>true if EDI document otherwise false</returns>
        public bool Initialize(Stream stream, IFatpipeManager fatpipeManager, IDocumentPlug documentPlug)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            Errors = new InterchangeErrors();

            FPManager    = fatpipeManager;
            DocumentPlug = documentPlug;

            long orgStartPos = stream.Position;

            Delimiters           delimiters;
            InterchangeTokenizer tokenizer = new InterchangeTokenizer(stream);
            bool isValidEDIDocument        = tokenizer.IsX12Interchange(out delimiters);

            FunctionalGroupNumber    = TransactionSetNumber = SegmentNumberFromStart = SegmentNumber = 0;
            ValidTransactionSetCount = InvalidTransactionSetCount = 0;
            PrevTransactionSetType   = 0;

            DocumentReader = null;
            if (isValidEDIDocument == true)
            {
                EDIDelimiters = delimiters;
                //TODO: Review following logic
                //Read ISA field till component separator - Do not include component separator
                ISASegment = tokenizer.ISARecord.Substring(0, tokenizer.ISARecord.IndexOf((char)EDIDelimiters.ComponentSeperator) + 1);

                //TODO: Suraj: confirm this special case - last value as data element separator
                ISARecordFields = ISASegment.Split((char)EDIDelimiters.FieldSeperator);

                SegmentNumberFromStart = 1;

                stream.Position     = orgStartPos;
                this.DocumentReader = new StreamReader(stream, Encoding.UTF8);
                //TODO: Why is seek required here?
                this.CurrentSegmentStartPos = 0;
                this.CurrentSegmentEndPos   = 0;
                //this.CurrentSegmentEndPos = this.CurrentSegmentStartPos + tokenizer.ISARecordLen - 1;
                //this.DocumentReader.BaseStream.Seek(this.CurrentSegmentEndPos + basePosition, SeekOrigin.Begin);
                FatpipeDocumentInst = new FatpipeDocument();
                FatpipeDocumentInst.BeautifiedOriginalPayloadStartHeader = string.Empty;
                FatpipeDocumentInst.BeautifiedOriginalPayloadBody        = string.Empty;
                FatpipeDocumentInst.BeautifiedOriginalPayloadEndHeader   = string.Empty;

                SegmentDelimiter          = ((char)EDIDelimiters.SegmentDelimiter).ToString();
                FormattedSegmentDelimiter = SegmentDelimiter;

                bool crLFPresent = delimiters.SegmentDelimiter == Delimiters.CarriageReturn || delimiters.SegmentDelimiterSuffix1 == Delimiters.CarriageReturn;
                if (!crLFPresent)
                {
                    FormattedSegmentDelimiter = string.Format("{0}{1}", FormattedSegmentDelimiter, Environment.NewLine);
                }

                Logger.Info("EDIReader.Initialize", "EDIDelimiters: SegmentDelimiter={0}, FieldSeperator={1}, ComponentSeperator={2}", (char)EDIDelimiters.SegmentDelimiter, (char)EDIDelimiters.FieldSeperator, (char)EDIDelimiters.ComponentSeperator);
            }
            else //invalid document code path
            {
                this.CurrentSegmentStartPos = orgStartPos;
                this.CurrentSegmentEndPos   = orgStartPos + 3;
                Logger.Error("EDIReader.Initialize", EventId.EDIReaderInvalidDocument, "EDI document is not valid. Error: {0}", tokenizer.Error);

                Errors.AddGenericError("ISA", SchemaErrorCode.SchemaCode100EInvalidDocType, tokenizer.Error, SegmentNumber, this.CurrentSegmentStartPos, this.CurrentSegmentEndPos);

                StringBuilder sb         = new StringBuilder();
                int           errorIndex = 1;
                Errors.IsaIeaErrorList.WriteError(sb, ref errorIndex);

                ISASegment = string.Empty;
            }

            LastState = EDIState.ISA;

            sw.Stop();
            Logger.Debug("EDIReader.Initialize", "Elapsed time {0} ms", sw.ElapsedMilliseconds);

            return(isValidEDIDocument);
        }