if (isCalendarInherited) {
Set<Group> groups = account.getClient().getGroups();
Long groupId = null;
if (groups.isEmpty()) {
final String defaultUserMessage = "Client does not belong to group/center. Cannot follow group/center meeting frequency.";
throw new GeneralPlatformDomainRuleException(
"error.msg.recurring.deposit.account.cannot.create.not.belongs.to.any.groups.to.follow.meeting.frequency",
defaultUserMessage, account.clientId());
} else if (groups.size() > 1) {
final String defaultUserMessage = "Client belongs to more than one group. Cannot support recurring deposit.";
throw new GeneralPlatformDomainRuleException(
"error.msg.recurring.deposit.account.cannot.create.belongs.to.multiple.groups", defaultUserMessage,
account.clientId());
} else {
Group group = groups.iterator().next();
Group parent = group.getParent();
Integer entityType = CalendarEntityType.GROUPS.getValue();
if (parent != null) {
groupId = parent.getId();
entityType = CalendarEntityType.CENTERS.getValue();
} else {
groupId = group.getId();
}
CalendarInstance parentCalendarInstance = this.calendarInstanceRepository.findByEntityIdAndEntityTypeIdAndCalendarTypeId(
groupId, entityType, CalendarType.COLLECTION.getValue());
calendarInstance = CalendarInstance.from(parentCalendarInstance.getCalendar(), account.getId(),
CalendarEntityType.SAVINGS.getValue());
}
} else {
LocalDate calendarStartDate = account.depositStartDate();
final Integer frequencyType = command.integerValueSansLocaleOfParameterNamed(recurringFrequencyTypeParamName);
final PeriodFrequencyType periodFrequencyType = PeriodFrequencyType.fromInt(frequencyType);
final Integer frequency = command.integerValueSansLocaleOfParameterNamed(recurringFrequencyParamName);
final Integer repeatsOnDay = calendarStartDate.getDayOfWeek();
final String title = "recurring_savings_" + account.getId();
final Calendar calendar = Calendar.createRepeatingCalendar(title, calendarStartDate, CalendarType.COLLECTION.getValue(),
CalendarFrequencyType.from(periodFrequencyType), frequency, repeatsOnDay);
calendarInstance = CalendarInstance.from(calendar, account.getId(), CalendarEntityType.SAVINGS.getValue());
}
if (calendarInstance == null) {
final String defaultUserMessage = "No valid recurring details available for recurring depost account creation.";
throw new GeneralPlatformDomainRuleException(
"error.msg.recurring.deposit.account.cannot.create.no.valid.recurring.details.available", defaultUserMessage,
account.clientId());
}
return calendarInstance;
}