MIFOSX-132: tidy up system defined implementation.
This commit is contained in:
+4
-6
@@ -1543,16 +1543,14 @@ public class PortfolioApiDataConversionServiceImpl implements PortfolioApiDataCo
|
|||||||
public CodeCommand convertApiRequestJsonToCodeCommand(final Long resourceIdentifier, final String json) {
|
public CodeCommand convertApiRequestJsonToCodeCommand(final Long resourceIdentifier, final String json) {
|
||||||
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
|
if (StringUtils.isBlank(json)) { throw new InvalidJsonException(); }
|
||||||
|
|
||||||
Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
|
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
|
||||||
Map<String, Object> requestMap = gsonConverter.fromJson(json, typeOfMap);
|
final Map<String, Object> requestMap = gsonConverter.fromJson(json, typeOfMap);
|
||||||
|
|
||||||
Set<String> supportedParams = new HashSet<String>(Arrays.asList("name","is_system_defined"));
|
final Set<String> supportedParams = new HashSet<String>(Arrays.asList("name"));
|
||||||
|
|
||||||
checkForUnsupportedParameters(requestMap, supportedParams);
|
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);
|
return this.portfolioCommandDeserializerService.deserializeCodeCommand(resourceIdentifier, json, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -131,9 +131,8 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo
|
|||||||
final Set<String> parametersPassedInRequest = new HashSet<String>();
|
final Set<String> parametersPassedInRequest = new HashSet<String>();
|
||||||
|
|
||||||
final String name = helper.extractStringNamed("name", element, parametersPassedInRequest);
|
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
|
@Override
|
||||||
|
|||||||
@@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-8
@@ -18,21 +18,15 @@ public class CodeCommand {
|
|||||||
private final transient Set<String> parametersPassedInRequest;
|
private final transient Set<String> parametersPassedInRequest;
|
||||||
private final transient boolean makerCheckerApproval;
|
private final transient boolean makerCheckerApproval;
|
||||||
private final transient Long id;
|
private final transient Long id;
|
||||||
private final transient boolean isSystemDefined;
|
|
||||||
|
|
||||||
public CodeCommand(final Set<String> modifiedParameters, final boolean makerCheckerApproval, final Long id, final String name,final boolean isSystemDefined) {
|
public CodeCommand(final Set<String> modifiedParameters, final boolean makerCheckerApproval, final Long id, final String name) {
|
||||||
this.parametersPassedInRequest = modifiedParameters;
|
this.parametersPassedInRequest = modifiedParameters;
|
||||||
this.makerCheckerApproval = makerCheckerApproval;
|
this.makerCheckerApproval = makerCheckerApproval;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.isSystemDefined = isSystemDefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getisSystemDefined() {
|
public String getName() {
|
||||||
return isSystemDefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+25
-10
@@ -1,13 +1,19 @@
|
|||||||
package org.mifosplatform.infrastructure.codes.domain;
|
package org.mifosplatform.infrastructure.codes.domain;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
import javax.persistence.UniqueConstraint;
|
import javax.persistence.UniqueConstraint;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
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.command.CodeCommand;
|
||||||
|
import org.mifosplatform.infrastructure.codes.exception.SystemDefinedCodeCannotBeChangedException;
|
||||||
import org.springframework.data.jpa.domain.AbstractPersistable;
|
import org.springframework.data.jpa.domain.AbstractPersistable;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@@ -18,30 +24,39 @@ public class Code extends AbstractPersistable<Long> {
|
|||||||
@Column(name = "code_name", length = 100)
|
@Column(name = "code_name", length = 100)
|
||||||
private String codeName;
|
private String codeName;
|
||||||
|
|
||||||
@SuppressWarnings("unused")
|
@Column(name = "is_system_defined")
|
||||||
@Column(name = "is_system_defined")
|
private final boolean systemDefined;
|
||||||
private boolean isSystemDefined;
|
|
||||||
|
|
||||||
public boolean getisSystemDefined() {
|
@SuppressWarnings("unused")
|
||||||
return isSystemDefined;
|
@LazyCollection(LazyCollectionOption.FALSE)
|
||||||
}
|
@OneToMany(cascade = CascadeType.ALL, mappedBy = "code", orphanRemoval = true)
|
||||||
|
private Set<CodeValue> values;
|
||||||
|
|
||||||
public static Code createNew(final String codeName) {
|
public static Code createNew(final String codeName) {
|
||||||
return new Code(codeName);
|
return new Code(codeName);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Code() {
|
protected Code() {
|
||||||
//
|
this.systemDefined = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Code(final String codeName) {
|
private Code(final String codeName) {
|
||||||
this.codeName = codeName;
|
this.codeName = codeName;
|
||||||
|
this.systemDefined = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isSystemDefined() {
|
||||||
|
return this.systemDefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(final CodeCommand command) {
|
public void update(final CodeCommand command) {
|
||||||
|
|
||||||
|
if (systemDefined) {
|
||||||
|
throw new SystemDefinedCodeCannotBeChangedException();
|
||||||
|
}
|
||||||
|
|
||||||
if (command.isNameChanged()) {
|
if (command.isNameChanged()) {
|
||||||
this.codeName = StringUtils.defaultIfEmpty(command.getName(), null);
|
this.codeName = StringUtils.defaultIfEmpty(command.getName(), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+13
@@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
-2
@@ -7,6 +7,7 @@ import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
|
|||||||
import org.mifosplatform.infrastructure.codes.command.CodeCommand;
|
import org.mifosplatform.infrastructure.codes.command.CodeCommand;
|
||||||
import org.mifosplatform.infrastructure.codes.domain.Code;
|
import org.mifosplatform.infrastructure.codes.domain.Code;
|
||||||
import org.mifosplatform.infrastructure.codes.domain.CodeRepository;
|
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.security.service.PlatformSecurityContext;
|
||||||
import org.mifosplatform.infrastructure.user.domain.Permission;
|
import org.mifosplatform.infrastructure.user.domain.Permission;
|
||||||
import org.mifosplatform.infrastructure.user.domain.PermissionRepository;
|
import org.mifosplatform.infrastructure.user.domain.PermissionRepository;
|
||||||
@@ -27,7 +28,8 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf
|
|||||||
private final PermissionRepository permissionRepository;
|
private final PermissionRepository permissionRepository;
|
||||||
|
|
||||||
@Autowired
|
@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.context = context;
|
||||||
this.codeRepository = codeRepository;
|
this.codeRepository = codeRepository;
|
||||||
this.permissionRepository = permissionRepository;
|
this.permissionRepository = permissionRepository;
|
||||||
@@ -65,6 +67,7 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf
|
|||||||
final Long codeId = command.getId();
|
final Long codeId = command.getId();
|
||||||
final Code code = this.codeRepository.findOne(codeId);
|
final Code code = this.codeRepository.findOne(codeId);
|
||||||
if (code == null) { throw new CodeValueNotFoundException(codeId); }
|
if (code == null) { throw new CodeValueNotFoundException(codeId); }
|
||||||
|
|
||||||
code.update(command);
|
code.update(command);
|
||||||
|
|
||||||
this.codeRepository.save(code);
|
this.codeRepository.save(code);
|
||||||
@@ -100,7 +103,11 @@ public class CodeWritePlatformServiceJpaRepositoryImpl implements CodeWritePlatf
|
|||||||
context.authenticatedUser();
|
context.authenticatedUser();
|
||||||
|
|
||||||
final Long codeId = command.getId();
|
final Long codeId = command.getId();
|
||||||
retrieveCodeBy(codeId);
|
Code code = retrieveCodeBy(codeId);
|
||||||
|
if (code.isSystemDefined()) {
|
||||||
|
throw new SystemDefinedCodeCannotBeChangedException();
|
||||||
|
}
|
||||||
|
|
||||||
this.codeRepository.delete(codeId);
|
this.codeRepository.delete(codeId);
|
||||||
|
|
||||||
final Permission thisTask = this.permissionRepository.findOneByCode("DELETE_CODE");
|
final Permission thisTask = this.permissionRepository.findOneByCode("DELETE_CODE");
|
||||||
|
|||||||
Reference in New Issue
Block a user