P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
package foundation.state;
 
import foundation.dao.DataPackage;
import foundation.data.entity.Entity;
import foundation.variant.expression.VariantExpression;
import foundation.variant.expression.VariantList;
import foundation.variant.expression.VariantSegment;
 
public class StateMessage {
    
    private String dataName;
    private String errorCode;
    private String template;
    private VariantExpression expression;
    
    public StateMessage() {
        
    }
    
    public StateMessage(String defaultValue) {
        this.template = defaultValue;
    }
    
    public static StateMessage getInstance(String targetId, String errorCode, String defaultValue) {
        String key = StateMessage.createKey(targetId, errorCode);
        
        StateMessageBucket bucket = StateMessageBucket.getInstance();
        StateMessage result = bucket.get(key);
        
        if (result == null) {
            result = new StateMessage(defaultValue);
        }
        
        return result;
    }
 
    public void load(Entity entity) throws Exception {
        dataName = entity.getString("data_name");
        errorCode = entity.getString("error_code");
        template = entity.getString("error_message");
        
        expression = new VariantExpression(template);
    }
    
    public String getContent(DataPackage... datas) {
        if (datas.length == 0) {
            return template;
        }
        
        //@{package.item.field}
        if(expression == null) {
            return template;
        }
        
        VariantList variants = expression.getVariantList();
        
        for (DataPackage data: datas) {
            String dataName = data.getName() + ".";
            dataName = dataName.toLowerCase();
            
            for (VariantSegment variant: variants) {
                String name = variant.getName();
                name = name.toLowerCase();
                
                if (!name.startsWith(dataName)) {
                    continue;
                }
                
                String segment = name.substring(dataName.length());
                String value = data.getItemValue(segment);
                variant.setValue(value);
            }
        }
        
        return expression.toString();
    }
    
    public StateMessage newInstance() {
        StateMessage result = new StateMessage(template);
        
        try {
            result.expression = this.expression.newInstance();
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
 
    public static String createKey(String targetId, String errorCode) {
        String result = targetId + "_" + errorCode;
        result = result.toLowerCase();
        return result;
    }
 
    public String createKey() {
        String result = dataName + "_" + errorCode;
        result = result.toLowerCase();
        return result;
    }
    
}