Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepav committed Jun 17, 2020
1 parent df9738d commit 972c98f
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 67 deletions.
24 changes: 23 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@
</dirset>
</path>

<path id="simple-hl7v2.module.test.sourcepath">
<dirset dir="${module.simple-hl7v2.basedir}">
<include name="examples"/>
</dirset>
</path>


<target name="compile.module.simple-hl7v2" depends="compile.module.simple-hl7v2.production,compile.module.simple-hl7v2.tests" description="Compile module simple-hl7v2"/>

Expand All @@ -137,7 +143,23 @@
</copy>
</target>

<target name="compile.module.simple-hl7v2.tests" depends="compile.module.simple-hl7v2.production" description="compile module simple-hl7v2; test classes" unless="skip.tests"/>
<target name="compile.module.simple-hl7v2.tests" depends="compile.module.simple-hl7v2.production" description="compile module simple-hl7v2; test classes" unless="skip.tests">
<mkdir dir="${simple-hl7v2.testoutput.dir}"/>
<javac destdir="${simple-hl7v2.testoutput.dir}" debug="${compiler.debug}" nowarn="${compiler.generate.no.warnings}" memorymaximumsize="${compiler.max.memory}" fork="true">
<compilerarg line="${compiler.args.simple-hl7v2}"/>
<bootclasspath refid="simple-hl7v2.module.bootclasspath"/>
<classpath refid="simple-hl7v2.module.classpath"/>
<src refid="simple-hl7v2.module.test.sourcepath"/>
<patternset refid="excluded.from.compilation.simple-hl7v2"/>
</javac>

<copy todir="${simple-hl7v2.testoutput.dir}/com/illcode/hl7">
<fileset dir="${module.simple-hl7v2.basedir}/examples">
<patternset refid="compiler.resources"/>
<type type="file"/>
</fileset>
</copy>
</target>

<target name="clean.module.simple-hl7v2" description="cleanup module">
<delete dir="${simple-hl7v2.output.dir}"/>
Expand Down
94 changes: 94 additions & 0 deletions examples/SimpleHL7v2Examples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.illcode.hl7;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import static com.illcode.hl7.Segment.FieldValue.composite;
import static com.illcode.hl7.Segment.FieldValue.scalar;
import static com.illcode.hl7.Segment.FieldValue.indexed;

public class SimpleHL7v2Examples
{
public static void createSegments() {
Segment MSH = new Segment("MSH");
MSH.addFieldValue(1, "|");
MSH.addFieldValue(2, "^~\\&");
MSH.addFieldValue(3, "EPIC");
MSH.addFieldValue(5, "SMS");
MSH.addFieldValue(6, "SMSDT");
MSH.addFieldValue(7, "201501011408");
MSH.addFieldValue(9, composite("ADT", "A04"));
MSH.addFieldValue(10, "9000123");
MSH.addFieldValue(11, "D");
MSH.addFieldValue(12, "2.7");
MSH.addFieldValue(13, scalar(null)); // You need the scalar() to disambiguate overload resolution

Segment PID = new Segment("PID");
PID.addFieldValue(2,
indexed(
1, "0493575",
4, composite("Big", null, "Elephant"),
5, "ID 1")
);
PID.addFieldValue(3, "454721");
PID.addFieldValue(5, composite("DOE", "JOHN"));

Segment PV1 = new Segment("PV1", 3);
PV1.addFieldValue(2, "O");
PV1.addFieldValue(3, "168 ");
PV1.addFieldValue(3, "219");
PV1.addFieldValue(3, "C");
PV1.addFieldValue(3, "P");

Message m = new Message();
m.putSegment(MSH);
m.putSegment(PID);
m.putSegment(PV1);
HL7Encoder encoder = new HL7Encoder();
// Since HL7 messages are delimited by '\r', we change the line endings for printing
System.out.println(encoder.encode(m).replace("\r", "\n"));
}

public static Message parseInput(String[] args) throws IOException {
if (args.length == 0)
return new Message();

final Reader r = new InputStreamReader(Files.newInputStream(Paths.get(args[0])), StandardCharsets.UTF_8);
final String hl7text = HL7Parser.slurpReaderText(r, 512).trim();
final HL7Parser parser = new HL7Parser(new HL7Params(hl7text));
final Message m = parser.parse(hl7text);

// If one argument is supplied, print the internal representation
if (args.length == 1) {
System.out.println(m.toString());
} else if (args.length == 2) { // Query a field value using Message#getField(String)
System.out.println(m.getField(args[1]));
} else if (args.length == 3) { // Set a field value using Message#setField(String, String)
m.setField(args[1], args[2]);
System.out.println(m);
}
return m;
}

public static void editMessage(Message m) {
if (m.getMessageType().equals("QRY^A19")) {
// Get and set some field values
String firstIdNumber = m.getField("QRD.8.1");
m.setField("QRD.8.2", "Good Family Name");

// Get the second repetition of a field value
String secondIdNumber = m.getField("QRD.8(2).1");
}
}

public static void main(String[] args) throws IOException {
System.out.println("\n--- parseInput() ----------------\n");
parseInput(args);
System.out.println("\n\n--- createSegments() ----------------\n");
createSegments();
}
}
1 change: 1 addition & 0 deletions simple-hl7v2.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="true" packagePrefix="com.illcode.hl7" />
<excludeFolder url="file://$MODULE_DIR$/.idea" />
<excludeFolder url="file://$MODULE_DIR$/__artifacts_temp" />
<excludeFolder url="file://$MODULE_DIR$/dist" />
Expand Down
29 changes: 0 additions & 29 deletions src/com/illcode/hl7/HL7Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,4 @@ public static String slurpReaderText(Reader r, int bufferSize) {
}
return s;
}

