MIFOSX-900 - Refuse deactivating charges that are currently in use

This commit is contained in:
Sander van der Heyden
2014-02-11 10:11:50 +01:00
parent ac932974f3
commit 289e5309fc
2 changed files with 60 additions and 3 deletions
@@ -0,0 +1,13 @@
package org.mifosplatform.portfolio.charge.exception;
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException;
public class ChargeCannotBeUpdatedException extends AbstractPlatformDomainRuleException {
public ChargeCannotBeUpdatedException(final String globalisationMessageCode, final String defaultUserMessage,
final Object... defaultUserMessageArgs) {
super(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs);
}
}
@@ -18,6 +18,7 @@ import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
import org.mifosplatform.portfolio.charge.domain.Charge;
import org.mifosplatform.portfolio.charge.domain.ChargeRepository;
import org.mifosplatform.portfolio.charge.exception.ChargeCannotBeDeletedException;
import org.mifosplatform.portfolio.charge.exception.ChargeCannotBeUpdatedException;
import org.mifosplatform.portfolio.charge.exception.ChargeNotFoundException;
import org.mifosplatform.portfolio.charge.serialization.ChargeDefinitionCommandFromApiJsonDeserializer;
import org.mifosplatform.portfolio.loanproduct.domain.LoanProduct;
@@ -82,7 +83,26 @@ public class ChargeWritePlatformServiceJpaRepositoryImpl implements ChargeWriteP
final Map<String, Object> changes = chargeForUpdate.update(command);
if (!changes.isEmpty()) {
// MIFOSX-900: Check if the Charge has been active before and now is deactivated:
if (changes.containsKey("active")) {
// IF the key exists then it has changed (otherwise it would have been filtered), so check current state:
if(!chargeForUpdate.isActive())
{
// TODO: Change this function to only check the mappings!!!
final Boolean isChargeExistWithLoans = isAnyLoanProductsAssociateWithThisCharge(chargeId);
final Boolean isChargeExistWithSavings = isAnySavingsProductsAssociateWithThisCharge(chargeId);
if(isChargeExistWithLoans || isChargeExistWithSavings)
{
throw new ChargeCannotBeUpdatedException(
"error.msg.charge.cannot.be.updated.it.is.used.in.loan",
"This charge cannot be updated, it is used in loan");
}
}
}
if (!changes.isEmpty()) {
this.chargeRepository.save(chargeForUpdate);
}
@@ -103,8 +123,10 @@ public class ChargeWritePlatformServiceJpaRepositoryImpl implements ChargeWriteP
final Collection<LoanProduct> loanProducts = this.loanProductRepository.retrieveLoanProductsByChargeId(chargeId);
final Boolean isChargeExistWithLoans = isAnyLoansAssociateWithThisCharge(chargeId);
if (!loanProducts.isEmpty() || isChargeExistWithLoans) { throw new ChargeCannotBeDeletedException(
final Boolean isChargeExistWithSavings = isAnySavingsAssociateWithThisCharge(chargeId);
// TODO: Change error messages around:
if (!loanProducts.isEmpty() || isChargeExistWithLoans || isChargeExistWithSavings) { throw new ChargeCannotBeDeletedException(
"error.msg.charge.cannot.be.deleted.it.is.already.used.in.loan",
"This charge cannot be deleted, it is already used in loan"); }
@@ -139,4 +161,26 @@ public class ChargeWritePlatformServiceJpaRepositoryImpl implements ChargeWriteP
final String isLoansUsingCharge = this.jdbcTemplate.queryForObject(sql, String.class, new Object[] { chargeId });
return new Boolean(isLoansUsingCharge);
}
private boolean isAnySavingsAssociateWithThisCharge(final Long chargeId) {
final String sql = "select if((exists (select 1 from m_savings_account_charge sc where sc.charge_id = ?)) = 1, 'true', 'false')";
final String isSavingsUsingCharge = this.jdbcTemplate.queryForObject(sql, String.class, new Object[] { chargeId });
return new Boolean(isSavingsUsingCharge);
}
private boolean isAnyLoanProductsAssociateWithThisCharge(final Long chargeId) {
final String sql = "select if((exists (select 1 from m_product_loan_charge lc where lc.charge_id = ?)) = 1, 'true', 'false')";
final String isLoansUsingCharge = this.jdbcTemplate.queryForObject(sql, String.class, new Object[] { chargeId });
return new Boolean(isLoansUsingCharge);
}
private boolean isAnySavingsProductsAssociateWithThisCharge(final Long chargeId) {
final String sql = "select if((exists (select 1 from m_savings_product_charge sc where sc.charge_id = ?)) = 1, 'true', 'false')";
final String isSavingsUsingCharge = this.jdbcTemplate.queryForObject(sql, String.class, new Object[] { chargeId });
return new Boolean(isSavingsUsingCharge);
}
}