/// <summary> /// Converts xml edi into message context /// </summary> /// <param name="message"> /// The xml edi. /// </param> /// <returns> /// The message context. /// </returns> private Tuple <string, string, string, EdiFormats> ToContextX12(XElement message) { XNamespace ns = message.Name.NamespaceName; var st = message.Element(ns + "S_" + EdiSegments.St); if (st == null) { throw new ParserException("Can't find ST segment."); } if (message.Parent == null || message.Parent.Parent == null || message.Parent.Parent.Parent == null || message.Parent.Parent.Parent.Parent == null) { throw new ParserException("Can't find GROUP segment."); } var gs = message.Parent.Parent.Parent.Parent.Element(ns + "S_" + EdiSegments.Gs); if (gs == null) { throw new ParserException("Can't find GS segment."); } var tag = st.Element(ns + EdiElements.StTag) ?? st.Element(ns + EdiElements.StTagHipaa); if (tag == null) { throw new ParserException("Can't find " + EdiElements.StTag); } var version = gs.Element(ns + EdiElements.GsVersion); if (version == null) { throw new ParserException("Can't find " + EdiElements.GsVersion); } if (version.Value.Length < 6) { throw new ParserException("Version length should be at least 6."); } var versionString = version.Value.Substring(0, 6); string origin = null; if (version.Value.Count() > 10) { origin = version.Value.Substring(6, 4); } var format = HipaaHelper.IsHipaa(versionString + origin) ? EdiFormats.Hipaa : EdiFormats.X12; return(new Tuple <string, string, string, EdiFormats>(tag.Value, versionString, origin, format)); }
/// <summary> /// Converts EDI interchange headers into message context. /// </summary> /// <param name="envelopes">The interchange headers.</param> /// <param name="interchangeContext">The interchange context.</param> private void ToContextX12(IEnumerable <string> envelopes, InterchangeContext interchangeContext) { var enumerable = envelopes as List <string> ?? envelopes.ToList(); string version; string origin; string format; string tag; try { var gs = enumerable.Single(es => es.StartsWith(EdiSegments.Gs)); var ediCompositeDataElementsGs = EdiHelper.GetEdiCompositeDataElements(gs, interchangeContext); version = ediCompositeDataElementsGs[7].Substring(0, 6); origin = ediCompositeDataElementsGs[7].Count() >= 10 ? ediCompositeDataElementsGs[7].Substring(6, 4) : null; format = HipaaHelper.IsHipaa(version + origin) ? EdiFormats.Hipaa.ToString() : EdiFormats.X12.ToString(); if (!HipaaHelper.IsHipaa(version + origin)) { origin = null; } } catch (Exception ex) { throw new ParserException("Can't parse GS segment.", ex); } try { var st = enumerable.Single(es => es.StartsWith(EdiSegments.St)); var ediCompositeDataElementsSt = EdiHelper.GetEdiCompositeDataElements(st, interchangeContext); tag = ediCompositeDataElementsSt[0]; } catch (Exception ex) { throw new ParserException("Can't parse ST segment.", ex); } Tag = tag; Version = version; Origin = origin; Format = ToFormat(format); }
/// <summary> /// Converts XML EDI into message context. /// </summary> /// <param name="message"> /// The XML EDI. /// </param> private void ToContextX12(XElement message) { XNamespace ns = message.Name.NamespaceName; var st = message.Element(ns + "S_" + EdiSegments.St); if (st == null) { throw new ParserException("Can't find ST segment."); } if (message.Parent == null || message.Parent.Parent == null || message.Parent.Parent.Parent == null || message.Parent.Parent.Parent.Parent == null) { throw new ParserException("Can't find GROUP segment."); } var gs = message.Parent.Parent.Parent.Parent.Element(ns + "S_" + EdiSegments.Gs); if (gs == null) { throw new ParserException("Can't find GS segment."); } XElement tag = null; if (st.Descendants().Any(x => x.Name.LocalName.IndexOf(EdiElements.StTag, StringComparison.Ordinal) != -1)) { tag = st.Descendants().First(x => x.Name.LocalName.IndexOf(EdiElements.StTag, StringComparison.Ordinal) != -1); } else if (st.Descendants().Any(x => x.Name.LocalName.IndexOf(EdiElements.StTagHipaa, StringComparison.Ordinal) != -1)) { tag = st.Descendants().First(x => x.Name.LocalName.IndexOf(EdiElements.StTagHipaa, StringComparison.Ordinal) != -1); } if (tag == null) { throw new ParserException("Can't find " + EdiElements.StTag); } var version = gs.Element(ns + EdiElements.GsVersion); if (version == null) { throw new ParserException("Can't find " + EdiElements.GsVersion); } if (version.Value.Length < 6) { throw new ParserException("Version length should be at least 6."); } var versionString = version.Value.Substring(0, 6); string origin = null; if (version.Value.Count() >= 10) { origin = version.Value.Substring(6, 4); } var format = HipaaHelper.IsHipaa(versionString + origin) ? EdiFormats.Hipaa : EdiFormats.X12; Tag = tag.Value; Version = versionString; Origin = origin; Format = format; }