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.JsonIgnore;
26  import com.fasterxml.jackson.annotation.JsonInclude;
27  import com.fasterxml.jackson.annotation.JsonProperty;
28  import org.apache.commons.lang3.builder.EqualsBuilder;
29  import org.apache.commons.lang3.builder.HashCodeBuilder;
30  import org.hl7.fhir.instance.model.api.IBaseResource;
31  import org.hl7.fhir.instance.model.api.IIdType;
32  import org.hl7.fhir.instance.model.api.IPrimitiveType;
33  import org.hl7.fhir.r4.model.EventDefinition;
34  import org.hl7.fhir.r4.model.Subscription;
35  
36  import java.io.Serializable;
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  import static org.apache.commons.lang3.StringUtils.isNotBlank;
41  
42  @JsonInclude(JsonInclude.Include.NON_NULL)
43  @JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
44  public class CanonicalSubscription implements Serializable {
45  
46  	private static final long serialVersionUID = 1L;
47  
48  	@JsonProperty("id")
49  	private String myIdElement;
50  	@JsonProperty("criteria")
51  	private String myCriteriaString;
52  	@JsonProperty("endpointUrl")
53  	private String myEndpointUrl;
54  	@JsonProperty("payload")
55  	private String myPayloadString;
56  	@JsonProperty("headers")
57  	private List<String> myHeaders;
58  	@JsonProperty("channelType")
59  	private Subscription.SubscriptionChannelType myChannelType;
60  	@JsonProperty("status")
61  	private Subscription.SubscriptionStatus myStatus;
62  	@JsonIgnore
63  	private transient IBaseResource myBackingSubscription;
64  	@JsonProperty("backingSubscription")
65  	private String myBackingSubscriptionString;
66  	@JsonProperty("triggerDefinition")
67  	private CanonicalEventDefinition myTrigger;
68  	@JsonProperty("emailDetails")
69  	private EmailDetails myEmailDetails;
70  	@JsonProperty("restHookDetails")
71  	private RestHookDetails myRestHookDetails;
72  
73  	/**
74  	 * For now we're using the R4 TriggerDefinition, but this
75  	 * may change in the future when things stabilize
76  	 */
77  	public void addTrigger(CanonicalEventDefinition theTrigger) {
78  		myTrigger = theTrigger;
79  	}
80  
81  	@Override
82  	public boolean equals(Object theO) {
83  		if (this == theO) return true;
84  
85  		if (theO == null || getClass() != theO.getClass()) return false;
86  
87  		CanonicalSubscription that = (CanonicalSubscription) theO;
88  
89  		return new EqualsBuilder()
90  			.append(getIdElementString(), that.getIdElementString())
91  			.isEquals();
92  	}
93  
94  	public IBaseResource getBackingSubscription(FhirContext theCtx) {
95  		if (myBackingSubscription == null && myBackingSubscriptionString != null) {
96  			myBackingSubscription = theCtx.newJsonParser().parseResource(myBackingSubscriptionString);
97  		}
98  		return myBackingSubscription;
99  	}
100 
101 	public Subscription.SubscriptionChannelType getChannelType() {
102 		return myChannelType;
103 	}
104 
105 	public void setChannelType(Subscription.SubscriptionChannelType theChannelType) {
106 		myChannelType = theChannelType;
107 	}
108 
109 	public String getCriteriaString() {
110 		return myCriteriaString;
111 	}
112 
113 	public void setCriteriaString(String theCriteriaString) {
114 		myCriteriaString = theCriteriaString;
115 	}
116 
117 	public EmailDetails getEmailDetails() {
118 		if (myEmailDetails == null) {
119 			myEmailDetails = new EmailDetails();
120 		}
121 		return myEmailDetails;
122 	}
123 
124 	public String getEndpointUrl() {
125 		return myEndpointUrl;
126 	}
127 
128 	public void setEndpointUrl(String theEndpointUrl) {
129 		myEndpointUrl = theEndpointUrl;
130 	}
131 
132 	public List<String> getHeaders() {
133 		return myHeaders;
134 	}
135 
136 	public void setHeaders(String theHeaders) {
137 		myHeaders = new ArrayList<>();
138 		if (isNotBlank(theHeaders)) {
139 			myHeaders.add(theHeaders);
140 		}
141 	}
142 
143 	public IIdType getIdElement(FhirContext theContext) {
144 		IIdType retVal = null;
145 		if (isNotBlank(myIdElement)) {
146 			retVal = theContext.getVersion().newIdType().setValue(myIdElement);
147 		}
148 		return retVal;
149 	}
150 
151 	String getIdElementString() {
152 		return myIdElement;
153 	}
154 
155 	public String getPayloadString() {
156 		return myPayloadString;
157 	}
158 
159 	public void setPayloadString(String thePayloadString) {
160 		myPayloadString = thePayloadString;
161 	}
162 
163 	public RestHookDetails getRestHookDetails() {
164 		if (myRestHookDetails == null) {
165 			myRestHookDetails = new RestHookDetails();
166 		}
167 		return myRestHookDetails;
168 	}
169 
170 	public Subscription.SubscriptionStatus getStatus() {
171 		return myStatus;
172 	}
173 
174 	public void setStatus(Subscription.SubscriptionStatus theStatus) {
175 		myStatus = theStatus;
176 	}
177 
178 	/**
179 	 * For now we're using the R4 triggerdefinition, but this
180 	 * may change in the future when things stabilize
181 	 */
182 	public CanonicalEventDefinition getTrigger() {
183 		return myTrigger;
184 	}
185 
186 	@Override
187 	public int hashCode() {
188 		return new HashCodeBuilder(17, 37)
189 			.append(getIdElementString())
190 			.toHashCode();
191 	}
192 
193 	public void setBackingSubscription(FhirContext theCtx, IBaseResource theBackingSubscription) {
194 		myBackingSubscription = theBackingSubscription;
195 		myBackingSubscriptionString = null;
196 		if (myBackingSubscription != null) {
197 			myBackingSubscriptionString = theCtx.newJsonParser().encodeResourceToString(myBackingSubscription);
198 		}
199 	}
200 
201 	public void setHeaders(List<? extends IPrimitiveType<String>> theHeader) {
202 		myHeaders = new ArrayList<>();
203 		for (IPrimitiveType<String> next : theHeader) {
204 			if (isNotBlank(next.getValueAsString())) {
205 				myHeaders.add(next.getValueAsString());
206 			}
207 		}
208 	}
209 
210 	public void setIdElement(IIdType theIdElement) {
211 		myIdElement = null;
212 		if (theIdElement != null) {
213 			myIdElement = theIdElement.toUnqualifiedVersionless().getValue();
214 		}
215 	}
216 
217 	@JsonInclude(JsonInclude.Include.NON_NULL)
218 	@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
219 	public static class EmailDetails {
220 		@JsonProperty("from")
221 		private String myFrom;
222 		@JsonProperty("subjectTemplate")
223 		private String mySubjectTemplate;
224 
225 		public String getFrom() {
226 			return myFrom;
227 		}
228 
229 		public void setFrom(String theFrom) {
230 			myFrom = theFrom;
231 		}
232 
233 		public String getSubjectTemplate() {
234 			return mySubjectTemplate;
235 		}
236 
237 		public void setSubjectTemplate(String theSubjectTemplate) {
238 			mySubjectTemplate = theSubjectTemplate;
239 		}
240 	}
241 
242 	@JsonInclude(JsonInclude.Include.NON_NULL)
243 	@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
244 	public static class RestHookDetails {
245 		@JsonProperty("stripVersionId")
246 		private boolean myStripVersionId;
247 		@JsonProperty("deliverLatestVersion")
248 		private boolean myDeliverLatestVersion;
249 
250 		public boolean isDeliverLatestVersion() {
251 			return myDeliverLatestVersion;
252 		}
253 
254 		public void setDeliverLatestVersion(boolean theDeliverLatestVersion) {
255 			myDeliverLatestVersion = theDeliverLatestVersion;
256 		}
257 
258 		public boolean isStripVersionId() {
259 			return myStripVersionId;
260 		}
261 
262 		public void setStripVersionId(boolean theStripVersionId) {
263 			myStripVersionId = theStripVersionId;
264 		}
265 
266 	}
267 
268 	@JsonInclude(JsonInclude.Include.NON_NULL)
269 	@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE)
270 	public static class CanonicalEventDefinition {
271 
272 		public CanonicalEventDefinition(EventDefinition theDef) {
273 			// nothing yet
274 		}
275 	}
276 
277 }