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