View Javadoc
1   package ca.uhn.fhir.rest.param;
2   
3   import static org.apache.commons.lang3.StringUtils.isNotBlank;
4   
5   import ca.uhn.fhir.context.FhirContext;
6   
7   /*
8    * #%L
9    * HAPI FHIR - Core Library
10   * %%
11   * Copyright (C) 2014 - 2018 University Health Network
12   * %%
13   * Licensed under the Apache License, Version 2.0 (the "License");
14   * you may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at
16   * 
17   * http://www.apache.org/licenses/LICENSE-2.0
18   * 
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   * #L%
25   */
26  
27  import ca.uhn.fhir.model.api.IQueryParameterType;
28  import ca.uhn.fhir.rest.api.Constants;
29  import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
30  
31  /**
32   * Base class for RESTful operation parameter types
33   */
34  abstract class BaseParam implements IQueryParameterType {
35  
36  	private Boolean myMissing;
37  
38  	abstract String doGetQueryParameterQualifier();
39  
40  	abstract String doGetValueAsQueryToken(FhirContext theContext);
41  
42  	abstract void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue);
43  
44  	/**
45  	 * If set to non-null value, indicates that this parameter has been populated with a "[name]:missing=true" or "[name]:missing=false" vale instead of a normal value
46  	 */
47  	@Override
48  	public Boolean getMissing() {
49  		return myMissing;
50  	}
51  
52  	@Override
53  	public final String getQueryParameterQualifier() {
54  		if (myMissing != null && myMissing.booleanValue()) {
55  			return Constants.PARAMQUALIFIER_MISSING;
56  		}
57  		return doGetQueryParameterQualifier();
58  	}
59  
60  	@Override
61  	public final String getValueAsQueryToken(FhirContext theContext) {
62  		if (myMissing != null) {
63  			return myMissing ? Constants.PARAMQUALIFIER_MISSING_TRUE : Constants.PARAMQUALIFIER_MISSING_FALSE;
64  		}
65  		return doGetValueAsQueryToken(theContext);
66  	}
67  
68  	/**
69  	 * Does this parameter type support chained parameters (only reference should return <code>true</code> for this)
70  	 */
71  	protected boolean isSupportsChain() {
72  		return false;
73  	}
74  
75  	/**
76  	 * If set to non-null value, indicates that this parameter has been populated
77  	 * with a "[name]:missing=true" or "[name]:missing=false" vale instead of a
78  	 * normal value
79  	 * 
80  	 * @return Returns a reference to <code>this</code> for easier method chaining
81  	 */
82  	@Override
83  	public BaseParam setMissing(Boolean theMissing) {
84  		myMissing = theMissing;
85  		return this;
86  	}
87  
88  	@Override
89  	public final void setValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
90  		if (Constants.PARAMQUALIFIER_MISSING.equals(theQualifier)) {
91  			myMissing = "true".equals(theValue);
92  			doSetValueAsQueryToken(theContext, theParamName, null, null);
93  		} else {
94  			if (isNotBlank(theQualifier) && theQualifier.charAt(0) == '.') {
95  				if (!isSupportsChain()) {
96  					String msg = theContext.getLocalizer().getMessage(BaseParam.class, "chainNotSupported", theParamName + theQualifier, theQualifier);
97  					throw new InvalidRequestException(msg);
98  				}
99  			}
100 
101 			myMissing = null;
102 			doSetValueAsQueryToken(theContext, theParamName, theQualifier, theValue);
103 		}
104 	}
105 
106 }