예제 #1
0
        /// <summary>
        /// Attempts to parse a container and validates it againsts its specification. A collection of <see cref="SegmentError"/> objects is returned
        /// </summary>
        /// <param name="container">Object to be validated</param>
        /// <returns>Collection of Segment errors, if found</returns>
        protected virtual IEnumerable <SegmentError> ValidateContainerAgainstSpec(ContainerInformation container)
        {
            var errors = new List <SegmentError>();

            foreach (var segmentSpec in container.Spec.SegmentSpecifications.Where(ss => ss.Usage == Usage.Required))
            {
                if (!container.Segments.Exists(s => s.SegmentId == segmentSpec.SegmentId))
                {
                    errors.Add(this.CreateSegmentError(
                                   new SegmentInformation
                    {
                        SegmentId       = segmentSpec.SegmentId,
                        LoopId          = container.Spec.LoopId,
                        SegmentPosition = container.Segments.Count > 0 ? container.Segments.First().SegmentPosition : 0
                    },
                                   "3"));
                }

                if (segmentSpec.Repeat > 0 && container.Segments.Count(s => s.SegmentId == segmentSpec.SegmentId) > segmentSpec.Repeat)
                {
                    errors.Add(
                        this.CreateSegmentError(
                            container.Segments.Last(s => s.SegmentId == segmentSpec.SegmentId),
                            "5"));
                }
            }

            foreach (var loopSpec in container.Spec.LoopSpecifications.Where(ls => ls.Usage == Usage.Required))
            {
                if (!container.Containers.Exists(c => c.Spec.LoopId == loopSpec.LoopId))
                {
                    errors.Add(
                        this.CreateSegmentError(
                            new SegmentInformation
                    {
                        SegmentId       = loopSpec.StartingSegment.SegmentId,
                        LoopId          = container.Spec.LoopId,
                        SegmentPosition = container.Segments.Count > 0 ? container.Segments.Last().SegmentPosition : 0
                    },
                            "I7"));
                }

                if (loopSpec.LoopRepeat > 0 && container.Containers.Count(c => c.Spec.LoopId == loopSpec.LoopId) > loopSpec.LoopRepeat)
                {
                    errors.Add(
                        this.CreateSegmentError(
                            container.Containers.Last(c => c.Spec.LoopId == loopSpec.LoopId).Segments.First(),
                            "4"));
                }
            }

            foreach (var childContainer in container.Containers)
            {
                errors.AddRange(this.ValidateContainerAgainstSpec(childContainer));
            }

            return(errors);
        }
