View Javadoc
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 "EncapsulatedData.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): Alessandro Delprete
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 java.io.ByteArrayOutputStream;
30  import java.io.IOException;
31  
32  import javax.mail.MessagingException;
33  import javax.mail.internet.MimeBodyPart;
34  import javax.mail.internet.MimeMultipart;
35  
36  import ca.uhn.hl7v2.HL7Exception;
37  import ca.uhn.hl7v2.model.v231.datatype.ED;
38  import ca.uhn.hl7v2.model.v231.message.MDM_T02;
39  import ca.uhn.hl7v2.model.v231.segment.OBX;
40  import ca.uhn.hl7v2.parser.PipeParser;
41  
42  public class EncapsulatedData {
43  
44  	public static void main(String[] args) throws MessagingException, IOException, HL7Exception {
45  
46  		/*
47  		 * In this example, a CDA r2 document is being embedded as mime content
48  		 * in an MDM_T02 message.
49  		 */
50  		
51  		MDM_T02 mdmMessage = new MDM_T02();
52  		
53  		// A few basic MSH fields are populated. In a real situation, this would not be enough
54          // to produce a valid message, but for demonstration purposes we'll skip a few
55          // fields.
56  		mdmMessage.initQuickstart("MDM", "T02", "P");
57  
58          OBX obx = mdmMessage.getOBX();
59  		
60          // This is not a complete CDA document, but it serves as a good example
61          String sampleClinicalDocument = "<ClinicalDocument xsi:schemaLocation=\"urn:hl7-org:v3 CDA.ReleaseTwo.CommitteeBallot03.Aug.2004.xsd\" templateId=\"2.16.840.1.113883.3.27.1776\">\r\n" + 
62          		"   <id extension=\"c266\" root=\"2.16.840.1.113883.3.933\"/>\r\n" + 
63          		"   <code code=\"11488-4\" codeSystem=\"2.16.840.1.113883.6.1\" displayName=\"Consultation note\"/>\r\n" + 
64          		"   <title>Good Health Clinic Consultation Note</title>\r\n" + 
65          		"   <effectiveTime value=\"20000407\"/>\r\n" + 
66          		"</ClinicalDocument>";
67          
68  
69  		MimeMultipart mimeMessage = new MimeMultipart();
70  		MimeBodyPart bodyPart = new MimeBodyPart();
71  		bodyPart.setText(sampleClinicalDocument);
72  		mimeMessage.addBodyPart(bodyPart);
73  		
74  		// send the MIME message into a ByteArrayOutputStream
75  		ByteArrayOutputStream byte1 = new ByteArrayOutputStream();
76  		mimeMessage.writeTo(byte1);
77  
78  		// set the value of ED with the String
79  		ED encapsuledData = new ED(mdmMessage);
80  
81  		encapsuledData.getTypeOfData().setValue("multipart");
82  		encapsuledData.getDataSubtype().setValue("x-hl7-cda-level-one");
83  		encapsuledData.getEncoding().setValue("A");
84  		
85  		encapsuledData.getData().setValue( byte1.toString() );
86  
87  		// setting the data in the OBX segment
88  		obx.getObservationIdentifier().getIdentifier().setValue("1");
89  		obx.getValueType().setValue("CE");
90  		obx.getObservationValue(0).setData(encapsuledData);
91  		
92  		// Print the message out
93  		System.out.println(new PipeParser().encode(mdmMessage));
94  		
95  		/*
96  			The following output is produced. Note that the newlines are unix newlines (\n)
97  			and not (\r), so they are OK to use within a segment.
98  
99  			MSH|^~\&
100 			OBX||CE|1||^multipart^x-hl7-cda-level-one^A^------=_Part_0_605645.1247937122564\X000d\
101 			\X000d\
102 			<ClinicalDocument xsi:schemaLocation="urn:hl7-org:v3 CDA.ReleaseTwo.CommitteeBallot03.Aug.2004.xsd" templateId="2.16.840.1.113883.3.27.1776">\X000d\
103 			   <id extension="c266" root="2.16.840.1.113883.3.933"/>\X000d\
104 			   <code code="11488-4" codeSystem="2.16.840.1.113883.6.1" displayName="Consultation note"/>\X000d\
105 			   <title>Good Health Clinic Consultation Note</title>\X000d\
106 			   <effectiveTime value="20000407"/>\X000d\
107 			</ClinicalDocument>\X000d\
108 			------=_Part_0_605645.1247937122564--\X000d\
109 		
110 		 */
111 	}
112 	
113 }