001package ca.uhn.fhir.rest.param;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2019 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import static org.apache.commons.lang3.StringUtils.isBlank;
024
025public abstract class BaseParamWithPrefix<T extends BaseParam> extends BaseParam {
026
027        private static final long serialVersionUID = 1L;
028        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseParamWithPrefix.class);
029        
030        private ParamPrefixEnum myPrefix;
031
032        /**
033         * Constructor
034         */
035        // Default since this is internal
036        BaseParamWithPrefix() {
037                super();
038        }
039
040        /**
041         * Eg. if this is invoked with "gt2012-11-02", sets the prefix to GREATER_THAN and returns "2012-11-02"
042         */
043        String extractPrefixAndReturnRest(String theString) {
044                int offset = 0;
045                while (true) {
046                        if (theString.length() == offset) {
047                                break;
048                        } else {
049                                char nextChar = theString.charAt(offset);
050                                if (nextChar == '-' || Character.isDigit(nextChar)) {
051                                        break;
052                                }
053                        }
054                        offset++;
055                }
056
057                String prefix = theString.substring(0, offset);
058                if (!isBlank(prefix)) {
059                
060                        myPrefix = ParamPrefixEnum.forValue(prefix);
061        
062                        if (myPrefix == null) {
063                                switch (prefix) {
064                                case ">=":
065                                        myPrefix = ParamPrefixEnum.GREATERTHAN_OR_EQUALS;
066                                        break;
067                                case ">":
068                                        myPrefix = ParamPrefixEnum.GREATERTHAN;
069                                        break;
070                                case "<=":
071                                        myPrefix = ParamPrefixEnum.LESSTHAN_OR_EQUALS;
072                                        break;
073                                case "<":
074                                        myPrefix = ParamPrefixEnum.LESSTHAN;
075                                        break;
076                                case "~":
077                                        myPrefix = ParamPrefixEnum.APPROXIMATE;
078                                        break;
079                                default :
080                                        ourLog.warn("Invalid prefix being ignored: {}", prefix);
081                                        break;
082                                }
083                                
084                                if (myPrefix != null) {
085                                        ourLog.warn("Date parameter has legacy prefix '{}' which has been removed from FHIR. This should be replaced with '{}'", prefix, myPrefix);
086                                }
087                                
088                        }
089                        
090                }
091                
092                return theString.substring(offset);
093        }
094
095        /**
096         * Returns the prefix used by this parameter (e.g. "<code>gt</code>", or "<code>eq</code>")
097         */
098        public ParamPrefixEnum getPrefix() {
099                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}