From abef4455ebe7055dedfd54dc024c15a3c10e03b0 Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Mon, 9 Sep 2019 17:22:07 -0400 Subject: [PATCH] done --- .../fhir/model/primitive/BaseDateTimeDt.java | 14 ++++--- .../matcher/InMemoryResourceMatcherTest.java | 41 ++++++++++++++++--- pom.xml | 2 +- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/BaseDateTimeDt.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/BaseDateTimeDt.java index 903c7e1cf87..09cac9f6575 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/BaseDateTimeDt.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/BaseDateTimeDt.java @@ -41,7 +41,8 @@ public abstract class BaseDateTimeDt extends BasePrimitive { private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM); private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM); - + private static final FastDateFormat ourXmlDateTimeFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss"); + public static final String NOW_DATE_CONSTANT = "%now"; private String myFractionalSeconds; private TemporalPrecisionEnum myPrecision = null; private TimeZone myTimeZone; @@ -342,10 +343,6 @@ public abstract class BaseDateTimeDt extends BasePrimitive { throwBadDateFormat(value); } - if (value.equalsIgnoreCase("%now")) { - return new Date(); - } - TemporalPrecisionEnum precision = null; cal.set(Calendar.YEAR, parseInt(value, value.substring(0, 4), 0, 9999)); precision = TemporalPrecisionEnum.YEAR; @@ -638,7 +635,12 @@ public abstract class BaseDateTimeDt extends BasePrimitive { @Override public void setValueAsString(String theValue) throws DataFormatException { clearTimeZone(); - super.setValueAsString(theValue); + + if (NOW_DATE_CONSTANT.equalsIgnoreCase(theValue)) { + super.setValueAsString(ourXmlDateTimeFormat.format(new Date())); + } else { + super.setValueAsString(theValue); + } } /** diff --git a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/matcher/InMemoryResourceMatcherTest.java b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/matcher/InMemoryResourceMatcherTest.java index 307dcc4e0bf..4dde484952c 100644 --- a/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/matcher/InMemoryResourceMatcherTest.java +++ b/hapi-fhir-jpaserver-searchparam/src/test/java/ca/uhn/fhir/jpa/searchparam/matcher/InMemoryResourceMatcherTest.java @@ -6,6 +6,7 @@ import ca.uhn.fhir.jpa.model.entity.ResourceIndexedSearchParamDate; import ca.uhn.fhir.jpa.searchparam.MatchUrlService; import ca.uhn.fhir.jpa.searchparam.extractor.ResourceIndexedSearchParams; import ca.uhn.fhir.jpa.searchparam.registry.ISearchParamRegistry; +import ca.uhn.fhir.model.primitive.BaseDateTimeDt; import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum; import ca.uhn.fhir.rest.param.ParamPrefixEnum; import org.hl7.fhir.r5.model.BaseDateTimeType; @@ -20,13 +21,17 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.junit4.SpringRunner; +import java.time.Duration; +import java.time.Instant; +import java.util.Date; + import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @RunWith(SpringRunner.class) public class InMemoryResourceMatcherTest { - public static final String OBS_DATE = "1970-10-17"; + public static final String OBSERVATION_DATE = "1970-10-17"; private static final String EARLY_DATE = "1965-08-09"; private static final String LATE_DATE = "2000-06-29"; @@ -63,7 +68,7 @@ public class InMemoryResourceMatcherTest { when(mySearchParamRegistry.getSearchParamByName(any(), any())).thenReturn(searchParams); when(mySearchParamRegistry.getActiveSearchParam("Observation", "date")).thenReturn(searchParams); myObservation = new Observation(); - myObservation.setEffective(new DateTimeType(OBS_DATE)); + myObservation.setEffective(new DateTimeType(OBSERVATION_DATE)); mySearchParams = extractDateSearchParam(myObservation); } @@ -77,7 +82,7 @@ public class InMemoryResourceMatcherTest { } private void testDateUnsupportedOp(ParamPrefixEnum theOperator) { - InMemoryMatchResult result = myInMemoryResourceMatcher.match("date=" + theOperator.getValue() + OBS_DATE, myObservation, mySearchParams); + InMemoryMatchResult result = myInMemoryResourceMatcher.match("date=" + theOperator.getValue() + OBSERVATION_DATE, myObservation, mySearchParams); assertFalse(result.supported()); assertEquals("Parameter: Reason: The prefix " + theOperator + " is not supported for param type DATE", result.getUnsupportedReason()); } @@ -99,7 +104,7 @@ public class InMemoryResourceMatcherTest { assertEquals(result.matched(), theEarly); } { - InMemoryMatchResult result = myInMemoryResourceMatcher.match(equation + OBS_DATE, myObservation, mySearchParams); + InMemoryMatchResult result = myInMemoryResourceMatcher.match(equation + OBSERVATION_DATE, myObservation, mySearchParams); assertTrue(result.getUnsupportedReason(), result.supported()); assertEquals(result.matched(), theSame); } @@ -111,8 +116,32 @@ public class InMemoryResourceMatcherTest { } @Test - public void testNow() { - InMemoryMatchResult result = myInMemoryResourceMatcher.match("date=lt%now", myObservation, mySearchParams); + public void testNowPast() { + InMemoryMatchResult result = myInMemoryResourceMatcher.match("date=lt" + BaseDateTimeDt.NOW_DATE_CONSTANT, myObservation, mySearchParams); + assertTrue(result.getUnsupportedReason(), result.supported()); + assertTrue(result.matched()); + } + + @Test + public void testNowNextWeek() { + Observation futureObservation = new Observation(); + Instant nextWeek = Instant.now().plus(Duration.ofDays(7)); + futureObservation.setEffective(new DateTimeType(Date.from(nextWeek))); + ResourceIndexedSearchParams searchParams = extractDateSearchParam(futureObservation); + + InMemoryMatchResult result = myInMemoryResourceMatcher.match("date=gt" + BaseDateTimeDt.NOW_DATE_CONSTANT, futureObservation, searchParams); + assertTrue(result.getUnsupportedReason(), result.supported()); + assertTrue(result.matched()); + } + + @Test + public void testNowNextMinute() { + Observation futureObservation = new Observation(); + Instant nextMinute = Instant.now().plus(Duration.ofMinutes(1)); + futureObservation.setEffective(new DateTimeType(Date.from(nextMinute))); + ResourceIndexedSearchParams searchParams = extractDateSearchParam(futureObservation); + + InMemoryMatchResult result = myInMemoryResourceMatcher.match("date=gt" + BaseDateTimeDt.NOW_DATE_CONSTANT, futureObservation, searchParams); assertTrue(result.getUnsupportedReason(), result.supported()); assertTrue(result.matched()); } diff --git a/pom.xml b/pom.xml index 1db1f35315b..1dc9267f17f 100755 --- a/pom.xml +++ b/pom.xml @@ -562,7 +562,7 @@ - 4.0.8-SNAPSHOT + 4.0.10-SNAPSHOT 1.0.2