this.fromApiJsonDeserializer.validateForCreate(command.json());
Long entityId = null;
CalendarEntityType entityType = CalendarEntityType.INVALID;
LocalDate entityActivationDate = null;
Group centerOrGroup = null;
if (command.getGroupId() != null) {
centerOrGroup = this.groupRepository.findOneWithNotFoundDetection(command.getGroupId());
entityActivationDate = centerOrGroup.getActivationLocalDate();
entityType = centerOrGroup.isCenter() ? CalendarEntityType.CENTERS : CalendarEntityType.GROUPS;
entityId = command.getGroupId();
} else if (command.getLoanId() != null) {
final Loan loan = this.loanRepository.findOne(command.getLoanId());
entityActivationDate = (loan.getApprovedOnDate() == null) ? loan.getSubmittedOnDate() : loan.getApprovedOnDate();
entityType = CalendarEntityType.LOANS;
entityId = command.getLoanId();
} else if (command.getClientId() != null) {
final Client client = this.clientRepository.findOneWithNotFoundDetection(command.getClientId());
entityActivationDate = client.getActivationLocalDate();
entityType = CalendarEntityType.CLIENTS;
entityId = command.getClientId();
}
final Integer entityTypeId = entityType.getValue();
final Calendar newCalendar = Calendar.fromJson(command);
final List<ApiParameterError> dataValidationErrors = new ArrayList<>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("calendar");
if (entityActivationDate == null || newCalendar.getStartDateLocalDate().isBefore(entityActivationDate)) {
final DateTimeFormatter formatter = DateTimeFormat.forPattern(command.dateFormat()).withLocale(command.extractLocale());
String dateAsString = "";
if (entityActivationDate != null) dateAsString = formatter.print(entityActivationDate);
final String errorMessage = "cannot.be.before." + entityType.name().toLowerCase() + ".activation.date";
baseDataValidator.reset().parameter(CALENDAR_SUPPORTED_PARAMETERS.START_DATE.getValue()).value(dateAsString)
.failWithCodeNoParameterAddedToErrorCode(errorMessage);
}
if (centerOrGroup != null) {
Long centerOrGroupId = centerOrGroup.getId();
Integer centerOrGroupEntityTypeId = entityType.getValue();
final Group parent = centerOrGroup.getParent();
if (parent != null) {
centerOrGroupId = parent.getId();
centerOrGroupEntityTypeId = CalendarEntityType.CENTERS.getValue();
}
final CalendarInstance collectionCalendarInstance = this.calendarInstanceRepository
.findByEntityIdAndEntityTypeIdAndCalendarTypeId(centerOrGroupId, centerOrGroupEntityTypeId,