Ejemplo n.º 1
0
        /// <summary>
        /// Return next EDI transaction set.
        /// IFatpipeDocument will contain ISA, GA and 1 ST segment
        /// </summary>
        /// <returns></returns>
        public IFatpipeDocument GetNextTransactionSet()
        {
            if (DocumentReader == null)
            {
                throw new EDIReaderException("Initialize API should be invoked successfully before calling GetNextTransactionSet");
            }

            string    location = "EDIReader.GetNextTransactionSet";
            Stopwatch sw       = new Stopwatch();

            sw.Start();

            Logger.Debug(location, "Start - {0}", GetCurrentPosContext());

            bool isTransactionSetFinished = false;

            while (isTransactionSetFinished == false &&
                   (CurrentSegmentDetails = ReadNextSegment()) != null &&
                   CurrentSegmentDetails.Length > 0)
            {
                Logger.Debug(location, "{0} - Next segment {1}", GetCurrentPosContext(), CurrentSegmentDetails[0]);

                EDIState nextState = GetEDIState(CurrentSegmentDetails[0]);

                switch (nextState)
                {
                case EDIState.GS:
                    if (!(LastState == EDIState.ISA || LastState == EDIState.GE))
                    {
                        throw new EDIReaderException(string.Format("GS segment should appear only after ISA or GE. {1}", GetCurrentPosContext()));
                    }

                    //Just set GSRecordFields, GS node will be constructed on first ST node
                    GSRecordFields     = CurrentSegmentDetails;
                    GSSegmentProcessed = false;

                    GSSegment = CurrentSegment;
                    FunctionalGroupNumber++;

                    // TODO: What is functionalId and ControlNumber? - set second and third parameter below
                    Errors.AddFunctionalGroupDetails(FunctionalGroupNumber, "", "", true);
                    break;

                case EDIState.GE:
                    // TODO: Remove GS check here in case empty GS segment is not valid scenario
                    if (!(LastState == EDIState.GS || LastState == EDIState.SE))
                    {
                        throw new EDIReaderException(string.Format("GE segment should appear only after GS or SE. {1}", GetCurrentPosContext()));
                    }

                    //TODO: Add validation of GE segment details values
                    //CheckForMissingSegments(CurrentSegmentDetails[0]);
                    break;

                case EDIState.ST:
                    if (!(LastState == EDIState.GS || LastState == EDIState.SE))
                    {
                        throw new EDIReaderException(string.Format("ST segment should appear only after GS or SE. {1}", GetCurrentPosContext()));
                    }

                    // Do not reset SegmentNumber as it's being used as over all segment number in EdiValidator for position
                    //SegmentNumber = 0;
                    TransactionSetNumber++;

                    // TODO: What is functionalId and ControlNumber? - set second and third parameter below
                    Errors.AddTransactionSetDetails(TransactionSetNumber, "", "", true);

                    FatpipeDocumentInst.BeautifiedOriginalPayloadBody = string.Empty;
                    TransactionSegment          = CurrentSegment;
                    FormattedTransactionSegment = CurrentSegment;

                    // If ST segment is not valid then move file reading pointer to SE segment
                    if (ProcessSTSegment() == false)
                    {
                        MoveToSESegment();
                        isTransactionSetFinished = true;
                        nextState = EDIState.SE;
                    }

                    break;

                case EDIState.SE:
                    if (!(LastState == EDIState.ST || LastState == EDIState.Other))
                    {
                        throw new EDIReaderException(string.Format("{0} segment should appear only after ST or Other. {1}", LastState, GetCurrentPosContext()));
                    }

                    //CheckForMissingSegments(CurrentSegmentDetails[0]);
                    CreateAndAddNewSegment(CurrentSegmentDetails[0], CurrentSegmentDetails);

                    //TODO: Add validation of SE segment details values

                    isTransactionSetFinished = true;

                    TransactionSegment          = TransactionSegment + SegmentDelimiter + CurrentSegment;
                    FormattedTransactionSegment = FormattedTransactionSegment + FormattedSegmentDelimiter + CurrentSegment;

                    //ValidateContingencies();
                    break;

                case EDIState.Other:
                    if (!(LastState == EDIState.ST || LastState == EDIState.Other))
                    {
                        throw new EDIReaderException(string.Format("{0} segment should appear only after ST or Other. {1}", LastState, GetCurrentPosContext()));
                    }

                    //Console.WriteLine("segment = " + CurrentSegmentDetails[0]);
                    CreateAndAddNewSegment(CurrentSegmentDetails[0], CurrentSegmentDetails);

                    TransactionSegment          = TransactionSegment + SegmentDelimiter + CurrentSegment;
                    FormattedTransactionSegment = FormattedTransactionSegment + FormattedSegmentDelimiter + CurrentSegment;
                    break;

                case EDIState.IEA:

                    //TODO: Add validation of IEA segment details values
                    break;
                }

                LastState = nextState;
            }

            // Construct original payload
            if (isTransactionSetFinished == true)
            {
                FatpipeDocumentInst.OriginalPayload  = ISASegment + SegmentDelimiter;
                FatpipeDocumentInst.OriginalPayload += GSSegment + SegmentDelimiter;
                FatpipeDocumentInst.OriginalPayload += TransactionSegment + SegmentDelimiter;
                FatpipeDocumentInst.OriginalPayload += "GE" + EDIDelimiters.FieldSeperator + "1" + EDIDelimiters.FieldSeperator + "1" + SegmentDelimiter;
                FatpipeDocumentInst.OriginalPayload += "IEA" + EDIDelimiters.FieldSeperator + "1" + EDIDelimiters.FieldSeperator + "1" + SegmentDelimiter;

                FatpipeDocumentInst.BeautifiedOriginalPayloadStartHeader  = ISASegment + FormattedSegmentDelimiter;
                FatpipeDocumentInst.BeautifiedOriginalPayloadStartHeader += GSSegment + FormattedSegmentDelimiter;
                FatpipeDocumentInst.BeautifiedOriginalPayloadBody        += FormattedTransactionSegment + FormattedSegmentDelimiter;
                FatpipeDocumentInst.BeautifiedOriginalPayloadEndHeader    = "GE" + EDIDelimiters.FieldSeperator + "1" + EDIDelimiters.FieldSeperator + "1" + FormattedSegmentDelimiter;
                FatpipeDocumentInst.BeautifiedOriginalPayloadEndHeader   += "IEA" + EDIDelimiters.FieldSeperator + "1" + EDIDelimiters.FieldSeperator + "1" + FormattedSegmentDelimiter;
            }

            sw.Stop();
            Logger.Debug(location, "Stop - {0}. Elapsed time {1} ms", GetCurrentPosContext(), sw.ElapsedMilliseconds);

            return(isTransactionSetFinished == true ? FatpipeDocumentInst : null);
        }