Exemplo n.º 1
0
        internal static List <SegmentCollection> ParseSegments(Hl7Encoding definedEncoding, List <string> allSegments)
        {
            var returnValue = new List <SegmentCollection>();

            if (allSegments == null)
            {
                return(returnValue);
            }
            short              ordinal         = 1;
            string             previous        = string.Empty;
            List <BaseSegment> currentSegments = new List <BaseSegment>();

            foreach (string strSegment in allSegments.Where(s => !string.IsNullOrWhiteSpace(s)))
            {
                BaseSegment newSegment = CreateSegment(strSegment, definedEncoding);
                previous = previous == string.Empty ? newSegment.Name : previous;
                if (previous != newSegment.Name)
                {
                    returnValue.Add(new SegmentCollection(previous, ordinal, currentSegments));
                    currentSegments = new List <BaseSegment>();
                    ordinal++;
                    previous = newSegment.Name;
                }
                currentSegments.Add(newSegment);
            }
            returnValue.Add(new SegmentCollection(previous, ordinal, currentSegments));
            return(returnValue);
        }
Exemplo n.º 2
0
        internal static List <BaseField> ParseFields(string value, Hl7Encoding encoding)
        {
            string name = GetSegmentName(value);
            IEnumerable <string> allFields = EncodingHelper.Split(value, encoding.FieldDelimiter);

            return(ParseFields(allFields, encoding, name == MSH));
        }
Exemplo n.º 3
0
 internal static BaseComponent CreateComponent(Hl7Encoding encoding, string value)
 {
     if (value.Contains(encoding.SubComponentDelimiter.ToString()))
     {
         return(new ComplexComponent(EncodingHelper.Split(value, encoding.SubComponentDelimiter).ConvertAll(s => new SubComponent(encoding.Decode(s), encoding)), encoding));
     }
     return(new SimpleComponent(value, encoding));
 }
Exemplo n.º 4
0
        public void ShouldBeParsingMessageWithOtherSegmentSeparator()
        {
            //Act
            var encoding = new Hl7Encoding {
                FieldSeparator = '$'
            };
            var segment = Hl7Segment.Parse(EvnSegmentDollarAsSeparatorTrimmed, encoding);

            //Assert
            Assert.Equal(EvnSegmentDollarAsSeparatorTrimmed, segment.ToString());
        }
Exemplo n.º 5
0
 internal static BaseField CreateField(Hl7Encoding encoding, string value)
 {
     if (value.Contains(encoding.RepeatDelimiter))
     {
         return(new RepetitionField(EncodingHelper.Split(value, encoding.RepeatDelimiter).ConvertAll(f => CreateField(encoding, f)), encoding));
     }
     if (value.Contains(encoding.ComponentDelimiter))
     {
         return(new ComponentField(EncodingHelper.Split(value, encoding.ComponentDelimiter).ConvertAll(c => CreateComponent(encoding, c)), encoding));
     }
     return(new ValueField(EncodingHelper.Decode(encoding, value), encoding));
 }
Exemplo n.º 6
0
        private static ValidationResult ValidateRequiredFields(Hl7Encoding encoding, string messageType, string controlId, string processingId, string versionId)
        {
            List <string> results = new List <string>();

            results.Add(encoding == null ? "encoding" : string.Empty);
            results.Add(string.IsNullOrWhiteSpace(messageType) ? "messageType" : string.Empty);
            results.Add(string.IsNullOrWhiteSpace(controlId) ? "controlId" : string.Empty);
            results.Add(string.IsNullOrWhiteSpace(processingId) ? "processingId" : string.Empty);
            results.Add(string.IsNullOrWhiteSpace(versionId) ? "versionId" : string.Empty);
            results.RemoveAll(r => string.IsNullOrWhiteSpace(r));
            return(new ValidationResult(string.Join(',', results)));
        }
