Skip to content

Commit

Permalink
Merge pull request #108 from folio-org/MODCAL-63
Browse files Browse the repository at this point in the history
MODCAL-63 implement validation for working hours overlapping, create …
  • Loading branch information
roman-barannyk authored Jan 10, 2020
2 parents 1ff0965 + 2cbce8a commit fdb760e
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private Criterion assembleCriterionToCheckOverlapExcludeOwnId(Openings openings)

private String getErrorMessage(boolean isExceptional) {
return isExceptional
? "Intervals can not overlap."
? "Intervals cannot overlap."
: "The date range entered overlaps with another calendar for this service point. Please correct the date range or enter the hours as exceptions.";
}
}
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 cannot 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 fdb760e

Please sign in to comment.