View Javadoc
1   package ca.uhn.fhir.util;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
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.Iterator;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.commons.lang3.builder.ToStringBuilder;
28  import org.apache.commons.lang3.builder.ToStringStyle;
29  import org.hl7.fhir.instance.model.api.IBaseReference;
30  import org.hl7.fhir.instance.model.api.IBaseResource;
31  
32  import ca.uhn.fhir.context.FhirContext;
33  import ca.uhn.fhir.context.RuntimeResourceDefinition;
34  import ca.uhn.fhir.context.RuntimeSearchParam;
35  import ca.uhn.fhir.model.api.Include;
36  
37  /**
38   * Created by Bill de Beaubien on 2/26/2015.
39   */
40  public class ResourceReferenceInfo {
41  	private String myOwningResource;
42  	private String myName;
43  	private IBaseReference myResource;
44  	private FhirContext myContext;
45  
46  	public ResourceReferenceInfo(FhirContext theContext, IBaseResource theOwningResource, List<String> thePathToElement, IBaseReference theElement) {
47  		myContext = theContext;
48  		myOwningResource = theContext.getResourceDefinition(theOwningResource).getName();
49  
50  		myResource = theElement;
51  		if (thePathToElement != null && !thePathToElement.isEmpty()) {
52  			StringBuilder sb = new StringBuilder();
53  			thePathToElement.iterator();
54  			for (Iterator<String> iterator = thePathToElement.iterator(); iterator.hasNext();) {
55  				sb.append(iterator.next());
56  				if (iterator.hasNext())
57  					sb.append(".");
58  			}
59  			myName = sb.toString();
60  		} else {
61  			myName = null;
62  		}
63  	}
64  
65  	@Override
66  	public String toString() {
67  		ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
68  		b.append("name", myName);
69  		b.append("resource", myResource.getReferenceElement());
70  		return b.build();
71  	}
72  
73  	public String getName() {
74  		return myName;
75  	}
76  
77  	public IBaseReference getResourceReference() {
78  		return myResource;
79  	}
80  
81  	public boolean matchesIncludeSet(Set<Include> theIncludes) {
82  		if (theIncludes == null)
83  			return false;
84  		for (Include include : theIncludes) {
85  			if (matchesInclude(include))
86  				return true;
87  		}
88  		return false;
89  	}
90  
91  	public boolean matchesInclude(Include theInclude) {
92  		if (theInclude.getValue().equals("*")) {
93  			return true;
94  		}
95  		int colonIndex = theInclude.getValue().indexOf(':');
96  		if (colonIndex != -1) {
97  			// DSTU2+ style
98  			String resourceName = theInclude.getValue().substring(0, colonIndex);
99  			String paramName = theInclude.getValue().substring(colonIndex + 1);
100 			RuntimeResourceDefinition resourceDef = myContext.getResourceDefinition(resourceName);
101 			if (resourceDef != null) {
102 				RuntimeSearchParam searchParamDef = resourceDef.getSearchParam(paramName);
103 				if (searchParamDef!=null) {
104 					final String completeName = myOwningResource + "." + myName;
105 					boolean matched = false;
106 					for (String s : searchParamDef.getPathsSplit()) {
107 						if (s.equals(completeName) ||
108 								       s.startsWith(completeName + ".")) {
109 							matched = true; break;
110 						}
111 					}
112 					return matched;
113 				}
114 			}
115 			return false;
116 		}
117 		// DSTU1 style
118 		return (theInclude.getValue().equals(myOwningResource + '.' + myName));
119 	}
120 }