1 package ca.uhn.hl7v2.examples; 2 3 import ca.uhn.hl7v2.DefaultHapiContext; 4 import ca.uhn.hl7v2.HL7Exception; 5 import ca.uhn.hl7v2.HapiContext; 6 import ca.uhn.hl7v2.model.v23.message.ORU_R01; 7 import ca.uhn.hl7v2.parser.Parser; 8 9 /** 10 * Example code which illustrates how to handle OBX segments 11 * with an invalid or missing OBX-2 value. 12 */ 13 public class ParseInvalidObx2Values { 14 15 16 public static void main(String[] args) throws HL7Exception { 17 18 /* 19 * OBX is a special segment in HL7. The value in OBX-2 20 * determines the data type of OBX-5. For example, in the 21 * following segment the DT in OBX 2 tells the parser that 22 * the value in OBX5 is a date (DT datatype): 23 * OBX|1|DT|||20111231 24 * 25 * For whatever reason, this is often implemented incorrectly 26 * by systems that you will need to exchange data with. This 27 * module demonstrates strategies for doing so. 28 * 29 * The following is a message in which the OBX segment is missing the 30 * mandatory OBX-2 value 31 */ 32 String string = "MSH|^~\\&|ULTRA|TML|OLIS|OLIS|200905011130||ORU^R01|20169838|T|2.3\r" 33 + "PID|||7005728\r" 34 + "OBR|1\r" 35 + "OBX|||||Sample Value\r"; 36 37 HapiContext context = new DefaultHapiContext(); 38 Parser parser = context.getPipeParser(); 39 40 ORU_R01 msg; 41 try { 42 // This throws an exception 43 msg = (ORU_R01) parser.parse(string); 44 } catch (Exception e) { 45 e.printStackTrace(); 46 /* 47 * Prints: 48 * ca.uhn.hl7v2.HL7Exception: OBX-5 is valued, but OBX-2 is not. A datatype for OBX-5 must be specified using OBX-2. See JavaDoc for Varies#fixOBX5(Segment, ModelClassFactory) 49 * at ca.uhn.hl7v2.model.Varies.fixOBX5(Varies.java:189) 50 * at ca.uhn.hl7v2.parser.PipeParser.parse(PipeParser.java:377) 51 * at ca.uhn.hl7v2.parser.PipeParser.parse(PipeParser.java:1009) 52 * at ca.uhn.hl7v2.model.AbstractMessage.parse(AbstractMessage.java:189) 53 * at ca.uhn.hl7v2.examples.ParseInvalidObx2Values.main(ParseInvalidObx2Values.java:28) 54 */ 55 } 56 57 58 /* 59 * Setting the following property allows you to specify a default 60 * value to assume if OBX-2 is missing. 61 */ 62 context.getParserConfiguration().setDefaultObx2Type("ST"); 63 64 // Parsing now succeeds 65 msg = (ORU_R01) parser.parse(string); 66 System.out.println(msg.encode()); 67 68 /* Prints: 69 * MSH|^~\\&|ULTRA|TML|OLIS|OLIS|200905011130||ORU^R01|20169838|T|2.3 70 * PID|||7005728 71 * OBR|1 72 * OBX||ST|||Sample Value\r"; 73 */ 74 75 /* 76 * The following message has an invalid value in OBX-2 77 */ 78 string = "MSH|^~\\&|ULTRA|TML|OLIS|OLIS|200905011130||ORU^R01|20169838|T|2.3\r" 79 + "PID|||7005728\r" 80 + "OBR|1\r" 81 + "OBX||BAD|||Sample Value\r"; 82 83 try { 84 // This throws an exception 85 msg = (ORU_R01) parser.parse(string); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 /* 89 * Prints: 90 * ca.uhn.hl7v2.HL7Exception: 'BAD' in record null is invalid for version 2.6. See JavaDoc for Varies#fixOBX5(Segment, ModelClassFactory): Segment: OBX Field #2 91 * at ca.uhn.hl7v2.model.Varies.fixOBX5(Varies.java:212) 92 * at ca.uhn.hl7v2.parser.PipeParser.parse(PipeParser.java:377) 93 * at ca.uhn.hl7v2.parser.PipeParser.parse(PipeParser.java:1009) 94 * at ca.uhn.hl7v2.model.AbstractMessage.parse(AbstractMessage.java:189) 95 * at ca.uhn.hl7v2.examples.ParseInvalidObx2Values.main(ParseInvalidObx2Values.java:62) 96 */ 97 } 98 99 /* 100 * Setting the following system property allows you to specify a default 101 * value to assume if OBX-2 is missing. 102 */ 103 context.getParserConfiguration().setInvalidObx2Type("ST"); 104 105 // Parsing now succeeds 106 msg = (ORU_R01) parser.parse(string); 107 108 } 109 110 }