public static List<HL7Value> extractValues( Message message ) throws Exception { Terser terser = new Terser( message ); SegmentFinder finder = terser.getFinder(); List<HL7Value> values = new ArrayList<HL7Value>(); int childNr = 1; while ( finder.hasNextChild() ) { // next group in the message (MSH, PID, EVN and so on) // finder.nextChild(); Structure[] structures = finder.getCurrentChildReps(); for ( int i = 0; i < structures.length; i++ ) { Structure structure = structures[i]; parseStructure( values, message, terser, structure, Integer.toString( childNr ) ); } childNr++; } return values; }
Type[] types = segment.getField( n ); for ( int t = 0; t < types.length; t++ ) { int nrComponents = Terser.numComponents( types[t] ); for ( int c = 1; c <= nrComponents; c++ ) { int nrSub = Terser.numSubComponents( types[t], c ); for ( int sc = 1; sc <= nrSub; sc++ ) { String string = Terser.get( segment, n, t, c, sc );
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 " + ackID); } } catch (Exception e) { e.printStackTrace(); } } });
/** * @return structure of the message, e.g. '2.5.1' */ public static String messageVersion(Terser t) throws HL7Exception { return t.get("/MSH-12-1"); }
/** * Copies data from the "from" Type into the "to" Type. Either Type may be * a Primitive, Composite, or Varies. If a Varies is provided, the operation is * performed on the result of calling its getData() method. A Primitive may be * copied into a Composite, in which case the value is copied into the first * component of the Composite. A Composite may be copied into a Primitive, * in which case the first component is copied. Given Composites with different * numbers of components, the first components are copied, up to the length * of the smaller one. * * @param from type to copy from * @param to type to copy to * @throws DataTypeException if the types are not compatible */ public static void copy(Type from, Type to) throws DataTypeException { for (int i = 1; i <= Terser.numComponents(from); i++) { for (int j = 1; j <= Terser.numSubComponents(from, i); j++) { String val = Terser.getPrimitive(from, i, j).getValue(); Terser.getPrimitive(to, i, j).setValue(val); } } }
String fieldSep = Terser.get(mshIn, 1, 0, 1, 1); String encChars = Terser.get(mshIn, 2, 0, 1, 1); String procID = Terser.get(mshIn, 11, 0, 1, 1); Terser.set(mshOut, 1, 0, 1, 1, fieldSep); Terser.set(mshOut, 2, 0, 1, 1, encChars); GregorianCalendar now = new GregorianCalendar(); now.setTime(new Date()); Terser.set(mshOut, 7, 0, 1, 1, CommonTS.toHl7TSFormat(now)); Terser.set(mshOut, 9, 0, 1, 1, "ACK"); Terser.set(mshOut, 9, 0, 2, 1, Terser.get(mshIn, 9, 0, 2, 1)); String v = mshOut.getMessage().getVersion(); if (v != null) { Version version = Version.versionOf(v); if (version != null && !Version.V25.isGreaterThan(version)) { Terser.set(mshOut, 9, 0, 3, 1, "ACK"); Terser.set(mshOut, 10, 0, 1, 1, mshIn.getMessage().getParser().getParserConfiguration().getIdGenerator().getID()); Terser.set(mshOut, 11, 0, 1, 1, procID); String versionId = Terser.get(mshIn, 12, 0, 1, 1); if (StringUtil.isBlank(versionId)) { versionId = Version.highestAvailableVersionOrDefault().getVersion(); Terser.set(mshOut, 12, 0, 1, 1, versionId); Terser.set(mshOut, 3, 0, 1, 1, Terser.get(mshIn, 5, 0, 1, 1)); Terser.set(mshOut, 4, 0, 1, 1, Terser.get(mshIn, 6, 0, 1, 1)); Terser.set(mshOut, 5, 0, 1, 1, Terser.get(mshIn, 3, 0, 1, 1));
public ResultImpl(Message theMessage, String[] theLoopPoints, Map<String, Integer> theLoopPointNames, String[] theFieldPaths, Map<String, Integer> theFieldNames, String[] theWherePaths, String[] theWherePatterns, boolean[] theExactMatchFlags) { myTerser = new Terser(theMessage); myLoopPoints = theLoopPoints; myIndices = new int[theLoopPoints.length]; myNumEmpty = new int[theLoopPoints.length]; myMaxNumEmpty = getMaxNumEmpty(theLoopPoints); myLoopPointNames = theLoopPointNames; myFieldPaths = theFieldPaths; myValues = new String[theFieldPaths.length]; myFieldNames = theFieldNames; myWherePaths = theWherePaths; myWherePatterns = theWherePatterns; myExactMatchFlags = theExactMatchFlags; if (theLoopPoints.length == 0) { myNonLoopingQuery = true; // if no loops, give ourselves 1 // iteration } else { myIndices[myIndices.length - 1] = -1; // start before 1st // iteration } }
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() .withSegmentName(segment.getName()) .withFieldIndizes(Terser.getIndices(expression)); return location; }
/** * <p> * Gets the string value of the field specified. See the class docs for syntax of the location * spec. * </p> * <p> * If a repetition is omitted for a repeating segment or field, the first rep is used. If the * component or subcomponent is not specified for a composite field, the first component is used * (this allows one to write code that will work with later versions of the HL7 standard). * * @param spec field specification * @return string value of the specified field * @throws HL7Exception if the primitive could not be obtained */ public String get(String spec) throws HL7Exception { StringTokenizer tok = new StringTokenizer(spec, "-", false); Segment segment = getSegment(tok.nextToken()); int[] ind = getIndices(spec); return get(segment, ind[0], ind[1], ind[2], ind[3]); }
/** * Fill segments for HL7 versions before 2.5. * <p/> * HL7 versions before 2.5 require to set MSA-1 and MSA-3. The ERR segment only has one * repeatable field (ERR-1) with components containing details about the exception. * * @param response the raw response message * @param acknowledgmentCode acknowledgment code * @param repetition repetition of the ERR segment that shall be popualted * @throws HL7Exception */ private Message populateResponseBefore25(Message response, AcknowledgmentCode acknowledgmentCode, int repetition) throws HL7Exception { // TODO define what should happen if there is no MSA or ERR Segment msa = (Segment) response.get("MSA"); Terser.set(msa, 1, 0, 1, 1, acknowledgmentCode.name()); Terser.set(msa, 3, 0, 1, 1, errorCode.getMessage()); Segment err = (Segment) response.get("ERR"); if (location != null) { if (location.getSegmentName() != null) Terser.set(err, 1, repetition, 1, 1, location.getSegmentName()); if (location.getField() > 0) Terser.set(err, 1, repetition, 3, 1, Integer.toString(location.getField())); } Terser.set(err, 1, repetition, 4, 1, Integer.toString(errorCode.getCode())); Terser.set(err, 1, repetition, 4, 2, errorCode.getMessage()); Terser.set(err, 1, repetition, 4, 3, ErrorCode.codeTable()); Terser.set(err, 1, repetition, 4, 5, getMessage()); return response; }
fieldOffset = 1; Terser.set(destination, 1, 0, 1, 1, String.valueOf(encodingChars.getFieldSeparator())); Terser.getPrimitive(field, 1, 1).setValue(reps[j]); } else { parse(field, reps[j], encodingChars);
/** * Sets the string value of the field specified. See class docs for location spec syntax. * * @param spec primitive path specification * @param value value to be set * @throws HL7Exception if the primitive does not exist */ public void set(String spec, String value) throws HL7Exception { StringTokenizer tok = new StringTokenizer(spec, "-", false); Segment segment = getSegment(tok.nextToken()); int[] ind = getIndices(spec); log.trace("Setting {} seg: {} ind: {} {} {} {}", new Object[] { spec, segment.getName(), ind[0], ind[1], ind[2], ind[3] }); set(segment, ind[0], ind[1], ind[2], ind[3], value); }
/** * Returns the number of components in the given type, i.e. the number of standard components * (e.g. 6 for CE) plus any extra components that have been added at runtime. * * @param t composite type * @return the number of components in the given type */ public static int numComponents(Type t) { if (!(t instanceof Varies)) { return numStandardComponents(t) + t.getExtraComponents().numComponents(); } return numComponents(((Varies) t).getData()); }
/** * Returns the number of sub-components in the specified component, i.e. the number of standard * sub-components (e.g. 6 for CE) plus any extra components that that have been added at * runtime. * * @param type composite type * @param component numbered from 1 * @return number of sub-components in the specified component */ public static int numSubComponents(Type type, int component) { if (component == 1 && Primitive.class.isAssignableFrom(type.getClass())) { // note that getComponent(primitive, 1) below returns the primitive // itself -- if we do numComponents on it, we'll end up with the // number of components in the field, not the number of subcomponents return 1; } Type comp = getComponent(type, component); return numComponents(comp); }
getFinder().reset(); SegmentFinder finder = getFinder(); while (tok.hasMoreTokens()) { String pathSpec = tok.nextToken(); Terser.PathSpec ps = parsePathSpec(pathSpec); ps.isGroup = tok.hasMoreTokens(); if (ps.isGroup) {
if (comp <= numStandardComponents(type) || type instanceof GenericComposite) { try { return ((Composite) type).getComponent(comp - 1); return getComponent(v.getData(), comp); return type.getExtraComponents().getComponent(comp - numStandardComponents(type) - 1);
Type comp = getComponent(type, component); if (type instanceof Varies && comp instanceof GenericPrimitive && subcomponent > 1) { try { final GenericComposite comp2 = new GenericComposite(type.getMessage()); varies.setData(comp2); comp = getComponent(type, component); } catch (final DataTypeException de) { final String message = "Unexpected exception copying data to generic composite. This is probably a bug within HAPI. " final Type sub = getComponent(comp, subcomponent); return getPrimitive(sub);
int[] indices = Terser.getIndices(thePathSpecs[i]); paths[i] = new DatumPath(); paths[i].add(segName).add(segRep);
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 " + ackID); } } catch (Exception e) { e.printStackTrace(); } } });