View Javadoc
1   package ca.uhn.fhir.rest.gclient;
2   
3   import static org.apache.commons.lang3.StringUtils.isBlank;
4   import static org.apache.commons.lang3.StringUtils.isNotBlank;
5   
6   import java.util.Collection;
7   
8   /*
9    * #%L
10   * HAPI FHIR - Core Library
11   * %%
12   * Copyright (C) 2014 - 2018 University Health Network
13   * %%
14   * Licensed under the Apache License, Version 2.0 (the "License");
15   * you may not use this file except in compliance with the License.
16   * You may obtain a copy of the License at
17   * 
18   *      http://www.apache.org/licenses/LICENSE-2.0
19   * 
20   * Unless required by applicable law or agreed to in writing, software
21   * distributed under the License is distributed on an "AS IS" BASIS,
22   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   * See the License for the specific language governing permissions and
24   * limitations under the License.
25   * #L%
26   */
27  
28  import java.util.List;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.hl7.fhir.instance.model.api.IBaseCoding;
32  
33  import ca.uhn.fhir.context.FhirContext;
34  import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
35  import ca.uhn.fhir.rest.param.ParameterUtil;
36  
37  class TokenCriterion implements ICriterion<TokenClientParam>, ICriterionInternal {
38  
39  	private String myValue;
40  	private String myName;
41  
42  	public TokenCriterion(String theName, String theSystem, String theCode) {
43  		myName = theName;
44  		myValue=toValue(theSystem, theCode);
45  	}
46  
47  	private String toValue(String theSystem, String theCode) {
48  		String system = ParameterUtil.escape(theSystem);
49  		String code = ParameterUtil.escape(theCode);
50  		String value;
51  		if (StringUtils.isNotBlank(system)) {
52  			value = system + "|" + StringUtils.defaultString(code);
53  		} else if (system == null) {
54  			value = StringUtils.defaultString(code);
55  		} else {
56  			value = "|" + StringUtils.defaultString(code);
57  		}
58  		return value;
59  	}
60  
61  	public TokenCriterion(String theParamName, List<BaseIdentifierDt> theValue) {
62  		myName=theParamName;
63  		StringBuilder b = new StringBuilder();
64  		for (BaseIdentifierDt next : theValue) {
65  			if (next.getSystemElement().isEmpty() && next.getValueElement().isEmpty()) {
66  				continue;
67  			}
68  			if (b.length() > 0) {
69  				b.append(',');
70  			}
71  			b.append(toValue(next.getSystemElement().getValueAsString(), next.getValueElement().getValue()));
72  		}
73  		myValue = b.toString();
74  	}
75  
76  	public TokenCriterion(String theParamName, String theSystem, Collection<String> theValues) {
77  		myName = theParamName;
78  		StringBuilder b = new StringBuilder();
79  		for (String next : theValues) {
80  			if (isNotBlank(next)) {
81  				if (b.length() > 0) {
82  					b.append(',');
83  				}
84  				b.append(toValue(theSystem, next));
85  			}
86  		}
87  		myValue = b.toString();
88  	}
89  
90  	public TokenCriterion(String theParamName, Collection<String> theCodes) {
91  		this(theParamName, null, theCodes);
92  	}
93  
94  	public TokenCriterion(String theParamName, IBaseCoding... theCodings) {
95  		myName=theParamName;
96  		StringBuilder b = new StringBuilder();
97  		if (theCodings != null) {
98  		for (IBaseCoding next : theCodings) {
99  			if (isBlank(next.getSystem()) && isBlank(next.getCode())) {
100 				continue;
101 			}
102 			if (b.length() > 0) {
103 				b.append(',');
104 			}
105 			b.append(toValue(next.getSystem(), next.getCode()));
106 		}
107 		}
108 		myValue = b.toString();
109 	}
110 
111 	@Override
112 	public String getParameterValue(FhirContext theContext) {
113 		return myValue;
114 	}
115 
116 	@Override
117 	public String getParameterName() {
118 		return myName;
119 	}
120 
121 }