View Javadoc
1   package ca.uhn.fhir.rest.param;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  import static org.apache.commons.lang3.StringUtils.defaultString;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.commons.lang3.builder.*;
26  
27  import ca.uhn.fhir.context.FhirContext;
28  import ca.uhn.fhir.model.api.IQueryParameterType;
29  import ca.uhn.fhir.model.primitive.StringDt;
30  import ca.uhn.fhir.rest.api.Constants;
31  
32  public class StringParam extends BaseParam implements IQueryParameterType {
33  
34  	private boolean myContains;
35  	private boolean myExact;
36  	private String myValue;
37  	
38  	/**
39  	 * Constructor
40  	 */
41  	public StringParam() {
42  	}
43  
44  	/**
45  	 * Constructor
46  	 */
47  	public StringParam(String theValue) {
48  		setValue(theValue);
49  	}
50  
51  	/**
52  	 * Constructor
53  	 */
54  	public StringParam(String theValue, boolean theExact) {
55  		setValue(theValue);
56  		setExact(theExact);
57  	}
58  
59  	@Override
60  	String doGetQueryParameterQualifier() {
61  		if (isExact()) {
62  			return Constants.PARAMQUALIFIER_STRING_EXACT;
63  		} else if (isContains()) {
64  			return Constants.PARAMQUALIFIER_STRING_CONTAINS;
65  		} else {
66  			return null;
67  		}
68  	}
69  
70  	@Override
71  	String doGetValueAsQueryToken(FhirContext theContext) {
72  		return ParameterUtil.escape(myValue);
73  	}
74  
75  	@Override
76  	void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) {
77  		if (Constants.PARAMQUALIFIER_STRING_EXACT.equals(theQualifier)) {
78  			setExact(true);
79  		} else {
80  			setExact(false);
81  		}
82  		if (Constants.PARAMQUALIFIER_STRING_CONTAINS.equals(theQualifier)) {
83  			setContains(true);
84  		} else {
85  			setContains(false);
86  		}
87  		myValue = ParameterUtil.unescape(theValue);
88  	}
89  
90  	@Override
91  	public boolean equals(Object obj) {
92  		if (this == obj) {
93  			return true;
94  		}
95  		if (obj == null) {
96  			return false;
97  		}
98  		if (!(obj instanceof StringParam)) {
99  			return false;
100 		}
101 
102 		StringParam other = (StringParam) obj;
103 
104 		EqualsBuilder eb = new EqualsBuilder();
105 		eb.append(myExact, other.myExact);
106 		eb.append(myContains, other.myContains);
107 		eb.append(myValue, other.myValue);
108 		eb.append(getMissing(), other.getMissing());
109 		
110 		return eb.isEquals();
111 	}
112 
113 	public String getValue() {
114 		return myValue;
115 	}
116 
117 	public StringDt getValueAsStringDt() {
118 		return new StringDt(myValue);
119 	}
120 
121 	public String getValueNotNull() {
122 		return defaultString(myValue);
123 	}
124 
125 	/**
126 	 * String parameter modifier <code>:contains</code>
127 	 */
128 	public boolean isContains() {
129 		return myContains;
130 	}
131 
132 	public boolean isEmpty() {
133 		return StringUtils.isEmpty(myValue);
134 	}
135 
136 	public boolean isExact() {
137 		return myExact;
138 	}
139 
140 	/**
141 	 * String parameter modifier <code>:contains</code>
142 	 */
143 	public StringParam setContains(boolean theContains) {
144 		myContains = theContains;
145 		if (myContains) {
146 			setExact(false);
147 			setMissing(null);
148 		}
149 		return this;
150 	}
151 
152 	public StringParam setExact(boolean theExact) {
153 		myExact = theExact;
154 		if (myExact) {
155 			setContains(false);
156 			setMissing(null);
157 		}
158 		return this;
159 	}
160 
161 	public StringParam setValue(String theValue) {
162 		myValue = theValue;
163 		return this;
164 	}
165 
166 	@Override
167 	public String toString() {
168 		ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
169 		builder.append("value", getValue());
170 		if (myExact) {
171 			builder.append("exact", myExact);
172 		}
173 		if (myContains) {
174 			builder.append("contains", myContains);
175 		}
176 		if (getMissing() != null) {
177 			builder.append("missing", getMissing().booleanValue());
178 		}
179 		return builder.toString();
180 	}
181 
182 }