diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java index 71a5e0ce4..54eb12f51 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/monetary/service/CurrencyWritePlatformServiceJpaRepositoryImpl.java @@ -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(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/domain/OrganisationCurrency.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/domain/OrganisationCurrency.java index f763da754..53f36b1ba 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/domain/OrganisationCurrency.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/office/domain/OrganisationCurrency.java @@ -54,4 +54,8 @@ public class OrganisationCurrency extends AbstractPersistable { this.nameCode = nameCode; this.displaySymbol = displaySymbol; } + + public final String getCode() { + return code; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformService.java index 199310c9a..a14084bdd 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformService.java @@ -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 retrieveAllCharges(); + Collection retrieveAllChargesForCurrency(OrganisationCurrency currency); + ChargeData retrieveCharge(Long chargeId); ChargeData retrieveNewChargeDetails(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformServiceImpl.java index d1857c9f6..a218fd525 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/charge/service/ChargeReadPlatformServiceImpl.java @@ -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 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 { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformService.java index 955e35031..b6615ce33 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformService.java @@ -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 retrieveAllLoanProductsForCurrency(OrganisationCurrency currency); Collection retrieveAvailableLoanProductsForMix(); diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformServiceImpl.java index 993a381ee..8bbb23e6d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductReadPlatformServiceImpl.java @@ -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 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 retrieveAvailableLoanProductsForMix() { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformService.java index 6a91bee0e..9f1c93ef8 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformService.java @@ -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 retrieveAllForLookupByType(Boolean isOverdraftType); + Collection retrieveAllForCurrency(OrganisationCurrency currency); + SavingsProductData retrieveOne(Long productId); } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformServiceImpl.java index 4629eedf4..3a82a94fd 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/savings/service/SavingsProductReadPlatformServiceImpl.java @@ -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 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() }); + } } \ No newline at end of file