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  	public static final Pattern SPLIT_R4 = Pattern.compile("\\|");
45  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseSearchParamExtractor.class);
46  	@Autowired
47  	private FhirContext myContext;
48  	@Autowired
49  	private DaoConfig myDaoConfig;
50  	@Autowired
51  	private ISearchParamRegistry mySearchParamRegistry;
52  
53  	public BaseSearchParamExtractor() {
54  		super();
55  	}
56  
57  	public BaseSearchParamExtractor(DaoConfig theDaoConfig, FhirContext theCtx, ISearchParamRegistry theSearchParamRegistry) {
58  		myContext = theCtx;
59  		mySearchParamRegistry = theSearchParamRegistry;
60  		myDaoConfig = theDaoConfig;
61  	}
62  
63  	@Override
64  	public List<PathAndRef> extractResourceLinks(IBaseResource theResource, RuntimeSearchParam theNextSpDef) {
65  		List<PathAndRef> refs = new ArrayList<PathAndRef>();
66  		String[] nextPathsSplit = theNextSpDef.getPath().split("\\|");
67  		for (String nextPath : nextPathsSplit) {
68  			nextPath = nextPath.trim();
69  			for (Object nextObject : extractValues(nextPath, theResource)) {
70  				if (nextObject == null) {
71  					continue;
72  				}
73  				refs.add(new PathAndRef(nextPath, nextObject));
74  			}
75  		}
76  		return refs;
77  	}
78  
79  	protected abstract List<Object> extractValues(String thePaths, IBaseResource theResource);
80  
81  	protected FhirContext getContext() {
82  		return myContext;
83  	}
84  
85  	public DaoConfig getDaoConfig() {
86  		return myDaoConfig;
87  	}
88  
89  	public Collection<RuntimeSearchParam> getSearchParams(IBaseResource theResource) {
90  		RuntimeResourceDefinition def = getContext().getResourceDefinition(theResource);
91  		Collection<RuntimeSearchParam> retVal = mySearchParamRegistry.getActiveSearchParams(def.getName()).values();
92  		List<RuntimeSearchParam> defaultList = Collections.emptyList();
93  		retVal = ObjectUtils.defaultIfNull(retVal, defaultList);
94  		return retVal;
95  	}
96  
97  	@VisibleForTesting
98  	void setContextForUnitTest(FhirContext theContext) {
99  		myContext = theContext;
100 	}
101 
102 
103 }