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