From 89377a23b4332352546c6192c55c0c216e999cd0 Mon Sep 17 00:00:00 2001 From: sachinkulkarni12 Date: Thu, 19 Mar 2015 16:01:55 +0530 Subject: [PATCH] MIFOSX-1885 Conflicts: mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java --- ...WritePlatformServiceJpaRepositoryImpl.java | 28 +++++++++++-------- .../loanaccount/domain/LoanRepository.java | 4 +++ .../domain/LoanRepositoryWrapper.java | 11 ++++++++ ...p_group_client_foreign_key_from_m_loan.sql | 2 ++ 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 mifosng-provider/src/main/resources/sql/migrations/core_db/V246__drop_group_client_foreign_key_from_m_loan.sql diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java index f0f5187c0..2afdd890f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java @@ -55,6 +55,7 @@ import org.mifosplatform.portfolio.group.exception.InvalidGroupStateTransitionEx import org.mifosplatform.portfolio.group.serialization.GroupingTypesDataValidator; import org.mifosplatform.portfolio.loanaccount.domain.Loan; import org.mifosplatform.portfolio.loanaccount.domain.LoanRepository; +import org.mifosplatform.portfolio.loanaccount.domain.LoanRepositoryWrapper; import org.mifosplatform.portfolio.note.domain.Note; import org.mifosplatform.portfolio.note.domain.NoteRepository; import org.mifosplatform.portfolio.savings.domain.SavingsAccount; @@ -89,6 +90,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group private final CalendarInstanceRepository calendarInstanceRepository; private final ConfigurationDomainService configurationDomainService; private final SavingsAccountRepository savingsAccountRepository; + private final LoanRepositoryWrapper loanRepositoryWrapper; @Autowired public GroupingTypesWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, @@ -98,7 +100,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group final LoanRepository loanRepository, final SavingsAccountRepository savingsRepository, final CodeValueRepositoryWrapper codeValueRepository, final CommandProcessingService commandProcessingService, final CalendarInstanceRepository calendarInstanceRepository, final ConfigurationDomainService configurationDomainService, - final SavingsAccountRepository savingsAccountRepository) { + final SavingsAccountRepository savingsAccountRepository, final LoanRepositoryWrapper loanRepositoryWrapper) { this.context = context; this.groupRepository = groupRepository; this.clientRepositoryWrapper = clientRepositoryWrapper; @@ -114,6 +116,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group this.calendarInstanceRepository = calendarInstanceRepository; this.configurationDomainService = configurationDomainService; this.savingsAccountRepository = savingsAccountRepository; + this.loanRepositoryWrapper = loanRepositoryWrapper; } private CommandProcessingResult createGroupingType(final JsonCommand command, final GroupTypes groupingType, final Long centerId) { @@ -131,7 +134,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group parentGroup = this.groupRepository.findOneWithNotFoundDetection(centerId); officeId = parentGroup.officeId(); } - + final Office groupOffice = this.officeRepository.findOne(officeId); if (groupOffice == null) { throw new OfficeNotFoundException(officeId); } @@ -184,10 +187,13 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group } // pre-save to generate id for use in group hierarchy - this.groupRepository.save(newGroup); + this.groupRepository.save(newGroup); - /* Generate hierarchy for a new center/group and all the child groups if they exist */ - newGroup.generateHierarchy(); + /* + * Generate hierarchy for a new center/group and all the child + * groups if they exist + */ + newGroup.generateHierarchy(); this.groupRepository.saveAndFlush(newGroup); newGroup.captureStaffHistoryDuringCenterCreation(staff, activationDate); @@ -415,7 +421,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group groupForUpdate.unassignStaff(); } this.groupRepository.saveAndFlush(groupForUpdate); - + actualChanges.put(staffIdParamName, null); return new CommandProcessingResultBuilder() // @@ -732,7 +738,7 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group final Set clientMembers = assembleSetOfClients(groupForUpdate.officeId(), command); // check if any client has got group loans - validateForJLGLoan(groupForUpdate.getId(), clientMembers); + checkForActiveJLGLoans(groupForUpdate.getId(), clientMembers); validateForJLGSavings(groupForUpdate.getId(), clientMembers); final Map actualChanges = new HashMap<>(); @@ -760,14 +766,14 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group final Group centerForUpdate = this.groupRepository.findOneWithNotFoundDetection(centerId); final Set groupMembers = assembleSetOfChildGroups(centerForUpdate.officeId(), command); checkGroupMembersMeetingSyncWithCenterMeeting(centerId, groupMembers); - + final Map actualChanges = new HashMap<>(); final List changes = centerForUpdate.associateGroups(groupMembers); if (!changes.isEmpty()) { actualChanges.put(GroupingTypesApiConstants.groupMembersParamName, changes); } - + this.groupRepository.saveAndFlush(centerForUpdate); return new CommandProcessingResultBuilder() // @@ -807,9 +813,9 @@ public class GroupingTypesWritePlatformServiceJpaRepositoryImpl implements Group } @Transactional - private void validateForJLGLoan(final Long groupId, final Set clientMembers) { + private void checkForActiveJLGLoans(final Long groupId, final Set clientMembers) { for (final Client client : clientMembers) { - final Collection loans = this.loanRepository.findByClientIdAndGroupId(client.getId(), groupId); + final Collection loans = this.loanRepositoryWrapper.findActiveLoansByLoanIdAndGroupId(client.getId(), groupId); if (!CollectionUtils.isEmpty(loans)) { final String defaultUserMessage = "Client with identifier " + client.getId() + " cannot be disassociated it has group loans."; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepository.java index 2a4bbb727..6017537eb 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepository.java @@ -92,6 +92,10 @@ public interface LoanRepository extends JpaRepository, JpaSpecificat @Query("from Loan loan where loan.client.id = :clientId and loan.group.id = :groupId") List findByClientIdAndGroupId(@Param("clientId") Long clientId, @Param("groupId") Long groupId); + @Query("from Loan loan where loan.client.id = :clientId and loan.group.id = :groupId and loan.loanStatus IN :loanStatuses") + List findByClientIdAndGroupIdAndLoanStatus(@Param("clientId") Long clientId, @Param("groupId") Long groupId, + @Param("loanStatuses") Collection loanStatuses); + @Query("from Loan loan where loan.client.id = :clientId") List findLoanByClientId(@Param("clientId") Long clientId); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java index beb165216..c69570286 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java @@ -5,6 +5,10 @@ */ package org.mifosplatform.portfolio.loanaccount.domain; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + import org.mifosplatform.portfolio.loanaccount.exception.LoanNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -31,4 +35,11 @@ public class LoanRepositoryWrapper { return loan; } + public Collection findActiveLoansByLoanIdAndGroupId(Long clientId, Long groupId) { + final Collection loanStatuses = new ArrayList<>(Arrays.asList(LoanStatus.SUBMITTED_AND_PENDING_APPROVAL.getValue(), + LoanStatus.APPROVED.getValue(), LoanStatus.ACTIVE.getValue())); + final Collection loans = this.repository.findByClientIdAndGroupIdAndLoanStatus(clientId, groupId, loanStatuses); + return loans; + } + } \ No newline at end of file diff --git a/mifosng-provider/src/main/resources/sql/migrations/core_db/V246__drop_group_client_foreign_key_from_m_loan.sql b/mifosng-provider/src/main/resources/sql/migrations/core_db/V246__drop_group_client_foreign_key_from_m_loan.sql new file mode 100644 index 000000000..4b9b539fa --- /dev/null +++ b/mifosng-provider/src/main/resources/sql/migrations/core_db/V246__drop_group_client_foreign_key_from_m_loan.sql @@ -0,0 +1,2 @@ +ALTER TABLE `m_loan` + DROP FOREIGN KEY `fk_m_group_client_001`; \ No newline at end of file