View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.client.samples.adt;
15  
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.util.Scanner;
19  import java.util.UUID;
20  
21  import uk.nhs.interoperability.infrastructure.ITKAddressImpl;
22  import uk.nhs.interoperability.infrastructure.ITKIdentity;
23  import uk.nhs.interoperability.infrastructure.ITKIdentityImpl;
24  import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
25  import uk.nhs.interoperability.infrastructure.ITKMessagePropertiesImpl;
26  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
27  import uk.nhs.interoperability.payload.ITKMessage;
28  import uk.nhs.interoperability.payload.SimpleMessage;
29  import uk.nhs.interoperability.source.ITKMessageSender;
30  import uk.nhs.interoperability.source.ITKMessageSenderImpl;
31  import uk.nhs.interoperability.util.Logger;
32  
33  /**
34   * The Class ADTAsyncSender.
35   *
36   * @author Michael Odling-Smee
37   * @author Nicholas Jones
38   * @since 0.1
39   */
40  public class ADTAsyncSender { 
41  	
42  	/** The Constant ORGB. */
43  	private static final String ORGB = "urn:nhs-uk:addressing:ods:TESTORGS:ORGB";
44  
45  	/** The Constant ORGID. */
46  	private static final String ORGID = "urn:nhs-uk:identity:ods:TESTORGS:ORGA";
47  
48  	/** The Constant AUDITID. */
49  	private static final ITKIdentity AUDITID = new ITKIdentityImpl(ORGID);
50  	
51  	/** The Constant FROMADDRESS. */
52  	private static final String FROMADDRESS = "urn:nhs-uk:addressing:ods:TESTORGS:ORGA";
53  
54  	/** The Constant TRANSFER_PATIENT_SERVICE. */
55  	private static final String TRANSFER_PATIENT_SERVICE = "urn:nhs-itk:services:201005:transferPatient-v1-0";
56  	
57  	/** The Constant TRANSFER_PATIENT_PROFILEID. */
58  	private static final String TRANSFER_PATIENT_PROFILEID = "urn:nhs-en:profile:201005:transferPatient-v1-0";
59  
60  	/**
61  	 * The main method.
62  	 *
63  	 * @param args the arguments
64  	 */
65  	public static void main(String[] args) {
66  
67  		ADTAsyncSender appA = new ADTAsyncSender();
68  		appA.sendAsyncMessage();
69  	
70  	}
71  
72  	/**
73  	 * Send async message.
74  	 */
75  	private void sendAsyncMessage(){
76  		Logger.info("*** ADTAsyncSender: Starting sendAsyncMessage");
77  		
78  		// Create the message
79  		ITKMessage msg = new SimpleMessage();
80  
81  		// populate the payload
82  		try {
83  			msg.setBusinessPayload(readFile("ADT_TransferPatient.txt"));
84  		} catch (IOException e1) {
85  			Logger.error("Error Loading TEST ADT Message",e1);
86  			return;
87  		}
88  		
89  		UUID messageUUID = UUID.randomUUID();
90  		String messageId = messageUUID.toString();
91  		
92  		// Build the message properties.
93  		ITKMessageProperties mp = new ITKMessagePropertiesImpl();
94  		mp.setAuditIdentity(AUDITID);
95  		mp.setFromAddress(new ITKAddressImpl(FROMADDRESS));
96  		mp.setToAddress(new ITKAddressImpl(ORGB));
97  		mp.setServiceId(TRANSFER_PATIENT_SERVICE);
98  		mp.setBusinessPayloadId(messageId);
99  		mp.setProfileId(TRANSFER_PATIENT_PROFILEID);
100 
101 		// Add the properties to the message
102 		msg.setMessageProperties(mp);
103 		
104 		// Create the sender
105 		ITKMessageSender sender = new ITKMessageSenderImpl();
106 
107 		try {
108 			// Send this message synchronously. The response message will be returned
109 			sender.sendAsync(msg);
110 			
111 		} catch (ITKMessagingException e) {
112 			Logger.error("Error Sending ITK Message",e);
113 		}
114 		
115 		Logger.info("*** ADTSyncSender: Ending sendAsyncMessage");
116 	}
117 
118 	/**
119 	 * Read file.
120 	 *
121 	 * @param fname the fname
122 	 * @return the string
123 	 * @throws IOException Signals that an I/O exception has occurred.
124 	 */
125 	private String readFile(String fname) throws IOException {
126 
127 	    InputStream tis = this.getClass().getResourceAsStream("/messages/"+fname);
128 	    StringBuilder fileContents = new StringBuilder();
129 	    Scanner scanner = new Scanner(tis);
130 	    String lineSeparator = System.getProperty("line.separator");
131 
132 	    try {
133 	        while(scanner.hasNextLine()) {        
134 	            fileContents.append(scanner.nextLine() + lineSeparator);
135 	        }
136 	        return fileContents.toString();
137 	    } finally {
138 	        scanner.close();
139 	    }
140 	}
141 
142 }