2013-05-13 43 views
2

我有一個SIU S12 message不包含PV2段。但是,當我從NHAPI獲得解析的消息時,PV2的父組SIU_S12_PATIENT組返回1,表示存在PV2,即currentReps ("PV2")如何通過NHAPI知道段是否實際存在於HL7消息中?

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
,我發現這樣做

最簡單的辦法是,以確定是否一個段是在消息是搜索消息的實際字符串爲段名稱加一個管道。因此,例如

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會返回一個空字符串。

+0

PiperParser不返回空字符串,但返回段名稱。但是,仍然可以用來檢查段是否爲空,謝謝。 – 2013-07-12 22:18:35

1

您可以逐行讀取段到文件並添加hl7記錄對象和檢查段是否存在。

package [email protected];

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 [email protected]; 

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; 
    } 
} 
相關問題