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.DaoMethodOutcome;
24  import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
25  import ca.uhn.fhir.jpa.util.ExpungeOptions;
26  import ca.uhn.fhir.jpa.util.ExpungeOutcome;
27  import ca.uhn.fhir.rest.annotation.*;
28  import ca.uhn.fhir.rest.api.PatchTypeEnum;
29  import ca.uhn.fhir.rest.api.server.IBundleProvider;
30  import ca.uhn.fhir.rest.api.server.RequestDetails;
31  import ca.uhn.fhir.rest.param.DateRangeParam;
32  import ca.uhn.fhir.rest.server.IResourceProvider;
33  import ca.uhn.fhir.util.CoverageIgnore;
34  import org.hl7.fhir.instance.model.api.IBaseResource;
35  import org.hl7.fhir.instance.model.api.IIdType;
36  import org.hl7.fhir.instance.model.api.IPrimitiveType;
37  import org.hl7.fhir.r4.model.Parameters;
38  import org.springframework.beans.factory.annotation.Required;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import java.util.Date;
42  
43  public abstract class BaseJpaResourceProvider<T extends IBaseResource> extends BaseJpaProvider implements IResourceProvider {
44  
45  	private IFhirResourceDao<T> myDao;
46  
47  	public BaseJpaResourceProvider() {
48  		// nothing
49  	}
50  
51  	@CoverageIgnore
52  	public BaseJpaResourceProvider(IFhirResourceDao<T> theDao) {
53  		myDao = theDao;
54  	}
55  
56  
57  	protected Parameters doExpunge(IIdType theIdParam, IPrimitiveType<? extends Integer> theLimit, IPrimitiveType<? extends Boolean> theExpungeDeletedResources, IPrimitiveType<? extends Boolean> theExpungeOldVersions, IPrimitiveType<? extends Boolean> theExpungeEverything) {
58  
59  		ExpungeOptions options = createExpungeOptions(theLimit, theExpungeDeletedResources, theExpungeOldVersions, theExpungeEverything);
60  
61  		ExpungeOutcome outcome;
62  		if (theIdParam != null) {
63  			outcome = getDao().expunge(theIdParam, options);
64  		} else {
65  			outcome = getDao().expunge(options);
66  		}
67  
68  		return createExpungeResponse(outcome);
69  	}
70  
71  	public IFhirResourceDao<T> getDao() {
72  		return myDao;
73  	}
74  
75  	@Required
76  	public void setDao(IFhirResourceDao<T> theDao) {
77  		myDao = theDao;
78  	}
79  
80  	@History
81  	public IBundleProvider getHistoryForResourceInstance(
82  		HttpServletRequest theRequest,
83  		@IdParam IIdType theId,
84  		@Since Date theSince,
85  		@At DateRangeParam theAt,
86  		RequestDetails theRequestDetails) {
87  
88  		startRequest(theRequest);
89  		try {
90  			DateRangeParam sinceOrAt = processSinceOrAt(theSince, theAt);
91  			return myDao.history(theId, sinceOrAt.getLowerBoundAsInstant(), sinceOrAt.getUpperBoundAsInstant(), theRequestDetails);
92  		} finally {
93  			endRequest(theRequest);
94  		}
95  	}
96  
97  	@History
98  	public IBundleProvider getHistoryForResourceType(
99  		HttpServletRequest theRequest,
100 		@Since Date theSince,
101 		@At DateRangeParam theAt,
102 		RequestDetails theRequestDetails) {
103 		startRequest(theRequest);
104 		try {
105 			DateRangeParam sinceOrAt = processSinceOrAt(theSince, theAt);
106 			return myDao.history(sinceOrAt.getLowerBoundAsInstant(), sinceOrAt.getUpperBoundAsInstant(), theRequestDetails);
107 		} finally {
108 			endRequest(theRequest);
109 		}
110 	}
111 
112 	@Override
113 	public Class<? extends IBaseResource> getResourceType() {
114 		return myDao.getResourceType();
115 	}
116 
117 	@Patch
118 	public DaoMethodOutcome patch(HttpServletRequest theRequest, @IdParam IIdType theId, RequestDetails theRequestDetails, @ResourceParam String theBody, PatchTypeEnum thePatchType) {
119 		startRequest(theRequest);
120 		try {
121 			return myDao.patch(theId, thePatchType, theBody, theRequestDetails);
122 		} finally {
123 			endRequest(theRequest);
124 		}
125 	}
126 
127 	@Read(version = true)
128 	public T read(HttpServletRequest theRequest, @IdParam IIdType theId, RequestDetails theRequestDetails) {
129 		startRequest(theRequest);
130 		try {
131 			return myDao.read(theId, theRequestDetails);
132 		} finally {
133 			endRequest(theRequest);
134 		}
135 	}
136 
137 }