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 java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.regex.Pattern;
28  
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 com.google.common.annotations.VisibleForTesting;
36  
37  import ca.uhn.fhir.context.FhirContext;
38  import ca.uhn.fhir.context.RuntimeResourceDefinition;
39  import ca.uhn.fhir.context.RuntimeSearchParam;
40  import ca.uhn.fhir.util.FhirTerser;
41  
42  public abstract class BaseSearchParamExtractor implements ISearchParamExtractor {
43  	
44  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseSearchParamExtractor.class);
45  	public static final Pattern SPLIT = Pattern.compile("\\||( or )");
46  
47  	@Autowired
48  	private FhirContext myContext;
49  	
50  	@Autowired
51  	private ISearchParamRegistry mySearchParamRegistry;
52  	
53  	public BaseSearchParamExtractor() {
54  		super();
55  	}
56  
57  	public BaseSearchParamExtractor(FhirContext theCtx, ISearchParamRegistry theSearchParamRegistry) {
58  		myContext = theCtx;
59  		mySearchParamRegistry = theSearchParamRegistry;
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 List<Object> extractValues(String thePaths, IBaseResource theResource) {
79  		List<Object> values = new ArrayList<Object>();
80  		String[] nextPathsSplit = SPLIT.split(thePaths);
81  		FhirTerser t = myContext.newTerser();
82  		for (String nextPath : nextPathsSplit) {
83  			String nextPathTrimmed = nextPath.trim();
84  			try {
85  				List<Object> allValues = t.getValues(theResource, nextPathTrimmed);
86  				for (Object next : allValues) {
87  					if (next instanceof IBaseExtension) {
88  						IBaseDatatype value = ((IBaseExtension) next).getValue();
89  						if (value != null) {
90  							values.add(value);
91  						}
92  					} else {
93  						values.add(next);
94  					}
95  				}
96  			} catch (Exception e) {
97  				RuntimeResourceDefinition def = myContext.getResourceDefinition(theResource);
98  				ourLog.warn("Failed to index values from path[{}] in resource type[{}]: {}", new Object[] { nextPathTrimmed, def.getName(), e.toString(), e } );
99  			}
100 		}
101 		return values;
102 	}
103 	
104 	protected FhirContext getContext() {
105 		return myContext;
106 	}
107 
108 	public Collection<RuntimeSearchParam> getSearchParams(IBaseResource theResource) {
109 		RuntimeResourceDefinition def = getContext().getResourceDefinition(theResource);
110 		Collection<RuntimeSearchParam> retVal = mySearchParamRegistry.getActiveSearchParams(def.getName()).values();
111 		List<RuntimeSearchParam> defaultList= Collections.emptyList();
112 		retVal = ObjectUtils.defaultIfNull(retVal, defaultList);
113 		return retVal;
114 	}
115 
116 	@VisibleForTesting
117 	void setContextForUnitTest(FhirContext theContext) {
118 		myContext = theContext;
119 	}
120 
121 
122 }