Wraps a message to provide access to fields using a terse location specification syntax. For example:
terser.set("MSH-9-3", "ADT_A01");
can be used instead of
message.getMSH().getMessageType().getMessageStructure().setValue("ADT_A01");
The syntax of a location spec is as follows:
location_spec: segment_path_spec "-" field ["(" rep ")"] ["-" component ["-" subcomponent]]
... where rep, field, component, and subcomponent are integers (representing, respectively, the field repetition (starting at 0), and the field number, component number, and subcomponent numbers (starting at 1). Omitting the rep is equivalent to specifying 0; omitting the component or subcomponent is equivalent to specifying 1.
The syntax for the segment_path_spec is as follows:
segment_path_spec: ["/"] (group_spec ["(" rep ")"] "/")* segment_spec ["(" rep ")"]
... where rep has the same meaning as for fields.
A leading "/" indicates that navigation to the location begins at the root of the message; omitting this indicates that navigation begins at the current location of the underlying SegmentFinder (see getFinder() -- this allows manual navigation if desired). The syntax for group_spec is:
group_spec: ["."] group_name_pattern
Here, a . indicates that the group should be searched for (using a SegmentFinder) starting at the current location in the message. The wildcards "*" and "?" represent any number of arbitrary characters, and a single arbitrary character, respectively. For example, "M*" and "?S?" match MSH. The first group with a name that matches the given group_name_pattern will be matched.
The segment_spec is analogous to the group_spec.
As another example, the following subcomponent in an SIU_S12 message:
msg.getSIU_S12_RGSAISNTEAIGNTEAILNTEAIPNTE(1).getSIU_S12_AIGNTE().getAIG().getResourceGroup(1).getIdentifier();
/SIU_S12_RGSAISNTEAIGNTEAILNTEAIPNTE(1)/SIU_S12_AIGNTE/AIG-5(1)-1
/*AIG*(1)/SIU_S12_AIGNTE/AIG-5(1)-1
/*AIG*(1)/.AIG-5(1)
The search function only iterates through rep 0 of each group. Thus if rep 0 of the first group in this example was desired instead of rep 1, the following syntax would also work (since there is only one AIG segment position in SUI_S12):
/.AIG-5(1)
@author Bryan Tripp
@author Ryan W. Gross (General Electric Corporation - Healthcare IT).
public void run() {
try {
// get message ID
String ID = generator.getID();
Message out = parser.parse(outText);
Terser tOut = new Terser(out);
tOut.set("/MSH-10", ID);
// send, get response
Message in = initiator.sendAndReceive(out);
// get ACK ID
Terser tIn = new Terser(in);
String ackID = tIn.get("/MSA-2");
if (ID.equals(ackID)) {
System.out.println("OK - ack ID matches");
} else {
throw new RuntimeException(
"Ack ID for message " + ID + " is "
* form required by <code>Terser</code>.
* @return a Map from Terser paths to field values
*/
public static Map<String, String> getFields(Message theMessage, List<String> theTerserPaths) throws HL7Exception {
Map<String, String> fields = new HashMap<String, String>();
Terser terser = new Terser(theMessage);
for (int i = 0; i < theTerserPaths.size(); i++) {
String path = theTerserPaths.get(i);
String fieldValue = terser.get(path);
fields.put(path, fieldValue);
}
return fields;
}
}
protected void applySuperStructureName(Message theMessage) throws HL7Exception {
if (theMessage instanceof AbstractSuperMessage) {
if (theMessage.getName() == null) {
Terser t = new Terser(theMessage);
String name = null;
try {
name = t.get("/MSH-9-3");
} catch (HL7Exception e) {
// ignore
}
if (StringUtil.isBlank(name)) {
name = t.get("/MSH-9-1") + "_" + t.get("/MSH-9-2");
}
((AbstractSuperMessage)theMessage).setName(name);
}
}
super();
this.expression = expression;
}
public Object evaluate(Message msg) throws HL7Exception {
return new Terser(msg).get(expression);
}
// Terser Expression is equivalent with Location
return "";
}
public Location getLocation(Message msg) throws HL7Exception {
Terser t = new Terser(msg);
StringTokenizer tok = new StringTokenizer(expression, "-", false);
Segment segment = t.getSegment(tok.nextToken());
Location location = new Location();
location.setSegmentName(segment.getName());
location.setFieldIndizes(Terser.getIndices(expression));
return location;
}
/**
* @see ca.uhn.hl7v2.protocol.Initiator#sendAndReceive(ca.uhn.hl7v2.model.Message)
*/
public Message sendAndReceive(Message theMessage) throws HL7Exception {
Terser t = new Terser(theMessage);
String appAckNeeded = t.get("/MSH-16");
String msgId = t.get("/MSH-10");
String messageText = getParser().encode(theMessage);
Map<String, Object> metadata = getMetadata(theMessage);
Transportable out = new TransportableImpl(messageText, metadata);
return problems.toArray(new ValidationException[problems.size()]);
}
private String[] getDeclaredProfileIDs(Message theMessage) throws HL7Exception {
Terser t = new Terser(theMessage);
boolean noMore = false;
int c = 0;
List<String> declaredProfiles = new ArrayList<String>(8);
while (!noMore) {
String path = "MSH-21(" + c++ + ")";
String idRep = t.get(path);
//FIXME fails if empty rep precedes full rep ... should add getAll() to Terser and use that
if (idRep == null || idRep.equals("")) {
noMore = true;
} else {
declaredProfiles.add(idRep);
return need;
}
private Map<String, Object> getMetadata(Message theMessage) throws HL7Exception {
Map<String, Object> md = new HashMap<String, Object>();
Terser t = new Terser(theMessage);
//snapshot so concurrent changes won't break our iteration
String[] fields = getMetadataFields().toArray(new String[0]);
for (int i = 0; i < fields.length; i++) {
String field = fields[i].toString();
String val = t.get(field);
md.put(field, val);
}
return md;
}
* @see Validator#validate
*/
public HL7Exception[] validate(Message message, StaticDef profile) throws ProfileException,
HL7Exception {
List<HL7Exception> exList = new ArrayList<HL7Exception>();
Terser t = new Terser(message);
// check msg type, event type, msg struct ID
String msgType = t.get("/MSH-9-1");
if (!msgType.equals(profile.getMsgType())) {
HL7Exception e = new ProfileNotFollowedException("Message type " + msgType
+ " doesn't match profile type of " + profile.getMsgType());
exList.add(e);
}
String evType = t.get("/MSH-9-2");
if (!evType.equals(profile.getEventType())
&& !profile.getEventType().equalsIgnoreCase("ALL")) {
HL7Exception e = new ProfileNotFollowedException("Event type " + evType
+ " doesn't match profile type of " + profile.getEventType());
exList.add(e);
}
String msgStruct = t.get("/MSH-9-3");
if (msgStruct == null || !msgStruct.equals(profile.getMsgStructID())) {
HL7Exception e = new ProfileNotFollowedException("Message structure " + msgStruct
+ " doesn't match profile type of " + profile.getMsgStructID());
exList.add(e);
}
* the type and trigger event of the given message, or null if there are
* none.
*/
private Application getMatchingApplication(Message message)
throws HL7Exception {
Terser t = new Terser(message);
String messageType = t.get("/MSH-9-1");
String triggerEvent = t.get("/MSH-9-2");
return this.getMatchingApplication(messageType, triggerEvent);
}
Related Classes of ca.uhn.hl7v2.util.Terser
Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.