private void handleClientTransferLifecycleEvent(final Client client, final Office destinationOffice,
final TransferEventType transferEventType, final JsonCommand jsonCommand) {
final Date todaysDate = DateUtils.getDateOfTenant();
/** Get destination loan officer if exists **/
Staff staff = null;
Group destinationGroup = null;
final Long staffId = jsonCommand.longValueOfParameterNamed(TransferApiConstants.newStaffIdParamName);
final Long destinationGroupId = jsonCommand.longValueOfParameterNamed(TransferApiConstants.destinationGroupIdParamName);
if (staffId != null) {
staff = this.staffRepositoryWrapper.findByOfficeHierarchyWithNotFoundDetection(staffId, destinationOffice.getHierarchy());
}
if (transferEventType.isAcceptance() && destinationGroupId != null) {
destinationGroup = this.groupRepository.findByOfficeWithNotFoundDetection(destinationGroupId, destinationOffice);
}
/*** Handle Active Loans ***/
if (this.loanRepository.doNonClosedLoanAccountsExistForClient(client.getId())) {
// get each individual loan for the client
for (final Loan loan : this.loanRepository.findLoanByClientId(client.getId())) {
/**
* We need to create transactions etc only for loans which are
* disbursed and not yet closed
**/
if (loan.isDisbursed() && !loan.isClosed()) {
switch (transferEventType) {
case ACCEPTANCE:
this.loanWritePlatformService.acceptLoanTransfer(loan.getId(), DateUtils.getLocalDateOfTenant(),
destinationOffice, staff);
break;
case PROPOSAL:
this.loanWritePlatformService.initiateLoanTransfer(loan.getId(), DateUtils.getLocalDateOfTenant());
break;
case REJECTION:
this.loanWritePlatformService.rejectLoanTransfer(loan.getId());
break;
case WITHDRAWAL:
this.loanWritePlatformService.withdrawLoanTransfer(loan.getId(), DateUtils.getLocalDateOfTenant());
}
}
}
}
/*** Handle Active Savings (Currently throw and exception) ***/
if (this.savingsAccountRepository.doNonClosedSavingAccountsExistForClient(client.getId())) {
// get each individual saving account for the client
for (final SavingsAccount savingsAccount : this.savingsAccountRepository.findSavingAccountByClientId(client.getId())) {
if (savingsAccount.isActivated() && !savingsAccount.isClosed()) {
switch (transferEventType) {
case ACCEPTANCE:
this.savingsAccountWritePlatformService.acceptSavingsTransfer(savingsAccount.getId(),
DateUtils.getLocalDateOfTenant(), destinationOffice, staff);
break;
case PROPOSAL:
this.savingsAccountWritePlatformService.initiateSavingsTransfer(savingsAccount.getId(),
DateUtils.getLocalDateOfTenant());
break;
case REJECTION:
this.savingsAccountWritePlatformService.rejectSavingsTransfer(savingsAccount.getId());
break;
case WITHDRAWAL:
this.savingsAccountWritePlatformService.withdrawSavingsTransfer(savingsAccount.getId(),
DateUtils.getLocalDateOfTenant());
}
}
}
}
switch (transferEventType) {
case ACCEPTANCE:
client.setStatus(ClientStatus.ACTIVE.getValue());
client.updateTransferToOffice(null);
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;