AccountValidation (in progress)
This commit is contained in:
parent
e27d50f481
commit
44ee51994a
6 changed files with 281 additions and 9 deletions
|
|
@ -0,0 +1,162 @@
|
|||
package ru.spcex.clearing.account.config.validation;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.clearing.classes.statics.data.account.Account;
|
||||
import ru.clearing.classes.statics.data.company.Company;
|
||||
import ru.clearing.platform.dictionary.AccountTypeDictionary;
|
||||
import ru.spcex.clearing.account.errors.AccountError;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.AccountNewRequest;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.account.AccountUpdateRequest;
|
||||
import ru.spcex.clearing.validation.common.rules.DictionaryPresentRule;
|
||||
import ru.spcex.clearing.validation.common.rules.EnumPresentRule;
|
||||
import ru.spcex.clearing.validation.common.rules.FieldRequiredRule;
|
||||
import ru.spcex.clearing.validation.common.rules.IdPresentRule;
|
||||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
import ru.spcex.platform.enumeration.AccountStatus;
|
||||
import ru.spcex.platform.enumeration.AccountType;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.enumeration.WorkflowStatus;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
||||
import ru.spcex.platform.imdg.validation.ImdgValidationContext;
|
||||
import ru.spcex.platform.utils.enumeration.IErrorEnumId;
|
||||
import ru.spcex.platform.utils.validation.IValidator;
|
||||
import ru.spcex.platform.utils.validation.ValidatorImpl;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Configuration
|
||||
public class AccountValidationConfig {
|
||||
|
||||
@Bean("bankAccountNewRequestValidator")
|
||||
public Function<AccountNewRequest, IValidator> accountNewRequestValidator(
|
||||
Map<String, Imdg<? extends SpcexObjectBase>> imdgForValidation
|
||||
) {
|
||||
return accountNewRequest -> {
|
||||
ImdgValidationContext<AccountNewRequest> context = new ImdgValidationContext<>();
|
||||
context.setValidatedObject(accountNewRequest);
|
||||
Consumer<String> addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s));
|
||||
addImdg.accept(IMDGDistributedNames.Map_Company);
|
||||
addImdg.accept(IMDGDistributedNames.Map_Account);
|
||||
addImdg.accept(IMDGDistributedNames.Map_AccountTypeDictionary);
|
||||
return new ValidatorImpl<>(context,
|
||||
IdPresentRule.instance("companyId",
|
||||
AccountNewRequest::getCompanyId,
|
||||
IMDGDistributedNames.Map_Company,
|
||||
Company.class,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
AccountError.CompanyNotFound,
|
||||
company -> WorkflowStatus.Active.getKey().equals(company.getWorkflowStatus()) ? null : AccountError.CompanyNotActive),
|
||||
FieldRequiredRule.instance("account",
|
||||
AccountNewRequest::getAccount,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
accountValue -> {
|
||||
Imdg<Account> accountImdg = context.obtainMap(
|
||||
IMDGDistributedNames.Map_Account, Account.class
|
||||
);
|
||||
ImdgPredicateBuilder pb = accountImdg.predicateBuilder();
|
||||
ImdgPredicate accountValuePredicate = pb.equals("account", accountValue);
|
||||
ImdgPredicate accountStatusPredicate = pb.equals("status", AccountStatus.ACTIVE.getKey());
|
||||
ImdgPredicate finalPredicate = pb.and(accountValuePredicate, accountStatusPredicate);
|
||||
Collection<Account> accounts = accountImdg.getCollectionObjectsByPredicate(finalPredicate);
|
||||
if (accounts.isEmpty()) return null;
|
||||
return AccountError.AccountAlreadyExist;
|
||||
}),
|
||||
FieldRequiredRule.instance("status",
|
||||
AccountNewRequest::getStatus,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
false,
|
||||
statusValue -> {
|
||||
if (statusValue == null || AccountStatus.ACTIVE.getKey().equalsIgnoreCase(statusValue))
|
||||
return null;
|
||||
return AccountError.WrongFieldValue;
|
||||
}),
|
||||
EnumPresentRule.instance("accountType",
|
||||
AccountNewRequest::getAccountType,
|
||||
AccountType.values(),
|
||||
AccountError.WrongFieldValue,
|
||||
AccountError.RequiredFieldEmpty),
|
||||
DictionaryPresentRule.instance("accountType",
|
||||
AccountNewRequest::getAccountType,
|
||||
IMDGDistributedNames.Map_AccountTypeDictionary,
|
||||
AccountTypeDictionary.class,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
AccountError.WrongFieldValue)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean("bankAccountUpdateRequestValidator")
|
||||
public Function<AccountUpdateRequest, IValidator> accountUpdateRequestValidator(
|
||||
Map<String, Imdg<? extends SpcexObjectBase>> imdgForValidation
|
||||
) {
|
||||
return accountUpdateRequest -> {
|
||||
ImdgValidationContext<AccountUpdateRequest> context = new ImdgValidationContext<>();
|
||||
context.setValidatedObject(accountUpdateRequest);
|
||||
Consumer<String> addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s));
|
||||
addImdg.accept(IMDGDistributedNames.Map_Company);
|
||||
addImdg.accept(IMDGDistributedNames.Map_Account);
|
||||
addImdg.accept(IMDGDistributedNames.Map_AccountTypeDictionary);
|
||||
return new ValidatorImpl<>(context,
|
||||
IdPresentRule.instance("id",
|
||||
AccountUpdateRequest::getId,
|
||||
IMDGDistributedNames.Map_Account,
|
||||
Account.class,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
AccountError.AccountNotFound,
|
||||
account -> {
|
||||
String statusFromRequest = accountUpdateRequest.getStatus();
|
||||
if (statusFromRequest != null && !statusFromRequest.equalsIgnoreCase(account.getStatus()))
|
||||
return AccountError.WrongFieldValue;
|
||||
return null;
|
||||
}),
|
||||
IdPresentRule.instance("companyId",
|
||||
AccountUpdateRequest::getCompanyId,
|
||||
IMDGDistributedNames.Map_Company,
|
||||
Company.class,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
AccountError.CompanyNotFound,
|
||||
false,
|
||||
company -> {
|
||||
IErrorEnumId error = WorkflowStatus.Active.getKey().equals(company.getWorkflowStatus()) ? null : AccountError.CompanyNotActive;
|
||||
error = Objects.equals(company.getId(), accountUpdateRequest.getCompanyId()) ? error : AccountError.WrongFieldValue;
|
||||
return error;
|
||||
}),
|
||||
EnumPresentRule.instance("status",
|
||||
AccountUpdateRequest::getStatus,
|
||||
AccountStatus.values(),
|
||||
false,
|
||||
AccountError.WrongFieldValue,
|
||||
AccountError.RequiredFieldEmpty),
|
||||
FieldRequiredRule.instance("status",
|
||||
AccountUpdateRequest::getStatus,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
false,
|
||||
statusValue -> {
|
||||
if (statusValue != null && !Status.Active.getKey().equalsIgnoreCase(statusValue))
|
||||
return AccountError.WrongFieldValue;
|
||||
return null;
|
||||
}),
|
||||
EnumPresentRule.instance("accountType",
|
||||
AccountUpdateRequest::getAccountType,
|
||||
AccountType.values(),
|
||||
AccountError.WrongFieldValue,
|
||||
AccountError.RequiredFieldEmpty),
|
||||
DictionaryPresentRule.instance("accountType",
|
||||
AccountUpdateRequest::getAccountType,
|
||||
IMDGDistributedNames.Map_AccountTypeDictionary,
|
||||
AccountTypeDictionary.class,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
AccountError.WrongFieldValue)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -14,8 +14,8 @@ import ru.spcex.clearing.validation.common.rules.EnumPresentRule;
|
|||
import ru.spcex.clearing.validation.common.rules.FieldRequiredRule;
|
||||
import ru.spcex.clearing.validation.common.rules.IdPresentRule;
|
||||
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||
import ru.spcex.platform.enumeration.AccountStatus;
|
||||
import ru.spcex.platform.enumeration.CurrencyCode;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.enumeration.WorkflowStatus;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||
|
|
@ -61,7 +61,7 @@ public class BankAccountValidationConfig {
|
|||
);
|
||||
ImdgPredicateBuilder pb = accountImdg.predicateBuilder();
|
||||
ImdgPredicate accountValuePredicate = pb.equals("account", accountValue);
|
||||
ImdgPredicate accountStatusPredicate = pb.equals("accountStatus", Status.Active.getKey());
|
||||
ImdgPredicate accountStatusPredicate = pb.equals("status", AccountStatus.ACTIVE.getKey());
|
||||
ImdgPredicate finalPredicate = pb.and(accountValuePredicate, accountStatusPredicate);
|
||||
Collection<Account> accounts = accountImdg.getCollectionObjectsByPredicate(finalPredicate);
|
||||
if (accounts.isEmpty()) return null;
|
||||
|
|
@ -111,15 +111,15 @@ public class BankAccountValidationConfig {
|
|||
IMDGDistributedNames.Map_BankAccount,
|
||||
BankAccount.class,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
AccountError.BankAccountNotFound,
|
||||
AccountError.AccountNotFound,
|
||||
bankAccount -> {
|
||||
Long accountId = bankAccount.getAccountId();
|
||||
Imdg<Account> accountImdg = context.obtainMap(
|
||||
IMDGDistributedNames.Map_Account, Account.class
|
||||
);
|
||||
Account account = accountImdg.getSingleObjectByID(accountId);
|
||||
if (account == null) return AccountError.BankAccountNotFound;
|
||||
if (!WorkflowStatus.Active.getKey().equalsIgnoreCase(account.getAccountStatus()))
|
||||
if (account == null) return AccountError.AccountNotFound;
|
||||
if (!AccountStatus.ACTIVE.getKey().equalsIgnoreCase(account.getStatus()))
|
||||
return AccountError.AccountNotActive;
|
||||
return null;
|
||||
}),
|
||||
|
|
@ -158,15 +158,15 @@ public class BankAccountValidationConfig {
|
|||
IMDGDistributedNames.Map_BankAccount,
|
||||
BankAccount.class,
|
||||
AccountError.RequiredFieldEmpty,
|
||||
AccountError.BankAccountNotFound,
|
||||
AccountError.AccountNotFound,
|
||||
bankAccount -> {
|
||||
Long accountId = bankAccount.getAccountId();
|
||||
Imdg<Account> accountImdg = context.obtainMap(
|
||||
IMDGDistributedNames.Map_Account, Account.class
|
||||
);
|
||||
Account account = accountImdg.getSingleObjectByID(accountId);
|
||||
if (account == null) return AccountError.BankAccountNotFound;
|
||||
if (!WorkflowStatus.Active.getKey().equalsIgnoreCase(account.getAccountStatus()))
|
||||
if (account == null) return AccountError.AccountNotFound;
|
||||
if (!AccountStatus.ACTIVE.getKey().equalsIgnoreCase(account.getStatus()))
|
||||
return AccountError.AccountNotActive;
|
||||
return null;
|
||||
})
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ public class ValidationConfig {
|
|||
|
||||
addImdg.accept(IMDGDistributedNames.Map_Company, Company.class);
|
||||
addImdg.accept(IMDGDistributedNames.Map_Account, Account.class);
|
||||
addImdg.accept(IMDGDistributedNames.Map_AccountTypeDictionary, Account.class);
|
||||
addImdg.accept(IMDGDistributedNames.Map_BankAccount, BankAccount.class);
|
||||
addImdg.accept(IMDGDistributedNames.Map_CurrencyCodeDictionary, CurrencyCodeDictionary.class);
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ public enum AccountError implements IErrorEnumId {
|
|||
RequiredFieldEmpty(5002L),
|
||||
WrongFieldValue(5004L),
|
||||
AccountAlreadyExist(5010L),
|
||||
BankAccountNotFound(5011L),
|
||||
AccountNotFound(5011L),
|
||||
AccountNotActive(5012L),
|
||||
CompanyNotFound(5013L),
|
||||
CompanyNotActive(5014L),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package ru.spcex.clearing.platform.messaging.domain.cud.account;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class AccountNewRequest {
|
||||
@JsonProperty
|
||||
public Long companyId;
|
||||
|
||||
@JsonProperty
|
||||
public String account;
|
||||
|
||||
@JsonProperty
|
||||
public String status;
|
||||
|
||||
@JsonProperty
|
||||
public String accountType;
|
||||
|
||||
public Long getCompanyId() {
|
||||
return companyId;
|
||||
}
|
||||
|
||||
public void setCompanyId(Long companyId) {
|
||||
this.companyId = companyId;
|
||||
}
|
||||
|
||||
public String getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(String account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getAccountType() {
|
||||
return accountType;
|
||||
}
|
||||
|
||||
public void setAccountType(String accountType) {
|
||||
this.accountType = accountType;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
package ru.spcex.clearing.platform.messaging.domain.cud.account;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class AccountUpdateRequest {
|
||||
@JsonProperty
|
||||
public Long id;
|
||||
|
||||
@JsonProperty
|
||||
public Long companyId;
|
||||
|
||||
@JsonProperty
|
||||
public String account;
|
||||
|
||||
@JsonProperty
|
||||
public String status;
|
||||
|
||||
@JsonProperty
|
||||
public String accountType;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getCompanyId() {
|
||||
return companyId;
|
||||
}
|
||||
|
||||
public void setCompanyId(Long companyId) {
|
||||
this.companyId = companyId;
|
||||
}
|
||||
|
||||
public String getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(String account) {
|
||||
this.account = account;
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getAccountType() {
|
||||
return accountType;
|
||||
}
|
||||
|
||||
public void setAccountType(String accountType) {
|
||||
this.accountType = accountType;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue