View Javadoc
1   package ca.uhn.fhir.jpa.graphql;
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.RuntimeResourceDefinition;
24  import ca.uhn.fhir.context.RuntimeSearchParam;
25  import ca.uhn.fhir.jpa.dao.BaseHapiFhirDao;
26  import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
27  import ca.uhn.fhir.jpa.dao.SearchParameterMap;
28  import ca.uhn.fhir.jpa.entity.BaseHasResource;
29  import ca.uhn.fhir.model.api.IQueryParameterType;
30  import ca.uhn.fhir.rest.api.server.IBundleProvider;
31  import ca.uhn.fhir.rest.param.*;
32  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
33  import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException;
34  import org.hl7.fhir.exceptions.FHIRException;
35  import org.hl7.fhir.instance.model.api.IBaseResource;
36  import org.hl7.fhir.instance.model.api.IIdType;
37  import org.hl7.fhir.r4.model.Bundle;
38  import org.hl7.fhir.r4.model.Reference;
39  import org.hl7.fhir.r4.model.Resource;
40  import org.hl7.fhir.r4.utils.GraphQLEngine;
41  import org.hl7.fhir.utilities.graphql.Argument;
42  import org.hl7.fhir.utilities.graphql.Value;
43  import org.springframework.transaction.annotation.Propagation;
44  import org.springframework.transaction.annotation.Transactional;
45  
46  import java.util.List;
47  
48  public class JpaStorageServices extends BaseHapiFhirDao<IBaseResource> implements GraphQLEngine.IGraphQLStorageServices {
49  
50  
51  	private IFhirResourceDao<? extends IBaseResource> getDao(String theResourceType) {
52  		RuntimeResourceDefinition typeDef = getContext().getResourceDefinition(theResourceType);
53  		return getDao(typeDef.getImplementingClass());
54  	}
55  
56  	@Transactional(propagation = Propagation.NEVER)
57  	@Override
58  	public void listResources(Object theAppInfo, String theType, List<Argument> theSearchParams, List<Resource> theMatches) throws FHIRException {
59  
60  		RuntimeResourceDefinition typeDef = getContext().getResourceDefinition(theType);
61  		IFhirResourceDao<? extends IBaseResource> dao = getDao(typeDef.getImplementingClass());
62  
63  		SearchParameterMap params = new SearchParameterMap();
64  
65  		for (Argument nextArgument : theSearchParams) {
66  
67  			RuntimeSearchParam searchParam = getSearchParamByName(typeDef, nextArgument.getName());
68  
69  			for (Value nextValue : nextArgument.getValues()) {
70  				String value = nextValue.getValue();
71  
72  				IQueryParameterType param = null;
73  				switch (searchParam.getParamType()) {
74  					case NUMBER:
75  						param = new NumberParam(value);
76  						break;
77  					case DATE:
78  						param = new DateParam(value);
79  						break;
80  					case STRING:
81  						param = new StringParam(value);
82  						break;
83  					case TOKEN:
84  						param = new TokenParam(null, value);
85  						break;
86  					case REFERENCE:
87  						param = new ReferenceParam(value);
88  						break;
89  					case COMPOSITE:
90  						throw new InvalidRequestException("Composite parameters are not yet supported in GraphQL");
91  					case QUANTITY:
92  						param = new QuantityParam(value);
93  						break;
94  					case URI:
95  						break;
96  					case HAS:
97  						break;
98  				}
99  
100 				params.add(nextArgument.getName(), param);
101 			}
102 		}
103 
104 		IBundleProvider response = dao.search(params);
105 		int size = response.size();
106 		if (response.preferredPageSize() != null && response.preferredPageSize() < size) {
107 			size = response.preferredPageSize();
108 		}
109 
110 		for (IBaseResource next : response.getResources(0, size)) {
111 			theMatches.add((Resource) next);
112 		}
113 
114 	}
115 
116 	@Transactional(propagation = Propagation.REQUIRED)
117 	@Override
118 	public Resource lookup(Object theAppInfo, String theType, String theId) throws FHIRException {
119 		IIdType refId = getContext().getVersion().newIdType();
120 		refId.setValue(theType + "/" + theId);
121 		IFhirResourceDao<? extends IBaseResource> dao = getDao(theType);
122 		BaseHasResource id = dao.readEntity(refId);
123 
124 		return (Resource) toResource(id, false);
125 	}
126 
127 	@Override
128 	public ReferenceResolution lookup(Object appInfo, Resource context, Reference reference) throws FHIRException {
129 		return null;
130 	}
131 
132 	@Transactional(propagation = Propagation.NEVER)
133 	@Override
134 	public Bundle search(Object theAppInfo, String theType, List<Argument> theSearchParams) throws FHIRException {
135 		throw new NotImplementedOperationException("Not yet able to handle this GraphQL request");
136 	}
137 }