diff --git a/mifosng-db/patches/patch-01-role-unique-name.sql b/mifosng-db/patches/01-patch-role-unique-name.sql similarity index 100% rename from mifosng-db/patches/patch-01-role-unique-name.sql rename to mifosng-db/patches/01-patch-role-unique-name.sql diff --git a/mifosng-db/patches/02-patch_add_unique_const_on_loanprodname.sql b/mifosng-db/patches/02-patch_add_unique_const_on_loanprodname.sql new file mode 100644 index 000000000..59c57585a --- /dev/null +++ b/mifosng-db/patches/02-patch_add_unique_const_on_loanprodname.sql @@ -0,0 +1,6 @@ +UPDATE m_product_loan SET name = CONCAT(id,'-',name); + +-- concating unique value to make sure column does not contain any duplicate value + +ALTER TABLE m_product_loan +ADD CONSTRAINT unq_name UNIQUE (name); \ No newline at end of file diff --git a/mifosng-db/patches/patch_add_unique_const_on_loanprodname.sql b/mifosng-db/patches/patch_add_unique_const_on_loanprodname.sql deleted file mode 100644 index b75908218..000000000 --- a/mifosng-db/patches/patch_add_unique_const_on_loanprodname.sql +++ /dev/null @@ -1,3 +0,0 @@ -UPDATE m_product_loan SET name = CONCAT(id,'-',name); -- concating unique value to make sure column does not contain any duplicate value -ALTER TABLE m_product_loan -ADD CONSTRAINT unq_name UNIQUE (name); \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductWritePlatformServiceJpaRepositoryImpl.java index 55273f7dc..c798efc5f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanproduct/service/LoanProductWritePlatformServiceJpaRepositoryImpl.java @@ -38,7 +38,7 @@ import org.slf4j.LoggerFactory; public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanProductWritePlatformService { private final static Logger logger = LoggerFactory.getLogger(LoanProductWritePlatformServiceJpaRepositoryImpl.class); - private final PlatformSecurityContext context; + private final PlatformSecurityContext context; private final LoanProductCommandFromApiJsonDeserializer fromApiJsonDeserializer; private final LoanProductRepository loanProductRepository; private final AprCalculator aprCalculator; @@ -67,48 +67,41 @@ public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanPro @Override public CommandProcessingResult createLoanProduct(final JsonCommand command) { - try { + try { - this.context.authenticatedUser(); + this.context.authenticatedUser(); - this.fromApiJsonDeserializer.validateForCreate(command.json()); + this.fromApiJsonDeserializer.validateForCreate(command.json()); - // associating fund with loan product at creation is optional for - // now. - final Fund fund = findFundByIdIfProvided(command - .longValueOfParameterNamed("fundId")); + // associating fund with loan product at creation is optional for + // now. + final Fund fund = findFundByIdIfProvided(command.longValueOfParameterNamed("fundId")); - final Long transactionProcessingStrategyId = command - .longValueOfParameterNamed("transactionProcessingStrategyId"); - final LoanTransactionProcessingStrategy loanTransactionProcessingStrategy = findStrategyByIdIfProvided(transactionProcessingStrategyId); + final Long transactionProcessingStrategyId = command.longValueOfParameterNamed("transactionProcessingStrategyId"); + final LoanTransactionProcessingStrategy loanTransactionProcessingStrategy = findStrategyByIdIfProvided(transactionProcessingStrategyId); - final String currencyCode = command - .stringValueOfParameterNamed("currencyCode"); - final Set charges = this.assembleSetOfCharges(command, - currencyCode); + final String currencyCode = command.stringValueOfParameterNamed("currencyCode"); + final Set charges = this.assembleSetOfCharges(command, currencyCode); - final LoanProduct loanproduct = LoanProduct.assembleFromJson(fund, - loanTransactionProcessingStrategy, charges, command, - this.aprCalculator); + final LoanProduct loanproduct = LoanProduct.assembleFromJson(fund, loanTransactionProcessingStrategy, charges, command, + this.aprCalculator); - this.loanProductRepository.save(loanproduct); + this.loanProductRepository.save(loanproduct); - // save accounting mappings - accountMappingWritePlatformService - .createLoanProductToGLAccountMapping(loanproduct.getId(), - command); + // save accounting mappings + accountMappingWritePlatformService.createLoanProductToGLAccountMapping(loanproduct.getId(), command); - return new CommandProcessingResultBuilder() // - .withCommandId(command.commandId()) // - .withEntityId(loanproduct.getId()) // - .build(); + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(loanproduct.getId()) // + .build(); - } catch (DataIntegrityViolationException dve) { - handleDataIntegrityIssues(command, dve); - return CommandProcessingResult.empty(); - } + } catch (DataIntegrityViolationException dve) { + handleDataIntegrityIssues(command, dve); + return CommandProcessingResult.empty(); + } - } + } private LoanTransactionProcessingStrategy findStrategyByIdIfProvided(final Long transactionProcessingStrategyId) { LoanTransactionProcessingStrategy strategy = null; @@ -132,67 +125,58 @@ public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanPro @Override public CommandProcessingResult updateLoanProduct(final Long loanProductId, final JsonCommand command) { - try { + try { - this.context.authenticatedUser(); + this.context.authenticatedUser(); - this.fromApiJsonDeserializer.validateForUpdate(command.json()); + this.fromApiJsonDeserializer.validateForUpdate(command.json()); - final LoanProduct product = this.loanProductRepository - .findOne(loanProductId); - if (product == null) { - throw new LoanProductNotFoundException(loanProductId); - } + final LoanProduct product = this.loanProductRepository.findOne(loanProductId); + if (product == null) { throw new LoanProductNotFoundException(loanProductId); } - final Map changes = product.update(command, - this.aprCalculator); + final Map changes = product.update(command, this.aprCalculator); - // associating fund with loan product at creation is optional for - // now. - if (changes.containsKey("fundId")) { - final Long fundId = (Long) changes.get("fundId"); - final Fund fund = findFundByIdIfProvided(fundId); - product.update(fund); - } + // associating fund with loan product at creation is optional for + // now. + if (changes.containsKey("fundId")) { + final Long fundId = (Long) changes.get("fundId"); + final Fund fund = findFundByIdIfProvided(fundId); + product.update(fund); + } - if (changes.containsKey("transactionProcessingStrategyId")) { - final Long transactionProcessingStrategyId = (Long) changes - .get("transactionProcessingStrategyId"); - final LoanTransactionProcessingStrategy loanTransactionProcessingStrategy = findStrategyByIdIfProvided(transactionProcessingStrategyId); - product.update(loanTransactionProcessingStrategy); - } + if (changes.containsKey("transactionProcessingStrategyId")) { + final Long transactionProcessingStrategyId = (Long) changes.get("transactionProcessingStrategyId"); + final LoanTransactionProcessingStrategy loanTransactionProcessingStrategy = findStrategyByIdIfProvided(transactionProcessingStrategyId); + product.update(loanTransactionProcessingStrategy); + } - if (changes.containsKey("charges")) { - final Set charges = this.assembleSetOfCharges(command, - product.getCurrency().getCode()); - product.update(charges); - } + if (changes.containsKey("charges")) { + final Set charges = this.assembleSetOfCharges(command, product.getCurrency().getCode()); + product.update(charges); + } - // accounting related changes - boolean accountingTypeChanged = changes - .containsKey("accountingType"); - if (accountingTypeChanged) { - accountMappingWritePlatformService - .updateLoanProductToGLAccountMapping(product.getId(), - command, accountingTypeChanged); - } + // accounting related changes + boolean accountingTypeChanged = changes.containsKey("accountingType"); + if (accountingTypeChanged) { + accountMappingWritePlatformService.updateLoanProductToGLAccountMapping(product.getId(), command, accountingTypeChanged); + } - if (!changes.isEmpty()) { - this.loanProductRepository.saveAndFlush(product); - } + if (!changes.isEmpty()) { + this.loanProductRepository.saveAndFlush(product); + } - return new CommandProcessingResultBuilder() // - .withCommandId(command.commandId()) // - .withEntityId(loanProductId) // - .with(changes) // - .build(); + return new CommandProcessingResultBuilder() // + .withCommandId(command.commandId()) // + .withEntityId(loanProductId) // + .with(changes) // + .build(); - } catch (DataIntegrityViolationException dve) { - handleDataIntegrityIssues(command, dve); - return new CommandProcessingResult(Long.valueOf(-1)); - } + } catch (DataIntegrityViolationException dve) { + handleDataIntegrityIssues(command, dve); + return new CommandProcessingResult(Long.valueOf(-1)); + } - } + } private Set assembleSetOfCharges(final JsonCommand command, final String currencyCode) { @@ -222,6 +206,7 @@ public class LoanProductWritePlatformServiceJpaRepositoryImpl implements LoanPro return charges; } + /* * Guaranteed to throw an exception no matter what the data integrity issue * is.