From bae2a7088eac7dd0a1a6265e48f121f41a7af579 Mon Sep 17 00:00:00 2001 From: Nayan Date: Thu, 4 Dec 2014 14:56:42 +0530 Subject: [PATCH] Option to change center meeting frequency and intervals --- .../portfolio/calendar/domain/Calendar.java | 15 ---------- .../domain/CalendarInstanceRepository.java | 8 +++++ ...alendarCommandFromApiJsonDeserializer.java | 29 +++++++++++++++++++ ...WritePlatformServiceJpaRepositoryImpl.java | 19 ++++++++++++ 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/Calendar.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/Calendar.java index 4d8df2ae3..11877313f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/Calendar.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/Calendar.java @@ -276,21 +276,6 @@ public class Calendar extends AbstractAuditableCustom { final String newRecurrence = Calendar.constructRecurrence(command, this); if (!StringUtils.isBlank(this.recurrence) && !newRecurrence.equalsIgnoreCase(this.recurrence)) { - - // FIXME: AA - Is this restriction required only for collection type - // meetings or for all?. - // Do not allow to change meeting frequency - - if (!CalendarUtils.isFrequencySame(this.recurrence, newRecurrence)) { - final String defaultUserMessage = "Update of meeting frequency is not supported"; - throw new CalendarParameterUpdateNotSupportedException("meeting.frequency", defaultUserMessage); - } - - // Do not allow to change meeting interval - if (!CalendarUtils.isIntervalSame(this.recurrence, newRecurrence)) { - final String defaultUserMessage = "Update of meeting interval is not supported"; - throw new CalendarParameterUpdateNotSupportedException("meeting.interval", defaultUserMessage); - } actualChanges.put("recurrence", newRecurrence); this.recurrence = StringUtils.defaultIfEmpty(newRecurrence, null); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstanceRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstanceRepository.java index 10bd67985..5aa34bd4b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstanceRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/domain/CalendarInstanceRepository.java @@ -41,4 +41,12 @@ public interface CalendarInstanceRepository extends JpaRepository findCalendarInstancesForActiveLoansByGroupIdAndClientId(@Param("groupId") Long groupId, @Param("clientId") Long clientId); + + /** + * EntityType = 3 is for loan + */ + + @Query("SELECT COUNT(ci.id) FROM CalendarInstance ci, Loan ln WHERE ln.id = ci.entityId AND ci.entityTypeId = 3 AND ci.calendar.id = :calendarId AND ln.loanStatus IN :loanStatuses ") + Integer countOfActiveLoansWithSyncedToCalendar(@Param("calendarId") Long calendarId, @Param("loanStatuses") Collection loanStatuses ); + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/serialization/CalendarCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/serialization/CalendarCommandFromApiJsonDeserializer.java index deeeee2b9..90e6ca8ec 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/serialization/CalendarCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/serialization/CalendarCommandFromApiJsonDeserializer.java @@ -25,6 +25,7 @@ import org.mifosplatform.portfolio.calendar.domain.CalendarEntityType; import org.mifosplatform.portfolio.calendar.domain.CalendarFrequencyType; import org.mifosplatform.portfolio.calendar.domain.CalendarRemindBy; import org.mifosplatform.portfolio.calendar.domain.CalendarWeekDaysType; +import org.mifosplatform.portfolio.calendar.exception.CalendarParameterUpdateNotSupportedException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -299,4 +300,32 @@ public class CalendarCommandFromApiJsonDeserializer extends AbstractFromApiJsonD "Validation errors exist.", dataValidationErrors); } } + public void validateBusinessRulesForUpdate(final String json, final Integer numberOfActiveLoansSyncedTheCalendar) { + + + final JsonElement element = this.fromApiJsonHelper.parse(json); + + /* + * BR1: If calendar is not associated with any of the active loans, RD + * accounts and any other active entity then only frequency and interval + * are allowed to change + */ + + if (numberOfActiveLoansSyncedTheCalendar > 0) { + + if (this.fromApiJsonHelper.parameterExists(CALENDAR_SUPPORTED_PARAMETERS.FREQUENCY.getValue(), element)) { + final String defaultUserMessage = "Update of meeting frequency is not supported"; + throw new CalendarParameterUpdateNotSupportedException("meeting.frequency", defaultUserMessage); + } + + if (this.fromApiJsonHelper.parameterExists(CALENDAR_SUPPORTED_PARAMETERS.INTERVAL.getValue(), element)) { + final String defaultUserMessage = "Update of meeting interval is not supported"; + throw new CalendarParameterUpdateNotSupportedException("meeting.interval", defaultUserMessage); + } + + } + + } + + } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java index 3c6fdb158..314becdaf 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/calendar/service/CalendarWritePlatformServiceJpaRepositoryImpl.java @@ -6,6 +6,7 @@ package org.mifosplatform.portfolio.calendar.service; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.List; @@ -39,6 +40,7 @@ import org.mifosplatform.portfolio.group.domain.Group; import org.mifosplatform.portfolio.group.domain.GroupRepositoryWrapper; import org.mifosplatform.portfolio.loanaccount.domain.Loan; import org.mifosplatform.portfolio.loanaccount.domain.LoanRepository; +import org.mifosplatform.portfolio.loanaccount.domain.LoanStatus; import org.mifosplatform.portfolio.loanaccount.service.LoanWritePlatformService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -154,9 +156,26 @@ public class CalendarWritePlatformServiceJpaRepositoryImpl implements CalendarWr @Override public CommandProcessingResult updateCalendar(final JsonCommand command) { + + /* + * Validate all the data for updating the calendar + */ + this.fromApiJsonDeserializer.validateForUpdate(command.json()); final Long calendarId = command.entityId(); + + final Collection loanStatuses = new ArrayList<>(Arrays.asList(LoanStatus.SUBMITTED_AND_PENDING_APPROVAL.getValue(), + LoanStatus.APPROVED.getValue(), LoanStatus.ACTIVE.getValue())); + + final Integer numberOfActiveLoansSyncedTheCalendar = this.calendarInstanceRepository.countOfActiveLoansWithSyncedToCalendar(calendarId, loanStatuses); + + /* + * Validate all the business rules + */ + this.fromApiJsonDeserializer.validateBusinessRulesForUpdate(command.json(), numberOfActiveLoansSyncedTheCalendar); + + final Calendar calendarForUpdate = this.calendarRepository.findOne(calendarId); if (calendarForUpdate == null) { throw new CalendarNotFoundException(calendarId); } final Date oldStartDate = calendarForUpdate.getStartDate();