From fcee0a333ed1bcef8387f756d831fd3c127f9f1a Mon Sep 17 00:00:00 2001 From: Vishwas Babu A J Date: Mon, 9 Sep 2013 04:56:35 +0530 Subject: [PATCH] cleaning up transfers functionality --- .../service/LoanReadPlatformServiceImpl.java | 12 ++++++++---- .../exception/TransferNotSupportedException.java | 10 +++++++++- ...sferWritePlatformServiceJpaRepositoryImpl.java | 15 +++++++++++---- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java index 044297758..11dadbc75 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/service/LoanReadPlatformServiceImpl.java @@ -139,9 +139,11 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { sqlBuilder.append("select "); sqlBuilder.append(rm.loanSchema()); sqlBuilder.append(" join m_office o on (o.id = c.office_id or o.id = g.office_id) "); - sqlBuilder.append(" where l.id=? and o.hierarchy like ?"); + sqlBuilder.append(" left join m_office transferToOffice on transferToOffice.id = c.transfer_to_office_id "); + sqlBuilder.append(" where l.id=? and ( o.hierarchy like ? or transferToOffice.hierarchy like ?)"); - return this.jdbcTemplate.queryForObject(sqlBuilder.toString(), rm, new Object[] { loanId, hierarchySearchString }); + return this.jdbcTemplate.queryForObject(sqlBuilder.toString(), rm, new Object[] { loanId, hierarchySearchString, + hierarchySearchString }); } catch (EmptyResultDataAccessException e) { throw new LoanNotFoundException(loanId); } @@ -202,11 +204,13 @@ public class LoanReadPlatformServiceImpl implements LoanReadPlatformService { // probably require a UNION query // but that at present is an edge case sqlBuilder.append(" join m_office o on o.id = c.office_id"); - sqlBuilder.append(" where o.hierarchy like ?"); + sqlBuilder.append(" left join m_office transferToOffice on transferToOffice.id = c.transfer_to_office_id "); + sqlBuilder.append(" where ( o.hierarchy like ? or transferToOffice.hierarchy like ?)"); final Object[] objectArray = new Object[2]; objectArray[0] = hierarchySearchString; - int arrayPos = 1; + objectArray[1] = hierarchySearchString; + int arrayPos = 2; String sqlQueryCriteria = searchParameters.getSqlSearch(); if (StringUtils.isNotBlank(sqlQueryCriteria)) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/exception/TransferNotSupportedException.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/exception/TransferNotSupportedException.java index 4f55a68d6..27273ee8e 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/exception/TransferNotSupportedException.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/exception/TransferNotSupportedException.java @@ -15,13 +15,17 @@ public class TransferNotSupportedException extends AbstractPlatformDomainRuleExc /*** enum of reasons for invalid Journal Entry **/ public static enum TRANSFER_NOT_SUPPORTED_REASON { - SOURCE_AND_DESTINATION_GROUP_CANNOT_BE_SAME, ACTIVE_SAVINGS_ACCOUNT, BULK_CLIENT_TRANSFER_ACROSS_BRANCHES, DESTINATION_GROUP_MEETING_FREQUENCY_MISMATCH, DESTINATION_GROUP_HAS_NO_MEETING; + CLIENT_DESTINATION_GROUP_NOT_SPECIFIED, CLIENT_BELONGS_TO_MULTIPLE_GROUPS, SOURCE_AND_DESTINATION_GROUP_CANNOT_BE_SAME, ACTIVE_SAVINGS_ACCOUNT, BULK_CLIENT_TRANSFER_ACROSS_BRANCHES, DESTINATION_GROUP_MEETING_FREQUENCY_MISMATCH, DESTINATION_GROUP_HAS_NO_MEETING; public String errorMessage() { if (name().toString().equalsIgnoreCase("ACTIVE_SAVINGS_ACCOUNT")) { return "Cannot transfer Clients/Groups having active Savings accounts"; } else if (name().toString().equalsIgnoreCase("BULK_CLIENT_TRANSFER_ACROSS_BRANCHES")) { return "Bulk Transfers of clients between Groups in different branches not allowed "; + } else if (name().toString().equalsIgnoreCase("CLIENT_DESTINATION_GROUP_NOT_SPECIFIED")) { + return "Destination Group for transfer of clients originally linked to a group not selected "; + } else if (name().toString().equalsIgnoreCase("CLIENT_BELONGS_TO_MULTIPLE_GROUPS")) { + return "Transfer of clients linked to multiple groups is not supported "; } else if (name().toString().equalsIgnoreCase("DESTINATION_GROUP_MEETING_FREQUENCY_MISMATCH")) { return "Cannot transfer Clients with active accounts between groups with different meeting frequency"; } else if (name().toString().equalsIgnoreCase("SOURCE_AND_DESTINATION_GROUP_CANNOT_BE_SAME")) { @@ -35,6 +39,10 @@ public class TransferNotSupportedException extends AbstractPlatformDomainRuleExc return "error.msg.entity.transfers.with.active.savings.accounts"; } else if (name().toString().equalsIgnoreCase("BULK_CLIENT_TRANSFER_ACROSS_BRANCHES")) { return "error.msg.groups.bulk.client.transfers.to.different.office"; + } else if (name().toString().equalsIgnoreCase("CLIENT_DESTINATION_GROUP_NOT_SPECIFIED")) { + return "error.msg.client.transfers.destination.group.absent"; + } else if (name().toString().equalsIgnoreCase("CLIENT_BELONGS_TO_MULTIPLE_GROUPS")) { + return "error.msg.client.transfers.with.multiple.group.linkages"; } else if (name().toString().equalsIgnoreCase("DESTINATION_GROUP_MEETING_FREQUENCY_MISMATCH")) { return "error.msg.client.transfers.with.active.accounts.between.groups.with.different.meeting.frequency"; } else if (name().toString().equalsIgnoreCase("SOURCE_AND_DESTINATION_GROUP_CANNOT_BE_SAME")) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/service/TransferWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/service/TransferWritePlatformServiceJpaRepositoryImpl.java index ec2f2430f..df7e1d12c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/service/TransferWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/transfer/service/TransferWritePlatformServiceJpaRepositoryImpl.java @@ -30,6 +30,7 @@ import org.mifosplatform.portfolio.client.exception.ClientHasBeenClosedException import org.mifosplatform.portfolio.group.domain.Group; import org.mifosplatform.portfolio.group.domain.GroupRepositoryWrapper; import org.mifosplatform.portfolio.group.exception.ClientNotInGroupException; +import org.mifosplatform.portfolio.group.exception.GroupNotActiveException; import org.mifosplatform.portfolio.loanaccount.domain.Loan; import org.mifosplatform.portfolio.loanaccount.domain.LoanRepository; import org.mifosplatform.portfolio.loanaccount.service.LoanWritePlatformService; @@ -235,8 +236,6 @@ public class TransferWritePlatformServiceJpaRepositoryImpl implements TransferWr final Long destinationOfficeId = jsonCommand.longValueOfParameterNamed(TransferApiConstants.destinationOfficeIdParamName); final Office office = this.officeRepository.findOneWithNotFoundDetection(destinationOfficeId); final Client client = this.clientRepository.findOneWithNotFoundDetection(clientId); - /*** Remove all group associations for this client ***/ - client.getGroups().clear(); handleClientTransferLifecycleEvent(client, office, TransferEventType.PROPOSAL, jsonCommand); this.clientRepository.save(client); @@ -266,8 +265,6 @@ public class TransferWritePlatformServiceJpaRepositoryImpl implements TransferWr final Client client = this.clientRepository.findOneWithNotFoundDetection(clientId); validateClientAwaitingTransferAcceptance(client); - /*** Remove all (old) group associations for this client ***/ - client.getGroups().clear(); handleClientTransferLifecycleEvent(client, client.getTransferToOffice(), TransferEventType.ACCEPTANCE, jsonCommand); this.clientRepository.save(client); @@ -358,7 +355,17 @@ public class TransferWritePlatformServiceJpaRepositoryImpl implements TransferWr client.updateOffice(destinationOffice); client.updateOfficeJoiningDate(todaysDate); if (client.getGroups().size() == 1) { + if (destinationGroup == null) { + throw new TransferNotSupportedException(TRANSFER_NOT_SUPPORTED_REASON.CLIENT_DESTINATION_GROUP_NOT_SPECIFIED, + client.getId()); + } else if (!destinationGroup.isActive()) { throw new GroupNotActiveException(destinationGroup.getId()); } transferClientBetweenGroups(Iterables.get(client.getGroups(), 0), client, destinationGroup, true, staff); + } else if (client.getGroups().size() == 0 && destinationGroup != null) { + client.getGroups().add(destinationGroup); + client.updateStaff(destinationGroup.getStaff()); + if (staff != null) { + client.updateStaff(staff); + } } break; case PROPOSAL: