MIFOSX-132: tidy up system defined implementation.

This commit is contained in:
Keith Woodlock
2012-11-29 20:11:38 +00:00
parent b001e3f999
commit 41536b1beb
7 changed files with 58 additions and 41 deletions
@@ -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<Map<String, Object>>() {}.getType();
Map<String, Object> requestMap = gsonConverter.fromJson(json, typeOfMap);
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
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);
// 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);
}
@@ -131,9 +131,8 @@ public class PortfolioCommandDeerializerServiceGoogleGson implements PortfolioCo
final Set<String> parametersPassedInRequest = new HashSet<String>();
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
@@ -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.");
}
}
@@ -18,21 +18,15 @@ public class CodeCommand {
private final transient Set<String> parametersPassedInRequest;
private final transient boolean makerCheckerApproval;
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.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;
}
@@ -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<Long> {
@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<CodeValue> 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);
}
}
}
@@ -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.");
}
}
@@ -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(); }