Move common work to DateUtils. Rename function for clarity.

This commit is contained in:
Gary Graham 2020-02-25 09:37:57 -05:00
parent 7f5b3394e0
commit 34d5925393
3 changed files with 54 additions and 34 deletions

View File

@ -6,7 +6,7 @@ import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.rest.api.QualifiedParamList; import ca.uhn.fhir.rest.api.QualifiedParamList;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import org.apache.commons.lang3.time.DateUtils; import ca.uhn.fhir.util.DateUtils;
import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.instance.model.api.IPrimitiveType;
import java.util.*; import java.util.*;
@ -262,21 +262,29 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
validateAndSet(myLowerBound, new DateParam(ParamPrefixEnum.LESSTHAN, theUpperBound)); validateAndSet(myLowerBound, new DateParam(ParamPrefixEnum.LESSTHAN, theUpperBound));
return this; return this;
} }
public Integer getLowerBoundAsDateOrdinal() {
/**
* Return the current lower bound as an integer representative of the date.
*
* e.g. 2019-02-22T04:22:00-0500 -> 20120922
*/
public Integer getLowerBoundAsDateInteger() {
if (myLowerBound == null || myLowerBound.getValue() == null) { if (myLowerBound == null || myLowerBound.getValue() == null) {
return null; return null;
} }
Calendar cal = DateUtils.toCalendar(myLowerBound.getValue()); return DateUtils.convertDatetoDayInteger(myLowerBound.getValue());
String s = new StringBuilder().append(cal.get(Calendar.YEAR)).append(cal.get(Calendar.MONTH)).append(cal.get(Calendar.DAY_OF_MONTH)).toString();
return Integer.parseInt(s);
} }
public Integer getUpperBoundAsDateOrdinal() {
/**
* Return the current upper bound as an integer representative of the date
*
* e.g. 2019-02-22T04:22:00-0500 -> 2019122
*/
public Integer getUpperBoundAsDateInteger() {
if (myUpperBound == null || myUpperBound.getValue() == null) { if (myUpperBound == null || myUpperBound.getValue() == null) {
return null; return null;
} }
Calendar cal = DateUtils.toCalendar(myUpperBound.getValue()); return DateUtils.convertDatetoDayInteger(myUpperBound.getValue());
String s = new StringBuilder().append(cal.get(Calendar.YEAR)).append(cal.get(Calendar.MONTH)).append(cal.get(Calendar.DAY_OF_MONTH)).toString();
return Integer.parseInt(s);
} }
public Date getLowerBoundAsInstant() { public Date getLowerBoundAsInstant() {
@ -286,10 +294,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
Date retVal = myLowerBound.getValue(); Date retVal = myLowerBound.getValue();
if (myLowerBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) { if (myLowerBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) {
Calendar cal = DateUtils.toCalendar(retVal); retVal = DateUtils.getLowestInstantFromDate(retVal);
cal.setTimeZone(TimeZone.getTimeZone("GMT-11:30"));
cal = DateUtils.truncate(cal, Calendar.DATE);
retVal = cal.getTime();
} }
if (myLowerBound.getPrefix() != null) { if (myLowerBound.getPrefix() != null) {
@ -350,17 +355,8 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
Date retVal = myUpperBound.getValue(); Date retVal = myUpperBound.getValue();
//if (myUpperBound.getPrecision().ordinal() == TemporalPrecisionEnum.DAY.ordinal()) {
// DateUtils.setHours(retVal, 23);
// DateUtils.setMinutes(retVal, 59);
// DateUtils.setSeconds(retVal, 59);
// }
if (myUpperBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) { if (myUpperBound.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal()) {
Calendar cal = DateUtils.toCalendar(retVal); retVal = DateUtils.getHighestInstantFromDate(retVal);
cal.setTimeZone(TimeZone.getTimeZone("GMT+11:30"));
cal = DateUtils.truncate(cal, Calendar.DATE);
retVal = cal.getTime();
} }
if (myUpperBound.getPrefix() != null) { if (myUpperBound.getPrefix() != null) {

View File

@ -153,6 +153,30 @@ public final class DateUtils {
return null; return null;
} }
public static Date getHighestInstantFromDate(Date theDateValue) {
return getInstantFromDateWithTimezone(theDateValue, TimeZone.getTimeZone("GMT+11:30"));
}
public static Date getLowestInstantFromDate(Date theDateValue) {
return getInstantFromDateWithTimezone(theDateValue, TimeZone.getTimeZone("GMT-11:30"));
}
public static Date getInstantFromDateWithTimezone(Date theDateValue, TimeZone theTimezone) {
Calendar cal = org.apache.commons.lang3.time.DateUtils.toCalendar(theDateValue);
cal.setTimeZone(theTimezone);
cal = org.apache.commons.lang3.time.DateUtils.truncate(cal, Calendar.DATE);
return cal.getTime();
}
public static int convertDatetoDayInteger(final Date theDateValue) {
notNull(theDateValue, "Date value");
Calendar cal = org.apache.commons.lang3.time.DateUtils.toCalendar(theDateValue);
String s = String.valueOf(cal.get(Calendar.YEAR)) + cal.get(Calendar.MONTH) + cal.get(Calendar.DAY_OF_MONTH);
return Integer.parseInt(s);
}
/** /**
* Formats the given date according to the RFC 1123 pattern. * Formats the given date according to the RFC 1123 pattern.
* *

View File

@ -152,7 +152,7 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
} }
//im like 80% sure this should be ub and not lb, as it is an UPPER bound. //im like 80% sure this should be ub and not lb, as it is an UPPER bound.
if (isOrdinalComparison) { if (isOrdinalComparison) {
lb = theBuilder.lessThan(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateOrdinal()); lb = theBuilder.lessThan(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateInteger());
} else { } else {
lb = theBuilder.lessThan(theFrom.get("myValueLow"), lowerBoundInstant); lb = theBuilder.lessThan(theFrom.get("myValueLow"), lowerBoundInstant);
} }
@ -162,7 +162,7 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
} }
//im like 80% sure this should be ub and not lb, as it is an UPPER bound. //im like 80% sure this should be ub and not lb, as it is an UPPER bound.
if (isOrdinalComparison) { if (isOrdinalComparison) {
lb = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateOrdinal()); lb = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateInteger());
} else { } else {
lb = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHigh"), upperBoundInstant); lb = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHigh"), upperBoundInstant);
} }
@ -171,7 +171,7 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
throw new InvalidRequestException("upperBound value not correctly specified for compare operation"); throw new InvalidRequestException("upperBound value not correctly specified for compare operation");
} }
if (isOrdinalComparison) { if (isOrdinalComparison) {
lb = theBuilder.greaterThan(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateOrdinal()); lb = theBuilder.greaterThan(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateInteger());
} else { } else {
lb = theBuilder.greaterThan(theFrom.get("myValueHigh"), upperBoundInstant); lb = theBuilder.greaterThan(theFrom.get("myValueHigh"), upperBoundInstant);
} }
@ -180,7 +180,7 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
throw new InvalidRequestException("lowerBound value not correctly specified for compare operation"); throw new InvalidRequestException("lowerBound value not correctly specified for compare operation");
} }
if (isOrdinalComparison) { if (isOrdinalComparison) {
lb = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateOrdinal()); lb = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateInteger());
} else { } else {
lb = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueLow"), lowerBoundInstant); lb = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueLow"), lowerBoundInstant);
} }
@ -190,8 +190,8 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
throw new InvalidRequestException("lowerBound and/or upperBound value not correctly specified for compare operation"); throw new InvalidRequestException("lowerBound and/or upperBound value not correctly specified for compare operation");
} }
if (isOrdinalComparison){ if (isOrdinalComparison){
lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateOrdinal()); lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateInteger());
gt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateOrdinal()); gt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateInteger());
} else { } else {
lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLow"), lowerBoundInstant); lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLow"), lowerBoundInstant);
gt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueHigh"), upperBoundInstant); gt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueHigh"), upperBoundInstant);
@ -201,8 +201,8 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
} else if ((operation == SearchFilterParser.CompareOperation.eq) || (operation == null)) { } else if ((operation == SearchFilterParser.CompareOperation.eq) || (operation == null)) {
if (lowerBoundInstant != null) { if (lowerBoundInstant != null) {
if (isOrdinalComparison) { if (isOrdinalComparison) {
gt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateOrdinal()); gt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getLowerBoundAsDateInteger());
lt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getLowerBoundAsDateOrdinal()); lt = theBuilder.greaterThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getLowerBoundAsDateInteger());
//also try a strict equality here. //also try a strict equality here.
} }
else { else {
@ -218,8 +218,8 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
if (upperBoundInstant != null) { if (upperBoundInstant != null) {
if (isOrdinalComparison) { if (isOrdinalComparison) {
gt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getUpperBoundAsDateOrdinal()); gt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLowDateOrdinal"), theRange.getUpperBoundAsDateInteger());
lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateOrdinal()); lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHighDateOrdinal"), theRange.getUpperBoundAsDateInteger());
} else { } else {
gt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLow"), upperBoundInstant); gt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueLow"), upperBoundInstant);
lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHigh"), upperBoundInstant); lt = theBuilder.lessThanOrEqualTo(theFrom.get("myValueHigh"), upperBoundInstant);
@ -235,7 +235,7 @@ public class PredicateBuilderDate extends BasePredicateBuilder implements IPredi
operation.name())); operation.name()));
} }
if (isOrdinalComparison) { if (isOrdinalComparison) {
ourLog.trace("Ordinal date range is {} - {} ", theRange.getLowerBoundAsDateOrdinal(), theRange.getUpperBoundAsDateOrdinal()); ourLog.trace("Ordinal date range is {} - {} ", theRange.getLowerBoundAsDateInteger(), theRange.getUpperBoundAsDateInteger());
} }
ourLog.trace("Date range is {} - {}", lowerBoundInstant, upperBoundInstant); ourLog.trace("Date range is {} - {}", lowerBoundInstant, upperBoundInstant);