public bool IsGroupStart1(ChoPeekEnumerator <ChoHL7Segment> segments) { var attr = GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order).FirstOrDefault(); if (attr == null) { return(false); } if (attr.IsGroup) { IChoHL7Group groupObj = attr.GetGroupObject(); return(groupObj.IsGroupStart(segments)); } else { if (segments.Peek == null) { return(false); } ChoHL7Segment segment1 = segments.Peek; return(segment1.TargetType == attr.Name); } }
private void AddOrUpdateGroupCache(IChoHL7Group group) { ((ChoHL7AbstractGroup)group).Validate(); if (!_groupCache.ContainsKey(group.GetType().Name)) { _groupCache.Add(group.GetType().Name, new List <IChoHL7Group>()); } _groupCache[group.GetType().Name].Add(group); }
public bool IsGroupStart(ChoPeekEnumerator <ChoHL7Segment> segments) { foreach (var attr in GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order)) { if (attr.IsGroup) { IChoHL7Group groupObj = attr.GetGroupObject(); if (groupObj.IsGroupStart(segments)) { return(true); } } else { if (segments.Peek == null) { return(false); } ChoHL7Segment segment1 = segments.Peek; if (segment1.TargetType == attr.Name) { return(true); } else { if (attr.Optional) { continue; } else { return(false); } } } } return(false); }
protected virtual bool Construct(ChoPeekEnumerator <ChoHL7Segment> segments) { //Verify first element is a required segment //var attr = GetType().GetCustomAttributes(true).OfType<ChoHL7ObjectAttribute>().OrderBy(a => a.Order).FirstOrDefault(); var hasRequiredSegment = GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().Where(s => !s.Optional).Any(); if (!hasRequiredSegment) { throw new ChoHL7Exception("Group must have at least one required segment."); } //Check for any duplicate orders var dupDict = GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().GroupBy(i => i.Order).Where(g => g.Count() > 1).ToDictionary(x => x.Key, y => y.Count()); if (dupDict.Count > 0) { throw new ChoHL7Exception("Found segments/group defined with [{1}] duplicate order(s) in '{0}'.".FormatString(GetType().Name, String.Join(",", dupDict.Keys.ToArray()))); } List <Attribute> attrs = new List <Attribute>(); LoadAttributes(GetType(), attrs); var outAttrs = attrs.OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order).ToArray(); foreach (var hl7Attr in GetType().GetCustomAttributes(true).OfType <ChoHL7ObjectAttribute>().OrderBy(a => a.Order)) { if (hl7Attr.IsGroup) { while (true) { ChoHL7AbstractMessage groupObj = Activator.CreateInstance(hl7Attr.GroupOrSegmentType) as ChoHL7AbstractMessage; if (groupObj == null) { throw new ChoHL7Exception("'{0}' is not a group object.".FormatString(hl7Attr.GroupOrSegmentType.FullName)); } IChoHL7Group group = groupObj as IChoHL7Group; if (group.IsGroupStart(segments)) { if (groupObj.Construct(segments)) { AddOrUpdateGroupCache(group); //repeatable if (!hl7Attr.Repeatable) { break; } } else { } } else { break; } } } else { string segmmentType = hl7Attr.Name; ChoHL7Segment segment1 = segments.Peek; if (segment1 == null) { return(true); } if (!hl7Attr.Optional && segment1.TargetType != segmmentType) { throw new ChoHL7Exception("Missing '{0}' segment in '{1}' message.".FormatString(segmmentType, GetType().Name)); } else { if (segment1.TargetType == segmmentType) { AddOrUpdateSegmentCache(segment1, hl7Attr.Repeatable); segments.MoveNext(); //repeatable if (hl7Attr.Repeatable) { bool canContinue = false; while (segments.Peek != null) { segment1 = segments.Peek; if (segment1.TargetType == segmmentType) { AddOrUpdateSegmentCache(segment1, hl7Attr.Repeatable); segments.MoveNext(); } else { canContinue = true; break; } } if (!canContinue) { return(true); } } } else { } } } } return(true); }