001package ca.uhn.fhir.rest.param; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2021 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.context.FhirContext; 024import ca.uhn.fhir.model.api.IQueryParameterOr; 025import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 026import ca.uhn.fhir.model.primitive.BaseDateTimeDt; 027import ca.uhn.fhir.model.primitive.DateDt; 028import ca.uhn.fhir.model.primitive.DateTimeDt; 029import ca.uhn.fhir.rest.api.QualifiedParamList; 030import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 031import ca.uhn.fhir.util.ValidateUtil; 032import org.apache.commons.lang3.builder.ToStringBuilder; 033import org.apache.commons.lang3.builder.ToStringStyle; 034import org.hl7.fhir.instance.model.api.IPrimitiveType; 035 036import java.util.Collections; 037import java.util.Date; 038import java.util.List; 039import java.util.Objects; 040 041import static org.apache.commons.lang3.StringUtils.isNotBlank; 042 043public class DateParam extends BaseParamWithPrefix<DateParam> implements /*IQueryParameterType , */IQueryParameterOr<DateParam> { 044 045 private static final long serialVersionUID = 1L; 046 047 private final DateParamDateTimeHolder myValue = new DateParamDateTimeHolder(); 048 049 /** 050 * Constructor 051 */ 052 public DateParam() { 053 super(); 054 } 055 056 /** 057 * Constructor 058 */ 059 public DateParam(ParamPrefixEnum thePrefix, Date theDate) { 060 setPrefix(thePrefix); 061 setValue(theDate); 062 } 063 064 /** 065 * Constructor 066 */ 067 public DateParam(ParamPrefixEnum thePrefix, DateTimeDt theDate) { 068 setPrefix(thePrefix); 069 myValue.setValueAsString(theDate != null ? theDate.getValueAsString() : null); 070 } 071 072 /** 073 * Constructor 074 */ 075 public DateParam(ParamPrefixEnum thePrefix, IPrimitiveType<Date> theDate) { 076 setPrefix(thePrefix); 077 myValue.setValueAsString(theDate != null ? theDate.getValueAsString() : null); 078 } 079 080 /** 081 * Constructor 082 */ 083 public DateParam(ParamPrefixEnum thePrefix, long theDate) { 084 ValidateUtil.isGreaterThan(theDate, 0, "theDate must not be 0 or negative"); 085 setPrefix(thePrefix); 086 setValue(new Date(theDate)); 087 } 088 089 /** 090 * Constructor 091 */ 092 public DateParam(ParamPrefixEnum thePrefix, String theDate) { 093 setPrefix(thePrefix); 094 setValueAsString(theDate); 095 } 096 097 098 /** 099 * Constructor which takes a complete [qualifier]{date} string. 100 * 101 * @param theString 102 * The string 103 */ 104 public DateParam(String theString) { 105 setValueAsQueryToken(null, null, null, theString); 106 } 107 108 @Override 109 String doGetQueryParameterQualifier() { 110 return null; 111 } 112 113 @Override 114 String doGetValueAsQueryToken(FhirContext theContext) { 115 StringBuilder b = new StringBuilder(); 116 if (getPrefix() != null) { 117 b.append(ParameterUtil.escapeWithDefault(getPrefix().getValue())); 118 } 119 120 b.append(ParameterUtil.escapeWithDefault(myValue.getValueAsString())); 121 122 return b.toString(); 123 } 124 125 @Override 126 void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { 127 setValueAsString(theValue); 128 } 129 130 public TemporalPrecisionEnum getPrecision() { 131 return myValue.getPrecision(); 132 } 133 134 public Date getValue() { 135 return myValue.getValue(); 136 } 137 138 public String getValueAsString() { 139 return myValue.getValueAsString(); 140 } 141 142 @Override 143 public List<DateParam> getValuesAsQueryTokens() { 144 return Collections.singletonList(this); 145 } 146 147 /** 148 * Returns <code>true</code> if no date/time is specified. Note that this method does not check the comparator, so a 149 * QualifiedDateParam with only a comparator and no date/time is considered empty. 150 */ 151 public boolean isEmpty() { 152 return myValue.isEmpty(); 153 } 154 155 /** 156 * Sets the value of the param to the given date (sets to the {@link TemporalPrecisionEnum#MILLI millisecond} 157 * precision, and will be encoded using the system local time zone). 158 */ 159 public DateParam setValue(Date theValue) { 160 myValue.setValue(theValue, TemporalPrecisionEnum.MILLI); 161 return this; 162 } 163 164 /** 165 * Sets the value using a FHIR Date type, such as a {@link DateDt}, or a DateTimeType. 166 */ 167 public void setValue(IPrimitiveType<Date> theValue) { 168 if (theValue != null) { 169 myValue.setValueAsString(theValue.getValueAsString()); 170 } else { 171 myValue.setValue(null); 172 } 173 } 174 175 /** 176 * Accepts values with or without a prefix (e.g. <code>gt2011-01-01</code> and <code>2011-01-01</code>). 177 * If no prefix is provided in the given value, the {@link #getPrefix() existing prefix} is preserved 178 */ 179 public void setValueAsString(String theDate) { 180 if (isNotBlank(theDate)) { 181 ParamPrefixEnum existingPrefix = getPrefix(); 182 myValue.setValueAsString(super.extractPrefixAndReturnRest(theDate)); 183 if (getPrefix() == null) { 184 setPrefix(existingPrefix); 185 } 186 } else { 187 myValue.setValue(null); 188 } 189 } 190 191 @Override 192 public void setValuesAsQueryTokens(FhirContext theContext, String theParamName, QualifiedParamList theParameters) { 193 setMissing(null); 194 setPrefix(null); 195 setValueAsString(null); 196 197 if (theParameters.size() == 1) { 198 setValueAsString(theParameters.get(0)); 199 } else if (theParameters.size() > 1) { 200 throw new InvalidRequestException("This server does not support multi-valued dates for this parameter: " + theParameters); 201 } 202 203 } 204 205 @Override 206 public boolean equals(Object obj) { 207 if (obj == this) { 208 return true; 209 } 210 if (!(obj instanceof DateParam)) { 211 return false; 212 } 213 DateParam other = (DateParam) obj; 214 return Objects.equals(getValue(), other.getValue()) && 215 Objects.equals(getPrefix(), other.getPrefix()); 216 } 217 218 @Override 219 public int hashCode() { 220 return Objects.hash(getValue(), getPrefix()); 221 } 222 223 @Override 224 public String toString() { 225 ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 226 b.append("prefix", getPrefix()); 227 b.append("value", getValueAsString()); 228 return b.build(); 229 } 230 231 public static class DateParamDateTimeHolder extends BaseDateTimeDt { 232 233 /** 234 * Constructor 235 */ 236 // LEAVE THIS AS PUBLIC!! 237 @SuppressWarnings("WeakerAccess") 238 public DateParamDateTimeHolder() { 239 super(); 240 } 241 242 @Override 243 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 244 return TemporalPrecisionEnum.SECOND; 245 } 246 247 @Override 248 protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 249 return true; 250 } 251 } 252}