/// <summary> /// Sets the data type of field 5 in the given OBX segment to the value of OBX-2. The argument /// is a Segment as opposed to a particular OBX because it is meant to work with any version. /// <para> /// Note that if no value is present in OBX-2, or an invalid value is present in /// OBX-2, this method will throw an error.This behaviour can be corrected by using the /// <see cref="ParserOptions.DefaultObx2Type"/> and <see cref="ParserOptions.InvalidObx2Type"/>. /// </para> /// </summary> /// <param name="segment"><see cref="ISegment"/> instance.</param> /// <param name="factory"><see cref="IModelClassFactory"/> to be used.</param> /// <param name="parserOptions"><see cref="ParserOptions"/> to be used.</param> /// <exception cref="HL7Exception">If no value is present in OBX-2.</exception> /// <exception cref="HL7Exception">If an invalid value is present in OBX-2.</exception> public static void FixOBX5(ISegment segment, IModelClassFactory factory, ParserOptions parserOptions) { try { // get unqualified class name var obx2 = (IPrimitive)segment.GetField(2, 0); foreach (var repetition in segment.GetField(5)) { var v = (Varies)repetition; SetObx2Fallback(obx2, segment, parserOptions); if (obx2.Value == null) { if (v.Data != null) { if (!(v.Data is IPrimitive) || ((IPrimitive)v.Data).Value != null) { throw new HL7Exception( "OBX-5 is valued, but OBX-2 is not. A datatype for OBX-5 must be specified using OBX-2.", ErrorCode.REQUIRED_FIELD_MISSING); } } } else { UseDTInsteadOfDTMForEarlierVersionsOfHL7(segment, obx2); var type = GetObx5Type(obx2, segment, factory, parserOptions); if (type == null) { var obx1 = (IPrimitive)segment.GetField(1, 0); var hl7Exception = new HL7Exception( $"'{obx2.Value}' in record {obx1.Value} is invalid for version {segment.Message.Version}"); hl7Exception.SegmentName = ((AbstractSegment)segment).GetStructureName(); hl7Exception.FieldPosition = 2; throw hl7Exception; } try { var constructor = type.GetConstructor(new[] { typeof(IMessage), typeof(string) }); v.Data = (IType)constructor.Invoke(new object[] { v.Message, v.Description }); } catch (NullReferenceException) { var constructor = type.GetConstructor(new[] { typeof(IMessage) }); v.Data = (IType)constructor.Invoke(new object[] { v.Message }); } } } } catch (HL7Exception e) { throw e; } catch (Exception e) { throw new HL7Exception( $"{e.GetType().FullName} trying to set data type of OBX-5", ErrorCode.APPLICATION_INTERNAL_ERROR, e); } }
private ParserResult SystemExceptionHandler(Exception se, string messageString) { if (string.IsNullOrEmpty(messageString)) { return(new ParserResult(null, null, false, null, se.Message)); } //Do not handle configuration Exceptions if (se is ConfigurationException) { throw (ConfigurationException)se; } if (typeof(System.Configuration.ConfigurationException).IsAssignableFrom(se.GetType())) { throw (ConfigurationException)se; } _errorMessage.AppendLine(se.Message); if (se.InnerException != null) { _errorMessage.AppendLine(se.InnerException.Message); } string messageType = null; string version = null; ISegment criticalSegment = null; try { criticalSegment = TryToRecoverCriticalDataFromMessage(messageString); if (criticalSegment == null) { version = TryToRecoverTheVersion(messageString); messageType = TryTorecoverTheMessageType(messageString); } else { version = Terser.Get(criticalSegment, 12, 0, 1, 1); messageType = $"{Terser.Get(criticalSegment, 9, 0, 1, 1)}" + $"_{Terser.Get(criticalSegment, 9, 0, 2, 1)}"; } if (messageType == null) { return(new ParserResult(null, null, false, null, _errorMessage.ToString())); } //the messageType is not null! if (version == null) { version = "2.5.1"; } string responseType = HL7Parser.GetMessageIDFromMessageType(messageType, version); if (responseType == null) { //the incoming message is an ack, the acknowledgment should not be aknoledged return(new ParserResult(null, null, false, true, _errorMessage.ToString())); } hl7Ack = hl7Parser.InstantiateMessage(responseType, version); ISegment err = (ISegment)hl7Ack.Message.GetStructure("ERR", 0); if ((se.InnerException != null) && typeof(HL7Exception).IsAssignableFrom(se.InnerException.GetType())) { HL7Exception he = (HL7Exception)se.InnerException; he.populate(err); } //if (typeof(HL7Exception).IsAssignableFrom(se.GetType())) //{ // HL7Exception he = (HL7Exception)se; // he.populate(err); // Debug.Print((new PipeParser()).Encode(ack)); //} SetACK(AckTypes.AR, messageString); } catch (Exception e) { _errorMessage.AppendLine(e.Message); return(new ParserResult(parsedMessage, hl7Ack, false, null, _errorMessage.ToString())); } return(new ParserResult(parsedMessage, hl7Ack, false, parsedMessage.IsAcknowledge, _errorMessage.ToString())); }