Fix MIFOSX-1302: Throw a CurrencyInUseException whenever a currency is requested for deletion that is still in use by a charge, a loan or a savings product.

This commit is contained in:
Martin Strauss
2014-06-10 15:37:42 -07:00
parent a450829b94
commit 54b60fdcd0
8 changed files with 81 additions and 1 deletions
@@ -18,9 +18,13 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.organisation.monetary.domain.ApplicationCurrency;
import org.mifosplatform.organisation.monetary.domain.ApplicationCurrencyRepositoryWrapper;
import org.mifosplatform.organisation.monetary.exception.CurrencyInUseException;
import org.mifosplatform.organisation.monetary.serialization.CurrencyCommandFromApiJsonDeserializer;
import org.mifosplatform.organisation.office.domain.OrganisationCurrency;
import org.mifosplatform.organisation.office.domain.OrganisationCurrencyRepository;
import org.mifosplatform.portfolio.charge.service.ChargeReadPlatformService;
import org.mifosplatform.portfolio.loanproduct.service.LoanProductReadPlatformService;
import org.mifosplatform.portfolio.savings.service.SavingsProductReadPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -32,16 +36,25 @@ public class CurrencyWritePlatformServiceJpaRepositoryImpl implements CurrencyWr
private final ApplicationCurrencyRepositoryWrapper applicationCurrencyRepository;
private final OrganisationCurrencyRepository organisationCurrencyRepository;
private final CurrencyCommandFromApiJsonDeserializer fromApiJsonDeserializer;
private final LoanProductReadPlatformService loanProductService;
private final SavingsProductReadPlatformService savingsProductService;
private final ChargeReadPlatformService chargeService;
@Autowired
public CurrencyWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
final CurrencyCommandFromApiJsonDeserializer fromApiJsonDeserializer,
final ApplicationCurrencyRepositoryWrapper applicationCurrencyRepository,
final OrganisationCurrencyRepository organisationCurrencyRepository) {
final OrganisationCurrencyRepository organisationCurrencyRepository,
final LoanProductReadPlatformService loanProductService,
final SavingsProductReadPlatformService savingsProductService,
final ChargeReadPlatformService chargeService) {
this.context = context;
this.fromApiJsonDeserializer = fromApiJsonDeserializer;
this.applicationCurrencyRepository = applicationCurrencyRepository;
this.organisationCurrencyRepository = organisationCurrencyRepository;
this.loanProductService = loanProductService;
this.savingsProductService = savingsProductService;
this.chargeService = chargeService;
}
@Transactional
@@ -67,6 +80,17 @@ public class CurrencyWritePlatformServiceJpaRepositoryImpl implements CurrencyWr
allowedCurrencies.add(allowedCurrency);
}
for (OrganisationCurrency priorCurrency : this.organisationCurrencyRepository.findAll()) {
if (!allowedCurrencyCodes.contains(priorCurrency.getCode())) {
// Check if it's safe to remove this currency.
if (!loanProductService.retrieveAllLoanProductsForCurrency(priorCurrency).isEmpty() ||
!savingsProductService.retrieveAllForCurrency(priorCurrency).isEmpty() ||
!chargeService.retrieveAllChargesForCurrency(priorCurrency).isEmpty()) {
throw new CurrencyInUseException(priorCurrency.getCode());
}
}
}
changes.put("currencies", allowedCurrencyCodes.toArray(new String[allowedCurrencyCodes.size()]));
this.organisationCurrencyRepository.deleteAll();
@@ -54,4 +54,8 @@ public class OrganisationCurrency extends AbstractPersistable<Long> {
this.nameCode = nameCode;
this.displaySymbol = displaySymbol;
}
public final String getCode() {
return code;
}
}
@@ -7,12 +7,15 @@ package org.mifosplatform.portfolio.charge.service;
import java.util.Collection;
import org.mifosplatform.organisation.office.domain.OrganisationCurrency;
import org.mifosplatform.portfolio.charge.data.ChargeData;
public interface ChargeReadPlatformService {
Collection<ChargeData> retrieveAllCharges();
Collection<ChargeData> retrieveAllChargesForCurrency(OrganisationCurrency currency);
ChargeData retrieveCharge(Long chargeId);
ChargeData retrieveNewChargeDetails();
@@ -18,6 +18,7 @@ import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.organisation.monetary.data.CurrencyData;
import org.mifosplatform.organisation.monetary.service.CurrencyReadPlatformService;
import org.mifosplatform.organisation.office.domain.OrganisationCurrency;
import org.mifosplatform.portfolio.charge.data.ChargeData;
import org.mifosplatform.portfolio.charge.domain.ChargeAppliesTo;
import org.mifosplatform.portfolio.charge.exception.ChargeNotFoundException;
@@ -63,6 +64,21 @@ public class ChargeReadPlatformServiceImpl implements ChargeReadPlatformService
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
@Override
@Cacheable(value = "charges", key = "T(org.mifosplatform.infrastructure.core.service.ThreadLocalContextUtil).getTenant().getTenantIdentifier().concat('ch')")
public Collection<ChargeData> retrieveAllChargesForCurrency(
OrganisationCurrency currency) {
this.context.authenticatedUser();
final ChargeMapper rm = new ChargeMapper();
final String sql = "select " + rm.chargeSchema()
+ " where c.is_deleted=0 and c.currency_code='" + currency.getCode()
+ "' order by c.name ";
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
@Override
public ChargeData retrieveCharge(final Long chargeId) {
try {
@@ -7,6 +7,7 @@ package org.mifosplatform.portfolio.loanproduct.service;
import java.util.Collection;
import org.mifosplatform.organisation.office.domain.OrganisationCurrency;
import org.mifosplatform.portfolio.loanproduct.data.LoanProductBorrowerCycleVariationData;
import org.mifosplatform.portfolio.loanproduct.data.LoanProductData;
@@ -19,6 +20,8 @@ public interface LoanProductReadPlatformService {
LoanProductData retrieveLoanProduct(Long productId);
LoanProductData retrieveNewLoanProductDetails();
Collection<LoanProductData> retrieveAllLoanProductsForCurrency(OrganisationCurrency currency);
Collection<LoanProductData> retrieveAvailableLoanProductsForMix();
@@ -19,6 +19,7 @@ import org.mifosplatform.infrastructure.core.service.DateUtils;
import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.organisation.monetary.data.CurrencyData;
import org.mifosplatform.organisation.office.domain.OrganisationCurrency;
import org.mifosplatform.portfolio.charge.data.ChargeData;
import org.mifosplatform.portfolio.charge.service.ChargeReadPlatformService;
import org.mifosplatform.portfolio.loanproduct.data.LoanProductBorrowerCycleVariationData;
@@ -290,6 +291,19 @@ public class LoanProductReadPlatformServiceImpl implements LoanProductReadPlatfo
}
}
@Override
public Collection<LoanProductData> retrieveAllLoanProductsForCurrency(
OrganisationCurrency currency) {
this.context.authenticatedUser();
final LoanProductMapper rm = new LoanProductMapper(null, null);
final String sql = "select " + rm.loanProductSchema()
+ " where lp.currency_code='" + currency.getCode() + "'";
return this.jdbcTemplate.query(sql, rm, new Object[] {});
}
@Override
public Collection<LoanProductData> retrieveAvailableLoanProductsForMix() {
@@ -7,6 +7,7 @@ package org.mifosplatform.portfolio.savings.service;
import java.util.Collection;
import org.mifosplatform.organisation.office.domain.OrganisationCurrency;
import org.mifosplatform.portfolio.savings.data.SavingsProductData;
public interface SavingsProductReadPlatformService {
@@ -17,6 +18,8 @@ public interface SavingsProductReadPlatformService {
Collection<SavingsProductData> retrieveAllForLookupByType(Boolean isOverdraftType);
Collection<SavingsProductData> retrieveAllForCurrency(OrganisationCurrency currency);
SavingsProductData retrieveOne(Long productId);
}
@@ -16,6 +16,7 @@ import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.organisation.monetary.data.CurrencyData;
import org.mifosplatform.organisation.office.domain.OrganisationCurrency;
import org.mifosplatform.portfolio.savings.DepositAccountType;
import org.mifosplatform.portfolio.savings.data.SavingsProductData;
import org.mifosplatform.portfolio.savings.exception.SavingsProductNotFoundException;
@@ -187,4 +188,16 @@ public class SavingsProductReadPlatformServiceImpl implements SavingsProductRead
return this.jdbcTemplate.query(sql, this.savingsProductLookupsRowMapper);
}
@Override
public Collection<SavingsProductData> retrieveAllForCurrency(
OrganisationCurrency currency) {
this.context.authenticatedUser();
final String sql = "select " + this.savingsProductRowMapper.schema()
+ " where sp.currency_code='" + currency.getCode() + "'";
return this.jdbcTemplate.query(sql, this.savingsProductRowMapper, new Object[] { DepositAccountType.SAVINGS_DEPOSIT.getValue() });
}
}