View Javadoc
1   package ca.uhn.fhir.jpa.subscription;
2   
3   /*-
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import ca.uhn.fhir.context.FhirContext;
24  import com.fasterxml.jackson.annotation.JsonAutoDetect;
25  import com.fasterxml.jackson.annotation.JsonInclude;
26  import com.fasterxml.jackson.annotation.JsonProperty;
27  import org.apache.commons.lang3.builder.EqualsBuilder;
28  import org.apache.commons.lang3.builder.HashCodeBuilder;
29  import org.hl7.fhir.instance.model.api.IIdType;
30  import org.hl7.fhir.instance.model.api.IPrimitiveType;
31  import org.hl7.fhir.r4.model.EventDefinition;
32  import org.hl7.fhir.r4.model.Subscription;
33  
34  import java.io.Serializable;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import static org.apache.commons.lang3.StringUtils.isNotBlank;
39  
40  @JsonInclude(JsonInclude.Include.NON_NULL)
41  @JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
42  public class CanonicalSubscription implements Serializable {
43  
44  	private static final long serialVersionUID = 1L;
45  
46  	@JsonProperty("id")
47  	private String myIdElement;
48  	@JsonProperty("criteria")
49  	private String myCriteriaString;
50  	@JsonProperty("endpointUrl")
51  	private String myEndpointUrl;
52  	@JsonProperty("payload")
53  	private String myPayloadString;
54  	@JsonProperty("headers")
55  	private List<String> myHeaders;
56  	@JsonProperty("channelType")
57  	private Subscription.SubscriptionChannelType myChannelType;
58  	@JsonProperty("status")
59  	private Subscription.SubscriptionStatus myStatus;
60  	@JsonProperty("triggerDefinition")
61  	private CanonicalEventDefinition myTrigger;
62  	@JsonProperty("emailDetails")
63  	private EmailDetails myEmailDetails;
64  	@JsonProperty("restHookDetails")
65  	private RestHookDetails myRestHookDetails;
66  
67  	/**
68  	 * For now we're using the R4 TriggerDefinition, but this
69  	 * may change in the future when things stabilize
70  	 */
71  	public void addTrigger(CanonicalEventDefinition theTrigger) {
72  		myTrigger = theTrigger;
73  	}
74  
75  
76  	public Subscription.SubscriptionChannelType getChannelType() {
77  		return myChannelType;
78  	}
79  
80  	public void setChannelType(Subscription.SubscriptionChannelType theChannelType) {
81  		myChannelType = theChannelType;
82  	}
83  
84  	public String getCriteriaString() {
85  		return myCriteriaString;
86  	}
87  
88  	public void setCriteriaString(String theCriteriaString) {
89  		myCriteriaString = theCriteriaString;
90  	}
91  
92  	public EmailDetails getEmailDetails() {
93  		if (myEmailDetails == null) {
94  			myEmailDetails = new EmailDetails();
95  		}
96  		return myEmailDetails;
97  	}
98  
99  	public String getEndpointUrl() {
100 		return myEndpointUrl;
101 	}
102 
103 	public void setEndpointUrl(String theEndpointUrl) {
104 		myEndpointUrl = theEndpointUrl;
105 	}
106 
107 	public List<String> getHeaders() {
108 		return myHeaders;
109 	}
110 
111 	public void setHeaders(List<? extends IPrimitiveType<String>> theHeader) {
112 		myHeaders = new ArrayList<>();
113 		for (IPrimitiveType<String> next : theHeader) {
114 			if (isNotBlank(next.getValueAsString())) {
115 				myHeaders.add(next.getValueAsString());
116 			}
117 		}
118 	}
119 
120 	public void setHeaders(String theHeaders) {
121 		myHeaders = new ArrayList<>();
122 		if (isNotBlank(theHeaders)) {
123 			myHeaders.add(theHeaders);
124 		}
125 	}
126 
127 	public IIdType getIdElement(FhirContext theContext) {
128 		IIdType retVal = null;
129 		if (isNotBlank(myIdElement)) {
130 			retVal = theContext.getVersion().newIdType().setValue(myIdElement);
131 		}
132 		return retVal;
133 	}
134 
135 	public String getIdElementString() {
136 		return myIdElement;
137 	}
138 
139 	public String getPayloadString() {
140 		return myPayloadString;
141 	}
142 
143 	public void setPayloadString(String thePayloadString) {
144 		myPayloadString = thePayloadString;
145 	}
146 
147 	public RestHookDetails getRestHookDetails() {
148 		if (myRestHookDetails == null) {
149 			myRestHookDetails = new RestHookDetails();
150 		}
151 		return myRestHookDetails;
152 	}
153 
154 	public Subscription.SubscriptionStatus getStatus() {
155 		return myStatus;
156 	}
157 
158 	public void setStatus(Subscription.SubscriptionStatus theStatus) {
159 		myStatus = theStatus;
160 	}
161 
162 	/**
163 	 * For now we're using the R4 triggerdefinition, but this
164 	 * may change in the future when things stabilize
165 	 */
166 	public CanonicalEventDefinition getTrigger() {
167 		return myTrigger;
168 	}
169 
170 	@Override
171 	public boolean equals(Object theO) {
172 		if (this == theO) return true;
173 
174 		if (theO == null || getClass() != theO.getClass()) return false;
175 
176 		CanonicalSubscription that = (CanonicalSubscription) theO;
177 
178 		EqualsBuilder b = new EqualsBuilder();
179 		b.append(myIdElement, that.myIdElement);
180 		b.append(myCriteriaString, that.myCriteriaString);
181 		b.append(myEndpointUrl, that.myEndpointUrl);
182 		b.append(myPayloadString, that.myPayloadString);
183 		b.append(myHeaders, that.myHeaders);
184 		b.append(myChannelType, that.myChannelType);
185 		b.append(myStatus, that.myStatus);
186 		b.append(myTrigger, that.myTrigger);
187 		b.append(myEmailDetails, that.myEmailDetails);
188 		b.append(myRestHookDetails, that.myRestHookDetails);
189 		return b.isEquals();
190 	}
191 
192 	@Override
193 	public int hashCode() {
194 		return new HashCodeBuilder(17, 37)
195 			.append(myIdElement)
196 			.append(myCriteriaString)
197 			.append(myEndpointUrl)
198 			.append(myPayloadString)
199 			.append(myHeaders)
200 			.append(myChannelType)
201 			.append(myStatus)
202 			.append(myTrigger)
203 			.append(myEmailDetails)
204 			.append(myRestHookDetails)
205 			.toHashCode();
206 	}
207 
208 	public void setIdElement(IIdType theIdElement) {
209 		myIdElement = null;
210 		if (theIdElement != null) {
211 			myIdElement = theIdElement.toUnqualifiedVersionless().getValue();
212 		}
213 	}
214 
215 	@JsonInclude(JsonInclude.Include.NON_NULL)
216 	@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
217 	public static class EmailDetails {
218 		@JsonProperty("from")
219 		private String myFrom;
220 		@JsonProperty("subjectTemplate")
221 		private String mySubjectTemplate;
222 
223 		public String getFrom() {
224 			return myFrom;
225 		}
226 
227 		public void setFrom(String theFrom) {
228 			myFrom = theFrom;
229 		}
230 
231 		public String getSubjectTemplate() {
232 			return mySubjectTemplate;
233 		}
234 
235 		public void setSubjectTemplate(String theSubjectTemplate) {
236 			mySubjectTemplate = theSubjectTemplate;
237 		}
238 	}
239 
240 	@JsonInclude(JsonInclude.Include.NON_NULL)
241 	@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
242 	public static class RestHookDetails {
243 		@JsonProperty("stripVersionId")
244 		private boolean myStripVersionId;
245 		@JsonProperty("deliverLatestVersion")
246 		private boolean myDeliverLatestVersion;
247 
248 		public boolean isDeliverLatestVersion() {
249 			return myDeliverLatestVersion;
250 		}
251 
252 		public void setDeliverLatestVersion(boolean theDeliverLatestVersion) {
253 			myDeliverLatestVersion = theDeliverLatestVersion;
254 		}
255 
256 		@Override
257 		public boolean equals(Object theO) {
258 			if (this == theO) return true;
259 
260 			if (theO == null || getClass() != theO.getClass()) return false;
261 
262 			RestHookDetails that = (RestHookDetails) theO;
263 
264 			return new EqualsBuilder()
265 				.append(myStripVersionId, that.myStripVersionId)
266 				.append(myDeliverLatestVersion, that.myDeliverLatestVersion)
267 				.isEquals();
268 		}
269 
270 		@Override
271 		public int hashCode() {
272 			return new HashCodeBuilder(17, 37)
273 				.append(myStripVersionId)
274 				.append(myDeliverLatestVersion)
275 				.toHashCode();
276 		}
277 
278 		public boolean isStripVersionId() {
279 			return myStripVersionId;
280 		}
281 
282 		public void setStripVersionId(boolean theStripVersionId) {
283 			myStripVersionId = theStripVersionId;
284 		}
285 
286 	}
287 
288 	@JsonInclude(JsonInclude.Include.NON_NULL)
289 	@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
290 	public static class CanonicalEventDefinition {
291 
292 		public CanonicalEventDefinition(EventDefinition theDef) {
293 			// nothing yet
294 		}
295 	}
296 
297 }