diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java index 02d14b03a..3338d1392 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioApiDataConversionServiceImpl.java @@ -1543,16 +1543,14 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo public CodeCommand convertApiRequestJsonToCodeCommand(final Long resourceIdentifier, final String json) { if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); } - Type typeOfMap = new TypeToken>() {}.getType(); - Map requestMap = gsonConverter.fromJson(json, typeOfMap); + final Type typeOfMap = new TypeToken>() {}.getType(); + final Map requestMap = gsonConverter.fromJson(json, typeOfMap); - Set supportedParams = new HashSet(Arrays.asList("name","is_system_defined")); + final Set supportedParams = new HashSet(Arrays.asList("name")); checkForUnsupportedParameters(requestMap, supportedParams); - - // no difference between api request json format and internal command - // json format. + // no difference between api request json format and internal command json format. return this.portfolioCommandDeserializerService.deserializeCodeCommand(resourceIdentifier, json, false); } diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java index 553936682..efadf536b 100644 --- a/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java +++ b/mifosng-provider/src/main/java/org/mifosng/platform/api/infrastructure/PortfolioCommandDeerializerServiceGoogleGson.java @@ -131,9 +131,8 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo final Set parametersPassedInRequest = new HashSet(); final String name = helper.extractStringNamed("name", element, parametersPassedInRequest); - final boolean isSystemDefined = helper.extractBooleanNamed("isSystemDefined", element, parametersPassedInRequest); - return new CodeCommand(parametersPassedInRequest, makerCheckerApproval, codeId, name,isSystemDefined); + return new CodeCommand(parametersPassedInRequest, makerCheckerApproval, codeId, name); } @Override diff --git a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeExceptionRule.java b/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeExceptionRule.java deleted file mode 100644 index 37f7d9a81..000000000 --- a/mifosng-provider/src/main/java/org/mifosng/platform/exceptions/CodeExceptionRule.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.mifosng.platform.exceptions; - -public class CodeExceptionRule extends AbstractPlatformDomainRuleException -{ - public CodeExceptionRule() - { - super("error.msg.code.systemdefined","This code is system defined and cannot be modified or deleted."); - } -} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/command/CodeCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/command/CodeCommand.java index 7231f3d73..1c58aa71c 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/command/CodeCommand.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/command/CodeCommand.java @@ -18,21 +18,15 @@ public class CodeCommand { private final transient Set parametersPassedInRequest; private final transient boolean makerCheckerApproval; private final transient Long id; - private final transient boolean isSystemDefined; - public CodeCommand(final Set modifiedParameters, final boolean makerCheckerApproval, final Long id, final String name,final boolean isSystemDefined) { + public CodeCommand(final Set modifiedParameters, final boolean makerCheckerApproval, final Long id, final String name) { this.parametersPassedInRequest = modifiedParameters; this.makerCheckerApproval = makerCheckerApproval; this.id = id; this.name = name; - this.isSystemDefined = isSystemDefined; } - public boolean getisSystemDefined() { - return isSystemDefined; - } - - public String getName() { + public String getName() { return this.name; } 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 9c4d5c69b..a95cf86a1 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 @@ -1,13 +1,19 @@ package org.mifosplatform.infrastructure.codes.domain; +import java.util.Set; + +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.UniqueConstraint; import org.apache.commons.lang.StringUtils; +import org.hibernate.annotations.LazyCollection; +import org.hibernate.annotations.LazyCollectionOption; import org.mifosplatform.infrastructure.codes.command.CodeCommand; - +import org.mifosplatform.infrastructure.codes.exception.SystemDefinedCodeCannotBeChangedException; import org.springframework.data.jpa.domain.AbstractPersistable; @Entity @@ -18,30 +24,39 @@ public class Code extends AbstractPersistable { @Column(name = "code_name", length = 100) private String codeName; - @SuppressWarnings("unused") - @Column(name = "is_system_defined") - private boolean isSystemDefined; + @Column(name = "is_system_defined") + private final boolean systemDefined; + + @SuppressWarnings("unused") + @LazyCollection(LazyCollectionOption.FALSE) + @OneToMany(cascade = CascadeType.ALL, mappedBy = "code", orphanRemoval = true) + private Set values; - public boolean getisSystemDefined() { - return isSystemDefined; - } - - public static Code createNew(final String codeName) { + public static Code createNew(final String codeName) { return new Code(codeName); } protected Code() { - // + this.systemDefined = false; } private Code(final String codeName) { this.codeName = codeName; + this.systemDefined = false; + } + + public boolean isSystemDefined() { + return this.systemDefined; } public void update(final CodeCommand command) { + + if (systemDefined) { + throw new SystemDefinedCodeCannotBeChangedException(); + } + if (command.isNameChanged()) { this.codeName = StringUtils.defaultIfEmpty(command.getName(), null); } } - } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/SystemDefinedCodeCannotBeChangedException.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/SystemDefinedCodeCannotBeChangedException.java new file mode 100644 index 000000000..b95cd351e --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/codes/exception/SystemDefinedCodeCannotBeChangedException.java @@ -0,0 +1,13 @@ +package org.mifosplatform.infrastructure.codes.exception; + +import org.mifosng.platform.exceptions.AbstractPlatformDomainRuleException; + +/** + * A {@link AbstractPlatformDomainRuleException} thrown when someone attempts to update or delete a system defined code. + */ +public class SystemDefinedCodeCannotBeChangedException extends AbstractPlatformDomainRuleException { + + public SystemDefinedCodeCannotBeChangedException() { + super("error.msg.code.systemdefined", "This code is system defined and cannot be modified or deleted."); + } +} 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 683aee568..15f4e48d3 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 @@ -7,6 +7,7 @@ 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.SystemDefinedCodeCannotBeChangedException; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; import org.mifosplatform.infrastructure.user.domain.Permission; import org.mifosplatform.infrastructure.user.domain.PermissionRepository; @@ -27,7 +28,8 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf private final PermissionRepository permissionRepository; @Autowired - public CodeWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, final CodeRepository codeRepository, final PermissionRepository permissionRepository) { + public CodeWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, final CodeRepository codeRepository, + final PermissionRepository permissionRepository) { this.context = context; this.codeRepository = codeRepository; this.permissionRepository = permissionRepository; @@ -44,7 +46,7 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf final Code code = Code.createNew(command.getName()); this.codeRepository.save(code); - + final Permission thisTask = this.permissionRepository.findOneByCode("CREATE_CODE"); if (thisTask.hasMakerCheckerEnabled() && !command.isApprovedByChecker()) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(); } @@ -65,13 +67,14 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf final Long codeId = command.getId(); final Code code = this.codeRepository.findOne(codeId); if (code == null) { throw new CodeValueNotFoundException(codeId); } + code.update(command); this.codeRepository.save(code); final Permission thisTask = this.permissionRepository.findOneByCode("UPDATE_CODE"); if (thisTask.hasMakerCheckerEnabled() && !command.isApprovedByChecker()) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(); } - + return code.getId(); } catch (DataIntegrityViolationException dve) { handleCodeDataIntegrityIssues(command, dve); @@ -100,9 +103,13 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf context.authenticatedUser(); final Long codeId = command.getId(); - retrieveCodeBy(codeId); - this.codeRepository.delete(codeId); + Code code = retrieveCodeBy(codeId); + if (code.isSystemDefined()) { + throw new SystemDefinedCodeCannotBeChangedException(); + } + this.codeRepository.delete(codeId); + final Permission thisTask = this.permissionRepository.findOneByCode("DELETE_CODE"); if (thisTask.hasMakerCheckerEnabled() && !command.isApprovedByChecker()) { throw new RollbackTransactionAsCommandIsNotApprovedByCheckerException(); }