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.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   * The Class ITKInfrastructureAck.
29   *
30   * @author Michael Odling-Smee
31   * @author Nicholas Jones
32   * @since 0.1
33   */
34  public class ITKInfrastructureAck extends ITKMessageImpl implements ITKAckDetails {
35  	
36  	/** The Constant ACK_PROFILE_ID. */
37  	public static final String ACK_PROFILE_ID = "urn:nhs-en:profile:ITKInfrastructureAcknowledgement-v1-0";
38  	
39  	/** The Constant ACK_SERVICE_ID. */
40  	public static final String ACK_SERVICE_ID = "urn:nhs-itk:ns:201005:InfrastructureAcknowledgment";
41  	
42  	/** The Constant DATE_FORMAT. */
43  	private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
44  	
45  	/** The ref to tracking id. */
46  	private String refToTrackingId;
47  	
48  	/** The ref to service id. */
49  	private String refToServiceId;
50  	
51  	/** The audit identity. */
52  	private String auditIdentity;
53  	
54  	/** The itk messaging exception. */
55  	private ITKMessagingException itkMessagingException;
56  	
57  	/**
58  	 * Instantiates a new iTK infrastructure ack.
59  	 *
60  	 * @param requestMessageProperties the request message properties
61  	 * @param auditIdentity the audit identity
62  	 */
63  	public ITKInfrastructureAck(ITKMessageProperties requestMessageProperties, String auditIdentity) {
64  		this(requestMessageProperties, auditIdentity, null);
65  	}
66  	
67  	/**
68  	 * Instantiates a new iTK infrastructure ack.
69  	 *
70  	 * @param requestMessageProperties the request message properties
71  	 * @param auditIdentity the audit identity
72  	 * @param itkMessagingException the itk messaging exception
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  		 * Service is not simply request service with "Response" suffix.
82  		 * InfrastructureAck has its own specific service
83  		 */
84  		super.getMessageProperties().setServiceId(ACK_SERVICE_ID);
85  		this.setIsReponse(true);
86  	}
87  
88  	/* (non-Javadoc)
89  	 * @see uk.nhs.interoperability.payload.ITKMessage#getFullPayload()
90  	 */
91  	@Override
92  	public String getFullPayload() throws ITKMessagingException {
93  		String fullPayload = "";
94  		
95  		// TODO: Create a DistributionEnvelope object with a serialize method
96  		//       to do this job.
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 			//TODO - need information to determine whether it is a client or server soap fault
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 	/* (non-Javadoc)
122 	 * @see uk.nhs.interoperability.payload.ITKMessageImpl#getBusinessPayload()
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 	/* (non-Javadoc)
135 	 * @see uk.nhs.interoperability.infrastructure.ITKAckDetails#getIntendedRecipient()
136 	 */
137 	@Override
138 	public ITKAddress getIntendedRecipient() {
139 		return this.messageProperties != null ? this.messageProperties.getFromAddress() : null;
140 	}
141 	
142 	/* (non-Javadoc)
143 	 * @see uk.nhs.interoperability.infrastructure.ITKAckDetails#getNackError()
144 	 */
145 	@Override
146 	public ITKMessagingException getNackError() {
147 		return this.itkMessagingException;
148 	}
149 	
150 	/* (non-Javadoc)
151 	 * @see uk.nhs.interoperability.infrastructure.ITKAckDetails#getReportingIdentity()
152 	 */
153 	@Override
154 	public String getReportingIdentity() {
155 		return this.auditIdentity;
156 	}
157 	
158 	/* (non-Javadoc)
159 	 * @see uk.nhs.interoperability.infrastructure.ITKAckDetails#getServiceRef()
160 	 */
161 	@Override
162 	public String getServiceRef() {
163 		return this.refToServiceId;
164 	}
165 	
166 	/* (non-Javadoc)
167 	 * @see uk.nhs.interoperability.infrastructure.ITKAckDetails#getTrackingRef()
168 	 */
169 	@Override
170 	public String getTrackingRef() {
171 		return this.refToTrackingId;
172 	}
173 	
174 	/* (non-Javadoc)
175 	 * @see uk.nhs.interoperability.infrastructure.ITKAckDetails#isNack()
176 	 */
177 	@Override
178 	public boolean isNack() {
179 		return this.getNackError() != null;
180 	}
181 
182 }