当我第一次看到HL7格式的对接文档的时候,整个人是懵逼的,完全没接触过,经过几天的查看,稍微对HL7有一部分了解。对于无任何HL7对接背景的同学,可以先看一下文档:saravanansubramanian.com/hl7tutorial…
本文将讲述一次使用开源项目Hapi创建附带Z段(Zxx)对象的方式
Hapi开源地址:github.com/hapifhir/ha…
本文为个人的作法,仅供参考,如有不正确,欢迎联系指正
正文开始
JAVA编写附带Z段HL7格式的文本
本文使用MFN_M01格式进行演示。
首先看一下最终成品的文本格式:
MSH|^~\&|his||EAI||20210719144402||MFN^M01|0cb16d6a-780d-4b71-8c7a-bea38e29d8a1|P|2.4|||AL|AL|CHN|GBK
MFI|Department^科室^HIS||UPD|20210719144402|20210719144402|AL
MFE|MUP|||1121|CE
Z01|1121|科室01|dept01|Y
- 引入依赖 在POM.xml文件里先引入Hapi
<!-- https://mvnrepository.com/artifact/ca.uhn.hapi.wso2/hapi -->
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-base</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>ca.uhn.hapi</groupId>
<artifactId>hapi-structures-v24</artifactId>
<version>2.2</version>
</dependency>
- 创建拓展Z段(Zxx)类 在本文中,创建的Z段命名为Z01。在Z01中,我们定了对接字段为deptId,deptName,deptEnName,isAvailable四个,可以自由根据自己需要进行扩展,这里仅做演示使用。
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Group;
import ca.uhn.hl7v2.model.Type;
import ca.uhn.hl7v2.model.v24.datatype.ST;
import ca.uhn.hl7v2.model.v24.segment.Zxx;
import ca.uhn.hl7v2.parser.ModelClassFactory;
public class Z01 extends Zxx {
/**
* Creates a new Zxx segment
*
* @param parent
* @param factory
*/
public Z01(Group parent, ModelClassFactory factory) {
super(parent, factory);
init(factory);
}
private void init(ModelClassFactory factory) {
try {
this.add(ST.class, true, 1, 250, new Object[]{ getMessage() }, "Dept Id");
this.add(ST.class, true, 1, 250, new Object[]{ getMessage() }, "Dept Name");
this.add(ST.class, true, 1, 250, new Object[]{ getMessage() }, "Dept ENAME");
this.add(ST.class, true, 1, 250, new Object[]{ getMessage() }, "Is Available");
} catch(HL7Exception e) {
log.error("Unexpected error creating Z01 - this is probably a bug in the source code generator.", e);
}
}
/**
* Returns
* Z01: "Dept Id" - creates it if necessary
*/
public ST getDeptId() {
ST retVal = this.getTypedField(1, 0);
return retVal;
}
/**
* Returns
* Z01: "Dept Name" - creates it if necessary
*/
public ST getDeptName() {
ST retVal = this.getTypedField(2, 0);
return retVal;
}
/**
* Returns
* Z01: "Dept En Name" - creates it if necessary
*/
public ST getDeptEnName() {
ST retVal = this.getTypedField(3, 0);
return retVal;
}
/**
* Returns
* Z01: "IsAvailable" - creates it if necessary
*/
public ST getIsAvailable() {
ST retVal = this.getTypedField(4, 0);
return retVal;
}
/** {@inheritDoc} */
@Override
protected Type createNewTypeWithoutReflection(int field) {
switch (field) {
case 0: return new ST(getMessage());
case 1: return new ST(getMessage());
case 2: return new ST(getMessage());
case 3: return new ST(getMessage());
default: return null;
}
}
}
- 新增包含Z01在内的MFN_M01_MF MFN_M01_MF为MFN_M01中的一个段,继承之后加入所需的段Z01
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Group;
import ca.uhn.hl7v2.model.v24.group.MFN_M01_MF;
import ca.uhn.hl7v2.parser.ModelClassFactory;
public class MFN_M01_MF_Z01 extends MFN_M01_MF {
/**
* Creates a new MFN_M01_MF_Z01 group
*
* @param parent
* @param factory
*/
public MFN_M01_MF_Z01(Group parent, ModelClassFactory factory) {
super(parent, factory);
init(factory);
}
private void init(ModelClassFactory factory) {
try {
this.add(Z01.class, false, false, false);
} catch(HL7Exception e) {
log.error("Unexpected error creating MFN_M01_MF_Z01 - this is probably a bug in the source code generator.", e);
}
}
/**
* Returns "2.4"
*/
@Override
public String getVersion() {
return "2.4";
}
/**
* Returns
* Z01 - creates it if necessary
*/
public Z01 getZ01() {
Z01 retVal = getTyped("Z01", Z01.class);
return retVal;
}
}
- 创建完整MFN_M01对象
public AbstractMessage getMsg() throws HL7Exception{
DefaultModelClassFactory defaultModelClassFactory = new DefaultModelClassFactory();
MFN_M01 mfn_m01 = new MFN_M01(defaultModelClassFactory);
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar calendar = Calendar.getInstance();
String dateTime = df.format(calendar.getTime());
//构建头
MSH msh = mfn_m01.getMSH();
msh.getFieldSeparator().setValue("|");
msh.getEncodingCharacters().setValue("^~\&");
msh.getSendingApplication().getNamespaceID().setValue("his");
msh.getSendingFacility().getNamespaceID().setValue("");
msh.getReceivingApplication().getNamespaceID().setValue("EAI");
Terser.set(msh, 9, 0, 1, 1, "MFN");
Terser.set(msh, 9, 0, 2, 1, "M01");
msh.getReceivingFacility().getNamespaceID().setValue("");
msh.getDateTimeOfMessage().getTimeOfAnEvent().setValue(dateTime);
msh.getProcessingID().getProcessingID().setValue("P");
msh.getMessageControlID().setValue(UUID.randomUUID().toString());
msh.getVersionID().getVersionID().setValue("2.4");
msh.getAcceptAcknowledgmentType().setValue("AL");
Terser.set(msh,16,0,1,1,"AL");
msh.getCountryCode().setValue("CHN");
Terser.set(msh,18,0,1,1,"GBK");
//构建主文档识别信息段
MFI mfi = mfn_m01.getMFI();
mfi.getMasterFileIdentifier().getCe1_Identifier().setValue("Department");
mfi.getMasterFileIdentifier().getCe2_Text().setValue("科室");
mfi.getMasterFileIdentifier().getCe3_NameOfCodingSystem().setValue("HIS");
mfi.getFileLevelEventCode().setValue("UPD");
Terser.set(mfi,4,0,1,1,dateTime);
Terser.set(mfi,5,0,1,1,dateTime);
mfi.getResponseLevelCode().setValue("AL");
//构建主文件项目段
MFN_M01_MF_Z01 mfn_m01_mf_z01 = new MFN_M01_MF_Z01(mfn_m01,mfn_m01.getModelClassFactory());
MFE mfe = mfn_m01_mf_z01.getMFE();
mfe.getRecordLevelEventCode().setValue("MUP");
Terser.set(mfe,4,0,1,1,"1121");
mfe.getPrimaryKeyValueType(0).setValue("CE");
//构建拓展段
Z01 z01 = mfn_m01_mf_z01.getZ01();
z01.getDeptId().setValue("1121");
z01.getDeptName().setValue("科室01");
z01.getDeptEnName().setValue("dept01");
z01.getIsAvailable().setValue("Y");
mfn_m01.insertMF(mfn_m01_mf_z01,0);
return mfn_m01;
}
- 发送HL7消息 此块使用Hapi提供的TestPanel进行测试
public static void main(String[] args) throws Exception {
HapiContext context = new DefaultHapiContext();
Connection connection = context.newClient("localhost", 49896, false);
// The initiator which will be used to transmit our message
Initiator initiator = connection.getInitiator();
Parser pipeParser = context.getPipeParser();
System.out.println("Message was constructed successfully..." + "\n");
MessageBuild messageBuild = getMsg();
System.out.println(pipeParser.encode(msg).replace("\r","\n\r"));
System.out.println("Printing message structure to console...");
System.out.println(msg.printStructure());
Message response = initiator.sendAndReceive(msg);
String responseString = pipeParser.encode(response);
System.out.println("Received response:\n" + responseString.replace("\r","\n\r"));
}
经过测试,可以发送出需要的HL7格式的文本,经过TestPanel也可以获取到AA返回的ACK文本。
关于GBK编码格式转换
HapiContext hapiContext = new DefaultHapiContext();
LowerLayerProtocol llp = new MinLowerLayerProtocol(false);
llp.setCharset("GBK");
hapiContext.setLowerLayerProtocol(llp);