View Javadoc
1   package ca.uhn.fhir.jpa.dao.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.context.RuntimeSearchParam;
24  import ca.uhn.fhir.context.RuntimeSearchParam.RuntimeSearchParamStatusEnum;
25  import ca.uhn.fhir.jpa.dao.BaseSearchParamRegistry;
26  import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
27  import ca.uhn.fhir.jpa.search.JpaRuntimeSearchParam;
28  import ca.uhn.fhir.jpa.util.JpaConstants;
29  import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
30  import ca.uhn.fhir.util.DatatypeUtil;
31  import org.hl7.fhir.instance.model.api.IIdType;
32  import org.hl7.fhir.instance.model.api.IPrimitiveType;
33  import org.hl7.fhir.r4.model.Extension;
34  import org.hl7.fhir.r4.model.Reference;
35  import org.hl7.fhir.r4.model.SearchParameter;
36  import org.springframework.beans.factory.annotation.Autowired;
37  
38  import java.util.ArrayList;
39  import java.util.Collections;
40  import java.util.List;
41  import java.util.Set;
42  
43  import static org.apache.commons.lang3.StringUtils.isBlank;
44  
45  public class SearchParamRegistryR4 extends BaseSearchParamRegistry<SearchParameter> {
46  
47  	@Autowired
48  	private IFhirResourceDao<SearchParameter> mySpDao;
49  
50  	@Override
51  	public IFhirResourceDao<SearchParameter> getSearchParameterDao() {
52  		return mySpDao;
53  	}
54  
55  	@Override
56  	protected RuntimeSearchParam toRuntimeSp(SearchParameter theNextSp) {
57  		String name = theNextSp.getCode();
58  		String description = theNextSp.getDescription();
59  		String path = theNextSp.getExpression();
60  		RestSearchParameterTypeEnum paramType = null;
61  		RuntimeSearchParamStatusEnum status = null;
62  		switch (theNextSp.getType()) {
63  			case COMPOSITE:
64  				paramType = RestSearchParameterTypeEnum.COMPOSITE;
65  				break;
66  			case DATE:
67  				paramType = RestSearchParameterTypeEnum.DATE;
68  				break;
69  			case NUMBER:
70  				paramType = RestSearchParameterTypeEnum.NUMBER;
71  				break;
72  			case QUANTITY:
73  				paramType = RestSearchParameterTypeEnum.QUANTITY;
74  				break;
75  			case REFERENCE:
76  				paramType = RestSearchParameterTypeEnum.REFERENCE;
77  				break;
78  			case STRING:
79  				paramType = RestSearchParameterTypeEnum.STRING;
80  				break;
81  			case TOKEN:
82  				paramType = RestSearchParameterTypeEnum.TOKEN;
83  				break;
84  			case URI:
85  				paramType = RestSearchParameterTypeEnum.URI;
86  				break;
87  			case NULL:
88  				break;
89  		}
90  		if (theNextSp.getStatus() != null) {
91  			switch (theNextSp.getStatus()) {
92  				case ACTIVE:
93  					status = RuntimeSearchParamStatusEnum.ACTIVE;
94  					break;
95  				case DRAFT:
96  					status = RuntimeSearchParamStatusEnum.DRAFT;
97  					break;
98  				case RETIRED:
99  					status = RuntimeSearchParamStatusEnum.RETIRED;
100 					break;
101 				case UNKNOWN:
102 					status = RuntimeSearchParamStatusEnum.UNKNOWN;
103 					break;
104 				case NULL:
105 					break;
106 			}
107 		}
108 		Set<String> providesMembershipInCompartments = Collections.emptySet();
109 		Set<String> targets = DatatypeUtil.toStringSet(theNextSp.getTarget());
110 
111 		if (isBlank(name) || isBlank(path) || paramType == null) {
112 			if (paramType != RestSearchParameterTypeEnum.COMPOSITE) {
113 				return null;
114 			}
115 		}
116 
117 		IIdType id = theNextSp.getIdElement();
118 		String uri = "";
119 		boolean unique = false;
120 
121 		List<Extension> uniqueExts = theNextSp.getExtensionsByUrl(JpaConstants.EXT_SP_UNIQUE);
122 		if (uniqueExts.size() > 0) {
123 			IPrimitiveType<?> uniqueExtsValuePrimitive = uniqueExts.get(0).getValueAsPrimitive();
124 			if (uniqueExtsValuePrimitive != null) {
125 				if ("true".equalsIgnoreCase(uniqueExtsValuePrimitive.getValueAsString())) {
126 					unique = true;
127 				}
128 			}
129 		}
130 
131 		List<JpaRuntimeSearchParam.Component> components = new ArrayList<>();
132 		for (org.hl7.fhir.r4.model.SearchParameter.SearchParameterComponentComponent next : theNextSp.getComponent()) {
133 			components.add(new JpaRuntimeSearchParam.Component(next.getExpression(), new Reference(next.getDefinition())));
134 		}
135 
136 		return new JpaRuntimeSearchParam(id, uri, name, description, path, paramType, providesMembershipInCompartments, targets, status, unique, components, theNextSp.getBase());
137 	}
138 
139 
140 }