如何通过NHAPI判断某个段是否实际存在于HL7消息中?

时间:2013-05-13 20:11:54

标签: hl7 nhapi

我有SIU S12 message,不包含PV2细分。但是,当我从NHAPI获得解析的消息时,SIU_S12_PATIENT组的PV2的父组为currentReps ("PV2")返回1,这意味着PV2存在。

var parser = new NHapi.Base.Parser.PipeParser();
var parsedMessage = parser.Parse(message) as NHapi.Model.V231.Message.SIU_S12;
var patientGroup=parsedMessage.GetPATIENT(0);
// This call should not create the segment if it does not exist
int pv2Count=patientGroup.currentReps("PV2");
//pv2Count is 1 here despite no PV2 segment exists in the message
//also Both GetAll("PV2") and SegmentFinder say the PV2 segment is present
//DG1RepetitionsUsed is also 1 despite no DG1 segment is present in the message

我试图避免编写代码来评估细分中的每个字段。 PV2只是一个例子 - 消息源中可能缺少更多的段。

我正在使用最新版本的NHAPI v 2.4。

更新:按照泰森的建议,我想出了这个方法;

var parser = new NHapi.Base.Parser.PipeParser();
var parsedMessage = parser.Parse(message) as NHapi.Model.V231.Message.SIU_S12;
var encodingChars = new NHapi.Base.Parser.EncodingCharacters('|', null);
var patientGroup = parsedMessage.GetPATIENT(0);
var dg1 = (NHapi.Model.V231.Segment.DG1) (patientGroup.GetStructure("DG1"));
string encodedDg1 = NHapi.Base.Parser.PipeParser.Encode(dg1, encodingChars);
bool dg1Exists = string.Compare(encodedDg1, 
    "DG1", StringComparison.InvariantCultureIgnoreCase)==0;

2 个答案:

答案 0 :(得分:2)

我发现最简单的事情是确定一个段是否在消息中是为了搜索段名称加上管道的消息的实际字符串。所以,例如

    if(message.Contains("PV2|"))
    {
        //do something neat

    }

根据我的经验,它要么是检查片段下的每个子字段,要么检查是否存在值。

修改

我找到了另一种方法来检查可能会更好一些。 PipeParser类上有几个静态方法,它们接收ISegment,IGroup和IType对象,这些对象将返回对象NHapi reference的字符串表示。

示例代码:

        string validTestMessages =
      "MSH|^~\\&|ADT1|MCM|LABADT|MCM|198808181126|SECURITY|ADT^A01|MSG00001|P|2.6\r" +
      "EVN|A01-|198808181123\r" +
      "PID|||PID1234^5^M11^HBOC^CPI^HV||JONES^WILLIAM^A^III||19610615000000|M||2106-3|1200 N ELM STREET^^GREENSBORO^NC^27401-1020|GL||||S||S|123456789|9-87654^NC\r" +
      "PV1|1|I|||||TEST^TEST^TEST||||||||||||||||||||||||||||||||||||||||||||\r";

        var encodingChars = new EncodingCharacters('|', null);

        PipeParser parser = new PipeParser();

        var message = parser.Parse(validTestMessages);

        PV1 pv1 = (PV1)message.GetStructure("PV1");
        var doctor = pv1.GetAttendingDoctor(0);


        string encodedMessage = PipeParser.Encode(pv1, encodingChars);
        Console.WriteLine(encodedMessage);

        encodedMessage = PipeParser.Encode(doctor, encodingChars);
        Console.WriteLine(encodedMessage);

输出:

PV1|1|I|||||TEST^TEST^TEST
TEST^TEST^TEST

如果没有段或该项为空,则PiperParser将返回一个空字符串。

答案 1 :(得分:1)

您可以逐行读取一段文件并添加hl7记录对象并检查段是否存在。

package com.sachan.ranvijay@gmail.com.hl7.msg;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import org.nule.lighthl7lib.hl7.Hl7Record;
    import org.nule.lighthl7lib.hl7.Hl7Segment;
    import com.stpl.hl7.dto.HL7PatientInfoDTO;

    /**
     * This class will parse the hl7 message. it can accept message  file in the format of java.io.file
     * as well as String. Its Uses org.nule.lighthl7lib.hl7.Hl7Record 
     * as a main component.
     * @author Ranvijay.Singh
     *
     */
    public class PrepareHL7Message {
        StringBuilder hl7Msg = new StringBuilder();
        Hl7Record record = null;
        public PrepareHL7Message(File file) throws Exception {

            BufferedReader reader = new BufferedReader(
                    new FileReader(file));
            String str = reader.readLine();
            while (str != null) {
                hl7Msg.append(str).append("\r");
                str = reader.readLine();
            }
            reader.close();
            try{
             record = new Hl7Record(hl7Msg.toString());
            }catch (Exception e) {
                throw e; 
            }
        }

        public PrepareHL7Message(String msg) throws Exception {

            try{
             record = new Hl7Record(msg);
            }catch (Exception e) {
                throw e; 
            }
        }
    private HL7PatientInfoDTO getPatientOrderingPhysician(HL7PatientInfoDTO padto) {
        Hl7Segment seg = record.getSegment("PV1");
        if(seg!=null)
        padto.setOrderingPhysician(seg.field(7).toString());
        return padto;
    }
    }

//DTO.............

package com.sachan.ranvijay@gmail.com.hl7.dto;

public class HL7PatientInfoDTO {

    /**
     * maped with PV1-7
     */
    private String orderingPhysician;

    /**
     * @return the orderingPhysician
     */
    public String getOrderingPhysician() {
        return orderingPhysician;
    }

    /**
     * @param orderingPhysician the orderingPhysician to set
     */
    public void setOrderingPhysician(String orderingPhysician) {
        this.orderingPhysician = orderingPhysician;
    }
}