View Javadoc
1   package ca.uhn.fhir.model.base.composite;
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  
23  import org.apache.commons.lang3.StringUtils;
24  
25  import ca.uhn.fhir.context.FhirContext;
26  import ca.uhn.fhir.model.api.BaseIdentifiableElement;
27  import ca.uhn.fhir.model.api.ICompositeDatatype;
28  import ca.uhn.fhir.model.api.IQueryParameterType;
29  import ca.uhn.fhir.model.primitive.CodeDt;
30  import ca.uhn.fhir.model.primitive.StringDt;
31  import ca.uhn.fhir.model.primitive.UriDt;
32  import ca.uhn.fhir.rest.param.ParameterUtil;
33  import ca.uhn.fhir.rest.param.TokenParam;
34  
35  public abstract class BaseCodingDt extends BaseIdentifiableElement implements ICompositeDatatype, IQueryParameterType {
36  
37  	private static final long serialVersionUID = 4425182816398730643L;
38  
39  	/**
40  	 * Gets the value(s) for <b>code</b> (Symbol in syntax defined by the system). creating it if it does not exist. Will not return <code>null</code>.
41  	 *
42  	 * <p>
43  	 * <b>Definition:</b> A symbol in syntax defined by the system. The symbol may be a predefined code or an expression in a syntax defined by the coding system (e.g. post-coordination)
44  	 * </p>
45  	 */
46  	public abstract CodeDt getCodeElement();
47  
48  	@Override
49  	public String getQueryParameterQualifier() {
50  		return null;
51  	}
52  
53  	/**
54  	 * Gets the value(s) for <b>system</b> (Identity of the terminology system). creating it if it does not exist. Will not return <code>null</code>.
55  	 *
56  	 * <p>
57  	 * <b>Definition:</b> The identification of the code system that defines the meaning of the symbol in the code.
58  	 * </p>
59  	 */
60  	public abstract UriDt getSystemElement();
61  
62  	/**
63  	 * Gets the value(s) for <b>display</b> (Representation defined by the system).
64  	 * creating it if it does
65  	 * not exist. Will not return <code>null</code>.
66  	 *
67       * <p>
68       * <b>Definition:</b>
69       * A representation of the meaning of the code in the system, following the rules of the system.
70       * </p> 
71  	 */
72  	public abstract StringDt getDisplayElement();
73  
74  	public abstract BaseCodingDt setDisplay( String theString);
75  
76  	/*
77  	todo: handle version
78  	public abstract StringDt getVersion();
79  
80  	public abstract BaseCodingDt setVersion ( String theString);
81  	*/
82  
83  	/**
84  	 * {@inheritDoc}
85  	 */
86  	@Override
87  	public String getValueAsQueryToken(FhirContext theContext) {
88  		if (getSystemElement().getValueAsString() != null) {
89  			return ParameterUtil.escape(StringUtils.defaultString(getSystemElement().getValueAsString())) + '|' + ParameterUtil.escape(getCodeElement().getValueAsString());
90  		} 
91  		return ParameterUtil.escape(getCodeElement().getValueAsString());
92  	}
93  
94  	/**
95  	 * {@inheritDoc}
96  	 */
97  	@Override
98  	public void setValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theParameter) {
99  		int barIndex = ParameterUtil.nonEscapedIndexOf(theParameter, '|');
100 		if (barIndex != -1) {
101 			setSystem(theParameter.substring(0, barIndex));
102 			setCode(ParameterUtil.unescape(theParameter.substring(barIndex + 1)));
103 		} else {
104 			setCode(ParameterUtil.unescape(theParameter));
105 		}
106 	}
107 
108 	/**
109 	 * Returns true if <code>this</code> Coding has the same {@link #getCodeElement() Code} and {@link #getSystemElement() system} (as compared by simple equals comparison). Does not compare other
110 	 * Codes (e.g. getUseElement()) or any extensions.
111 	 */
112 	public boolean matchesSystemAndCode(BaseCodingDt theCoding) {
113 		if (theCoding == null) {
114 			return false;
115 		}
116 		return getCodeElement().equals(theCoding.getCodeElement()) && getSystemElement().equals(theCoding.getSystemElement());
117 	}
118 
119 	/**
120 	 * returns true if <code>this</code> Coding matches a search for the coding specified by <code>theSearchParam</code>, according
121 	 * to the following:
122 	 * <ul>
123 	 *		<li>[parameter]=[namespace]|[code] matches a code/value in the given system namespace</li>
124 	 *		<li>[parameter]=[code] matches a code/value irrespective of it's system namespace</li>
125 	 *		<li>[parameter]=|[code] matches a code/value that has no system namespace</li>
126 	 * </ul>
127 	 * @param theSearchParam - coding to test <code>this</code> against
128 	 * @return true if the coding matches, false otherwise
129 	 */
130 	public boolean matchesToken(BaseCodingDt theSearchParam) {
131 		if (theSearchParam.isSystemPresent()) {
132 			if (theSearchParam.isSystemBlank()) {
133 				//  [parameter]=|[code] matches a code/value that has no system namespace
134 				if (isSystemPresent() && !isSystemBlank())
135 					return false;
136 			} else {
137 				//  [parameter]=[namespace]|[code] matches a code/value in the given system namespace
138 				if (!isSystemPresent())
139 					return false;
140 				if (!getSystemElement().equals(theSearchParam.getSystemElement()))
141 					return false;
142 			}
143 		} else {
144 			//  [parameter]=[code] matches a code/value irrespective of it's system namespace
145 			// (nothing to do for system for this case)
146 		}
147 
148 		return getCodeElement().equals(theSearchParam.getCodeElement());
149 	}
150 
151 	private boolean isSystemPresent() {
152 		return !getSystemElement().isEmpty();
153 	}
154 
155 	private boolean isSystemBlank() {
156 		return isSystemPresent() && getSystemElement().getValueAsString().equals("");
157 	}
158 
159 	/**
160 	 * Sets the value for <b>code</b> (Symbol in syntax defined by the system)
161 	 *
162 	 * <p>
163 	 * <b>Definition:</b> A symbol in syntax defined by the system. The symbol may be a predefined code or an expression in a syntax defined by the coding system (e.g. post-coordination)
164 	 * </p>
165 	 */
166 	public abstract BaseCodingDt setCode(String theCode);
167 
168 	/**
169 	 * Sets the value for <b>system</b> (Identity of the terminology system)
170 	 *
171 	 * <p>
172 	 * <b>Definition:</b> The identification of the code system that defines the meaning of the symbol in the code.
173 	 * </p>
174 	 */
175 	public abstract BaseCodingDt setSystem(String theUri);
176 
177 
178 	/**
179 	 * <b>Not supported!</b>
180 	 * 
181 	 * @deprecated get/setMissing is not supported in StringDt. Use {@link TokenParam} instead if you
182 	 * need this functionality
183 	 */
184 	@Deprecated
185 	@Override
186 	public Boolean getMissing() {
187 		return null;
188 	}
189 
190 	/**
191 	 * <b>Not supported!</b>
192 	 * 
193 	 * @deprecated get/setMissing is not supported in StringDt. Use {@link TokenParam} instead if you
194 	 * need this functionality
195 	 */
196 	@Deprecated
197 	@Override
198 	public IQueryParameterType setMissing(Boolean theMissing) {
199 		throw new UnsupportedOperationException("get/setMissing is not supported in StringDt. Use {@link StringParam} instead if you need this functionality");
200 	}
201 
202 }