Skip to content

Commit

Permalink
MODCAL-61 implement validation for working hours overlapping, create …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
roman-barannyk committed Jan 9, 2020
1 parent 5b19f13 commit f7708c7
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 52 deletions.
34 changes: 32 additions & 2 deletions src/main/java/org/folio/rest/utils/CalendarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,42 @@ private static Map<DayOfWeek, OpeningDayWeekDay> getOpeningDays(OpeningPeriod en
EnumMap openingDays = new EnumMap(DayOfWeek.class);

for (OpeningDayWeekDay openingDay : entity.getOpeningDays()) {
openingDays.put(DayOfWeek.valueOf(openingDay.getWeekdays().getDay().toString()), openingDay);
if (!isOverlap(openingDay.getOpeningDay().getOpeningHour())) {
openingDays.put(DayOfWeek.valueOf(openingDay.getWeekdays().getDay().toString()), openingDay);
} else {
throw new OverlapIntervalException("Intervals can not overlap.");
}
}

return openingDays;
}

private static boolean isOverlap(List<OpeningHour> openingHours)
{
// Sort intervals in increasing order of start time
openingHours.sort(Comparator.comparingInt(hours -> toMinutes(hours.getStartTime())));

// In the sorted array, if start time of an interval
// is less than end of previous interval, then there is an overlap
for (int i = 1; i < openingHours.size(); i++) {
if (toMinutes(openingHours.get(i - 1).getEndTime()) > toMinutes(openingHours.get(i).getStartTime())) {
return true;
}
}
return false;
}

/**
* @param time H:m timestamp, i.e. [Hour in day (0-23)]:[Minute in hour (0-59)]
* @return total minutes after 00:00
*/
private static int toMinutes(String time) {
String[] hourMin = time.split(":");
int hour = Integer.parseInt(hourMin[0]);
int minutes = Integer.parseInt(hourMin[1]);
int hoursInMinutes = hour * 60;
return hoursInMinutes + minutes;
}

private static List<ActualOpeningHours> createEvents(OpeningDay openingDay, Calendar actualDay, String generatedId, boolean isExceptional) {
Calendar currentStartDate = Calendar.getInstance(TimeZone.getTimeZone(ZoneOffset.UTC));
currentStartDate.setTimeInMillis(actualDay.getTimeInMillis());
Expand Down
Loading

0 comments on commit f7708c7

Please sign in to comment.