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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package foundation.code;
 
import java.util.HashMap;
import java.util.Map;
 
import foundation.code.segment.DaySegment;
import foundation.code.segment.DynamicStringSegment;
import foundation.code.segment.MonthSegment;
import foundation.code.segment.MonthlyNoSegment;
import foundation.code.segment.NoSegment;
import foundation.code.segment.StringSegment;
import foundation.code.segment.YearSegment;
import foundation.code.segment.YearlyNoSegment;
import foundation.variant.expression.Segment;
import foundation.variant.provider.IVariantsProvider;
 
 
public abstract class SegmentCreator {
 
    private static Map<String, Class<? extends SegmentCreator>> segmentCreatorMap;
    protected CodeRule codeRule;
    protected int size;
    
    static {
        initCreatorMap();
    }
    
    public SegmentCreator() {
        
    }
    
    protected void init(CodeRule rule) {
        
    }
 
    public static SegmentCreator getInstance(CodeRule codeRule, Segment segment) {
        try {
            if (!segment.isVariant()) {
                return new StringSegment(segment);
            }
            else {
                String name = segment.getName(); 
    
                if (name == null) {
                    return null;
                }
                
                String lower = name.toLowerCase();
                
                if (lower.startsWith("dynamic(")) {
                    String rule = name.substring("dynamic(".length(), name.length() - 1);
                    codeRule.setRule(rule);
                    
                    lower = "dynamic";
                }
    
                Class<? extends SegmentCreator> clazz = segmentCreatorMap.get(lower);
                
                if (clazz == null) {
                    return null;
                }
 
                SegmentCreator creator = clazz.newInstance();
                creator.setCodeRule(codeRule);
                creator.setSize(name.length());
                
                return creator;
            }
        } 
        catch (Exception e) {
            return new StringSegment(segment);
        }
    }
 
    public abstract String getNextSequenceValue(String dynamic, IVariantsProvider... entitys);
    
    public abstract String getCurrSequenceValue(String dynamic, IVariantsProvider... entitys);
    
    public abstract String getTempValue(String dynamic, IVariantsProvider... entitys);
    
    protected String format(String value) {
        if (value == null) {
            return value;
        }
        
        int length = value.length();
        
        if (length > size) {
            return value.substring(0, size);
        }
        else if (length == size) {
            return value;
        }
        else {
            while (value.length() < size) {
                value = "0" + value;
            }
            
            return value;
        }
    }
    
    private static void initCreatorMap() {
        segmentCreatorMap = new HashMap<String, Class<? extends SegmentCreator>>();
        
        segmentCreatorMap.put("yyyy", YearSegment.class);
        segmentCreatorMap.put("yy", YearSegment.class);
        segmentCreatorMap.put("mm", MonthSegment.class);
        segmentCreatorMap.put("dd", DaySegment.class);
        segmentCreatorMap.put("00", NoSegment.class);
        segmentCreatorMap.put("000", NoSegment.class);
        segmentCreatorMap.put("0000", NoSegment.class);
        segmentCreatorMap.put("00000", NoSegment.class);
        segmentCreatorMap.put("000000", NoSegment.class);
        segmentCreatorMap.put("0000000", NoSegment.class);
        segmentCreatorMap.put("00000000", NoSegment.class);
        segmentCreatorMap.put("000000000", NoSegment.class);
        segmentCreatorMap.put("0000000000", NoSegment.class);
        segmentCreatorMap.put("dynamic", DynamicStringSegment.class);
        segmentCreatorMap.put("m00", MonthlyNoSegment.class);
        segmentCreatorMap.put("m000", MonthlyNoSegment.class);
        segmentCreatorMap.put("m0000", MonthlyNoSegment.class);
        segmentCreatorMap.put("m00000", MonthlyNoSegment.class);
        segmentCreatorMap.put("m000000", MonthlyNoSegment.class);
        segmentCreatorMap.put("m0000000", MonthlyNoSegment.class);
        segmentCreatorMap.put("m00000000", MonthlyNoSegment.class);
        segmentCreatorMap.put("m000000000", MonthlyNoSegment.class);
        segmentCreatorMap.put("m0000000000", MonthlyNoSegment.class);
        segmentCreatorMap.put("y00", YearlyNoSegment.class);
        segmentCreatorMap.put("y000", YearlyNoSegment.class);
        segmentCreatorMap.put("y0000", YearlyNoSegment.class);
        segmentCreatorMap.put("y00000", YearlyNoSegment.class);
        segmentCreatorMap.put("y000000", YearlyNoSegment.class);
        segmentCreatorMap.put("y0000000", YearlyNoSegment.class);
        segmentCreatorMap.put("y00000000", YearlyNoSegment.class);
        segmentCreatorMap.put("y000000000", YearlyNoSegment.class);
        segmentCreatorMap.put("y0000000000", YearlyNoSegment.class);        
    }
    
    public void setSize(int size) {
        this.size = size;
    }
    
    protected void setCodeRule(CodeRule codeRule) {
        this.codeRule = codeRule;
        init(codeRule);
    }
 
}