diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java index 72778f887..fe077fe02 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/client/service/ClientWritePlatformServiceJpaRepositoryImpl.java @@ -17,7 +17,6 @@ import org.mifosng.platform.client.domain.Note; import org.mifosng.platform.client.domain.NoteRepository; import org.mifosng.platform.exceptions.ClientIdentifierNotFoundException; import org.mifosng.platform.exceptions.ClientNotFoundException; -import org.mifosng.platform.exceptions.CodeValueNotFoundException; import org.mifosng.platform.exceptions.DocumentManagementException; import org.mifosng.platform.exceptions.DuplicateClientIdentifierException; import org.mifosng.platform.exceptions.NoteNotFoundException; @@ -27,6 +26,7 @@ import org.mifosng.platform.infrastructure.Base64EncodedImage; import org.mifosng.platform.infrastructure.FileUtils; import org.mifosplatform.infrastructure.codes.domain.CodeValue; import org.mifosplatform.infrastructure.codes.domain.CodeValueRepository; +import org.mifosplatform.infrastructure.codes.exception.CodeValueNotFoundException; import org.mifosplatform.infrastructure.office.domain.Office; import org.mifosplatform.infrastructure.office.domain.OfficeRepository; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/data/CodeData.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/data/CodeData.java index 471a10613..2d95c059a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/data/CodeData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/data/CodeData.java @@ -9,9 +9,12 @@ public class CodeData { private final Long id; @SuppressWarnings("unused") private final String name; + @SuppressWarnings("unused") + private final boolean systemDefined; - public CodeData(final Long id, final String name) { + public CodeData(final Long id, final String name, final boolean systemDefined) { this.id = id; this.name = name; + this.systemDefined = systemDefined; } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java index a95cf86a1..f5e151e82 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/Code.java @@ -22,7 +22,7 @@ public class Code extends AbstractPersistable { @SuppressWarnings("unused") @Column(name = "code_name", length = 100) - private String codeName; + private String name; @Column(name = "is_system_defined") private final boolean systemDefined; @@ -40,8 +40,8 @@ public class Code extends AbstractPersistable { this.systemDefined = false; } - private Code(final String codeName) { - this.codeName = codeName; + private Code(final String name) { + this.name = name; this.systemDefined = false; } @@ -56,7 +56,7 @@ public class Code extends AbstractPersistable { } if (command.isNameChanged()) { - this.codeName = StringUtils.defaultIfEmpty(command.getName(), null); + this.name = StringUtils.defaultIfEmpty(command.getName(), null); } } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/CodeRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/CodeRepository.java index 81f0ea4e9..6cfe307e3 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/CodeRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/domain/CodeRepository.java @@ -4,5 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface CodeRepository extends JpaRepository, JpaSpecificationExecutor { - // no added behaviour -} + + Code findOneByName(String name); +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeNotFoundException.java new file mode 100644 index 000000000..49dff1dad --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeNotFoundException.java @@ -0,0 +1,13 @@ +package org.mifosplatform.infrastructure.codes.exception; + +import org.mifosng.platform.exceptions.AbstractPlatformResourceNotFoundException; + +/** + * A {@link RuntimeException} thrown when a code is not found. + */ +public class CodeNotFoundException extends AbstractPlatformResourceNotFoundException { + + public CodeNotFoundException(final String name) { + super("error.msg.code.not.found", "Code with name `" + name + "` does not exist", name); + } +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeValueNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeValueNotFoundException.java old mode 100755 new mode 100644 similarity index 59% rename from mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeValueNotFoundException.java rename to mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeValueNotFoundException.java index 55f18771a..34d610da4 --- a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeValueNotFoundException.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/CodeValueNotFoundException.java @@ -1,11 +1,13 @@ -package org.mifosng.platform.exceptions; +package org.mifosplatform.infrastructure.codes.exception; + +import org.mifosng.platform.exceptions.AbstractPlatformResourceNotFoundException; /** * A {@link RuntimeException} thrown when client resources are not found. */ public class CodeValueNotFoundException extends AbstractPlatformResourceNotFoundException { - public CodeValueNotFoundException(Long id) { + public CodeValueNotFoundException(final Long id) { super("error.msg.codevalue.id.invalid", "Code value with identifier " + id + " does not exist", id); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java index d891443c9..11aa37fcc 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeReadPlatformServiceImpl.java @@ -29,7 +29,7 @@ public class CodeReadPlatformServiceImpl implements CodeReadPlatformService { private static final class CodeMapper implements RowMapper { public String schema() { - return " c.id as id, c.code_name as code_name from m_code c "; + return " c.id as id, c.code_name as code_name, c.is_system_defined as systemDefined from m_code c "; } @Override @@ -37,8 +37,9 @@ public class CodeReadPlatformServiceImpl implements CodeReadPlatformService { final Long id = rs.getLong("id"); final String code_name = rs.getString("code_name"); - - return new CodeData(id, code_name); + final boolean systemDefined = rs.getBoolean("systemDefined"); + + return new CodeData(id, code_name, systemDefined); } } diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformService.java index e9480eaa0..11c27282b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformService.java @@ -5,9 +5,9 @@ import org.mifosplatform.infrastructure.codes.command.CodeCommand; public interface CodeWritePlatformService { - Long createCode(final CodeCommand command); + Long createCode(CodeCommand command); - Long updateCode(final CodeCommand command); + Long updateCode(CodeCommand command); - EntityIdentifier deleteCode(final CodeCommand command); + EntityIdentifier deleteCode(CodeCommand command); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java index 15f4e48d3..b672b3427 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/service/CodeWritePlatformServiceJpaRepositoryImpl.java @@ -2,11 +2,11 @@ package org.mifosplatform.infrastructure.codes.service; import org.mifosng.platform.api.data.EntityIdentifier; import org.mifosng.platform.client.service.RollbackTransactionAsCommandIsNotApprovedByCheckerException; -import org.mifosng.platform.exceptions.CodeValueNotFoundException; import org.mifosng.platform.exceptions.PlatformDataIntegrityException; import org.mifosplatform.infrastructure.codes.command.CodeCommand; import org.mifosplatform.infrastructure.codes.domain.Code; import org.mifosplatform.infrastructure.codes.domain.CodeRepository; +import org.mifosplatform.infrastructure.codes.exception.CodeNotFoundException; import org.mifosplatform.infrastructure.codes.exception.SystemDefinedCodeCannotBeChangedException; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.infrastructure.user.domain.Permission; @@ -64,10 +64,7 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf context.authenticatedUser(); command.validateForUpdate(); - final Long codeId = command.getId(); - final Code code = this.codeRepository.findOne(codeId); - if (code == null) { throw new CodeValueNotFoundException(codeId); } - + final Code code = retrieveCodeBy(command.getName()); code.update(command); this.codeRepository.save(code); @@ -102,23 +99,22 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf context.authenticatedUser(); - final Long codeId = command.getId(); - Code code = retrieveCodeBy(codeId); + final Code code = retrieveCodeBy(command.getName()); if (code.isSystemDefined()) { throw new SystemDefinedCodeCannotBeChangedException(); } - this.codeRepository.delete(codeId); + this.codeRepository.delete(code); final Permission thisTask = this.permissionRepository.findOneByCode("DELETE_CODE"); if (thisTask.hasMakerCheckerEnabled() && !command.isApprovedByChecker()) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(); } - return new EntityIdentifier(codeId); + return new EntityIdentifier(code.getId()); } - private Code retrieveCodeBy(final Long codeId) { - final Code code = this.codeRepository.findOne(codeId); - if (code == null) { throw new CodeValueNotFoundException(codeId); } + private Code retrieveCodeBy(final String name) { + final Code code = this.codeRepository.findOneByName(name); + if (code == null) { throw new CodeNotFoundException(name); } return code; } } \ No newline at end of file