001package ca.uhn.fhir.rest.param; 002 003/*- 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 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 ca.uhn.fhir.parser.DataFormatException; 024 025import static org.apache.commons.lang3.StringUtils.isBlank; 026 027public abstract class BaseParamWithPrefix<T extends BaseParam> extends BaseParam { 028 029 private static final long serialVersionUID = 1L; 030 private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseParamWithPrefix.class); 031 032 private ParamPrefixEnum myPrefix; 033 034 /** 035 * Constructor 036 */ 037 // Default since this is internal 038 BaseParamWithPrefix() { 039 super(); 040 } 041 042 /** 043 * Eg. if this is invoked with "gt2012-11-02", sets the prefix to GREATER_THAN and returns "2012-11-02" 044 */ 045 String extractPrefixAndReturnRest(String theString) { 046 int offset = 0; 047 while (true) { 048 if (theString.length() == offset) { 049 break; 050 } else { 051 char nextChar = theString.charAt(offset); 052 if (nextChar == '-' || nextChar == '%' || Character.isDigit(nextChar)) { 053 break; 054 } 055 } 056 offset++; 057 } 058 059 if (offset > 0 && theString.length() == offset) { 060 throw new DataFormatException("Invalid date/time format: \"" + theString + "\""); 061 } 062 063 String prefix = theString.substring(0, offset); 064 if (!isBlank(prefix)) { 065 066 myPrefix = ParamPrefixEnum.forValue(prefix); 067 068 if (myPrefix == null) { 069 // prefix doesn't match standard values. Try legacy values 070 switch (prefix) { 071 case ">=": 072 myPrefix = ParamPrefixEnum.GREATERTHAN_OR_EQUALS; 073 break; 074 case ">": 075 myPrefix = ParamPrefixEnum.GREATERTHAN; 076 break; 077 case "<=": 078 myPrefix = ParamPrefixEnum.LESSTHAN_OR_EQUALS; 079 break; 080 case "<": 081 myPrefix = ParamPrefixEnum.LESSTHAN; 082 break; 083 case "~": 084 myPrefix = ParamPrefixEnum.APPROXIMATE; 085 break; 086 case "=": 087 myPrefix = ParamPrefixEnum.EQUAL; 088 break; 089 default : 090 throw new DataFormatException("Invalid prefix: \"" + prefix + "\""); 091 } 092 ourLog.warn("Date parameter has legacy prefix '{}' which has been removed from FHIR. This should be replaced with '{}'", prefix, myPrefix.getValue()); 093 } 094 095 } 096 097 return theString.substring(offset); 098 } 099 100 /** 101 * Returns the prefix used by this parameter (e.g. "<code>gt</code>", or "<code>eq</code>") 102 */ 103 public ParamPrefixEnum getPrefix() { 104 return myPrefix; 105 } 106 107 /** 108 * Sets the prefix used by this parameter (e.g. "<code>gt</code>", or "<code>eq</code>") 109 */ 110 @SuppressWarnings("unchecked") 111 public T setPrefix(ParamPrefixEnum thePrefix) { 112 myPrefix = thePrefix; 113 return (T) this; 114 } 115 116}