1 /**
2 * The contents of this file are subject to the Mozilla Public License Version 1.1
3 * (the "License"); you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
5 * Software distributed under the License is distributed on an "AS IS" basis,
6 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7 * specific language governing rights and limitations under the License.
8 *
9 * The Original Code is "CreateAMessage.java". Description:
10 * "Example Code"
11 *
12 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
13 * 2001. All Rights Reserved.
14 *
15 * Contributor(s): James Agnew
16 *
17 * Alternatively, the contents of this file may be used under the terms of the
18 * GNU General Public License (the �GPL�), in which case the provisions of the GPL are
19 * applicable instead of those above. If you wish to allow use of your version of this
20 * file only under the terms of the GPL and not to allow others to use your version
21 * of this file under the MPL, indicate your decision by deleting the provisions above
22 * and replace them with the notice and other provisions required by the GPL License.
23 * If you do not delete the provisions above, a recipient may use your version of
24 * this file under either the MPL or the GPL.
25 *
26 */
27 package ca.uhn.hl7v2.examples;
28
29 import ca.uhn.hl7v2.HL7Exception;
30 import ca.uhn.hl7v2.model.v24.message.ADT_A01;
31 import ca.uhn.hl7v2.model.v24.segment.MSH;
32 import ca.uhn.hl7v2.model.v24.segment.PID;
33 import ca.uhn.hl7v2.parser.DefaultXMLParser;
34 import ca.uhn.hl7v2.parser.Parser;
35 import ca.uhn.hl7v2.parser.PipeParser;
36
37 /**
38 * Example transmitting a message
39 *
40 * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
41 * @version $Revision: 1.4 $ updated on $Date: 2009/10/03 15:29:05 $ by $Author: jamesagnew $
42 */
43 public class CreateAMessage
44 {
45
46 /**
47 * @param args
48 * @throws HL7Exception
49 */
50 public static void main(String[] args) throws HL7Exception {
51
52 ADT_A01 adt = new ADT_A01();
53
54 // Populate the MSH Segment
55 MSH mshSegment = adt.getMSH();
56 mshSegment.getFieldSeparator().setValue("|");
57 mshSegment.getEncodingCharacters().setValue("^~\\&");
58 mshSegment.getDateTimeOfMessage().getTimeOfAnEvent().setValue("200701011539");
59 mshSegment.getSendingApplication().getNamespaceID().setValue("TestSendingSystem");
60 mshSegment.getSequenceNumber().setValue("123");
61 mshSegment.getMessageType().getMessageType().setValue("ADT");
62 mshSegment.getMessageType().getTriggerEvent().setValue("A01");
63 mshSegment.getMessageType().getMessageStructure().setValue("ADT A01");
64
65 // Populate the PID Segment
66 PID pid = adt.getPID();
67 pid.getPatientName(0).getFamilyName().getSurname().setValue("Doe");
68 pid.getPatientName(0).getGivenName().setValue("John");
69 pid.getPatientIdentifierList(0).getID().setValue("123456");
70
71 /*
72 * In a real situation, of course, many more segments and fields would be populated
73 */
74
75 // Now, let's encode the message and look at the output
76 Parser parser = new PipeParser();
77 String encodedMessage = parser.encode(adt);
78 System.out.println("Printing ER7 Encoded Message:");
79 System.out.println(encodedMessage);
80
81 /*
82 * Prints:
83 *
84 * MSH|^~\&|TestSendingSystem||||200701011539||ADT^A01^ADT A01||||123
85 * PID|||123456||Doe^John
86 */
87
88 // Next, let's use the XML parser to encode as XML
89 parser = new DefaultXMLParser();
90 encodedMessage = parser.encode(adt);
91 System.out.println("Printing XML Encoded Message:");
92 System.out.println(encodedMessage);
93
94 /*
95 * Prints:
96 *
97 * <?xml version="1.0" encoding="UTF-8"?>
98 <ADT_A01 xmlns="urn:hl7-org:v2xml">
99 <MSH>
100 <MSH.1>|</MSH.1>
101 <MSH.2>^~\&</MSH.2>
102 <MSH.3>
103 <HD.1>TestSendingSystem</HD.1>
104 </MSH.3>
105 <MSH.7>
106 <TS.1>200701011539</TS.1>
107 </MSH.7>
108 <MSH.9>
109 <MSG.1>ADT</MSG.1>
110 <MSG.2>A01</MSG.2>
111 <MSG.3>ADT A01</MSG.3>
112 </MSH.9>
113 <MSH.13>123</MSH.13>
114 </MSH>
115 <PID>
116 <PID.3>
117 <CX.1>123456</CX.1>
118 </PID.3>
119 <PID.5>
120 <XPN.1>
121 <FN.1>Doe</FN.1>
122 </XPN.1>
123 <XPN.2>John</XPN.2>
124 </PID.5>
125 </PID>
126 </ADT_A01>
127 */
128
129 }
130
131 }