Skip to content

Commit

Permalink
Merge branch 'develop' into 246_export_include_participants_and_integ…
Browse files Browse the repository at this point in the history
…rations
  • Loading branch information
drtyyj committed Jun 5, 2024
2 parents 423e76c + 8324d86 commit 540ec1d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -98,6 +99,14 @@ public StudyTimeline getTimeline(Long studyId, Integer participantId, Integer st
final Duration studyDuration = Optional.ofNullable(effectiveGroup)
.flatMap(eg -> studyService.getStudyDuration(studyId, eg))
.or(() -> studyService.getStudyDuration(studyId))
.or(() -> Optional.of(new Duration()
.setValue(
(int) ChronoUnit.DAYS.between(
Objects.requireNonNullElse(study.getStartDate(), study.getPlannedStartDate()),
Objects.requireNonNullElse(study.getEndDate(), study.getPlannedEndDate())
) + 1)
.setUnit(Duration.Unit.DAY)
))
.orElseThrow(() -> NotFoundException.Study(studyId));

final LocalDate lastDayInStudy = firstDayInStudy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.redlink.more.studymanager.repository.StudyGroupRepository;
import io.redlink.more.studymanager.repository.StudyRepository;
import io.redlink.more.studymanager.repository.UserRepository;

import java.time.temporal.ChronoUnit;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -202,17 +204,7 @@ public Optional<StudyUserRoles> getRolesForStudy(Long studyId, String userId) {

public Optional<Duration> getStudyDuration(Long studyId) {
return studyRepository.getById(studyId)
.flatMap(study ->
Optional.ofNullable(study.getDuration())
.or(() -> Optional.of(new Duration()
.setValue(
java.time.Period.between(
Objects.requireNonNullElse(study.getStartDate(), study.getPlannedStartDate()),
Objects.requireNonNullElse(study.getEndDate(), study.getPlannedEndDate())
).getDays() + 1)
.setUnit(Duration.Unit.DAY)
))
);
.map(Study::getDuration);
}

public Optional<Duration> getStudyDuration(Long studyId, Integer studyGroupId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.redlink.more.studymanager.model.scheduler.*;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.stream.Stream;
import org.apache.commons.lang3.Range;
import org.quartz.CronExpression;

Expand Down Expand Up @@ -121,22 +122,23 @@ public static LocalDate alignStartDateToSignupInstant(final Instant signup, List
public static List<Instant> parseToInterventionSchedules(Trigger trigger, Instant start, Instant end) {
if(trigger == null) return Collections.emptyList();
if(Objects.equals(trigger.getType(), "relative-time-trigger")) {
return parseToInterventionSchedulesForRelativeTrigger(trigger, start);
return parseToInterventionSchedulesForRelativeTrigger(trigger, start, end);
} else if(Objects.equals(trigger.getType(), "scheduled-trigger")) {
return parseToInterventionSchedulesForScheduledTrigger(trigger, start, end);
} else {
return Collections.emptyList();
}
}

private static List<Instant> parseToInterventionSchedulesForRelativeTrigger(Trigger trigger, Instant start) {
return List.of(
toInstantFrom(
new RelativeDate()
.setTime(LocalTime.of(trigger.getProperties().getInt("hour"), 0))
.setOffset(new Duration().setValue(trigger.getProperties().getInt("day")).setUnit(Duration.Unit.DAY)),
start)
);
private static List<Instant> parseToInterventionSchedulesForRelativeTrigger(Trigger trigger, Instant start, Instant end) {
return Stream.of(toInstantFrom(
new RelativeDate()
.setTime(LocalTime.of(trigger.getProperties().getInt("hour"), 0))
.setOffset(new Duration().setValue(trigger.getProperties().getInt("day")).setUnit(Duration.Unit.DAY)),
start
))
.filter(i -> i.isBefore(end))
.toList();
}

private static List<Instant> parseToInterventionSchedulesForScheduledTrigger(Trigger trigger, Instant start, Instant end) {
Expand Down

0 comments on commit 540ec1d

Please sign in to comment.