Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.5 kB
2
Indexable
Never
  public void validateTimeScheduleEvent(UUID organizationId, TimeScheduleEvent event) {
    var existingTimeScheduleEvents = timeScheduleEventRepository
        .findAllByOrganizationIdAndEmployeeId(organizationId, event.getEmployeeId());
    if (CollectionUtils.isEmpty(existingTimeScheduleEvents)) {
      return;
    }
    validateTimeScheduleEventRanges(existingTimeScheduleEvents, event);
  }


  private void validateTimeScheduleEventUpdate(UUID organizationId, TimeScheduleEvent event) {
    var existingTimeScheduleEvents = seq(
        timeScheduleEventRepository.findAllByOrganizationIdAndEmployeeId(
            organizationId, event.getEmployeeId()
        )

    )
        .filter(e -> !Objects.equals(e.getId(), event.getId()))
        .collect(Collectors.toList());
    if (CollectionUtils.isEmpty(existingTimeScheduleEvents)) {
      return;
    }
    validateTimeScheduleEventRanges(existingTimeScheduleEvents, event);
  }

  private void validateTimeScheduleEventRanges(
      List<TimeScheduleEvent> existing, TimeScheduleEvent event
  ) {
    var expected = Lists.newArrayList(existing);
    expected.add(event);
    Collections.sort(expected, Comparator.comparing(TimeScheduleEvent::getStart));

    for (int i = 0; i < expected.size() - 1; ++i) {
      var isInterception = expected.get(i).getEnd().isAfter(expected.get(i + 1).getStart());
      if (isInterception) {
        throw new ConflictException("Time schedule events should not intercept");
      }
    }
  }