View Javadoc
1   package ca.uhn.fhir.rest.gclient;
2   
3   import ca.uhn.fhir.context.FhirContext;
4   import org.apache.commons.lang3.Validate;
5   import org.hl7.fhir.instance.model.api.IIdType;
6   
7   import java.util.Arrays;
8   import java.util.Collection;
9   
10  import static org.apache.commons.lang3.StringUtils.isNotBlank;
11  
12  /*
13   * #%L
14   * HAPI FHIR - Core Library
15   * %%
16   * Copyright (C) 2014 - 2019 University Health Network
17   * %%
18   * Licensed under the Apache License, Version 2.0 (the "License");
19   * you may not use this file except in compliance with the License.
20   * You may obtain a copy of the License at
21   * 
22   *      http://www.apache.org/licenses/LICENSE-2.0
23   * 
24   * Unless required by applicable law or agreed to in writing, software
25   * distributed under the License is distributed on an "AS IS" BASIS,
26   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27   * See the License for the specific language governing permissions and
28   * limitations under the License.
29   * #L%
30   */
31  
32  
33  public class ReferenceClientParam extends BaseClientParam  implements IParam {
34  
35  	private String myName;
36  
37  	public ReferenceClientParam(String theName) {
38  		myName = theName;
39  	}
40  
41  	@Override
42  	public String getParamName() {
43  		return myName;
44  	}
45  
46  	/**
47  	 * Include a chained search. For example:
48  	 * <pre>
49  	 * Bundle resp = ourClient
50  	 *   .search()
51  	 *   .forResource(QuestionnaireResponse.class)
52  	 *   .where(QuestionnaireResponse.SUBJECT.hasChainedProperty(Patient.FAMILY.matches().value("SMITH")))
53  	 *   .returnBundle(Bundle.class)
54  	 *   .execute();
55  	 * </pre>
56  	 */
57  	public ICriterion<ReferenceClientParam> hasChainedProperty(ICriterion<?> theCriterion) {
58  		return new ReferenceChainCriterion(getParamName(), theCriterion);
59  	}
60  
61  	/**
62  	 * Include a chained search with a resource type. For example:
63  	 * <pre>
64  	 * Bundle resp = ourClient
65  	 *   .search()
66  	 *   .forResource(QuestionnaireResponse.class)
67  	 *   .where(QuestionnaireResponse.SUBJECT.hasChainedProperty("Patient", Patient.FAMILY.matches().value("SMITH")))
68  	 *   .returnBundle(Bundle.class)
69  	 *   .execute();
70  	 * </pre>
71  	 */
72  	public ICriterion<ReferenceClientParam> hasChainedProperty(String theResourceType, ICriterion<?> theCriterion) {
73  		return new ReferenceChainCriterion(getParamName(), theResourceType, theCriterion);
74  	}
75  
76  	/**
77  	 * Match the referenced resource if the resource has the given ID (this can be
78  	 * the logical ID or the absolute URL of the resource)
79  	 */
80  	public ICriterion<ReferenceClientParam> hasId(IIdType theId) {
81  		return new StringCriterion<>(getParamName(), theId.getValue());
82  	}
83  
84  	/**
85  	 * Match the referenced resource if the resource has the given ID (this can be
86  	 * the logical ID or the absolute URL of the resource)
87  	 */
88  	public ICriterion<ReferenceClientParam> hasId(String theId) {
89  		return new StringCriterion<>(getParamName(), theId);
90  	}
91  
92  	/**
93  	 * Match the referenced resource if the resource has ANY of the given IDs
94  	 * (this is an OR search, not an AND search), (this can be the logical ID or
95  	 * the absolute URL of the resource). Note that to specify an AND search,
96  	 * simply add a subsequent {@link IQuery#where(ICriterion) where} criteria
97  	 * with the same parameter.
98  	 */
99  	public ICriterion<ReferenceClientParam> hasAnyOfIds(Collection<String> theIds) {
100 		return new StringCriterion<>(getParamName(), theIds);
101 	}
102 
103 	/**
104 	 * Match the referenced resource if the resource has ANY of the given IDs
105 	 * (this is an OR search, not an AND search), (this can be the logical ID or
106 	 * the absolute URL of the resource). Note that to specify an AND search,
107 	 * simply add a subsequent {@link IQuery#where(ICriterion) where} criteria
108 	 * with the same parameter.
109 	 */
110 	public ICriterion<ReferenceClientParam> hasAnyOfIds(String... theIds) {
111 		Validate.notNull(theIds, "theIds must not be null");
112 		return hasAnyOfIds(Arrays.asList(theIds));
113 	}
114 
115 	private static class ReferenceChainCriterion implements ICriterion<ReferenceClientParam>, ICriterionInternal {
116 
117 		private final String myResourceTypeQualifier;
118 		private String myParamName;
119 		private ICriterionInternal myWrappedCriterion;
120 
121 		ReferenceChainCriterion(String theParamName, ICriterion<?> theWrappedCriterion) {
122 			this(theParamName, null, theWrappedCriterion);
123 		}
124 
125 		ReferenceChainCriterion(String theParamName, String theResourceType, ICriterion<?> theWrappedCriterion) {
126 			myParamName = theParamName;
127 			myResourceTypeQualifier = isNotBlank(theResourceType) ? ":" + theResourceType : "";
128 			myWrappedCriterion = (ICriterionInternal) theWrappedCriterion;
129 		}
130 
131 		@Override
132 		public String getParameterName() {
133 			return myParamName + myResourceTypeQualifier + "." + myWrappedCriterion.getParameterName();
134 		}
135 
136 		@Override
137 		public String getParameterValue(FhirContext theContext) {
138 			return myWrappedCriterion.getParameterValue(theContext);
139 		}
140 
141 	}
142 
143 }