1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  package uk.nhs.interoperability.transport;
15  
16  import java.text.SimpleDateFormat;
17  import java.util.Calendar;
18  
19  import uk.nhs.interoperability.infrastructure.ITKAckDetails;
20  import uk.nhs.interoperability.infrastructure.ITKAddress;
21  import uk.nhs.interoperability.infrastructure.ITKMessageProperties;
22  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
23  import uk.nhs.interoperability.payload.ITKMessageImpl;
24  import uk.nhs.interoperability.transform.TransformManager;
25  import uk.nhs.interoperability.util.Logger;
26  
27  
28  
29  
30  
31  
32  
33  
34  public class ITKInfrastructureAck extends ITKMessageImpl implements ITKAckDetails {
35  	
36  	
37  	public static final String ACK_PROFILE_ID = "urn:nhs-en:profile:ITKInfrastructureAcknowledgement-v1-0";
38  	
39  	
40  	public static final String ACK_SERVICE_ID = "urn:nhs-itk:ns:201005:InfrastructureAcknowledgment";
41  	
42  	
43  	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
44  	
45  	
46  	private String refToTrackingId;
47  	
48  	
49  	private String refToServiceId;
50  	
51  	
52  	private String auditIdentity;
53  	
54  	
55  	private ITKMessagingException itkMessagingException;
56  	
57  	
58  
59  
60  
61  
62  
63  	public ITKInfrastructureAck(ITKMessageProperties requestMessageProperties, String auditIdentity) {
64  		this(requestMessageProperties, auditIdentity, null);
65  	}
66  	
67  	
68  
69  
70  
71  
72  
73  
74  	public ITKInfrastructureAck(ITKMessageProperties requestMessageProperties, String auditIdentity, ITKMessagingException itkMessagingException) {
75  		super(requestMessageProperties, auditIdentity, ACK_PROFILE_ID , true);
76  		this.refToTrackingId = requestMessageProperties.getTrackingId();
77  		this.refToServiceId = requestMessageProperties.getServiceId();
78  		this.auditIdentity = auditIdentity;
79  		this.itkMessagingException = itkMessagingException;
80  		
81  
82  
83  
84  		super.getMessageProperties().setServiceId(ACK_SERVICE_ID);
85  		this.setIsReponse(true);
86  	}
87  
88  	
89  
90  
91  	@Override
92  	public String getFullPayload() throws ITKMessagingException {
93  		String fullPayload = "";
94  		
95  		
96  		
97  		String XML = "<ITKInfraAck>";		
98  		XML += "<RefToTrackingId>" + this.refToTrackingId + "</RefToTrackingId>";
99  		XML += "<RefToService>"+this.refToServiceId+"</RefToService>";
100 		XML += "<AuditId>"+this.auditIdentity+"</AuditId>";
101 		XML += "<Created>" + DATE_FORMAT.format(Calendar.getInstance().getTime()) + "</Created>";
102 		
103 		
104 		if (this.itkMessagingException != null) {
105 			ITKMessagingException ex = this.itkMessagingException;
106 			XML += "<FaultDetail>";
107 			
108 			XML += "  <ErrorID>" + ex.getErrorId() + "</ErrorID>";
109 			XML += "  <ErrorCode codeSystem=\"" + ex.getErrorCodeSystem() + "\">" + ex.getErrorCode() + "</ErrorCode>";
110 			XML += "  <ErrorText>" + ex.decodeErrorCode() + "</ErrorText>";
111 			XML += "  <ErrorDiagnosticText>" + ex.getLocalizedMessage() + "</ErrorDiagnosticText>";
112 			XML += "</FaultDetail>";
113 		}
114 		XML += "</ITKInfraAck>";
115 
116 		fullPayload = TransformManager.doTransform("ToInfrastructureAck.xslt", XML);
117 
118 		return fullPayload;
119 	}
120 	
121 	
122 
123 
124 	@Override
125 	public String getBusinessPayload() {
126 		try {
127 			return this.getFullPayload();
128 		} catch (ITKMessagingException e) {
129 			Logger.error("Could not create infrastructure ack payload", e);
130 		}
131 		return null;
132 	}
133 	
134 	
135 
136 
137 	@Override
138 	public ITKAddress getIntendedRecipient() {
139 		return this.messageProperties != null ? this.messageProperties.getFromAddress() : null;
140 	}
141 	
142 	
143 
144 
145 	@Override
146 	public ITKMessagingException getNackError() {
147 		return this.itkMessagingException;
148 	}
149 	
150 	
151 
152 
153 	@Override
154 	public String getReportingIdentity() {
155 		return this.auditIdentity;
156 	}
157 	
158 	
159 
160 
161 	@Override
162 	public String getServiceRef() {
163 		return this.refToServiceId;
164 	}
165 	
166 	
167 
168 
169 	@Override
170 	public String getTrackingRef() {
171 		return this.refToTrackingId;
172 	}
173 	
174 	
175 
176 
177 	@Override
178 	public boolean isNack() {
179 		return this.getNackError() != null;
180 	}
181 
182 }