View Javadoc
1   package ca.uhn.fhir.jpa.provider.dstu3;
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.jpa.dao.IFhirResourceDao;
24  import ca.uhn.fhir.jpa.provider.BaseJpaResourceProvider;
25  import ca.uhn.fhir.jpa.util.JpaConstants;
26  import ca.uhn.fhir.rest.annotation.*;
27  import ca.uhn.fhir.rest.api.EncodingEnum;
28  import ca.uhn.fhir.rest.api.MethodOutcome;
29  import ca.uhn.fhir.rest.api.ValidationModeEnum;
30  import ca.uhn.fhir.rest.api.server.RequestDetails;
31  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
32  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
33  import org.hl7.fhir.convertors.VersionConvertor_30_40;
34  import org.hl7.fhir.dstu3.model.*;
35  import org.hl7.fhir.exceptions.FHIRException;
36  import org.hl7.fhir.instance.model.api.IAnyResource;
37  import org.hl7.fhir.instance.model.api.IIdType;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import static ca.uhn.fhir.jpa.util.JpaConstants.OPERATION_META;
42  import static ca.uhn.fhir.jpa.util.JpaConstants.OPERATION_META_ADD;
43  import static ca.uhn.fhir.jpa.util.JpaConstants.OPERATION_META_DELETE;
44  
45  public class JpaResourceProviderDstu3<T extends IAnyResource> extends BaseJpaResourceProvider<T> {
46  
47  	public JpaResourceProviderDstu3() {
48  		// nothing
49  	}
50  
51  	public JpaResourceProviderDstu3(IFhirResourceDao<T> theDao) {
52  		super(theDao);
53  	}
54  
55  	@Create
56  	public MethodOutcome create(HttpServletRequest theRequest, @ResourceParam T theResource, @ConditionalUrlParam String theConditional, RequestDetails theRequestDetails) {
57  		startRequest(theRequest);
58  		try {
59  			if (theConditional != null) {
60  				return getDao().create(theResource, theConditional, theRequestDetails);
61  			} else {
62  				return getDao().create(theResource, theRequestDetails);
63  			}
64  		} finally {
65  			endRequest(theRequest);
66  		}
67  	}
68  
69  	@Delete()
70  	public MethodOutcome delete(HttpServletRequest theRequest, @IdParam IdType theResource, @ConditionalUrlParam(supportsMultiple = true) String theConditional, RequestDetails theRequestDetails) {
71  		startRequest(theRequest);
72  		try {
73  			if (theConditional != null) {
74  				return getDao().deleteByUrl(theConditional, theRequestDetails);
75  			} else {
76  				return getDao().delete(theResource, theRequestDetails);
77  			}
78  		} finally {
79  			endRequest(theRequest);
80  		}
81  	}
82  
83  	@Operation(name = JpaConstants.OPERATION_EXPUNGE, idempotent = false, returnParameters = {
84  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_OUT_PARAM_EXPUNGE_COUNT, type = IntegerType.class)
85  	})
86  	public Parameters expunge(
87  		@IdParam IIdType theIdParam,
88  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_LIMIT) IntegerType theLimit,
89  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_DELETED_RESOURCES) BooleanType theExpungeDeletedResources,
90  		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_PREVIOUS_VERSIONS) BooleanType theExpungeOldVersions
91  	) {
92  		org.hl7.fhir.r4.model.Parameters retVal = super.doExpunge(theIdParam, theLimit, theExpungeDeletedResources, theExpungeOldVersions, null);
93  		try {
94  			return VersionConvertor_30_40.convertParameters(retVal);
95  		} catch (FHIRException e) {
96  			throw new InternalErrorException(e);
97  		}
98  	}
99  
100 	@Operation(name = JpaConstants.OPERATION_EXPUNGE, idempotent = false, returnParameters = {
101 		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_OUT_PARAM_EXPUNGE_COUNT, type = IntegerType.class)
102 	})
103 	public Parameters expunge(
104 		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_LIMIT) IntegerType theLimit,
105 		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_DELETED_RESOURCES) BooleanType theExpungeDeletedResources,
106 		@OperationParam(name = JpaConstants.OPERATION_EXPUNGE_PARAM_EXPUNGE_PREVIOUS_VERSIONS) BooleanType theExpungeOldVersions
107 	) {
108 		org.hl7.fhir.r4.model.Parameters retVal = super.doExpunge(null, theLimit, theExpungeDeletedResources, theExpungeOldVersions, null);
109 		try {
110 			return VersionConvertor_30_40.convertParameters(retVal);
111 		} catch (FHIRException e) {
112 			throw new InternalErrorException(e);
113 		}
114 	}
115 
116 	@Operation(name = OPERATION_META, idempotent = true, returnParameters = {
117 		@OperationParam(name = "return", type = Meta.class)
118 	})
119 	public Parameters meta(RequestDetails theRequestDetails) {
120 		Parameters parameters = new Parameters();
121 		Meta metaGetOperation = getDao().metaGetOperation(Meta.class, theRequestDetails);
122 		parameters.addParameter().setName("return").setValue(metaGetOperation);
123 		return parameters;
124 	}
125 
126 	@Operation(name = OPERATION_META, idempotent = true, returnParameters = {
127 		@OperationParam(name = "return", type = Meta.class)
128 	})
129 	public Parameters meta(@IdParam IdType theId, RequestDetails theRequestDetails) {
130 		Parameters parameters = new Parameters();
131 		Meta metaGetOperation = getDao().metaGetOperation(Meta.class, theId, theRequestDetails);
132 		parameters.addParameter().setName("return").setValue(metaGetOperation);
133 		return parameters;
134 	}
135 
136 	@Operation(name = OPERATION_META_ADD, idempotent = true, returnParameters = {
137 		@OperationParam(name = "return", type = Meta.class)
138 	})
139 	public Parameters metaAdd(@IdParam IdType theId, @OperationParam(name = "meta") Meta theMeta, RequestDetails theRequestDetails) {
140 		if (theMeta == null) {
141 			throw new InvalidRequestException("Input contains no parameter with name 'meta'");
142 		}
143 		Parameters parameters = new Parameters();
144 		Meta metaAddOperation = getDao().metaAddOperation(theId, theMeta, theRequestDetails);
145 		parameters.addParameter().setName("return").setValue(metaAddOperation);
146 		return parameters;
147 	}
148 
149 	@Operation(name = OPERATION_META_DELETE, idempotent = true, returnParameters = {
150 		@OperationParam(name = "return", type = Meta.class)
151 	})
152 	public Parameters metaDelete(@IdParam IdType theId, @OperationParam(name = "meta") Meta theMeta, RequestDetails theRequestDetails) {
153 		if (theMeta == null) {
154 			throw new InvalidRequestException("Input contains no parameter with name 'meta'");
155 		}
156 		Parameters parameters = new Parameters();
157 		parameters.addParameter().setName("return").setValue(getDao().metaDeleteOperation(theId, theMeta, theRequestDetails));
158 		return parameters;
159 	}
160 
161 	@Update
162 	public MethodOutcome update(HttpServletRequest theRequest, @ResourceParam T theResource, @IdParam IdType theId, @ConditionalUrlParam String theConditional, RequestDetails theRequestDetails) {
163 		startRequest(theRequest);
164 		try {
165 			if (theConditional != null) {
166 				return getDao().update(theResource, theConditional, theRequestDetails);
167 			} else {
168 				return getDao().update(theResource, theRequestDetails);
169 			}
170 		} finally {
171 			endRequest(theRequest);
172 		}
173 	}
174 
175 	@Validate
176 	public MethodOutcome validate(@ResourceParam T theResource, @ResourceParam String theRawResource, @ResourceParam EncodingEnum theEncoding, @Validate.Mode ValidationModeEnum theMode,
177 											@Validate.Profile String theProfile, RequestDetails theRequestDetails) {
178 		return validate(theResource, null, theRawResource, theEncoding, theMode, theProfile, theRequestDetails);
179 	}
180 
181 	@Validate
182 	public MethodOutcome validate(@ResourceParam T theResource, @IdParam IdType theId, @ResourceParam String theRawResource, @ResourceParam EncodingEnum theEncoding, @Validate.Mode ValidationModeEnum theMode,
183 											@Validate.Profile String theProfile, RequestDetails theRequestDetails) {
184 		return getDao().validate(theResource, theId, theRawResource, theEncoding, theMode, theProfile, theRequestDetails);
185 	}
186 
187 }