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 ADTSyncSender.
35   *
36   * @author Michael Odling-Smee
37   * @author Nicholas Jones
38   * @since 0.1
39   */
40  public class ADTSyncSender { 
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  		ADTSyncSender appA = new ADTSyncSender();
68  		appA.sendSyncMessage();
69  	
70  	}
71  
72  	/**
73  	 * Send sync message.
74  	 */
75  	private void sendSyncMessage(){
76  		Logger.info("*** ADTSyncSender: Starting sendSyncMessage");
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 			ITKMessage response = sender.sendSync(msg);
110 		
111 			Logger.trace("ADTSyncSender received response:"+response.getBusinessPayload());
112 			
113 		} catch (ITKMessagingException e) {
114 			Logger.error("Error Sending ITK Message",e);
115 		}
116 		
117 		Logger.info("*** ADTSyncSender: Ending sendSyncMessage");
118 	}
119 
120 	/**
121 	 * Read file.
122 	 *
123 	 * @param fname the fname
124 	 * @return the string
125 	 * @throws IOException Signals that an I/O exception has occurred.
126 	 */
127 	private String readFile(String fname) throws IOException {
128 
129 	    InputStream tis = this.getClass().getResourceAsStream("/messages/"+fname);
130 	    StringBuilder fileContents = new StringBuilder();
131 	    Scanner scanner = new Scanner(tis);
132 	    String lineSeparator = System.getProperty("line.separator");
133 
134 	    try {
135 	        while(scanner.hasNextLine()) {        
136 	            fileContents.append(scanner.nextLine() + lineSeparator);
137 	        }
138 	        return fileContents.toString();
139 	    } finally {
140 	        scanner.close();
141 	    }
142 	}
143 
144 }