1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  package uk.nhs.interoperability.payload;
15  
16  import java.util.Map;
17  import java.util.UUID;
18  
19  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
20  import uk.nhs.interoperability.service.ITKService;
21  import uk.nhs.interoperability.transform.TransformManager;
22  import uk.nhs.interoperability.util.Logger;
23  
24  import com.xmlsolutions.annotation.Requirement;
25  
26  
27  
28  
29  
30  
31  
32  
33  public class DEWrappedMessage extends ITKMessageImpl {
34  	
35  	
36  
37  
38  
39  
40  	public static final boolean DE_ADDRESSED = true;
41  	
42  	
43  
44  
45  
46  
47  	public static final boolean DE_UNADDRESSED = false;
48  	
49  	
50  	private String trackingId;
51  	
52  	
53  
54  
55  
56  	private boolean addressed;
57  	
58  	
59  	private boolean isBase64;
60  	
61  	
62  	private String mimeType;
63  	
64  	
65  
66  
67  	@Requirement(traceTo="COR-DEH-02", status="implemented")
68  	public DEWrappedMessage() {
69  		
70  		this.trackingId = UUID.randomUUID().toString().toUpperCase();
71  	}
72  	
73  	
74  
75  
76  
77  
78  	public DEWrappedMessage(ITKMessage message){
79  		this();
80  		this.setBusinessPayload(message.getBusinessPayload());
81  		this.messageProperties = message.getMessageProperties();
82  		this.addressed = true;
83  	}
84  	
85  	
86  
87  
88  
89  
90  
91  
92  	public DEWrappedMessage(ITKService service, ITKMessage message, boolean addressed){
93  		this();
94  		this.setBusinessPayload(message.getBusinessPayload());
95  		this.messageProperties = message.getMessageProperties();
96  		this.addressed = addressed;
97  		this.isBase64 = service.isBase64();
98  		this.mimeType = service.getMimeType();
99  	}
100 	
101 	
102 
103 
104 	@Requirement(traceTo={"COR-DEH-01", "COR-DEH-04"}, status="implemented")
105 	public String getFullPayload() throws ITKMessagingException {
106 		String fullPayload = "";
107 		
108 		
109 		
110 		String deXML = "<ITKMessage>";
111 		deXML += "<Service>"+this.messageProperties.getServiceId()+"</Service>";
112 		deXML += "<TrackingId>" + this.trackingId + "</TrackingId>";
113 		deXML += "<Sender>"+this.messageProperties.getFromAddress().getURI()+"</Sender>";
114 		deXML += "<SenderType>"+this.messageProperties.getFromAddress().getType()+"</SenderType>";
115 		deXML += "<Recipient>"+this.messageProperties.getToAddress().getURI()+"</Recipient>";
116 		deXML += "<RecipientType>"+this.messageProperties.getToAddress().getType()+"</RecipientType>";
117 		deXML += "<Author>"+this.messageProperties.getAuditIdentity().getURI()+"</Author>";
118 		deXML += "<AuthorType>"+this.messageProperties.getAuditIdentity().getType()+"</AuthorType>";
119 		deXML += "<Manifest id=\""+this.messageProperties.getBusinessPayloadId()+
120 					"\" type=\""+this.mimeType+"\""+
121 				    " profileid=\""+this.messageProperties.getProfileId()+"\" ";
122 		if (this.isBase64){
123 			deXML += " base64=\"true\" ";
124 		}
125 		deXML += " />";
126 		
127 		if (!this.getMessageProperties().getHandlingSpecifications().isEmpty()) {
128 			deXML += "<HandlingSpecs>";
129 			for (Map.Entry<String, String> entry: this.getMessageProperties().getHandlingSpecifications().entrySet()) {
130 				
131 				deXML += "<Spec key=\"" + entry.getKey() + "\" value=\"" + entry.getValue() + "\"/>";
132 			}
133 			deXML += "</HandlingSpecs>";
134 		}
135 		deXML += "<Payload id=\""+this.messageProperties.getBusinessPayloadId()+
136 				"\">"+this.getBusinessPayload()+"</Payload>";
137 		if (this.addressed){
138 			deXML += "<Addressed/>";
139 		}
140 		deXML += "</ITKMessage>";
141 		Logger.trace("DEXML:"+deXML);
142 		fullPayload = TransformManager.doTransform("ToDistributionEnvelope.xslt", deXML);
143 
144 		return fullPayload;
145 		
146 	}
147 	
148 	
149 
150 
151 	public void setBusinessPayload(String businessPayload) {
152 		if (businessPayload != null) {
153 			if (businessPayload.startsWith("<?xml")) {
154 				
155 				this.businessPayload = businessPayload.substring(businessPayload.indexOf(">")+1);
156 			} else {
157 				this.businessPayload = businessPayload;
158 			}
159 			
160 		}
161 	}
162 }