package jnpf.limsService.impl;
|
|
import jnpf.limsEntity.LimsMessageConfigRow;
|
import jnpf.limsMapper.LimsMessageConfigMapper;
|
import lombok.AllArgsConstructor;
|
import lombok.Getter;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.StringUtils;
|
|
import java.util.Collections;
|
import java.util.LinkedHashSet;
|
import java.util.List;
|
import java.util.Set;
|
import java.util.stream.Collectors;
|
|
@Service
|
@RequiredArgsConstructor
|
public class LimsMessageConfigValidator {
|
private static final String CUSTOM_MESSAGE_SOURCE = "6";
|
private static final String IN_SITE_MESSAGE_TYPE = "1";
|
|
private final LimsMessageConfigMapper configMapper;
|
|
public ValidationResult validate(String eventCode) {
|
return validateRows(configMapper.selectByEventCode(eventCode));
|
}
|
|
static ValidationResult validateRows(List<LimsMessageConfigRow> rows) {
|
List<LimsMessageConfigRow> values = rows == null ? Collections.emptyList() : rows;
|
Set<String> configIds = values.stream()
|
.map(LimsMessageConfigRow::getConfigId)
|
.filter(StringUtils::hasText)
|
.collect(Collectors.toCollection(LinkedHashSet::new));
|
if (configIds.isEmpty()) {
|
return ValidationResult.invalid("CONFIG_NOT_FOUND: send config does not exist");
|
}
|
if (configIds.size() != 1) {
|
return ValidationResult.invalid("CONFIG_INVALID: send config code is not unique");
|
}
|
|
LimsMessageConfigRow config = values.get(0);
|
if (!Integer.valueOf(1).equals(config.getConfigEnabled())) {
|
return ValidationResult.invalid("CONFIG_INVALID: send config is disabled");
|
}
|
if (!CUSTOM_MESSAGE_SOURCE.equals(config.getConfigSource())) {
|
return ValidationResult.invalid("CONFIG_INVALID: message source must be 6");
|
}
|
|
List<LimsMessageConfigRow> relations = values.stream()
|
.filter(row -> StringUtils.hasText(row.getRelationId()))
|
.collect(Collectors.toList());
|
if (relations.size() != 1) {
|
return ValidationResult.invalid("CONFIG_INVALID: exactly one template relation is required");
|
}
|
|
LimsMessageConfigRow relation = relations.get(0);
|
if (!Integer.valueOf(1).equals(relation.getRelationEnabled())) {
|
return ValidationResult.invalid("CONFIG_INVALID: template relation is disabled");
|
}
|
if (!IN_SITE_MESSAGE_TYPE.equals(relation.getRelationMessageType())) {
|
return ValidationResult.invalid("CONFIG_INVALID: relation message type must be 1");
|
}
|
if (!StringUtils.hasText(relation.getTemplateId())) {
|
return ValidationResult.invalid("CONFIG_INVALID: related template does not exist");
|
}
|
if (!Integer.valueOf(1).equals(relation.getTemplateEnabled())) {
|
return ValidationResult.invalid("CONFIG_INVALID: message template is disabled");
|
}
|
if (!IN_SITE_MESSAGE_TYPE.equals(relation.getTemplateMessageType())) {
|
return ValidationResult.invalid("CONFIG_INVALID: template message type must be 1");
|
}
|
if (!CUSTOM_MESSAGE_SOURCE.equals(relation.getTemplateSource())) {
|
return ValidationResult.invalid("CONFIG_INVALID: template message source must be 6");
|
}
|
if (!StringUtils.hasText(relation.getMenuUrl())) {
|
return ValidationResult.invalid("CONFIG_INVALID: template menu URL is empty");
|
}
|
return ValidationResult.valid();
|
}
|
|
@Getter
|
@AllArgsConstructor
|
public static class ValidationResult {
|
private final boolean valid;
|
private final String error;
|
|
private static ValidationResult valid() {
|
return new ValidationResult(true, null);
|
}
|
|
private static ValidationResult invalid(String error) {
|
return new ValidationResult(false, error);
|
}
|
}
|
}
|