Example #1
0
        private void ValidateContingencies()
        {
            // Assumption: If id data segment is not present then this function will not report error
            // as we must have already reported that data segment as missing segment earlier
            Dictionary <string, List <IDocumentFragment> > contingencyOccurances = new Dictionary <string, List <IDocumentFragment> >();

            ReadSegmentsWithIdDataTypeChildrens(contingencyOccurances, FatpipeDocumentInst.RootFragment);

            foreach (string path in contingencyOccurances.Keys)
            {
                // Select pluglet for current path
                // Selecting pluglet from the 1st documentFragment as pluglet will be
                // same for all documentFragments with same path
                IDocumentFragment documentFragment = contingencyOccurances[path][0];
                IPluglet          pluglet          = documentFragment.Pluglet;

                // Pluglet will point to segment, process all child with data type as X12_IdDataType
                // Filter out non-mandatory data type pluglets
                foreach (Pluglet child in pluglet.Children)
                {
                    if (child.PlugletType == PlugletType.Data && child.DataType is X12_IdDataType && child.IsMandatory == true)
                    {
                        List <string> presentValues = GetAllPresentValues(contingencyOccurances[path], child);

                        X12_IdDataType dataType = child.DataType as X12_IdDataType;
                        foreach (string allowedValue in dataType.AllowedValues.Keys)
                        {
                            Contingency contingencies = dataType.GetContingencies(allowedValue);

                            // TODO: Use Ignore flag at id value level

                            // If Id value does not have any contingency then segment with that value must exist
                            if (contingencies == null || contingencies.ContingencyValues.Count == 0)
                            {
                                if (presentValues.Contains(allowedValue) == false && dataType.IsOptionalValue(allowedValue) == false)
                                {
                                    Errors.AddSegmentError(pluglet.Tag, X12ErrorCode.DeMandatoryIdValueMissingCode
                                                           , string.Format("{0} : {1}", X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeMandatoryIdValueMissingCode), allowedValue)
                                                           , documentFragment.SequenceNumber, documentFragment.StartOffset, documentFragment.EndOffset, EdiErrorType.Error);
                                }
                            }
                            // If Id value has contingencies of type Enumeration then segment with that value or any contingency value must exist
                            else if (contingencies.Type == ContingencyType.Enumeration)
                            {
                                bool valuePresent = presentValues.Contains(allowedValue);
                                if (valuePresent == false)
                                {
                                    foreach (string alternateValue in contingencies.ContingencyValues)
                                    {
                                        valuePresent = presentValues.Contains(alternateValue);
                                        if (valuePresent)
                                        {
                                            break;
                                        }
                                    }
                                }

                                if (valuePresent == false)
                                {
                                    Errors.AddSegmentError(pluglet.Tag, X12ErrorCode.DeMandatoryIdValueOrAlternativeValueMissingCode
                                                           , string.Format("{0} : {1}", X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeMandatoryIdValueOrAlternativeValueMissingCode), allowedValue)
                                                           , documentFragment.SequenceNumber, documentFragment.StartOffset, documentFragment.EndOffset, EdiErrorType.Error);
                                }
                            }
                            // If contingency type is cross segment then either both values must exist or both values missing
                            else if (contingencies.Type == ContingencyType.CrossSegment)
                            {
                                // TODO: handle all values in contingencies.ContingencyValues
                                string xPath = contingencies.ContingencyValues[0];
                                bool   currentValuePresent      = presentValues.Contains(allowedValue);
                                bool   crossSegmentValuePresent = IsCrossSegmentValuePresent(contingencyOccurances, xPath, pluglet.PathSeperator);

                                if (currentValuePresent != crossSegmentValuePresent)
                                {
                                    Errors.AddSegmentError(pluglet.Tag, X12ErrorCode.DeCrossSegmentIdValueOccurancesDoesNotMatch
                                                           , string.Format("{0} : {1} {2}", X12ErrorCode.GetDataElementErrorDescription(X12ErrorCode.DeCrossSegmentIdValueOccurancesDoesNotMatch), allowedValue, xPath)
                                                           , documentFragment.SequenceNumber, documentFragment.StartOffset, documentFragment.EndOffset, EdiErrorType.Error);
                                }
                            }
                        }
                    }
                }
            }
        }