刘光辉
15 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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);
        }
    }
}