View Javadoc
1   package ca.uhn.hl7v2.examples;
2   
3   import ca.uhn.hl7v2.DefaultHapiContext;
4   import ca.uhn.hl7v2.HL7Exception;
5   import ca.uhn.hl7v2.model.Message;
6   import ca.uhn.hl7v2.model.v25.message.ADT_A01;
7   import ca.uhn.hl7v2.model.v25.message.ADT_A02;
8   import ca.uhn.hl7v2.model.v25.message.ADT_AXX;
9   import ca.uhn.hl7v2.model.v25.segment.PID;
10  import ca.uhn.hl7v2.parser.CanonicalModelClassFactory;
11  
12  public class ExampleSuperStructures {
13  
14     public static void main(String[] args) throws HL7Exception {
15  
16        /*
17         * When writing HL7 compliant applications, a common complaint is that
18         * different message types use different structures which are similar, but
19         * not identical.
20         * 
21         * When your application receives an ADT^A01 message, it will parse it
22         * into an ADT_A01 structure. When your application receives an ADT^A17
23         * message, it will parse it into an ADT_A17 structure.
24         * 
25         * Naturally you can use casting as a means of dealing with this:
26         */
27  
28        DefaultHapiContextDefaultHapiContext">DefaultHapiContext ctx = new DefaultHapiContext();
29        Message msg = ctx.getPipeParser().parse(" .. some hl7 message ..");
30        if (msg instanceof ADT_A01) {
31           processAdtA01((ADT_A01) msg);
32        } else if (msg instanceof ADT_A02) {
33           processAdtA02((ADT_A02) msg);
34        } // etc...
35  
36        /*
37         * An alternative way of accomplishing this is to use a SuperStructure.
38         * The ADT_AXX structure is a single message structure class which
39         * contains all of the segments for *every* possible ADT message. This
40         * allows you to write a single routine which handles all ADT messages.
41         * 
42         * To take advantage of the ADT_AXX structure, you configure your
43         * HapiContext to use a special ModelClassFactory, which tells parsers to
44         * always use the same structure.
45         */
46  
47        ctx.setModelClassFactory(new CanonicalModelClassFactory(ADT_AXX.class));
48  
49        msg = ctx.getPipeParser().parse(" .. some hl7 message ..");
50  
51        // Since we configured the ModelClassFactory to use the ADT_AXX above, we
52        // can cast
53        ADT_AXX="../../../../ca/uhn/hl7v2/model/v25/message/ADT_AXX.html#ADT_AXX">ADT_AXX axx = (ADT_AXX) msg;
54  
55        // We can now always ask for the patient from the message in the same way
56        processPID(axx.getPID());
57  
58        // Some segments are specific to certain message types only
59        String trigger = axx.getMSH().getMsh9_MessageType().getMsg2_TriggerEvent().getValue();
60        if ("A17".equals(trigger)) {
61           PID pid1 = axx.getPID();
62           PID pid2 = axx.getPID2(); // This exists only in ADT^A17
63           processBedSwap(pid1, pid2);
64        }
65        
66        /*
67         * It is important to stress that the AXX Superstructure has placeholders
68         * for every possible segment in every ADT message structure, but for
69         * any given message only some segments will be used (except MSH and PID, 
70         * which are common to all ADT messages). This means you will need to 
71         * be careful to check the MSH-9-2 value to figure out which data belongs.
72         */
73        
74        /*
75         * The Superstructure can also be used to create messages for sending.
76         * As with receiving, you need to be careful to only populate segments
77         * which belong to a given message type. If you do not, the validator
78         * will throw an exception 
79         */
80  
81     }
82  
83     private static void processBedSwap(PIDef="../../../../ca/uhn/hl7v2/model/v25/segment/PID.html#PID">PID thePid1, PID thePid2) {
84        // swap beds
85     }
86  
87     private static void processAdtA01(ADT_A01 theMsg) {
88        // Process the PID segment
89        processPID(theMsg.getPID());
90  
91        // do other things...
92     }
93  
94     private static void processAdtA02(ADT_A02 theMsg) {
95        // Process the PID segment
96        processPID(theMsg.getPID());
97  
98        // do other things...
99     }
100 
101    private static void processPID(PID thePid) {
102       // do things...
103    }
104 
105 }