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