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