This commit is contained in:
parent
134de7d9ab
commit
28b5ef8b4b
3 changed files with 79 additions and 15 deletions
|
|
@ -0,0 +1,35 @@
|
||||||
|
package ru.spcex.clearing.scheduler.config.validation;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||||
|
import ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest;
|
||||||
|
import ru.spcex.clearing.scheduler.error.ValidationError;
|
||||||
|
import ru.spcex.clearing.validation.common.rules.ValidateSymbolsRule;
|
||||||
|
import ru.spcex.platform.classes.base.SpcexObjectBase;
|
||||||
|
import ru.spcex.platform.imdg.api.Imdg;
|
||||||
|
import ru.spcex.platform.imdg.validation.ImdgValidationContext;
|
||||||
|
import ru.spcex.platform.utils.validation.IValidator;
|
||||||
|
import ru.spcex.platform.utils.validation.ValidatorImpl;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class LauncherValidationConfig {
|
||||||
|
|
||||||
|
@Bean("launcherRequestValidator")
|
||||||
|
public Function<LauncherCommandRequest, IValidator> launcherRequestValidator(Map<String, Imdg<? extends SpcexObjectBase>> imdgForValidation) {
|
||||||
|
return launcherCommandRequest -> {
|
||||||
|
ImdgValidationContext<LauncherCommandRequest> context = new ImdgValidationContext<>();
|
||||||
|
context.setValidatedObject(launcherCommandRequest);
|
||||||
|
Consumer<String> addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s));
|
||||||
|
addImdg.accept(IMDGDistributedNames.Map_ValidationSymbols);
|
||||||
|
return new ValidatorImpl<>(context,
|
||||||
|
ValidateSymbolsRule.notRequired("PaymentPurpose",
|
||||||
|
LauncherCommandRequest::getPaymentPurpose,
|
||||||
|
ValidationError.IncorrectPaymentPurpose)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -13,7 +13,8 @@ public enum ValidationError implements IErrorEnumId {
|
||||||
CompanyNotFound(7014L),
|
CompanyNotFound(7014L),
|
||||||
SecurityNotFound(7012L),
|
SecurityNotFound(7012L),
|
||||||
CompanyNotActive(7015L),
|
CompanyNotActive(7015L),
|
||||||
SecurityNotActive(7013L);
|
SecurityNotActive(7013L),
|
||||||
|
IncorrectPaymentPurpose(7018L);
|
||||||
|
|
||||||
private final Long id;
|
private final Long id;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,10 @@
|
||||||
package ru.spcex.clearing.scheduler.service;
|
package ru.spcex.clearing.scheduler.service;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.function.Function;
|
||||||
import org.apache.kafka.clients.consumer.Consumer;
|
import org.apache.kafka.clients.consumer.Consumer;
|
||||||
import org.apache.kafka.clients.producer.Producer;
|
import org.apache.kafka.clients.producer.Producer;
|
||||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
|
|
@ -24,13 +29,11 @@ import ru.spcex.clearing.util.security.UserRoleVerification;
|
||||||
import ru.spcex.platform.enumeration.Task;
|
import ru.spcex.platform.enumeration.Task;
|
||||||
import ru.spcex.platform.imdg.api.Imdg;
|
import ru.spcex.platform.imdg.api.Imdg;
|
||||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||||
import ru.spcex.platform.utils.log.ExceptionUtils;
|
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||||
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
import java.util.concurrent.Future;
|
|
||||||
|
|
||||||
import static ru.spcex.platform.utils.enumeration.IEnumKey.getEnumByKey;
|
import static ru.spcex.platform.utils.enumeration.IEnumKey.getEnumByKey;
|
||||||
|
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
||||||
|
import ru.spcex.platform.utils.log.ExceptionUtils;
|
||||||
|
import ru.spcex.platform.utils.validation.IValidator;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class LauncherService extends QueueConsumer implements InitializingBean {
|
public class LauncherService extends QueueConsumer implements InitializingBean {
|
||||||
|
|
@ -38,23 +41,30 @@ public class LauncherService extends QueueConsumer implements InitializingBean {
|
||||||
private final Imdg<Launcher> launcherMap;
|
private final Imdg<Launcher> launcherMap;
|
||||||
private final Producer<String, Object> kafkaProducer;
|
private final Producer<String, Object> kafkaProducer;
|
||||||
private final UserRoleVerification userRoleVerification;
|
private final UserRoleVerification userRoleVerification;
|
||||||
|
private final Function<LauncherCommandRequest, IValidator> launcherRequestValidator;
|
||||||
|
private final IMessageResolver messageResolver;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public LauncherService(Consumer<String, Object> kafkaQueue, Producer<String, Object> kafkaProducer,
|
public LauncherService(Consumer<String, Object> kafkaQueue, Producer<String, Object> kafkaProducer,
|
||||||
@Qualifier("userRoleVerificationBean") UserRoleVerification userRoleVerification,
|
@Qualifier("userRoleVerificationBean") UserRoleVerification userRoleVerification,
|
||||||
ImdgProvider imdgProvider) {
|
ImdgProvider imdgProvider,
|
||||||
|
@Qualifier("launcherRequestValidator")
|
||||||
|
Function<LauncherCommandRequest, IValidator> launcherRequestValidator,
|
||||||
|
IMessageResolver messageResolver) {
|
||||||
super(kafkaQueue, kafkaProducer);
|
super(kafkaQueue, kafkaProducer);
|
||||||
this.kafkaProducer = kafkaProducer;
|
this.kafkaProducer = kafkaProducer;
|
||||||
this.userRoleVerification = userRoleVerification;
|
this.userRoleVerification = userRoleVerification;
|
||||||
this.launcherMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Launcher, Launcher.class);
|
this.launcherMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Launcher, Launcher.class);
|
||||||
|
this.launcherRequestValidator = launcherRequestValidator;
|
||||||
|
this.messageResolver = messageResolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
callback(LauncherCommandRequest.class)
|
callback(LauncherCommandRequest.class)
|
||||||
.setFunction(this::newLauncher)
|
.setFunction(this::newLauncher)
|
||||||
.forDestination(Consts.LAUNCHER_NEW, callbacks::put);
|
.forDestination(Consts.LAUNCHER_NEW, callbacks::put);
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -69,18 +79,27 @@ public class LauncherService extends QueueConsumer implements InitializingBean {
|
||||||
throw new IllegalStateException(String.format("User id in request must match: baseRequest.userId = %s, launcherCommandRequest.userId = %s", userRequest.getUserId(), req.getUserId()));
|
throw new IllegalStateException(String.format("User id in request must match: baseRequest.userId = %s, launcherCommandRequest.userId = %s", userRequest.getUserId(), req.getUserId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Instant created = Instant.now();
|
Task task = getEnumByKey(Task.class, req.getTaskName());
|
||||||
Launcher launcher = new Launcher();
|
if (task == null) {
|
||||||
if (getEnumByKey(Task.class, req.getTaskName()) == null) {
|
|
||||||
throw new IllegalStateException("Illegal task name:\t" + req.getTaskName());
|
throw new IllegalStateException("Illegal task name:\t" + req.getTaskName());
|
||||||
}
|
}
|
||||||
|
if (Task.dbfExport_OUTV == task) {
|
||||||
|
IValidator validator = launcherRequestValidator.apply(req);
|
||||||
|
Optional<EnumMessage> errorMessage = validator.tillFirstError();
|
||||||
|
if (errorMessage.isPresent()) {
|
||||||
|
String error = messageResolver.resolve(errorMessage.get());
|
||||||
|
return makeErrorResponse(userRequest, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Instant created = Instant.now();
|
||||||
|
Launcher launcher = new Launcher();
|
||||||
launcher.setTask(req.getTaskName());
|
launcher.setTask(req.getTaskName());
|
||||||
launcher.setSenderId(req.getUserId());
|
launcher.setSenderId(req.getUserId());
|
||||||
launcher.setCreated(created);
|
launcher.setCreated(created);
|
||||||
launcher.setUpdated(created);
|
launcher.setUpdated(created);
|
||||||
launcherMap.insert(launcher);
|
launcherMap.insert(launcher);
|
||||||
log.debug("successfully processed, new id {}", launcher.getId());
|
log.debug("successfully processed, new id {}", launcher.getId());
|
||||||
if (Task.dbfExport_OUTV.equalsByKey(req.getTaskName())) {
|
if (Task.dbfExport_OUTV == task) {
|
||||||
return processRequestOUTV(userRequest, req);
|
return processRequestOUTV(userRequest, req);
|
||||||
} else {
|
} else {
|
||||||
kafkaProducer.send(new ProducerRecord<>("launcher-" + launcher.getTask(), userRequest));
|
kafkaProducer.send(new ProducerRecord<>("launcher-" + launcher.getTask(), userRequest));
|
||||||
|
|
@ -90,7 +109,7 @@ public class LauncherService extends QueueConsumer implements InitializingBean {
|
||||||
|
|
||||||
private RequestInfoUpdate processRequestOUTV(BaseRequest<LauncherCommandRequest> userRequest, LauncherCommandRequest launcherNew) {
|
private RequestInfoUpdate processRequestOUTV(BaseRequest<LauncherCommandRequest> userRequest, LauncherCommandRequest launcherNew) {
|
||||||
log.debug("Redirect {} (userRequest.id={}) to queue {}", launcherNew.getTaskName(),
|
log.debug("Redirect {} (userRequest.id={}) to queue {}", launcherNew.getTaskName(),
|
||||||
userRequest.getId(), Consts.PAYMENT_INSTRUCTION_CLEARING_OUTBOUND_ACTION);
|
userRequest.getId(), Consts.PAYMENT_INSTRUCTION_CLEARING_OUTBOUND_ACTION);
|
||||||
var req = new PIClearingOutbondActionNewRequest();
|
var req = new PIClearingOutbondActionNewRequest();
|
||||||
req.setSenderId(launcherNew.getSenderId());
|
req.setSenderId(launcherNew.getSenderId());
|
||||||
req.setAddresseeId(launcherNew.getAddresseeId());
|
req.setAddresseeId(launcherNew.getAddresseeId());
|
||||||
|
|
@ -134,4 +153,13 @@ public class LauncherService extends QueueConsumer implements InitializingBean {
|
||||||
return successRouting;
|
return successRouting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private RequestInfoUpdate makeErrorResponse(BaseRequest<LauncherCommandRequest> launcherCommandRequest,
|
||||||
|
String errorMessage) {
|
||||||
|
RequestInfoUpdate requestInfoUpdate = new RequestInfoUpdate();
|
||||||
|
requestInfoUpdate.setId(launcherCommandRequest.getId());
|
||||||
|
requestInfoUpdate.setStatus(Status.Error);
|
||||||
|
requestInfoUpdate.setMessage(errorMessage);
|
||||||
|
return requestInfoUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue