@Override DateParam newInstance() { return new DateParam(); }
@Search() public List<Patient> searchByObservationNames( @RequiredParam(name=Patient.SP_BIRTHDATE) DateParam theDate ) { ParamPrefixEnum prefix = theDate.getPrefix(); // e.g. gt, le, etc.. Date date = theDate.getValue(); // e.g. 2011-01-02 TemporalPrecisionEnum precision = theDate.getPrecision(); // e.g. DAY List<Patient> retVal = new ArrayList<Patient>(); // ...populate... return retVal; } //END SNIPPET: dates
private void addParam(DateParam theParsed) throws InvalidRequestException { if (theParsed.getPrefix() == null || theParsed.getPrefix() == EQUAL) { if (myLowerBound != null || myUpperBound != null) { throw new InvalidRequestException("Can not have multiple date range parameters for the same param without a qualifier"); if (theParsed.getMissing() != null) { myLowerBound = theParsed; myUpperBound = theParsed; } else { myLowerBound = new DateParam(EQUAL, theParsed.getValueAsString()); myUpperBound = new DateParam(EQUAL, theParsed.getValueAsString()); switch (theParsed.getPrefix()) { case GREATERTHAN: case GREATERTHAN_OR_EQUALS: break; default: throw new InvalidRequestException("Unknown comparator: " + theParsed.getPrefix());
@Override public List<DateParam> getValuesAsQueryTokens() { ArrayList<DateParam> retVal = new ArrayList<>(); if (myLowerBound != null && myLowerBound.getMissing() != null) { retVal.add((myLowerBound)); } else { if (myLowerBound != null && !myLowerBound.isEmpty()) { retVal.add((myLowerBound)); } if (myUpperBound != null && !myUpperBound.isEmpty()) { retVal.add((myUpperBound)); } } return retVal; }
/** * Returns a new param containing the same value as this param, but with the type copnverted * to {@link DateParam}. This is useful if you are using reference parameters and want to handle * chained parameters of different types in a single method. * <p> * See <a href="http://jamesagnew.github.io/hapi-fhir/doc_rest_operations.html#dynamic_chains">Dynamic Chains</a> * in the HAPI FHIR documentation for an example of how to use this method. * </p> */ public DateParam toDateParam(FhirContext theContext) { DateParam retVal = new DateParam(); retVal.setValueAsQueryToken(theContext, null, null, getValueAsQueryToken(theContext)); return retVal; }
private void validateAndSet(DateParam lowerBound, DateParam upperBound) { if (hasBound(lowerBound) && hasBound(upperBound)) { if (lowerBound.getValue().getTime() > upperBound.getValue().getTime()) { throw new DataFormatException(format( "Lower bound of %s is after upper bound of %s", lowerBound.getValueAsString(), upperBound.getValueAsString())); if (lowerBound.getPrefix() == null) { lowerBound.setPrefix(GREATERTHAN_OR_EQUALS); switch (lowerBound.getPrefix()) { case GREATERTHAN: case GREATERTHAN_OR_EQUALS: case LESSTHAN: case LESSTHAN_OR_EQUALS: throw new DataFormatException("Lower bound comparator must be > or >=, can not be " + lowerBound.getPrefix().getValue()); if (upperBound.getPrefix() == null) { upperBound.setPrefix(LESSTHAN_OR_EQUALS); switch (upperBound.getPrefix()) { case LESSTHAN: case LESSTHAN_OR_EQUALS: case GREATERTHAN: case GREATERTHAN_OR_EQUALS: throw new DataFormatException("Upper bound comparator must be < or <=, can not be " + upperBound.getPrefix().getValue());
@Override public String toString() { ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); b.append("prefix", getPrefix()); b.append("value", getValueAsString()); return b.build(); }
@Override public void setValuesAsQueryTokens(FhirContext theContext, String theParamName, List<QualifiedParamList> theParameters) throws InvalidRequestException { boolean haveHadUnqualifiedParameter = false; for (QualifiedParamList paramList : theParameters) { if (paramList.size() == 0) { continue; } if (paramList.size() > 1) { throw new InvalidRequestException("DateRange parameter does not suppport OR queries"); } String param = paramList.get(0); /* * Since ' ' is escaped as '+' we'll be nice to anyone might have accidentally not * escaped theirs */ param = param.replace(' ', '+'); DateParam parsed = new DateParam(); parsed.setValueAsQueryToken(theContext, theParamName, paramList.getQualifier(), param); addParam(parsed); if (parsed.getPrefix() == null) { if (haveHadUnqualifiedParameter) { throw new InvalidRequestException("Multiple date parameters with the same name and no qualifier (>, <, etc.) is not supported"); } haveHadUnqualifiedParameter = true; } } }
throw new NullPointerException("theDateParam can not be null"); if (theDateParam.isEmpty()) { throw new IllegalArgumentException("theDateParam can not be empty"); if (theDateParam.getPrefix() == null) { setRangeFromDatesInclusive(theDateParam.getValueAsString(), theDateParam.getValueAsString()); } else { switch (theDateParam.getPrefix()) { case EQUAL: setRangeFromDatesInclusive(theDateParam.getValueAsString(), theDateParam.getValueAsString()); break; case STARTS_AFTER: default: throw new InvalidRequestException("Invalid comparator for date range parameter:" + theDateParam.getPrefix() + ". This is a bug.");
/** * Sets the range from a pair of dates, inclusive on both ends * * @param theLowerBound A qualified date param representing the lower date bound (optionally may include time), e.g. * "2011-02-22" or "2011-02-22T13:12:00Z". Will be treated inclusively. Either theLowerBound or * theUpperBound may both be populated, or one may be null, but it is not valid for both to be null. * @param theUpperBound A qualified date param representing the upper date bound (optionally may include time), e.g. * "2011-02-22" or "2011-02-22T13:12:00Z". Will be treated inclusively. Either theLowerBound or * theUpperBound may both be populated, or one may be null, but it is not valid for both to be null. */ public void setRangeFromDatesInclusive(String theLowerBound, String theUpperBound) { DateParam lowerBound = theLowerBound != null ? new DateParam(GREATERTHAN_OR_EQUALS, theLowerBound) : null; DateParam upperBound = theUpperBound != null ? new DateParam(LESSTHAN_OR_EQUALS, theUpperBound) : null; if (isNotBlank(theLowerBound) && isNotBlank(theUpperBound) && theLowerBound.equals(theUpperBound)) { lowerBound.setPrefix(EQUAL); upperBound.setPrefix(EQUAL); } validateAndSet(lowerBound, upperBound); }
@Override String doGetValueAsQueryToken(FhirContext theContext) { StringBuilder b = new StringBuilder(); if (getPrefix() != null) { b.append(ParameterUtil.escapeWithDefault(getPrefix().getValue())); } if (myValue != null) { b.append(ParameterUtil.escapeWithDefault(myValue.getValueAsString())); } return b.toString(); }
/** * Accepts values with or without a prefix (e.g. <code>gt2011-01-01</code> and <code>2011-01-01</code>). * If no prefix is provided in the given value, the {@link #getPrefix() existing prefix} is preserved */ public void setValueAsString(String theDate) { if (isNotBlank(theDate)) { ParamPrefixEnum existingPrefix = getPrefix(); myValue.setValueAsString(super.extractPrefixAndReturnRest(theDate)); if (getPrefix() == null) { setPrefix(existingPrefix); } } else { myValue.setValue(null); } }
private void addLastUpdateParam(StringBuilder b, DateParam date) { if (date != null && isNotBlank(date.getValueAsString())) { addUrlParamSeparator(b); b.append(Constants.PARAM_LASTUPDATED); b.append('='); b.append(date.getValueAsString()); } }
private boolean hasBound(DateParam bound) { return bound != null && !bound.isEmpty(); }
private void validateAndSet(DateParam lowerBound, DateParam upperBound) { if (hasBound(lowerBound) && hasBound(upperBound)) { if (lowerBound.getValue().getTime() > upperBound.getValue().getTime()) { throw new DataFormatException(format( "Lower bound of %s is after upper bound of %s", lowerBound.getValueAsString(), upperBound.getValueAsString())); if (lowerBound.getPrefix() == null) { lowerBound.setPrefix(GREATERTHAN_OR_EQUALS); switch (lowerBound.getPrefix()) { case GREATERTHAN: case GREATERTHAN_OR_EQUALS: case LESSTHAN: case LESSTHAN_OR_EQUALS: throw new DataFormatException("Lower bound comparator must be > or >=, can not be " + lowerBound.getPrefix().getValue()); if (upperBound.getPrefix() == null) { upperBound.setPrefix(LESSTHAN_OR_EQUALS); switch (upperBound.getPrefix()) { case LESSTHAN: case LESSTHAN_OR_EQUALS: case GREATERTHAN: case GREATERTHAN_OR_EQUALS: throw new DataFormatException("Upper bound comparator must be < or <=, can not be " + upperBound.getPrefix().getValue());
@Override public String toString() { StringBuilder b = new StringBuilder(); b.append(getClass().getSimpleName()); b.append("["); if (hasBound(myLowerBound)) { if (myLowerBound.getPrefix() != null) { b.append(myLowerBound.getPrefix().getValue()); } b.append(myLowerBound.getValueAsString()); } if (hasBound(myUpperBound)) { if (hasBound(myLowerBound)) { b.append(" "); } if (myUpperBound.getPrefix() != null) { b.append(myUpperBound.getPrefix().getValue()); } b.append(myUpperBound.getValueAsString()); } else { if (!hasBound(myLowerBound)) { b.append("empty"); } } b.append("]"); return b.toString(); }
@Override public void setValuesAsQueryTokens(FhirContext theContext, String theParamName, List<QualifiedParamList> theParameters) throws InvalidRequestException { boolean haveHadUnqualifiedParameter = false; for (QualifiedParamList paramList : theParameters) { if (paramList.size() == 0) { continue; } if (paramList.size() > 1) { throw new InvalidRequestException("DateRange parameter does not suppport OR queries"); } String param = paramList.get(0); /* * Since ' ' is escaped as '+' we'll be nice to anyone might have accidentally not * escaped theirs */ param = param.replace(' ', '+'); DateParam parsed = new DateParam(); parsed.setValueAsQueryToken(theContext, theParamName, paramList.getQualifier(), param); addParam(parsed); if (parsed.getPrefix() == null) { if (haveHadUnqualifiedParameter) { throw new InvalidRequestException("Multiple date parameters with the same name and no qualifier (>, <, etc.) is not supported"); } haveHadUnqualifiedParameter = true; } } }
throw new NullPointerException("theDateParam can not be null"); if (theDateParam.isEmpty()) { throw new IllegalArgumentException("theDateParam can not be empty"); if (theDateParam.getPrefix() == null) { setRangeFromDatesInclusive(theDateParam.getValueAsString(), theDateParam.getValueAsString()); } else { switch (theDateParam.getPrefix()) { case EQUAL: setRangeFromDatesInclusive(theDateParam.getValueAsString(), theDateParam.getValueAsString()); break; case STARTS_AFTER: default: throw new InvalidRequestException("Invalid comparator for date range parameter:" + theDateParam.getPrefix() + ". This is a bug.");