use of org.apache.commons.math3.exception.TooManyIterationsException in project axelor-open-suite by axelor.
the class EventServiceImpl method addRecurrentEventsByMonths.
@Override
@Transactional
public void addRecurrentEventsByMonths(Event event, int periodicity, int endType, int repetitionsNumber, LocalDate endDate, int monthRepeatType) {
int weekNo = 1 + (event.getStartDateTime().getDayOfMonth() - 1) / 7;
Duration duration = Duration.between(event.getStartDateTime(), event.getEndDateTime());
Event lastEvent = event;
BiFunction<Integer, LocalDateTime, Boolean> breakConditionFunc;
Function<LocalDateTime, LocalDateTime> nextStartDateTimeFunc;
LocalDateTime nextStartDateTime;
if (endType == RecurrenceConfigurationRepository.END_TYPE_REPET) {
breakConditionFunc = (iteration, dateTime) -> iteration >= repetitionsNumber;
} else {
breakConditionFunc = (iteration, dateTime) -> dateTime.toLocalDate().isAfter(endDate);
}
if (monthRepeatType == RecurrenceConfigurationRepository.REPEAT_TYPE_MONTH) {
nextStartDateTimeFunc = dateTime -> dateTime.withDayOfMonth(1).plusMonths(periodicity).withDayOfMonth(event.getStartDateTime().getDayOfMonth());
} else {
nextStartDateTimeFunc = dateTime -> {
LocalDateTime baseNextDateTime = dateTime.withDayOfMonth(1).plusMonths(periodicity);
dateTime = baseNextDateTime.with(TemporalAdjusters.dayOfWeekInMonth(weekNo, event.getStartDateTime().getDayOfWeek()));
if (!dateTime.getMonth().equals(baseNextDateTime.getMonth()) && weekNo > 1) {
dateTime = baseNextDateTime.with(TemporalAdjusters.dayOfWeekInMonth(weekNo - 1, event.getStartDateTime().getDayOfWeek()));
}
return dateTime;
};
}
for (int iteration = 0; ; ++iteration) {
if (iteration > ITERATION_LIMIT) {
throw new TooManyIterationsException(iteration);
}
nextStartDateTime = nextStartDateTimeFunc.apply(lastEvent.getStartDateTime());
if (breakConditionFunc.apply(iteration, nextStartDateTime)) {
break;
}
Event copy = eventRepo.copy(lastEvent, false);
copy.setParentEvent(event);
copy.setStartDateTime(nextStartDateTime);
copy.setEndDateTime(nextStartDateTime.plus(duration));
lastEvent = eventRepo.save(copy);
}
}
use of org.apache.commons.math3.exception.TooManyIterationsException in project axelor-open-suite by axelor.
the class EventServiceImpl method addRecurrentEventsByWeeks.
@Override
@Transactional
public void addRecurrentEventsByWeeks(Event event, int periodicity, int endType, int repetitionsNumber, LocalDate endDate, Map<Integer, Boolean> daysCheckedMap) {
List<DayOfWeek> dayOfWeekList = daysCheckedMap.keySet().stream().sorted().map(DayOfWeek::of).collect(Collectors.toList());
Duration duration = Duration.between(event.getStartDateTime(), event.getEndDateTime());
Event lastEvent = event;
BiFunction<Integer, LocalDateTime, Boolean> breakCondition;
if (endType == RecurrenceConfigurationRepository.END_TYPE_REPET) {
breakCondition = (iteration, dateTime) -> iteration >= repetitionsNumber;
} else {
breakCondition = (iteration, dateTime) -> dateTime.toLocalDate().isAfter(endDate);
}
boolean loop = true;
for (int iteration = 0; loop; ++iteration) {
if (iteration > ITERATION_LIMIT) {
throw new TooManyIterationsException(iteration);
}
LocalDateTime nextStartDateTime = lastEvent.getStartDateTime().plusWeeks(periodicity - 1L);
for (DayOfWeek dayOfWeek : dayOfWeekList) {
nextStartDateTime = nextStartDateTime.with(TemporalAdjusters.next(dayOfWeek));
if (breakCondition.apply(iteration, nextStartDateTime)) {
loop = false;
break;
}
Event copy = eventRepo.copy(lastEvent, false);
copy.setParentEvent(event);
copy.setStartDateTime(nextStartDateTime);
copy.setEndDateTime(nextStartDateTime.plus(duration));
lastEvent = eventRepo.save(copy);
}
}
}
Aggregations