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.FhirContext;
24  import ca.uhn.fhir.context.RuntimeResourceDefinition;
25  import ca.uhn.fhir.context.RuntimeSearchParam;
26  import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
27  import ca.uhn.fhir.util.FhirTerser;
28  import com.google.common.annotations.VisibleForTesting;
29  import org.apache.commons.lang3.ObjectUtils;
30  import org.hl7.fhir.instance.model.api.IBaseDatatype;
31  import org.hl7.fhir.instance.model.api.IBaseExtension;
32  import org.hl7.fhir.instance.model.api.IBaseResource;
33  import org.springframework.beans.factory.annotation.Autowired;
34  
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.List;
39  import java.util.regex.Pattern;
40  
41  public abstract class BaseSearchParamExtractor implements ISearchParamExtractor {
42  
43  	public static final Pattern SPLIT = Pattern.compile("\\||( or )");
44  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseSearchParamExtractor.class);
45  	@Autowired
46  	private FhirContext myContext;
47  	@Autowired
48  	private DaoConfig myDaoConfig;
49  	@Autowired
50  	private ISearchParamRegistry mySearchParamRegistry;
51  
52  	public BaseSearchParamExtractor() {
53  		super();
54  	}
55  
56  	public BaseSearchParamExtractor(DaoConfig theDaoConfig, FhirContext theCtx, ISearchParamRegistry theSearchParamRegistry) {
57  		myContext = theCtx;
58  		mySearchParamRegistry = theSearchParamRegistry;
59  		myDaoConfig = theDaoConfig;
60  	}
61  
62  	@Override
63  	public List<PathAndRef> extractResourceLinks(IBaseResource theResource, RuntimeSearchParam theNextSpDef) {
64  		List<PathAndRef> refs = new ArrayList<PathAndRef>();
65  		String[] nextPathsSplit = theNextSpDef.getPath().split("\\|");
66  		for (String nextPath : nextPathsSplit) {
67  			nextPath = nextPath.trim();
68  			for (Object nextObject : extractValues(nextPath, theResource)) {
69  				if (nextObject == null) {
70  					continue;
71  				}
72  				refs.add(new PathAndRef(nextPath, nextObject));
73  			}
74  		}
75  		return refs;
76  	}
77  
78  	protected abstract List<Object> extractValues(String thePaths, IBaseResource theResource);
79  
80  	protected FhirContext getContext() {
81  		return myContext;
82  	}
83  
84  	public DaoConfig getDaoConfig() {
85  		return myDaoConfig;
86  	}
87  
88  	public Collection<RuntimeSearchParam> getSearchParams(IBaseResource theResource) {
89  		RuntimeResourceDefinition def = getContext().getResourceDefinition(theResource);
90  		Collection<RuntimeSearchParam> retVal = mySearchParamRegistry.getActiveSearchParams(def.getName()).values();
91  		List<RuntimeSearchParam> defaultList = Collections.emptyList();
92  		retVal = ObjectUtils.defaultIfNull(retVal, defaultList);
93  		return retVal;
94  	}
95  
96  	@VisibleForTesting
97  	void setContextForUnitTest(FhirContext theContext) {
98  		myContext = theContext;
99  	}
100 
101 
102 }