001package ca.uhn.fhir.rest.gclient;
002
003import static org.apache.commons.lang3.StringUtils.defaultString;
004
005import java.util.*;
006
007import org.apache.commons.lang3.ObjectUtils;
008import org.hl7.fhir.instance.model.api.IBaseCoding;
009
010import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
011
012/*
013 * #%L
014 * HAPI FHIR - Core Library
015 * %%
016 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
017 * %%
018 * Licensed under the Apache License, Version 2.0 (the "License");
019 * you may not use this file except in compliance with the License.
020 * You may obtain a copy of the License at
021 *
022 * http://www.apache.org/licenses/LICENSE-2.0
023 *
024 * Unless required by applicable law or agreed to in writing, software
025 * distributed under the License is distributed on an "AS IS" BASIS,
026 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
027 * See the License for the specific language governing permissions and
028 * limitations under the License.
029 * #L%
030 */
031
032/**
033 * Token parameter type for use in fluent client interfaces
034 */
035public class TokenClientParam extends BaseClientParam implements IParam {
036
037        private static final String[] EMPTY_STRING_LIST = new String[0];
038
039        private String myParamName;
040
041        public TokenClientParam(String theParamName) {
042                myParamName = theParamName;
043        }
044
045        public IMatches exactly() {
046                return new IMatches() {
047                        @Override
048                        public ICriterion<TokenClientParam> code(String theCode) {
049                                return new TokenCriterion(getParamName(), null, theCode);
050                        }
051
052                        @Override
053                        public ICriterion<?> codes(Collection<String> theCodes) {
054                                return new TokenCriterion(getParamName(), theCodes);
055                        }
056
057                        @Override
058                        public ICriterion<?> codes(String... theCodes) {
059                                return new TokenCriterion(getParamName(), convertToList(theCodes));
060                        }
061
062                        private List<String> convertToList(String[] theValues) {
063                                String[] values = ObjectUtils.defaultIfNull(theValues, EMPTY_STRING_LIST);
064                                return Arrays.asList(values);
065                        }
066
067                        @Override
068                        public ICriterion<TokenClientParam> identifier(BaseIdentifierDt theIdentifier) {
069                                return new TokenCriterion(getParamName(), theIdentifier.getSystemElement().getValueAsString(), theIdentifier.getValueElement().getValue());
070                        }
071
072                        @Override
073                        public ICriterion<TokenClientParam> identifier(String theIdentifier) {
074                                return new TokenCriterion(getParamName(), null, theIdentifier);
075                        }
076
077                        @Override
078                        public ICriterion<TokenClientParam> identifiers(BaseIdentifierDt... theIdentifiers) {
079                                return new TokenCriterion(getParamName(), Arrays.asList(theIdentifiers));
080                        }
081
082                        @Override
083                        public ICriterion<TokenClientParam> identifiers(List<BaseIdentifierDt> theIdentifiers) {
084                                return new TokenCriterion(getParamName(), theIdentifiers);
085                        }
086
087                        @Override
088                        public ICriterion<TokenClientParam> systemAndCode(String theSystem, String theCode) {
089                                return new TokenCriterion(getParamName(), defaultString(theSystem), theCode);
090                        }
091
092                        @Override
093                        public ICriterion<TokenClientParam> systemAndIdentifier(String theSystem, String theCode) {
094                                return new TokenCriterion(getParamName(), defaultString(theSystem), theCode);
095                        }
096
097                        @Override
098                        public ICriterion<?> systemAndValues(String theSystem, Collection<String> theValues) {
099                                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}