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  
23  import static org.apache.commons.lang3.StringUtils.isBlank;
24  
25  public abstract class BaseParamWithPrefix<T extends BaseParam> extends BaseParam {
26  
27  	private static final long serialVersionUID = 1L;
28  	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseParamWithPrefix.class);
29  	
30  	private ParamPrefixEnum myPrefix;
31  
32  	/**
33  	 * Constructor
34  	 */
35  	// Default since this is internal
36  	BaseParamWithPrefix() {
37  		super();
38  	}
39  
40  	/**
41  	 * Eg. if this is invoked with "gt2012-11-02", sets the prefix to GREATER_THAN and returns "2012-11-02"
42  	 */
43  	String extractPrefixAndReturnRest(String theString) {
44  		int offset = 0;
45  		while (true) {
46  			if (theString.length() == offset) {
47  				break;
48  			} else {
49  				char nextChar = theString.charAt(offset);
50  				if (nextChar == '-' || Character.isDigit(nextChar)) {
51  					break;
52  				}
53  			}
54  			offset++;
55  		}
56  
57  		String prefix = theString.substring(0, offset);
58  		if (!isBlank(prefix)) {
59  		
60  			myPrefix = ParamPrefixEnum.forValue(prefix);
61  	
62  			if (myPrefix == null) {
63  				switch (prefix) {
64  				case ">=":
65  					myPrefix = ParamPrefixEnum.GREATERTHAN_OR_EQUALS;
66  					break;
67  				case ">":
68  					myPrefix = ParamPrefixEnum.GREATERTHAN;
69  					break;
70  				case "<=":
71  					myPrefix = ParamPrefixEnum.LESSTHAN_OR_EQUALS;
72  					break;
73  				case "<":
74  					myPrefix = ParamPrefixEnum.LESSTHAN;
75  					break;
76  				case "~":
77  					myPrefix = ParamPrefixEnum.APPROXIMATE;
78  					break;
79  				default :
80  					ourLog.warn("Invalid prefix being ignored: {}", prefix);
81  					break;
82  				}
83  				
84  				if (myPrefix != null) {
85  					ourLog.warn("Date parameter has legacy prefix '{}' which has been removed from FHIR. This should be replaced with '{}'", prefix, myPrefix);
86  				}
87  				
88  			}
89  			
90  		}
91  		
92  		return theString.substring(offset);
93  	}
94  
95  	/**
96  	 * Returns the prefix used by this parameter (e.g. "<code>gt</code>", or "<code>eq</code>")
97  	 */
98  	public ParamPrefixEnum getPrefix() {
99  		return myPrefix;
100 	}
101 
102 	/**
103 	 * Sets the prefix used by this parameter (e.g. "<code>gt</code>", or "<code>eq</code>")
104 	 */
105 	@SuppressWarnings("unchecked")
106 	public T setPrefix(ParamPrefixEnum thePrefix) {
107 		myPrefix = thePrefix;
108 		return (T) this;
109 	}
110 }