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);
|
}
|
}
|