P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package foundation.io.template;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.List;
 
import foundation.dao.DataPackage;
import foundation.data.entity.Entity;
import foundation.data.object.DataObject;
import foundation.io.FileRepository;
import foundation.server.config.ServerStatus;
import foundation.util.Util;
import foundation.variant.expression.Segment;
import foundation.variant.expression.VariantExpression;
import foundation.variant.expression.VariantSegment;
 
public class SendTemplate {
 
    private String templateName;
    private String fileName;
    private File file;
    private String rootType;
    private long lastloadTime;
    private VariantExpression content;
    
    public SendTemplate(String dataName, String id) throws Exception {
        DataObject dataObject = DataObject.getInstance(dataName);
        Entity entity = dataObject.getTableEntity(id);
        
        this.rootType = entity.getString("root_type");
        this.fileName = entity.getString("file_name");
        this.templateName = entity.getString("template_name");
        this.lastloadTime = 0;
        
        content = new VariantExpression();
        content.setFlag_Variant('$');
        
        try {
            load();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public SendTemplate(String templateName) {
        this.templateName = templateName;
        this.lastloadTime = 0;
        this.rootType = "PathRepository";
        
        content = new VariantExpression();
        content.setFlag_Variant('$');
        
        try {
            load();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public boolean isDirty() {
        //1. 如果已经加载过,且不需要检查配置更新, 不需要重新加载
        if (lastloadTime > 0  && !ServerStatus.isCheckMetaUpdateOnTime()) {
            return false;
        }
        
        //2. 如果文件最后更新时间小于等于加载时间,不需要重新加载
        if (file != null) {
            long now = file.lastModified();
            if (lastloadTime >= now) {
                return false;
            }
        }
        
        //3. 其他情况,需要重新加载
        return true;
    }
 
    public void load() throws Exception {
        if (!isDirty()) {
            return ;
        }
        
        File path = FileRepository.getTemplatePath(rootType); 
        file = new File(path, templateName);
        
        if (!file.exists()) {
            throw new Exception("mail template not exists: " + file);
        }
        
        lastloadTime = file.lastModified();
        content.clear();
        
        InputStream inputStream = new FileInputStream(file);
        try {
            InputStreamReader reader = null; BufferedReader bufferedReader = null;
            
            try {
                reader = new InputStreamReader(inputStream, "UTF-8");
                bufferedReader = new BufferedReader(reader);
                
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    content.parseOneLine(line);
                }
            }
            finally {
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    }
                    catch (Exception e) {
                    }
                }
                if (reader != null) {
                    try {
                        reader.close();
                    }
                    catch (Exception e) {
                    }
                } 
            }
        } 
        finally {
            try {
                inputStream.close();
            }
            catch (Exception e) {
            }
        }
    }
    
    public String generateFill(DataPackage dataPackage) throws Exception {
        fill(dataPackage);
        return getContentValue();
    }
    
    public void fill(DataPackage dataPackage) throws Exception {
        VariantExpression content = getContent();
        
        for (VariantSegment variant: content) {
            String variantName = variant.getName();
            if (variantName.startsWith("newDate")) {
                String value = getCurrentDate(variantName);
                variant.setValue(value);
                continue;
            }
            
            String value = dataPackage.getString(variantName, "");
            variant.setValue(value);
        }
    }
 
    public File generateFillFile(String filePath, DataPackage dataPackage) throws Exception {
        fill(dataPackage);
        FileOutputStream fos = new FileOutputStream(filePath);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        BufferedWriter bufferedWriter = new BufferedWriter(osw);
        
        List<Segment> segments = content.getSegments();
        int n = segments.size();
        Segment segment;
        String value;
        
        for (int i = 0; i < n; i++) {
            segment = segments.get(i);
            value = segment.getValue();
            bufferedWriter.write(value);
            bufferedWriter.newLine();
        }
        
        bufferedWriter.flush();
        osw.close();
        fos.close();
        
        File file = new File(filePath);
        return file;
    }
    
    public String getTemplateName() {
        return templateName;
    }
 
    public String getFileName() {
        return fileName;
    }
    
    public VariantExpression getContent() {
        return content;
    }
    
    public String getContentValue() {
        return content.getString();
    }
 
    private String getCurrentDate(String variantName) {
        int pos = variantName.indexOf(".");
        String format = null;
        if (pos >= 0) {
            format = variantName.substring(pos + 1);
        }
        
        return Util.DataTimeToString(new Date(), format);
    }
}