예제 #2
0
        /// <summary>
        /// Builds a <see cref="TransactionSetResponse"/> object from the provided stream
        /// </summary>
        /// <param name="reader">Stream to pull transaction set data from</param>
        /// <param name="functionalCode">Function group code to associate with transaction set</param>
        /// <param name="versionIdentifierCode">Specification version code</param>
        /// <param name="transaction">Transaction segment string to be parsed</param>
        /// <returns>Transaction set response whether the set is valid, and the segment data</returns>
        protected virtual TransactionSetResponse AcknowledgeTransaction(X12StreamReader reader, string functionalCode, string versionIdentifierCode, string transaction)
        {
            string[] transactionElements = reader.SplitSegment(transaction);
            var      response            = new TransactionSetResponse
            {
                TransactionSetIdentifierCode = transactionElements[1],
                TransactionSetControlNumber  = transactionElements[2]
            };

            if (transactionElements.Length >= 4)
            {
                response.ImplementationConventionReference = transactionElements[3];
            }

            TransactionSpecification transactionSpec = this.specFinder.FindTransactionSpec(
                functionalCode,
                versionIdentifierCode,
                response.TransactionSetIdentifierCode);

            if (transactionSpec == null)
            {
                response.SyntaxErrorCodes.Add("1");
                response.AcknowledgmentCode = AcknowledgmentCode.R_Rejected;
                return(response);
            }

            var containers           = new Stack <ContainerInformation>();
            var transactionContainer = new ContainerInformation {
                Spec = transactionSpec
            };

            containers.Push(transactionContainer);
            var segmentInfos = new List <SegmentInformation>();

            string[] segments = transaction.Split(new[] { reader.Delimiters.SegmentTerminator }, System.StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < segments.Length; i++)
            {
                string[] elements    = segments[i].Split(reader.Delimiters.ElementSeparator);
                var      segmentInfo = new SegmentInformation {
                    SegmentId = elements[0], SegmentPosition = i + 1, Elements = elements
                };
                segmentInfo.Spec = this.specFinder.FindSegmentSpec(versionIdentifierCode, segmentInfo.SegmentId);
                segmentInfos.Add(segmentInfo);

                ContainerInformation currentContainer = containers.Peek();

                switch (segmentInfo.SegmentId)
                {
                case "ST":
                case "SE":
                    segmentInfo.LoopId = string.Empty;
                    transactionContainer.Segments.Add(segmentInfo);
                    break;

                case "HL":
                    string hloopNumber       = segmentInfo.Elements[1];
                    string hloopParentNumber = segmentInfo.Elements[2];
                    string hloopLevelCode    = segmentInfo.Elements[3];
                    HierarchicalLoopSpecification hloopSpec = transactionSpec.HierarchicalLoopSpecifications.FirstOrDefault(hls => hls.LevelCode == hloopLevelCode);
                    if (hloopSpec != null)
                    {
                        while (!(containers.Peek().Spec is TransactionSpecification))
                        {
                            if (containers.Peek().HLoopNumber == hloopParentNumber)
                            {
                                break;
                            }

                            containers.Pop();
                        }

                        segmentInfo.LoopId = hloopSpec.LoopId;
                        var hloopContainer = new ContainerInformation {
                            Spec = hloopSpec, HLoopNumber = hloopNumber
                        };
                        hloopContainer.Segments.Add(segmentInfo);
                        containers.Peek().Containers.Add(hloopContainer);
                        containers.Push(hloopContainer);
                    }
                    else
                    {
                        response.SegmentErrors.Add(this.CreateDataElementError(segmentInfo, 3, "I6", hloopLevelCode));
                        response.AcknowledgmentCode = AcknowledgmentCode.X_Rejected_ContentCouldNotBeAnalyzed;
                    }

                    break;

                default:
                    bool matchFound = false;
                    do
                    {
                        var matchingLoopSpecs = currentContainer.Spec.LoopSpecifications.Where(ls => ls.StartingSegment.SegmentId == segmentInfo.SegmentId).ToList();

                        if (matchingLoopSpecs.Count > 0)
                        {
                            IContainerSpecification matchingLoopSpec;
                            if (matchingLoopSpecs.Count == 1)
                            {
                                matchingLoopSpec = matchingLoopSpecs.First();
                            }
                            else
                            {
                                string entityCode = elements[1];
                                matchingLoopSpec = matchingLoopSpecs.FirstOrDefault(ls => ls.StartingSegment.EntityIdentifiers.Exists(ei => ei.Code == entityCode));
                            }

                            if (matchingLoopSpec != null)
                            {
                                segmentInfo.LoopId = matchingLoopSpec.LoopId;
                                var loopContainer = new ContainerInformation {
                                    Spec = matchingLoopSpec
                                };
                                loopContainer.Segments.Add(segmentInfo);
                                containers.Peek().Containers.Add(loopContainer);
                                containers.Push(loopContainer);
                                matchFound = true;
                            }
                            else
                            {
                                response.SegmentErrors.Add(this.CreateSegmentError(segmentInfo, "6"));
                                response.AcknowledgmentCode = AcknowledgmentCode.X_Rejected_ContentCouldNotBeAnalyzed;
                                return(response);
                            }
                        }
                        else if (currentContainer.Spec.SegmentSpecifications.Exists(ss => ss.SegmentId == segmentInfo.SegmentId))
                        {
                            segmentInfo.LoopId = currentContainer.Spec.LoopId;
                            currentContainer.Segments.Add(segmentInfo);
                            matchFound = true;
                        }
                        else
                        {
                            if (currentContainer.Spec is TransactionSpecification)
                            {
                                response.SegmentErrors.Add(this.CreateSegmentError(segmentInfo, "2"));
                                response.AcknowledgmentCode = AcknowledgmentCode.X_Rejected_ContentCouldNotBeAnalyzed;
                                return(response);
                            }

                            containers.Pop();
                            currentContainer = containers.Peek();
                        }
                    }while (!matchFound);

                    break;
                }

                response.SegmentErrors.AddRange(this.ValidateSegmentAgainstSpec(segmentInfo));
            }

            response.SegmentErrors.AddRange(this.ValidateContainerAgainstSpec(transactionContainer));

            var trailerSegment = segmentInfos.FirstOrDefault(si => si.SegmentId == "SE");

            if (trailerSegment == null)
            {
                response.SyntaxErrorCodes.Add("2");
            }
            else
            {
                if (trailerSegment.Elements.Length <= 2 || trailerSegment.Elements[2] != response.TransactionSetControlNumber)
                {
                    response.SyntaxErrorCodes.Add("3");
                }

                if (trailerSegment.Elements.Length >= 2)
                {
                    int segmentCount;
                    int.TryParse(trailerSegment.Elements[1], out segmentCount);
                    if (segmentCount != segmentInfos.Count)
                    {
                        response.SyntaxErrorCodes.Add("4");
                    }
                }
                else
                {
                    response.SyntaxErrorCodes.Add("4");
                }
            }

            if (response.SegmentErrors.Count > 0 || response.SyntaxErrorCodes.Count > 0)
            {
                if (response.SegmentErrors.Count > 0)
                {
                    response.SyntaxErrorCodes.Add("5");
                }

                if (response.AcknowledgmentCode == AcknowledgmentCode.A_Accepted)
                {
                    response.AcknowledgmentCode = AcknowledgmentCode.E_Accepted_ButErrorsWereNoted;
                }
            }

            return(response);
        }