Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create StartDate and EndDate types [DHIS2-16019] #17454

Merged
merged 16 commits into from
May 22, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
Expand Down Expand Up @@ -689,8 +691,9 @@ public static Date parseDateEndOfTheDay(String dateString) {

try {
Date date = safeParseDateTime(dateString, ONLY_DATE_FORMATTER);
org.joda.time.LocalDateTime localDateTime = org.joda.time.LocalDateTime.fromDateFields(date);
return localDateTime.withMillisOfDay(localDateTime.millisOfDay().getMaximumValue()).toDate();
LocalDateTime localDateTime =
LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(LocalTime.MAX);
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
} catch (IllegalArgumentException e) {
// dateString has time defined
teleivo marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@
import org.hisp.dhis.webapi.controller.tracker.imports.IdSchemeParamEditor;
import org.hisp.dhis.webapi.security.apikey.ApiTokenAuthenticationException;
import org.hisp.dhis.webapi.security.apikey.ApiTokenError;
import org.hisp.dhis.webapi.webdomain.EndDate;
import org.hisp.dhis.webapi.webdomain.StartDate;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.TypeDescriptor;
Expand Down Expand Up @@ -152,13 +150,6 @@ public CrudControllerAdvice() {

@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(
StartDate.class,
new FromTextPropertyEditor(dateString -> new StartDate(DateUtils.parseDate(dateString))));
binder.registerCustomEditor(
EndDate.class,
new FromTextPropertyEditor(
dateString -> new EndDate(DateUtils.parseDateEndOfTheDay(dateString))));
binder.registerCustomEditor(Date.class, new FromTextPropertyEditor(DateUtils::parseDate));
binder.registerCustomEditor(
IdentifiableProperty.class, new FromTextPropertyEditor(String::toUpperCase));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import org.hisp.dhis.webapi.controller.tracker.view.Event;
import org.hisp.dhis.webapi.controller.tracker.view.TrackedEntity;
import org.hisp.dhis.webapi.controller.tracker.view.User;
import org.hisp.dhis.webapi.webdomain.EndDate;
import org.hisp.dhis.webapi.webdomain.StartDate;

/**
* Represents query parameters sent to {@link EventsExportController}.
Expand Down Expand Up @@ -139,9 +141,9 @@ public class EventRequestParams implements PageRequestParams {

private Date scheduledBefore;

private Date updatedAfter;
private StartDate updatedAfter;
teleivo marked this conversation as resolved.
Show resolved Hide resolved

private Date updatedBefore;
private EndDate updatedBefore;

private String updatedWithin;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,14 @@ public EventOperationParams map(EventRequestParams eventRequestParams)
.occurredBefore(eventRequestParams.getOccurredBefore())
.scheduledAfter(eventRequestParams.getScheduledAfter())
.scheduledBefore(eventRequestParams.getScheduledBefore())
.updatedAfter(eventRequestParams.getUpdatedAfter())
.updatedBefore(eventRequestParams.getUpdatedBefore())
.updatedAfter(
eventRequestParams.getUpdatedAfter() != null
? eventRequestParams.getUpdatedAfter().getDate()
: null)
jbee marked this conversation as resolved.
Show resolved Hide resolved
.updatedBefore(
eventRequestParams.getUpdatedBefore() != null
? eventRequestParams.getUpdatedBefore().getDate()
: null)
.updatedWithin(eventRequestParams.getUpdatedWithin())
.enrollmentEnrolledBefore(eventRequestParams.getEnrollmentEnrolledBefore())
.enrollmentEnrolledAfter(eventRequestParams.getEnrollmentEnrolledAfter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
package org.hisp.dhis.webapi.webdomain;

import java.util.Date;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.util.DateUtils;

/**
* EndDate represents an upper limit date used to filter results in search APIs.
Expand All @@ -40,14 +43,12 @@
* search including start and end dates. startDate=2020-10-10&endDate=2020-10-12 will include
* anything between 2020-10-10T00:00:00.000 and 2020-10-12T23:59:59.999.
*/
@RequiredArgsConstructor
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
enricocolasante marked this conversation as resolved.
Show resolved Hide resolved
public class EndDate {
private final Date date;

public static Date getDate(EndDate date) {
if (date == null) {
return null;
}
return date.date;
public static EndDate valueOf(String date) {
return new EndDate(DateUtils.parseDateEndOfTheDay(date));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
package org.hisp.dhis.webapi.webdomain;

import java.util.Date;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.util.DateUtils;

/**
* StartDate represents a lower limit date used to filter results in search APIs.
Expand All @@ -40,14 +43,12 @@
* including start and end dates. startDate=2020-10-10&endDate=2020-10-12 will include anything
* between 2020-10-10T00:00:00.000 and 2020-10-12T23:59:59.999.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we leave

startDate=2020-10-10&endDate=2020-10-12 will include anything between 2020-10-10T00:00:00.000 and 2020-10-12T23:59:59.999.

from these docs here as this is at the discretion of the endpoint itself?

Or do we "standardize" on this behavior, in that case it makes sense to keep it here. I am not sure its 100% clear right now with include anything between that the endpoints are included as well. So maybe we could change the wording or add (represent a closed interval [startDate, endDate].

*/
@RequiredArgsConstructor
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class StartDate {
private final Date date;

public static Date getDate(StartDate date) {
if (date == null) {
return null;
}
return date.date;
public static StartDate valueOf(String date) {
return new StartDate(DateUtils.parseDate(date));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ class UIDBindingTest {

@BeforeEach
public void setUp() {
mockMvc =
MockMvcBuilders.standaloneSetup(new UIDController())
.setControllerAdvice(new CrudControllerAdvice())
.build();
mockMvc = MockMvcBuilders.standaloneSetup(new UIDController()).build();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ class DatesBindingTest {

@BeforeEach
public void setUp() {
mockMvc =
MockMvcBuilders.standaloneSetup(new BindingController())
.setControllerAdvice(new CrudControllerAdvice())
.build();
mockMvc = MockMvcBuilders.standaloneSetup(new BindingController()).build();
enricocolasante marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
Expand Down Expand Up @@ -93,11 +90,11 @@ void shouldReturnADateWithTimeWhenAnStartDateIsPassedWithTime() throws Exception
}

@Controller
private class BindingController extends CrudControllerAdvice {
private class BindingController {
Fixed Show fixed Hide fixed
@GetMapping(value = ENDPOINT)
public @ResponseBody WebMessage getDefault(Criteria criteria) {
Date startDate = StartDate.getDate(criteria.getStartDate());
Date endDate = EndDate.getDate(criteria.getEndDate());
Date startDate = criteria.getStartDate() == null ? null : criteria.getStartDate().getDate();
Date endDate = criteria.getEndDate() == null ? null : criteria.getEndDate().getDate();
return ok(DateUtils.toIso8601NoTz(startDate) + " - " + DateUtils.toIso8601NoTz(endDate));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ class OrderBindingTest {

@BeforeEach
public void setUp() {
mockMvc =
MockMvcBuilders.standaloneSetup(new OrderingController())
.setControllerAdvice(new CrudControllerAdvice())
.build();
mockMvc = MockMvcBuilders.standaloneSetup(new OrderingController()).build();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ class SpringBindingTest {

@BeforeEach
public void setUp() {
mockMvc =
MockMvcBuilders.standaloneSetup(new BindingController())
.setControllerAdvice(new CrudControllerAdvice())
.build();
mockMvc = MockMvcBuilders.standaloneSetup(new BindingController()).build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is enum related code in the CrudControllerAdvice. Not sure what it does but I wonder if CrudControllerAdvice should stay in here or the code in CrudControllerAdvice relating enums should be removed if it has no influence. Either way maybe do this in a separate PR.

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserService;
import org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria;
import org.hisp.dhis.webapi.webdomain.EndDate;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -264,7 +265,7 @@ void shouldMapAfterAndBeforeDatesWhenSupplied() throws BadRequestException {

Date updatedAfter = parseDate("2022-01-01");
eventRequestParams.setUpdatedAfter(updatedAfter);
Date updatedBefore = parseDate("2022-09-12");
EndDate updatedBefore = EndDate.valueOf("2022-09-12");
eventRequestParams.setUpdatedBefore(updatedBefore);

EventOperationParams params = mapper.map(eventRequestParams);
Expand All @@ -290,7 +291,7 @@ void shouldFailWithBadRequestExceptionWhenTryingToMapAllUpdateDatesTogether() {

Date updatedAfter = parseDate("2022-01-01");
eventRequestParams.setUpdatedAfter(updatedAfter);
Date updatedBefore = parseDate("2022-09-12");
EndDate updatedBefore = EndDate.valueOf("2022-09-12");
eventRequestParams.setUpdatedBefore(updatedBefore);
String updatedWithin = "P6M";
eventRequestParams.setUpdatedWithin(updatedWithin);
Expand Down
Loading