Merge pull request #476 from goutham-M/mifosx-470
Avoiding Deletion of active Charge which was already assigned to a Loanproduct.
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package org.mifosplatform.portfolio.charge.exception;
|
||||||
|
|
||||||
|
import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException;
|
||||||
|
|
||||||
|
|
||||||
|
public class ChargeCannotBeDeletedException extends AbstractPlatformDomainRuleException {
|
||||||
|
|
||||||
|
public ChargeCannotBeDeletedException(String globalisationMessageCode, String defaultUserMessage, Object... defaultUserMessageArgs) {
|
||||||
|
|
||||||
|
super(globalisationMessageCode, defaultUserMessage, defaultUserMessageArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+30
-2
@@ -5,21 +5,29 @@
|
|||||||
*/
|
*/
|
||||||
package org.mifosplatform.portfolio.charge.service;
|
package org.mifosplatform.portfolio.charge.service;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
import org.mifosplatform.infrastructure.core.api.JsonCommand;
|
||||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
import org.mifosplatform.infrastructure.core.data.CommandProcessingResult;
|
||||||
import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder;
|
import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder;
|
||||||
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
|
import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException;
|
||||||
|
import org.mifosplatform.infrastructure.core.service.RoutingDataSource;
|
||||||
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
|
||||||
import org.mifosplatform.portfolio.charge.domain.Charge;
|
import org.mifosplatform.portfolio.charge.domain.Charge;
|
||||||
import org.mifosplatform.portfolio.charge.domain.ChargeRepository;
|
import org.mifosplatform.portfolio.charge.domain.ChargeRepository;
|
||||||
|
import org.mifosplatform.portfolio.charge.exception.ChargeCannotBeDeletedException;
|
||||||
import org.mifosplatform.portfolio.charge.exception.ChargeNotFoundException;
|
import org.mifosplatform.portfolio.charge.exception.ChargeNotFoundException;
|
||||||
import org.mifosplatform.portfolio.charge.serialization.ChargeDefinitionCommandFromApiJsonDeserializer;
|
import org.mifosplatform.portfolio.charge.serialization.ChargeDefinitionCommandFromApiJsonDeserializer;
|
||||||
|
import org.mifosplatform.portfolio.loanproduct.domain.LoanProduct;
|
||||||
|
import org.mifosplatform.portfolio.loanproduct.domain.LoanProductRepository;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
@@ -30,14 +38,20 @@ public class ChargeWritePlatformServiceJpaRepositoryImpl implements ChargeWriteP
|
|||||||
|
|
||||||
private final PlatformSecurityContext context;
|
private final PlatformSecurityContext context;
|
||||||
private final ChargeDefinitionCommandFromApiJsonDeserializer fromApiJsonDeserializer;
|
private final ChargeDefinitionCommandFromApiJsonDeserializer fromApiJsonDeserializer;
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
private final DataSource dataSource;
|
||||||
private final ChargeRepository chargeRepository;
|
private final ChargeRepository chargeRepository;
|
||||||
|
private final LoanProductRepository loanProductRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
public ChargeWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
|
public ChargeWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context,
|
||||||
final ChargeDefinitionCommandFromApiJsonDeserializer fromApiJsonDeserializer, final ChargeRepository chargeRepository) {
|
final ChargeDefinitionCommandFromApiJsonDeserializer fromApiJsonDeserializer, final ChargeRepository chargeRepository,
|
||||||
|
final LoanProductRepository loanProductRepository, final RoutingDataSource dataSource) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.fromApiJsonDeserializer = fromApiJsonDeserializer;
|
this.fromApiJsonDeserializer = fromApiJsonDeserializer;
|
||||||
|
this.dataSource = dataSource;
|
||||||
|
this.jdbcTemplate = new JdbcTemplate(this.dataSource);
|
||||||
this.chargeRepository = chargeRepository;
|
this.chargeRepository = chargeRepository;
|
||||||
|
this.loanProductRepository = loanProductRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@@ -92,6 +106,13 @@ public class ChargeWritePlatformServiceJpaRepositoryImpl implements ChargeWriteP
|
|||||||
final Charge chargeForDelete = this.chargeRepository.findOne(chargeId);
|
final Charge chargeForDelete = this.chargeRepository.findOne(chargeId);
|
||||||
if (chargeForDelete == null || chargeForDelete.isDeleted()) { throw new ChargeNotFoundException(chargeId); }
|
if (chargeForDelete == null || chargeForDelete.isDeleted()) { throw new ChargeNotFoundException(chargeId); }
|
||||||
|
|
||||||
|
Collection<LoanProduct> loanProducts = this.loanProductRepository.retrieveLoanProductsByChargeId(chargeId);
|
||||||
|
Boolean isChargeExistWithLoans = isAnyLoansAssociateWithThisCharge(chargeId);
|
||||||
|
|
||||||
|
if (!loanProducts.isEmpty() || isChargeExistWithLoans) { 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"); }
|
||||||
|
|
||||||
chargeForDelete.delete();
|
chargeForDelete.delete();
|
||||||
|
|
||||||
chargeRepository.save(chargeForDelete);
|
chargeRepository.save(chargeForDelete);
|
||||||
@@ -116,4 +137,11 @@ public class ChargeWritePlatformServiceJpaRepositoryImpl implements ChargeWriteP
|
|||||||
throw new PlatformDataIntegrityException("error.msg.charge.unknown.data.integrity.issue",
|
throw new PlatformDataIntegrityException("error.msg.charge.unknown.data.integrity.issue",
|
||||||
"Unknown data integrity issue with resource: " + realCause.getMessage());
|
"Unknown data integrity issue with resource: " + realCause.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isAnyLoansAssociateWithThisCharge(final Long chargeId) {
|
||||||
|
|
||||||
|
final String sql = "select if((exists (select 1 from m_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-1
@@ -5,9 +5,15 @@
|
|||||||
*/
|
*/
|
||||||
package org.mifosplatform.portfolio.loanproduct.domain;
|
package org.mifosplatform.portfolio.loanproduct.domain;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
public interface LoanProductRepository extends JpaRepository<LoanProduct, Long>, JpaSpecificationExecutor<LoanProduct> {
|
public interface LoanProductRepository extends JpaRepository<LoanProduct, Long>, JpaSpecificationExecutor<LoanProduct> {
|
||||||
// no behaviour added
|
|
||||||
|
@Query("select loanProduct from LoanProduct loanProduct, IN(loanProduct.charges) charge where charge.id = :chargeId")
|
||||||
|
List<LoanProduct> retrieveLoanProductsByChargeId(@Param("chargeId") Long chargeId);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user