Exemplo n.º 7
0
        public void ShouldUnescapeData()
        {
            // Arrange
            const string content = @"\T\\E\\H\\N\\F\\R\\S\\X0A\";

            // Act
            var result = new Hl7Encoding().Unescape(content);

            // Assert
            const string expectedResult = "&\\H\\\\N\\|~^\n";

            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 8
0
        public void ShouldEscapeData()
        {
            // Arrange
            const string content = "\r\n";

            // Act
            var result = new Hl7Encoding().EscapeAllCharacters(content);

            // Assert
            const string expectedResult = @"\X0D0A\";

            Assert.Equal(expectedResult, result);
        }
Exemplo n.º 9
0
        public void ShouldBeParsingMessageWithOtherSegmentSeparator()
        {
            //Arrange
            var encoding = new Hl7Encoding {
                SegmentSeparator = new[] { "$" }
            };
            var message = Hl7Message.Parse(AdtA01Message.Replace("\r\n", "$"), encoding);

            //Act
            var messageString = message.ToString();

            //Assert
            Assert.Equal(AdtA01MessageNoTrailingSeparators.Replace("\r\n", "$"), messageString);
        }
Exemplo n.º 10
0
 private static ValidationResult ValidateSegments(List <string> segments, Hl7Encoding encoding)
 {
     if (segments == null || segments.Count == 0 || segments[0].Count(f => f == encoding.FieldDelimiter) < 11)// Count field separators, MSH.12 is required so there should be at least 11 field separators in MSH
     {
         return(ErrorReturn("first segment (MSH) doesn't contain all the required fields"));
     }
     foreach (string strSegment in segments.Where(s => !string.IsNullOrWhiteSpace(s)))
     {
         if (!ValidSegmentName.IsMatch(SegmentHelper.GetSegmentName(strSegment)))
         {
             return(ErrorReturn($"Invalid segment name found: {strSegment}"));
         }
         if (encoding.FieldDelimiter != strSegment[3])
         {
             return(ErrorReturn($"Invalid segment found: {strSegment}"));
         }
     }
     return(new ValidationResult());
 }
Exemplo n.º 11
0
        internal const string AR  = "AR";  //NACK -- Application Reject - MSA-1 response code

        public static Result <Message> CreateBasicMessage(Hl7Encoding encoding, string messageType, string controlId, string processingId, string versionId, string?sendingApplication = null,
                                                          string?sendingFacility   = null, string?receivingApplication = null, string?receivingFacility = null, DateTime?messageDateTime = null, string?security     = null,
                                                          string?sequenceNumber    = null, string?continuationPointer  = null, string?acceptAckType     = null, string?countryCode       = null, string?characterSet = null,
                                                          string?principalLanguage = null)
        {
            if (ValidateRequiredFields(encoding, messageType, controlId, processingId, versionId).DecomposeResult(out string?missingFields))
            {
                return(ErrorReturn <Message>($"Missing required argument to create message required fields: {missingFields}"));
            }
            List <BaseField> fields = new List <BaseField>()
            {
                new DelimitersField(encoding),//required
                new ValueField(sendingApplication ?? string.Empty, encoding),
                new ValueField(sendingFacility ?? string.Empty, encoding),
                new ValueField(receivingApplication ?? string.Empty, encoding),
                new ValueField(receivingFacility ?? string.Empty, encoding),
                new ValueField(messageDateTime?.FormatDate(encoding) ?? string.Empty, encoding),
                new ValueField(security ?? string.Empty, encoding),
                new ValueField(messageType, encoding),  //required
                new ValueField(controlId, encoding),    //required
                new ValueField(processingId, encoding), //required
                new ValueField(versionId, encoding),    //required
                new ValueField(sequenceNumber ?? string.Empty, encoding),
                new ValueField(continuationPointer ?? string.Empty, encoding),
                new ValueField(acceptAckType ?? string.Empty, encoding),
                new ValueField(countryCode ?? string.Empty, encoding),
                new ValueField(characterSet ?? string.Empty, encoding),
                new ValueField(principalLanguage ?? string.Empty, encoding),
            };

            //remove trailing empties beyond versionid
            for (int i = fields.Count; i > 10 && string.IsNullOrEmpty(fields[i].GetRawValue()); i--)
            {
                fields.RemoveAt(i);
            }
            return(CreateMessageFromSegment(new Segment(encoding, SegmentHelper.MSH, fields)));
        }
Exemplo n.º 12
0
        internal static List <BaseField> ParseFields(IEnumerable <string?> allFields, Hl7Encoding encoding, bool includeEncoding = false, bool fieldsIncludesName = true, bool skipEncodingField = true)
        {
            List <BaseField> fields = new List <BaseField>();

            if (fieldsIncludesName && allFields.Count() > 1)
            {
                allFields = allFields.Skip(1);///skip the name
            }
            if (includeEncoding)
            {//handle the special delimiters field for MSH
                fields.Add(new DelimitersField(encoding));
                if (skipEncodingField)
                {
                    allFields = allFields.Skip(1);
                }
            }
            fields.AddRange(allFields.Where(f => f != null).Select(f => CreateField(encoding, f !)));
            return(fields);
        }
Exemplo n.º 13
0
 public ComplexComponent(IEnumerable <BaseSubComponent> subComponents, Hl7Encoding encoding) : base(encoding) => this.SubComponents = subComponents.ToImmutableList();
Exemplo n.º 14
0
 public ComponentField(IEnumerable <BaseComponent> components, Hl7Encoding encoding) : base(encoding) => this.Components = components.ToImmutableList();
Exemplo n.º 15
0
 public RepetitionField(IEnumerable <BaseField> repetitions, Hl7Encoding encoding) : base(encoding) => this.Repetitions = repetitions.ToImmutableList();
Exemplo n.º 16
0
 public static BaseSegment CreateSegment(string value, Hl7Encoding encoding) =>
 CreateSegment(encoding, SegmentHelper.GetSegmentName(value), value);
Exemplo n.º 17
0
 public static BaseSegment CreateSegment(Hl7Encoding encoding, string name, IEnumerable <BaseField> fields) =>
 new Segment(encoding, name, fields);
Exemplo n.º 18
0
 protected MessageElement(Hl7Encoding encoding) => this.Encoding = encoding;
Exemplo n.º 19
0
 public static Result <Message> CreateMessageFromSegmentString(Hl7Encoding encoding, string segmentString, string segmentName) =>
 CreateMessageFromSegment(CreateSegment(encoding, segmentName, segmentString));
Exemplo n.º 20
0
 public static Result <Message> CreateBasicMessage(string sendingApplication, string sendingFacility, string receivingApplication, string receivingFacility,
                                                   string security, string messageType, string messageControlId, string processingId, string version, Hl7Encoding encoding) =>
 CreateBasicMessage(encoding, messageType, messageControlId, processingId, version, sendingApplication, sendingFacility, receivingApplication,
                    receivingFacility, DateTime.UtcNow, security);
Exemplo n.º 21
0
 public static BaseSegment CreateSegment(Hl7Encoding encoding, string name, string value) =>
 CreateSegment(encoding, name, ParseFields(value, encoding));
Exemplo n.º 22
0
 private Hl7Component(string value, Hl7Encoding encoding)
 {
     Encoding = encoding;
     Value    = value;
 }