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