private string TryToRecoverTheVersion(string messageString) { String version = null; String wholeField12; try { String[] fields = PipeParser.Split(messageString.Substring(0, (Math.Max(messageString.IndexOf("\r"), messageString.Length)) - (0)), Convert.ToString(enchars.FieldSeparator)); wholeField12 = fields[11]; //message structure is component 3 but we'll accept a composite of 1 and 2 if there is no component 3 ... // if component 1 is ACK, then the structure is ACK regardless of component 2 String[] comps = PipeParser.Split(wholeField12, Convert.ToString(enchars.ComponentSeparator)); if (comps.Length > 0) { //messageStructure = comps[2]; version = comps[0]; } else { StringBuilder buf = new StringBuilder("Can't determine message version from MSH-12: "); throw new HL7Exception(buf.ToString(), HL7Exception.UNSUPPORTED_VERSION_ID); } } catch (IndexOutOfRangeException e) { throw new HL7Exception("Can't find message structure (MSH-9-3): " + e.Message, HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } return(ParserBase.ValidVersion(version) ? version : null); }
private string TryTorecoverTheMessageType(string messageString) { EncodingCharacters ec = new EncodingCharacters('|', null); String messageStructure = null; bool explicityDefined = true; String wholeFieldNine; try { String[] fields = PipeParser.Split(messageString.Substring(0, (Math.Max(messageString.IndexOf("\r"), messageString.Length)) - (0)), Convert.ToString(ec.FieldSeparator)); wholeFieldNine = fields[8]; //message structure is component 3 but we'll accept a composite of 1 and 2 if there is no component 3 ... // if component 1 is ACK, then the structure is ACK regardless of component 2 String[] comps = PipeParser.Split(wholeFieldNine, Convert.ToString(ec.ComponentSeparator)); if (comps.Length >= 3) { //messageStructure = comps[2]; messageStructure = comps[0] + "_" + comps[1]; } else if (comps.Length > 0 && comps[0] != null && comps[0].Equals("ACK")) { messageStructure = "ACK"; } else if (comps.Length == 2) { explicityDefined = false; messageStructure = comps[0] + "_" + comps[1]; } /*else if (comps.length == 1 && comps[0] != null && comps[0].equals("ACK")) { * messageStructure = "ACK"; //it's common for people to only populate component 1 in an ACK msg * }*/ else { StringBuilder buf = new StringBuilder("Can't determine message structure from MSH-9: "); buf.Append(wholeFieldNine); if (comps.Length < 3) { buf.Append(" HINT: there are only "); buf.Append(comps.Length); buf.Append(" of 3 components present"); } throw new HL7Exception(buf.ToString(), HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } } catch (IndexOutOfRangeException e) { throw new HL7Exception("Can't find message structure (MSH-9-3): " + e.Message, HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } return(messageStructure); }
private ISegment TryToRecoverCriticalDataFromMessage(string message) { ISegment msh = null; if (!message.StartsWith("MSH|")) { return(null); } //try to get MSH segment int locStartMSH = message.IndexOf("MSH"); if (locStartMSH < 0) { return(null); } int locEndMSH = message.IndexOf('\r', locStartMSH + 1); if (locEndMSH < 0) { locEndMSH = message.Length; } String mshString = message.Substring(locStartMSH, (locEndMSH) - (locStartMSH)); //find out what the field separator is char fieldSep = mshString[3]; //get field array String[] fields = PipeParser.Split(mshString, Convert.ToString(fieldSep)); try { //parse required fields String encChars = fields[1]; char compSep = encChars[0]; String messControlID = fields[9]; String[] procIDComps = PipeParser.Split(fields[10], Convert.ToString(compSep)); //fill MSH segment String version = NHapi.Model.V251.Constants.VERSION; //default try { version = contextParser.GetVersion(message); } catch (Exception) { /* use the default */ } //IModelClassFactory factory = parser.Factory; //msh = new MSH(new NHapiBugsFixing.V251(factory), factory); //msh = new GenericSegment(new NHapiBugsFixing.GenericMessageVersion(version), "MSH"); Terser.Set(msh, 1, 0, 1, 1, Convert.ToString(fieldSep)); Terser.Set(msh, 2, 0, 1, 1, encChars); Terser.Set(msh, 10, 0, 1, 1, messControlID); Terser.Set(msh, 12, 0, 1, 1, version); string messageType = TryTorecoverTheMessageType(message); if (!string.IsNullOrEmpty(messageType)) { Terser.Set(msh, 9, 0, 1, 1, messageType.Split('_')[0]); if (messageType.Split('_').Length > 0) { Terser.Set(msh, 9, 0, 2, 1, messageType.Split('_')[1]); } } //Terser.Set(msh, 11, 0, 1, 1, procIDComps[0]); } catch (HL7Exception he) { _errorMessage.AppendLine(he.Message); return(null); } catch (System.Exception se) { _errorMessage.AppendLine(se.Message); return(null); } return(msh); }