CustomXmlMaker.java
4.3 KB
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
package com.sunyo.wlpt.message.builder.util;
import com.tianbo.util.Date.DateUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import java.io.File;
import java.io.IOException;
@Component
public class CustomXmlMaker {
@Autowired
private Configuration freemakerTemplate;
@Value("${custom.send-path}")
private String sendPath;
@Value("${tcs.mq-number}")
private String TCSMQNumber;
@Value("${custom.custom-code}")
private String customCode;
@Value("${custom.org-code}")
private String orgCode;
public CustomXmlMaker() {
if (freemakerTemplate==null){
this.freemakerTemplate = new Configuration();
}
}
/**
* 所有填制项中不得出现“<”、“&”符号,如有实际需要,请填写对应的转义符“<”、“&”。
十、报文中不允许出现空节点,如<Consignee></Consignee>或<Consignee/>。
* @param tplName 模板名称
* @param fileName 生成的报文名称
* @param manifestMap 数据实体
* @param sendPath 生成路径
* @return 0失败,1成功
* @throws IOException
*/
public int makeXmlToFile(String tplName,String fileName,Object manifestMap, String sendPath) throws IOException{
try {
Template template = freemakerTemplate.getTemplate(tplName);
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template,manifestMap);
String filename = sendPath+fileName;
File sendfile = new File(filename);
FileUtils.writeStringToFile(sendfile,content,"UTF-8");
}catch (IOException e){
e.printStackTrace();
return 0;
}catch (TemplateException ee){
ee.printStackTrace();
return 0;
}
return 1;
}
/**
* 生成MessageId
* MessageID:报文编号,由企业自定义,
* 要求与其它企业同类型报文不得重复,
* 最大长度为64位,由字母、数字、特定符号组成,并不得出现除“-”、“_”“/”等以外的特殊符号,
* 且符号不得作为报文编号的开头与结尾;
* @param mtType 舱单业务申报类型 如:MT1201
* @param ORGSN 使用企业在海关备案的代码,备案规则为:4位关区代码+9位企业组织机构代码+50位自定义扩展字符。:460470678920X
* @param waylbillNo 运单号
* @return CN_MT1201_1P0_460470678920X_17218902832_yyyyMMddHHmmssSSS
*/
public String makeMsgID(String mtType,String ORGSN,String waylbillNo){
StringBuilder sb = new StringBuilder();
sb.append("CN_")
.append(mtType)
.append("_1P0_")
.append(ORGSN).append("_")
.append(waylbillNo)
.append("_").append(DateUtil.getCurrentTime17());
return sb.toString();
}
/**
* 生成新舱单报文报头的SenderID。
* 发送方代码,使用企业在海关备案的代码,备案规则为:4位关区代码+9位企业组织机构代码+50位自定义扩展字符,
* 由字母和数字组成,并不得出现除“-”、“_”“/”、“@”、“.”、“*”等以外的特殊字符,且符号不得作为结尾。
* 我们这里的备案规则设置为:关区代码+9位企业组织机构代码+_TCS通道号。
* @param customCode 关区代码
* @param ORGSN 海关企业备案编码
* @param TCSSN TCS分配的通道编号
* @return 460470678920X_DXPENT0000460002
*/
public String makeSenderID(String customCode,String ORGSN,String TCSSN){
StringBuilder sb = new StringBuilder();
sb.append(customCode)
.append(ORGSN)
.append("_")
.append(TCSMQNumber);
return sb.toString();
}
public String makeSenderID(String customCode,String ORGSN){
return makeSenderID(customCode,ORGSN,TCSMQNumber);
}
}