刘光辉
4 小时以前 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
133
134
package jnpf.limsService.provider;
 
import jnpf.exception.DataException;
import jnpf.limsService.LimsTestWorkItemSnapshotService;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.Value;
 
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
 
/** Reads persisted executable definitions; never accepts physical mappings from a caller. */
public class LimsElnReadonlyResolver {
    private final LimsElnSourceCatalog catalog;
    private final LimsElnSourceExecution execution;
    private final Supplier<LimsElnSourceCatalog.Actor> actors;
 
    public LimsElnReadonlyResolver(LimsElnSourceCatalog catalog, DataSource readOnlyDataSource,
            LimsTestWorkItemSnapshotService taskAccess, Supplier<LimsElnSourceCatalog.Actor> actors) {
        this.catalog = catalog;
        this.execution = new LimsElnSourceExecution(readOnlyDataSource, taskAccess);
        this.actors = actors;
    }
 
    public List<Snapshot> resolve(List<String> itemIds, List<PropertyRef> properties) {
        return resolve(itemIds, properties, actors.get());
    }
 
    List<Snapshot> resolve(List<String> itemIds, List<PropertyRef> properties, LimsElnSourceCatalog.Actor actor) {
        LimsElnSourceCatalog.requireActor(actor);
        if (properties == null || properties.size() > 50) throw new DataException("Source request exceeds limits");
        if (properties.stream().anyMatch(ref -> ref == null || ref.getRevision() < 1
                || ref.getPropertyCode() == null || !ref.getPropertyCode().matches("[a-zA-Z][a-zA-Z0-9_.-]{0,79}"))) {
            throw new DataException("Invalid source request");
        }
        List<LimsElnSourceCatalog.Version> versions = new ArrayList<>();
        for (PropertyRef property : new LinkedHashSet<>(properties)) {
            versions.add(catalog.published(property.getPropertyCode(), property.getRevision(), actor));
        }
        return execution.execute(itemIds, versions, actor);
    }
 
    @Value
    public static class PropertyRef {
        String propertyCode;
        int revision;
    }
 
    public static class SourceFailure extends DataException {
        private final String code;
 
        public SourceFailure(String code, String message) {
            super(message);
            this.code = code;
        }
 
        public String getCode() {
            return code;
        }
    }
 
    @Value
    @AllArgsConstructor
    @com.fasterxml.jackson.databind.annotation.JsonNaming(com.fasterxml.jackson.databind.PropertyNamingStrategies.LowerCamelCaseStrategy.class)
    public static class Snapshot {
        String sourceRef;
        String propertyCode;
        int propertyRevision;
        String dataType;
        String outcomeCode;
        @com.fasterxml.jackson.databind.annotation.JsonSerialize(using = LimsElnSourceScalarSerializer.class)
        Object value;
        String acquiredAt;
        String lineage;
        String reasonCode;
        String formattedValue;
 
        public Snapshot(String sourceRef, String propertyCode, int propertyRevision, String dataType,
                String outcomeCode, Object value, String acquiredAt, String lineage) {
            this(sourceRef, propertyCode, propertyRevision, dataType, outcomeCode, value, acquiredAt, lineage, null, null);
        }
    }
 
    @Data
    public static class FrozenDefinition {
        private String datasetCode;
        private int datasetRevision;
        private String schema;
        private String table;
        private String idColumn;
        private String tenantColumn;
        private String deleteColumn;
        private Map<String, Field> fields;
        private String resultField;
        private List<Join> joins = new ArrayList<>();
        private List<Condition> conditions = new ArrayList<>();
        private String scopeMode = "ALL";
        private Map<String, Map<String, String>> columnIdentities;
    }
 
    @Data
    public static class Field {
        private String column;
        private String dataType;
        private int step;
        private String valueMode = "RAW";
        private Map<String, String> nameOptions;
        private String format = "RAW";
    }
 
    @Data
    public static class Join {
        private String schema;
        private String table;
        private String tenantColumn;
        private int fromStep;
        private String fromColumn;
        private String targetColumn;
        private String deleteColumn;
        private String qualifierColumn;
        private String qualifierValue;
    }
 
    @Data
    public static class Condition {
        private String field;
        private String operator;
        private List<String> values = new ArrayList<>();
    }
}