View Javadoc
1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package ca.uhn.hl7v2.examples;
7   
8   import ca.uhn.hl7v2.DefaultHapiContext;
9   import ca.uhn.hl7v2.HL7Exception;
10  import ca.uhn.hl7v2.HapiContext;
11  import ca.uhn.hl7v2.examples.custommodel.v25.message.ZDT_A01;
12  import ca.uhn.hl7v2.examples.custommodel.v25.segment.ZPI;
13  import ca.uhn.hl7v2.model.Segment;
14  import ca.uhn.hl7v2.model.v25.message.ADT_A01;
15  import ca.uhn.hl7v2.parser.CustomModelClassFactory;
16  import ca.uhn.hl7v2.parser.ModelClassFactory;
17  import ca.uhn.hl7v2.parser.Parser;
18  
19  
20  /**
21   * Example using custom model classes
22   */
23  public class CustomModelClasses {
24  
25      public static void main(String[] args) throws HL7Exception {
26  
27          // Suppose we need to parse a message that has some local customization
28          // or quirk. The most common example is Z-segments in messages, but we
29          // could even be talking about completely non conformant structures.
30  
31          // For this example, let's imagine an application that has a custom
32          // patient segment called ZPI after the PID segment in an ADT^A01 message.
33          // This ZPI segment has two fields:
34          //   1 - a repeating ST called "Pet Name"
35          //   2 - a non repeating NM called "shoe size"
36  
37          // Here's an example message
38  		String messageText = "MSH|^~\\&|IRIS|SANTER|AMB_R|SANTER|200803051508||ADT^A01|263206|P|2.5\r"
39  				+ "EVN||200803051509||||200803031508\r"
40  				+ "PID|||5520255^^^PK^PK~ZZZZZZ83M64Z148R^^^CF^CF~ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103^99991231~^^^^TEAM||ZZZ^ZZZ||19830824|F||||||||||||||||||||||N\r"
41  				+ "ZPI|Fido~Fred|13\r"
42  				+ "PV1||I|6402DH^^^^^^^^MED. 1 - ONCOLOGIA^^OSPEDALE MAGGIORE DI LODI&LODI|||^^^^^^^^^^OSPEDALE MAGGIORE DI LODI&LODI|13936^TEST^TEST||||||||||5068^TEST2^TEST2||2008003369||||||||||||||||||||||||||200803031508\r"
43  				+ "PR1|1||1111^Mastoplastica|Protesi|20090224|02|";
44  
45          // HAPI will still parse this message fine
46  		HapiContext context = new DefaultHapiContext();
47  		Parser parser = context.getPipeParser();
48          ADT_A01/../../../ca/uhn/hl7v2/model/v25/message/ADT_A01.html#ADT_A01">ADT_A01 message = (ADT_A01) parser.parse(messageText);
49          
50          // If we want to access the data in the ZPI segment, it's pretty easy
51          Segmentca/uhn/hl7v2/model/Segment.html#Segment">Segment zpiGenericSegment = (Segment) message.get("ZPI");
52  
53          String firstPetName  = zpiGenericSegment.getField(1, 0).encode();
54          String secondPetName = zpiGenericSegment.getField(1, 1).encode();
55          System.out.println(firstPetName); // Fido
56          System.out.println(secondPetName); // Fred
57  
58          String shoeSize = zpiGenericSegment.getField(2, 0).encode();
59          System.out.println(shoeSize); // 13
60  
61  
62          // Now, suppose our message is actually called ZDT^A01, and we plan on
63          // using it frequently, so we want to explicitly define a class to
64          // handle it. That can be done as well.
65  
66          // Here is a segment class we have created (click to view):
67          ZPI zpi;
68  
69          // Here is a message class we have created (click to view):
70          ZDT_A01 zdtA01;
71  
72          // These classes are both in the package ca.uhn.hl7v2.examples.custommodel.[version].[type]
73          // We associated the HapiContext with a custom model class factory to use it
74          ModelClassFactory cmf = new CustomModelClassFactory("ca.uhn.hl7v2.examples.custommodel");
75          context.setModelClassFactory(cmf);
76  
77          // The resulting message will be an instance of our custom type (this time, MSH-9 says ZDT^A01)
78  		messageText = "MSH|^~\\&|IRIS|SANTER|AMB_R|SANTER|200803051508||ZDT^A01|263206|P|2.5\r"
79  				+ "EVN||200803051509||||200803031508\r"
80  				+ "PID|||5520255^^^PK^PK~ZZZZZZ83M64Z148R^^^CF^CF~ZZZZZZ83M64Z148R^^^SSN^SSN^^20070103^99991231~^^^^TEAM||ZZZ^ZZZ||19830824|F||||||||||||||||||||||N\r"
81  				+ "ZPI|Fido~Fred|13\r"
82  				+ "PV1||I|6402DH^^^^^^^^MED. 1 - ONCOLOGIA^^OSPEDALE MAGGIORE DI LODI&LODI|||^^^^^^^^^^OSPEDALE MAGGIORE DI LODI&LODI|13936^TEST^TEST||||||||||5068^TEST2^TEST2||2008003369||||||||||||||||||||||||||200803031508\r"
83  				+ "PR1|1||1111^Mastoplastica|Protesi|20090224|02|";
84          zdtA01 = (ZDT_A01) parser.parse(messageText);
85  
86          // Now we have easy access to our new segment and fields
87          zpi = zdtA01.getZPI();
88  
89          System.out.println(zpi.getPetName()[0].encode()); // Fido
90          System.out.println(zpi.getPetName()[1].encode()); // Fred
91          System.out.println(zpi.getShoeSize().encode()); // 13
92  
93      }
94  
95  }