刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package jnpf.elnService;
 
import lombok.Value;
import jnpf.base.ActionResult;
import jnpf.base.ActionResultCode;
import jnpf.exception.DataException;
import jnpf.lims.api.ElnSourceCatalogApi;
import jnpf.lims.api.model.ElnSourceReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
public interface ElnFieldProviderCatalog {
 
    List<Property> list(String source);
 
    boolean contains(String source, String property);
 
    default Property reference(String source, String property, Integer revision) {
        return list(source).stream().filter(item -> java.util.Objects.equals(property, item.getProperty())
                && java.util.Objects.equals(revision, item.getPropertyRevision())).findFirst().orElse(null);
    }
 
    @Value
    @lombok.AllArgsConstructor
    class Property {
        String source;
        String property;
        String label;
        String dataType;
        boolean readOnly;
        Integer propertyRevision;
        boolean enabled;
 
        public Property(String source, String property, String label, String dataType, boolean readOnly) {
            this(source, property, label, dataType, readOnly, null, true);
        }
    }
}
 
@Component
class DefaultElnFieldProviderCatalog implements ElnFieldProviderCatalog {
    private final ElnSourceCatalogApi remote;
 
    @Autowired
    public DefaultElnFieldProviderCatalog(ElnSourceCatalogApi remote) {
        this.remote = remote;
    }
 
    DefaultElnFieldProviderCatalog() {
        this(null);
    }
 
    private static final List<Property> PROPERTIES = Collections.unmodifiableList(Arrays.asList(
            property("LIMS_TASK", "test.basis", "检验依据", "TEXT"),
            property("LIMS_TASK", "sample.name", "供试品名称", "TEXT"),
            property("LIMS_TASK", "sample.batch_no", "批号", "TEXT"),
            property("LIMS_TASK", "sample.production_date", "生产日期", "DATE"),
            property("LIMS_TASK", "quality_standard.source", "质量标准来源", "TEXT"),
            property("LIMS_TASK", "sample.specification", "规格/浓度", "TEXT"),
            property("LIMS_TASK", "test.type", "检验类型", "TEXT"),
            property("SYSTEM", "current_user.id", "当前用户 ID", "TEXT"),
            property("SYSTEM", "current_user.real_name", "当前用户姓名", "TEXT"),
            property("SYSTEM", "current_date", "当前日期", "DATE"),
            property("SYSTEM", "current_datetime", "当前日期时间", "DATETIME")
    ));
 
    @Override
    public List<Property> list(String source) {
        if ("LIMS_TASK".equals(source) && remote != null) {
            ActionResult<List<ElnSourceReference>> response = remote.references();
            if (response == null || !ActionResultCode.SUCCESS.getCode().equals(response.getCode())
                    || response.getData() == null) throw new DataException("LIMS 来源目录不可用");
            List<Property> result = new ArrayList<>();
            for (ElnSourceReference item : response.getData()) {
                result.add(new Property("LIMS_TASK", item.getPropertyCode(), item.getDisplayName(),
                        item.getDataType(), true, item.getRevision(), item.isEnabled()));
            }
            return result;
        }
        if (source == null) {
            return Collections.emptyList();
        }
        List<Property> result = new ArrayList<>();
        for (Property property : PROPERTIES) {
            if (source.equals(property.getSource())) {
                result.add(property);
            }
        }
        return result;
    }
 
    @Override
    public Property reference(String source, String property, Integer revision) {
        if (revision == null) {
            // Legacy unversioned references are explicit compatibility, not a failure fallback.
            return PROPERTIES.stream().filter(item -> java.util.Objects.equals(source, item.getSource())
                    && java.util.Objects.equals(property, item.getProperty())).findFirst().orElse(null);
        }
        if (!"LIMS_TASK".equals(source) || revision < 1 || remote == null) return null;
        ActionResult<ElnSourceReference> response = remote.reference(property, revision);
        if (response == null || !ActionResultCode.SUCCESS.getCode().equals(response.getCode())
                || response.getData() == null) throw new DataException("LIMS 固定来源版本不可用");
        ElnSourceReference item = response.getData();
        if (!java.util.Objects.equals(property, item.getPropertyCode()) || !revision.equals(item.getRevision())) {
            throw new DataException("LIMS 返回的来源版本不一致");
        }
        return new Property(source, item.getPropertyCode(), item.getDisplayName(), item.getDataType(),
                true, item.getRevision(), item.isEnabled());
    }
 
    @Override
    public boolean contains(String source, String property) {
        if (source == null || property == null) {
            return false;
        }
        for (Property item : list(source)) {
            if (source.equals(item.getSource()) && property.equals(item.getProperty())) {
                return true;
            }
        }
        return false;
    }
 
    private static Property property(String source, String property, String label, String dataType) {
        return new Property(source, property, label, dataType, true);
    }
}