public static void main(String[] args) throws IOException {
Reader r;
if (args.length > 0)
r = new InputStreamReader(Files.newInputStream(Paths.get(args[0])), StandardCharsets.UTF_8);
else
r = new InputStreamReader(System.in);
String hl7 = slurpReaderText(r, 512).trim();
r.close();
HL7Parser parser = new HL7Parser(new HL7Params(hl7));
Message m = parser.parse(hl7);
if (args.length == 1) {
System.out.println(m.getMessageType() + "\n--------------------------");
System.out.println(m);
} else if (args.length == 2) {
System.out.println(m.getField(args[1]));
} else if (args.length == 3) {
m.setField(args[1], args[2]);
System.out.println(m);
}
/*
Segment PV1 = m.getSegment("PV1");
PV1.setFieldValue(8, 1, 1, 3, "Foo");
PV1.setFieldValue(8, 1, 1, 4, "bar");
System.err.println(m.toString());
HL7Encoder encoder = new HL7Encoder();
System.out.print(encoder.encode(m).replace("\r", "\n"));
*/
}
}
37 changes: 0 additions & 37 deletions src/com/illcode/hl7/Segment.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,41 +437,4 @@ else if (idx > maxIdx)
return new FieldValue(children);
}
}

public static void main(String[] args) {
Segment MSH = new Segment("MSH");
MSH.addFieldValue(1, "|");
MSH.addFieldValue(2, "^~\\&");
MSH.addFieldValue(3, "EPIC");
MSH.addFieldValue(5, "SMS");
MSH.addFieldValue(6, "SMSDT");
MSH.addFieldValue(7, "201501011408");
MSH.addFieldValue(9, FieldValue.composite("ADT", "A04"));
MSH.addFieldValue(10, "9000123");
MSH.addFieldValue(11, "D");
MSH.addFieldValue(12, "2.7");
MSH.addFieldValue(13, FieldValue.scalar(null));

Segment PID = new Segment("PID");
PID.addFieldValue(2, FieldValue.indexed(
1, "0493575",
4, FieldValue.composite("Big", null, "Elephant"),
5, "ID 1"));
PID.addFieldValue(3, "454721");
PID.addFieldValue(5, FieldValue.composite("DOE", "JOHN"));

Segment PV1 = new Segment("PV1", 3);
PV1.addFieldValue(2, "O");
PV1.addFieldValue(3, "168 ");
PV1.addFieldValue(3, "219");
PV1.addFieldValue(3, "C");
PV1.addFieldValue(3, "P");

Message m = new Message();
m.putSegment(MSH);
m.putSegment(PID);
m.putSegment(PV1);
HL7Encoder encoder = new HL7Encoder();
System.out.println(encoder.encode(m).replace("\r", "\n"));
}
}

0 comments on commit 972c98f

Please sign in to comment.