View Javadoc
1   package ca.uhn.fhir.rest.gclient;
2   
3   import static org.apache.commons.lang3.StringUtils.defaultString;
4   
5   import java.util.*;
6   
7   import org.apache.commons.lang3.ObjectUtils;
8   import org.hl7.fhir.instance.model.api.IBaseCoding;
9   
10  import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
11  
12  /*
13   * #%L
14   * HAPI FHIR - Core Library
15   * %%
16   * Copyright (C) 2014 - 2018 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   * Token parameter type for use in fluent client interfaces
34   */
35  public class TokenClientParam extends BaseClientParam implements IParam {
36  
37  	private static final String[] EMPTY_STRING_LIST = new String[0];
38  
39  	private String myParamName;
40  
41  	public TokenClientParam(String theParamName) {
42  		myParamName = theParamName;
43  	}
44  
45  	public IMatches exactly() {
46  		return new IMatches() {
47  			@Override
48  			public ICriterion<TokenClientParam> code(String theCode) {
49  				return new TokenCriterion(getParamName(), null, theCode);
50  			}
51  
52  			@Override
53  			public ICriterion<?> codes(Collection<String> theCodes) {
54  				return new TokenCriterion(getParamName(), theCodes);
55  			}
56  
57  			@Override
58  			public ICriterion<?> codes(String... theCodes) {
59  				return new TokenCriterion(getParamName(), convertToList(theCodes));
60  			}
61  
62  			private List<String> convertToList(String[] theValues) {
63  				String[] values = ObjectUtils.defaultIfNull(theValues, EMPTY_STRING_LIST);
64  				return Arrays.asList(values);
65  			}
66  
67  			@Override
68  			public ICriterion<TokenClientParam> identifier(BaseIdentifierDt theIdentifier) {
69  				return new TokenCriterion(getParamName(), theIdentifier.getSystemElement().getValueAsString(), theIdentifier.getValueElement().getValue());
70  			}
71  
72  			@Override
73  			public ICriterion<TokenClientParam> identifier(String theIdentifier) {
74  				return new TokenCriterion(getParamName(), null, theIdentifier);
75  			}
76  
77  			@Override
78  			public ICriterion<TokenClientParam> identifiers(BaseIdentifierDt... theIdentifiers) {
79  				return new TokenCriterion(getParamName(), Arrays.asList(theIdentifiers));
80  			}
81  
82  			@Override
83  			public ICriterion<TokenClientParam> identifiers(List<BaseIdentifierDt> theIdentifiers) {
84  				return new TokenCriterion(getParamName(), theIdentifiers);
85  			}
86  
87  			@Override
88  			public ICriterion<TokenClientParam> systemAndCode(String theSystem, String theCode) {
89  				return new TokenCriterion(getParamName(), defaultString(theSystem), theCode);
90  			}
91  
92  			@Override
93  			public ICriterion<TokenClientParam> systemAndIdentifier(String theSystem, String theCode) {
94  				return new TokenCriterion(getParamName(), defaultString(theSystem), theCode);
95  			}
96  
97  			@Override
98  			public ICriterion<?> systemAndValues(String theSystem, Collection<String> theValues) {
99  				return new TokenCriterion(getParamName(), defaultString(theSystem), theValues);
100 			}
101 
102 			@Override
103 			public ICriterion<?> systemAndValues(String theSystem, String... theValues) {
104 				return new TokenCriterion(getParamName(), defaultString(theSystem), convertToList(theValues));
105 			}
106 
107 			@Override
108 			public ICriterion<?> codings(IBaseCoding... theCodings) {
109 				return new TokenCriterion(getParamName(), theCodings);
110 			}
111 		};
112 	}
113 
114 	@Override
115 	public String getParamName() {
116 		return myParamName;
117 	}
118 
119 	/**
120 	 * Create a search criterion that matches against the given system
121 	 * value but does not specify a code. This means that any code/identifier with
122 	 * the given system should match.
123 	 * <p>
124 	 * Use {@link #exactly()} if you want to specify a code.
125 	 * </p>
126 	 */
127 	public ICriterion<TokenClientParam> hasSystemWithAnyCode(String theSystem) {
128 		return new TokenCriterion(getParamName(), theSystem, (String) null);
129 	}
130 
131 	public interface IMatches {
132 		/**
133 		 * Creates a search criterion that matches against the given code, with no code system specified
134 		 * 
135 		 * @param theIdentifier
136 		 *           The identifier
137 		 * @return A criterion
138 		 */
139 		ICriterion<TokenClientParam> code(String theIdentifier);
140 
141 		/**
142 		 * Creates a search criterion that matches a given system with a collection of possible
143 		 * codes (this will be used to form a comma-separated OR query) with any system value.
144 		 * The URL form of this method will create a parameter like
145 		 * <code>parameter=code1,code2</code>
146 		 * 
147 		 * @param theCodes
148 		 *           The codes
149 		 */
150 		ICriterion<?> codes(Collection<String> theCodes);
151 
152 		/**
153 		 * Creates a search criterion that matches a given system with a collection of possible
154 		 * codes (this will be used to form a comma-separated OR query) with any system value.
155 		 * The URL form of this method will create a parameter like
156 		 * <code>parameter=code1,code2</code>
157 		 * 
158 		 * @param theCodes
159 		 *           The codes
160 		 */
161 		ICriterion<?> codes(String... theCodes);
162 
163 		/**
164 		 * Creates a search criterion that matches a given system with a collection of possible
165 		 * codes (this will be used to form a comma-separated OR query) with the given
166 		 * <code>Coding.system</code> and <code>Coding.value</code> values.
167 		 * <p>
168 		 * The URL form of this method will create a parameter like
169 		 * <code>parameter=system1|code1,system2|code2</code>
170 		 * </p>
171 		 * 
172 		 * @param theCodings
173 		 *           The codings
174 		 */
175 		ICriterion<?> codings(IBaseCoding... theCodings);
176 
177 		/**
178 		 * Creates a search criterion that matches against the given identifier (system and code if both are present, or whatever is present)
179 		 * 
180 		 * @param theIdentifier
181 		 *           The identifier
182 		 * @return A criterion
183 		 */
184 		ICriterion<TokenClientParam> identifier(BaseIdentifierDt theIdentifier);
185 
186 		/**
187 		 * Creates a search criterion that matches against the given identifier, with no system specified
188 		 * 
189 		 * @param theIdentifier
190 		 *           The identifier
191 		 * @return A criterion
192 		 */
193 		ICriterion<TokenClientParam> identifier(String theIdentifier);
194 
195 		/**
196 		 * Creates a search criterion that matches against the given collection of identifiers (system and code if both are present, or whatever is present).
197 		 * In the query URL that is generated, identifiers will be joined with a ',' to create an OR query.
198 		 * 
199 		 * @param theIdentifiers
200 		 *           The identifier
201 		 * @return A criterion
202 		 */
203 		ICriterion<TokenClientParam> identifiers(BaseIdentifierDt... theIdentifiers);
204 
205 		/**
206 		 * Creates a search criterion that matches against the given collection of identifiers (system and code if both are present, or whatever is present).
207 		 * In the query URL that is generated, identifiers will be joined with a ',' to create an OR query.
208 		 * 
209 		 * @param theIdentifiers
210 		 *           The identifier
211 		 * @return A criterion
212 		 */
213 		ICriterion<TokenClientParam> identifiers(List<BaseIdentifierDt> theIdentifiers);
214 
215 		/**
216 		 * Creates a search criterion that matches against the given code system and code
217 		 * 
218 		 * @param theSystem
219 		 *           The code system (should be a URI)
220 		 * @param theCode
221 		 *           The code
222 		 * @return A criterion
223 		 */
224 		ICriterion<TokenClientParam> systemAndCode(String theSystem, String theCode);
225 
226 		/**
227 		 * Creates a search criterion that matches against the given system and identifier
228 		 * 
229 		 * @param theSystem
230 		 *           The code system (should be a URI)
231 		 * @param theIdentifier
232 		 *           The identifier
233 		 * @return A criterion
234 		 */
235 		ICriterion<TokenClientParam> systemAndIdentifier(String theSystem, String theIdentifier);
236 
237 		/**
238 		 * Creates a search criterion that matches a given system with a collection of possible
239 		 * values (this will be used to form a comma-separated OR query)
240 		 * 
241 		 * @param theSystem
242 		 *           The system, which will be used with each value
243 		 * @param theValues
244 		 *           The values
245 		 */
246 		public ICriterion<?> systemAndValues(String theSystem, Collection<String> theValues);
247 
248 		/**
249 		 * Creates a search criterion that matches a given system with a collection of possible
250 		 * values (this will be used to form a comma-separated OR query)
251 		 * 
252 		 * @param theSystem
253 		 *           The system, which will be used with each value
254 		 * @param theValues
255 		 *           The values
256 		 */
257 		ICriterion<?> systemAndValues(String theSystem, String... theValues);
258 
259 	}
260 
261 }