Commit 45675193 by chengzhuoshen

1.修改swift dto生成MT报文的block1,block2,block3,

block5代码
2.根据配置文件把MX模型转SwiftDto,目前实现了PACS008001
parent be410ce5
......@@ -84,5 +84,10 @@
<artifactId>eibs-ext</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -5,7 +5,6 @@ import com.brilliance.swift.mt2SwiftDto.Mt2SwiftDtoCreateManager;
import com.brilliance.swift.mx2swiftdto.Mx2SwiftDtoCreatorManager;
import com.brilliance.swift.swiftdto2mt.SwiftDto2MtCreatorManager;
import com.brilliance.swift.swiftdto2mx.SwiftDto2MxCreatorManager;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.vo.SwiftDto;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import org.apache.commons.io.FileUtils;
......@@ -20,23 +19,23 @@ public class SwiftTransfer {
* SWIFTDTO transfer MT
* @param file
* @param fileOutputPath
* @param mtIoType
* @param extraMap
* @return
* @throws SwiftException
*/
public static String swiftDto2Mt(File file, String fileOutputPath, String mtIoType) throws SwiftException {
public static String swiftDto2Mt(File file, String fileOutputPath, Map<String, Object> extraMap) throws SwiftException {
try {
String gsonStr = FileUtils.readFileToString(file);
SwiftDto swiftDto = SwiftDto.fromJson(gsonStr);
return new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, fileOutputPath, StringUtil.isEmpty(mtIoType)?"O":mtIoType);
return new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, fileOutputPath, extraMap);
} catch (IOException e) {
throw new SwiftException("ERROR", e.getMessage());
}
}
public static String swiftDto2Mt(String gsonStr, String fileOutputPath, String mtIoType) throws SwiftException{
public static String swiftDto2Mt(String gsonStr, String fileOutputPath, Map<String, Object> extraMap) throws SwiftException{
SwiftDto swiftDto = SwiftDto.fromJson(gsonStr);
return new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, fileOutputPath, StringUtil.isEmpty(mtIoType)?"O":mtIoType);
return new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, fileOutputPath, extraMap);
}
/**
......@@ -119,17 +118,7 @@ public class SwiftTransfer {
public static String mx2Mt(String xmlStr, String outPutFilePath, Map<String, Object> extraMap) throws SwiftException {
SwiftDto swiftDto = new Mx2SwiftDtoCreatorManager().convert(xmlStr, outPutFilePath, extraMap);
String bnkBic = null;
String mtIoType = "O";//代表是收报,默认值
if (extraMap != null) {
bnkBic = (String)extraMap.get("bnkBic");
}
if (StringUtil.isNotEmpty(bnkBic)) {
if (bnkBic.equals(swiftDto.getSwiftHeader().getSenderBic())) {
mtIoType = "I";//如果发报行和本行bic一样,表示是发报
}
}
return new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, outPutFilePath, mtIoType);
return new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, outPutFilePath, extraMap);
}
/**
......@@ -160,17 +149,7 @@ public class SwiftTransfer {
*/
public static Map<String, String> mx2MtMap(String xmlStr, Map<String, Object> extraMap) throws SwiftException {
SwiftDto swiftDto = new Mx2SwiftDtoCreatorManager().convert(xmlStr, null, null);
String bnkBic = null;
String mtIoType = "O";//代表是收报,默认值
if (extraMap != null) {
bnkBic = (String)extraMap.get("bnkBic");//得到本行的bicCode
}
if (StringUtil.isNotEmpty(bnkBic)) {
if (bnkBic.equals(swiftDto.getSwiftHeader().getSenderBic())) {
mtIoType = "I";//如果发报行和本行bic一样,表示是发报
}
}
String mtStr = new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, null, mtIoType);
String mtStr = new SwiftDto2MtCreatorManager().swiftDto2Mt(swiftDto, null, extraMap);
Map<String, String> maps = MessageReader.getSwfMsgInfoViaMsg(mtStr);
maps.put("std", "MX");
AbstractMX abstractMx = AbstractMX.parse(xmlStr);
......
......@@ -88,4 +88,33 @@ public class Mx2MtConstants {
public static final String MT_21_DEFAULT_VALUE = "NONREF";
public static final String MX_END_TO_END_ID_DEFAULT_VALUE = "NOTPROVIDED";
/**
* 外部参数名称
*/
public static final String APPLICATION_MODE = "ApplicationMode";
public static final String RECEIVERS_ADDRESS = "ReceiversAddress";
public static final String SENDERS_ADDRESS = "SendersAddress";
public static final String OUTPUT_DATE = "OutputDate";
public static final String INTPUT_DATE = "IntputDate";
public static final String MESSAGE_PRIORITY = "MessagePriority";
public static final String BLOCK3_103 = "Block3_103";
public static final String BLOCK3_108 = "Block3_108";
public static final String BLOCK3_165 = "Block3_165";
public static final String BLOCK3_433 = "Block3_433";
public static final String BLOCK3_434 = "Block3_434";
public static final String BLOCK5_TNG = "Block5_TNG";
public static final String BLOCK5_PDE = "Block5_PDE";
}
......@@ -6,16 +6,17 @@ import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.util.XmlUtil;
import com.brilliance.swift.vo.SwiftHeader;
import com.brilliance.swift.vo.common.*;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;
public abstract class AbstractMx2SwiftDtoCreator implements Mx2SwiftDtoCreator{
......@@ -39,6 +40,8 @@ public abstract class AbstractMx2SwiftDtoCreator implements Mx2SwiftDtoCreator{
protected String defaultDateFormate = "yyyy-MM-dd";
protected Map<String, Object> maps = new HashMap<>();
@Override
public void appHeader() throws SwiftException {
try {
......@@ -310,4 +313,119 @@ public abstract class AbstractMx2SwiftDtoCreator implements Mx2SwiftDtoCreator{
throw new SwiftException("ERROR", e.getMessage());
}
}
public Object getXmlPathValue(Document document, String xmlPath) {
try {
xmlPath = xmlPath.replace("#APP", appHdrParentElementName).replace("#GRP", grpHdrParentElementName).replace("#BODY", bodyHdrParentElementName);
Object value = null;
if (xmlPath.indexOf("*") > -1) { //出现多个值
String beginPath = xmlPath.substring(0, xmlPath.indexOf("*"));
String endPath = xmlPath.substring(xmlPath.indexOf("*")+1);
String type = "String";
if (endPath.indexOf("&") > -1) {
type = endPath.substring(endPath.indexOf("&")+1);
endPath = endPath.substring(0, endPath.indexOf("&"));
}
int count = XmlUtil.getChildrenCount(document, beginPath, null);
if (count > 0) {
if ("String".equalsIgnoreCase(type)) {
List<String> list = new ArrayList<>();
for (int i=0; i<count; i++) {
String xmlValue = XmlUtil.getXmlNodeValue(document, beginPath+"("+i+")."+endPath);
list.add(xmlValue);
}
value = list;
} else if ("BigDecimal".equalsIgnoreCase(type)) {
List<BigDecimal> list = new ArrayList<>();
for (int i=0; i<count; i++) {
String xmlValue = XmlUtil.getXmlNodeValue(document, beginPath+"("+i+")."+endPath);
list.add(new BigDecimal(xmlValue));
}
value = list;
}
}
} else if (xmlPath.indexOf("&") > -1) { //数据类型
String type = xmlPath.substring(xmlPath.indexOf("&")+1);
xmlPath = xmlPath.substring(0, xmlPath.indexOf("&"));
String xmlValue = XmlUtil.getXmlNodeValue(document, xmlPath);
if ("DATE".equalsIgnoreCase(type) && StringUtil.isNotEmpty(xmlValue)) {
XMLGregorianCalendar dtTm = DatatypeFactory.newInstance().newXMLGregorianCalendar(xmlValue);
value = DateUtil.format(dtTm.toGregorianCalendar().getTime(), defaultFormate);
} else if ("BigDecimal".equalsIgnoreCase(type) && StringUtil.isNotEmpty(xmlValue)) {
value = new BigDecimal(xmlValue);
} else if ("List<String>".equalsIgnoreCase(type) && StringUtil.isNotEmpty(xmlValue)) {
int count = XmlUtil.getChildrenCount(document, xmlPath, null);
if (count > 0) {
List<String> list = new ArrayList<>();
for (int i=0; i<count; i++) {
xmlValue = XmlUtil.getXmlNodeValue(document, xmlPath+"("+i+")");
list.add(xmlValue);
}
value = list;
}
}
} else {
value = XmlUtil.getXmlNodeValue(document, xmlPath);
}
return value;
} catch (DatatypeConfigurationException e) {
throw new SwiftException("ERROR", e.getMessage());
}
}
/**
* 传配置文件名称,
* 根据配置文件将MX 模型转换成Swift Dto JSON map
* @param fileName
*/
protected void readPropertyFile(String fileName) {
try {
File propertyFile = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\template\\mx2swiftdto\\"+ fileName);
List<String> properties = FileUtils.readLines(propertyFile);
if (properties.size() > 0) {
for (String property : properties) {
if (property.startsWith("#")) {
continue;//过滤注释
}
String[] strArr = property.split("=");
if (strArr.length != 2) {
continue;//过滤掉不符合规则的配置
}
String key = strArr[0];
String xmlPath = strArr[1];
String[] xmlPaths = xmlPath.split("\\?");
Object xmlValue = null;
for (String path : xmlPaths) {
xmlValue = getXmlPathValue(document, path);//?表示优先级,如果取到值就跳出循环
if (xmlValue != null) break;
}
if (xmlValue == null) continue;
String[] keys = key.split("\\.");
if (keys.length == 1) {
maps.put(keys[0], xmlValue); //如果只有一个节点,直接put值
} else { //如果节点值超过1个,先赋值第一个节点,再循环赋值,最后一个节点直接put值。
Map<String, Object> keyMap = null;
if (maps.containsKey(keys[0])) {
keyMap = (Map<String, Object>)maps.get(keys[0]);
} else {
keyMap = new HashMap<>();
maps.put(keys[0], keyMap);
}
for (int i=1; i<keys.length; i++) {
if (i == keys.length-1) {
keyMap.put(keys[i], xmlValue);
break;
}
if (!keyMap.containsKey(keys[i])) {
keyMap.put(keys[i], new HashMap<>());
}
keyMap = (Map<String, Object>)keyMap.get(keys[i]);
}
}
}
}
} catch (IOException e) {
throw new SwiftException("ERROR", e.getMessage());
}
}
}
......@@ -8,11 +8,7 @@ public class Mx2SwiftDtoContextIdentifier {
public static final String MX_VERSION = "mx.version";
public static final String APPHDR_PARENT_ELEMENT_NAME = "apphdr.parent.element.name";
public static final String GRPHDR_PARENT_ELEMENT_NAME = "grphdr.parent.element.name";
public static final String BODY_PARENT_ELEMENT_NAME = "body.parent.element.name";
public static final String MX_MESSAGETYPE = "mx.messageType";
public static final String MX_TO_SWIFTDTO_OUTPUT_FILE_PATH = "m.to.swiftdto.output.filepath";
......
......@@ -26,11 +26,12 @@ public class Mx2SwiftDtoCreatorManager {
public void init(String xmlStr) {
AbstractMX abstractMX = AbstractMX.parse(xmlStr);
msgType = (abstractMX.getMxId().getBusinessProcess().name()
+ "."
+ abstractMX.getMxId().getFunctionality()
+ "."
+ abstractMX.getMxId().getVariant())
//+ abstractMX.getMxId().getVersion())
.trim()
.replace(".", "");
.trim();
mxObj = abstractMX;
this.xmlStr = xmlStr;
version = abstractMX.getMxId().getVersion();
......@@ -46,6 +47,7 @@ public class Mx2SwiftDtoCreatorManager {
context.set(Mx2SwiftDtoContextIdentifier.MX_XMl_DOCUMENT, document);
context.set(Mx2SwiftDtoContextIdentifier.MX_OBJ, mxObj);
context.set(Mx2SwiftDtoContextIdentifier.MX_VERSION, version);
context.set(Mx2SwiftDtoContextIdentifier.MX_MESSAGETYPE, msgType);
AbstractMx2SwiftDtoCreator creator = getMx2SwiftDtoCreator(msgType);
creator.setContext(context);
creator.preProcess();
......@@ -60,13 +62,13 @@ public class Mx2SwiftDtoCreatorManager {
}
public AbstractMx2SwiftDtoCreator getMx2SwiftDtoCreator(String msgType) throws SwiftException {
if ("pacs008001".equals(msgType)) {
if ("pacs.008.001".equals(msgType)) {
return new Mx2SwiftDtoCCTCreator();
} else if ("pacs009001".equals(msgType)){
} else if ("pacs.009.001".equals(msgType)){
return new Mx2SwiftDtoFICTCreator();
}else if("camt054001".equals(msgType)){
}else if("camt.054.001".equals(msgType)){
return new Mx2SwiftDtoBTCDCNCreator();
} else if("camt053001".equals(msgType)){
} else if("camt.053.001".equals(msgType)){
return new Mx2SwiftDtoBTCSCreator();
} else {
throw new SwiftException("Invalid message type");
......
......@@ -15,7 +15,6 @@ import org.apache.commons.lang3.Validate;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
public abstract class AbstractSwiftDto2MtCreator implements SwiftDto2MtCreator{
......@@ -69,51 +68,99 @@ public abstract class AbstractSwiftDto2MtCreator implements SwiftDto2MtCreator{
public void withBlock1() throws SwiftException {
SwiftBlock1 block1 = new SwiftBlock1();
SwiftDto swiftDto = (SwiftDto)context.get(SwiftDto2MtContextIdentifier.SWIFT_DTO_OBJ);
String mtIoType = (String)context.get(SwiftDto2MtContextIdentifier.MT_IO_TYPE, true);
if ("O".equalsIgnoreCase(mtIoType)) {
block1.setLogicalTerminal(processBicCode(swiftDto.getSwiftHeader().getReceiverBic()));
String senderBic = (String)context.get(Mx2MtConstants.SENDERS_ADDRESS, true);
if (StringUtil.isEmpty(senderBic) && swiftDto.getSwiftHeader() != null) {
senderBic = swiftDto.getSwiftHeader().getSenderBic();
}
String receiverBic = (String)context.get(Mx2MtConstants.RECEIVERS_ADDRESS, true);
if (StringUtil.isEmpty(receiverBic) && swiftDto.getSwiftHeader() != null) {
receiverBic = swiftDto.getSwiftHeader().getReceiverBic();
}
String applicationMode = (String)context.get(Mx2MtConstants.APPLICATION_MODE, true);
if ("O".equalsIgnoreCase(applicationMode)) {
block1.setLogicalTerminal(processBicCode(receiverBic));
} else {
block1.setLogicalTerminal(processBicCode(swiftDto.getSwiftHeader().getSenderBic()));
block1.setLogicalTerminal(processBicCode(senderBic));
}
block1.setSessionNumber(getRandomNumber(4));
block1.setSequenceNumber(getRandomNumber(6));
block1.setSessionNumber(getRandomNumber(4));//mapping文档中给的是0000
block1.setSequenceNumber(getRandomNumber(6));//mapping文档中给的是000000
context.get(SwiftMessage.class).setBlock1(block1);
}
@Override
public void withBlock2() throws SwiftException {
SwiftDto swiftDto = (SwiftDto)context.get(SwiftDto2MtContextIdentifier.SWIFT_DTO_OBJ);
String mtIoType = (String)context.get(SwiftDto2MtContextIdentifier.MT_IO_TYPE, true);
String messagePriority = MessagePriority.NORM.equals(swiftDto.getSwiftHeader().getMessagePriority()) ? "N" : "U";
if ("O".equalsIgnoreCase(mtIoType)) {
String senderBic = (String)context.get(Mx2MtConstants.SENDERS_ADDRESS, true);
if (StringUtil.isEmpty(senderBic) && swiftDto.getSwiftHeader() != null) {
senderBic = swiftDto.getSwiftHeader().getSenderBic();
}
String receiverBic = (String)context.get(Mx2MtConstants.RECEIVERS_ADDRESS, true);
if (StringUtil.isEmpty(receiverBic) && swiftDto.getSwiftHeader() != null) {
receiverBic = swiftDto.getSwiftHeader().getReceiverBic();
}
String messagePriority = (String)context.get(Mx2MtConstants.MESSAGE_PRIORITY, true);
if (StringUtil.isEmpty(messagePriority) && swiftDto.getSwiftHeader() != null) {
messagePriority = MessagePriority.NORM.equals(swiftDto.getSwiftHeader().getMessagePriority()) ? "N" : "U";
}
Date inputDate = (Date)context.get(Mx2MtConstants.INTPUT_DATE, true);
if (inputDate == null && swiftDto.getSwiftHeader() != null) {
inputDate = swiftDto.getSwiftHeader().getCreateDate();
}
Date outputDate = (Date)context.get(Mx2MtConstants.OUTPUT_DATE, true);
if (outputDate == null) {
outputDate = new Date();
}
String applicationMode = (String)context.get(Mx2MtConstants.APPLICATION_MODE, true);
if ("O".equalsIgnoreCase(applicationMode)) {
SwiftBlock2Output block2 = new SwiftBlock2Output();
block2.setMessageType(getMtType());
block2.setSenderInputTime(DateUtil.format(swiftDto.getSwiftHeader().getCreateDate(), "HHmm"));
block2.setMIRDate(DateUtil.format(swiftDto.getSwiftHeader().getCreateDate(), "yyMMdd"));
block2.setMIRLogicalTerminal(processBicCode(swiftDto.getSwiftHeader().getSenderBic()));
block2.setSenderInputTime(DateUtil.format(inputDate, "HHmm"));
block2.setMIRDate(DateUtil.format(inputDate, "yyMMdd"));
block2.setMIRLogicalTerminal(processBicCode(senderBic));
block2.setMIRSessionNumber(getRandomNumber(4));
block2.setMIRSequenceNumber(getRandomNumber(6));
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.setTime(outputDate);
block2.setReceiverOutputDateTime(calendar);
block2.setMessagePriority(messagePriority);
context.get(SwiftMessage.class).setBlock2(block2);
} else {
SwiftBlock2Input block2 = new SwiftBlock2Input();
block2.setMessageType(getMtType());
block2.setReceiverAddress(processBicCode(swiftDto.getSwiftHeader().getReceiverBic()));
block2.setReceiverAddress(processBicCode(receiverBic));
block2.setMessagePriority(messagePriority);
if ("U".equalsIgnoreCase(messagePriority)) {
block2.setDeliveryMonitoring("1");
}
context.get(SwiftMessage.class).setBlock2(block2);
}
}
@Override
public void withBlock3() throws SwiftException {
SwiftDto swiftDto = (SwiftDto)context.get(SwiftDto2MtContextIdentifier.SWIFT_DTO_OBJ);
List<Tag> tags = new ArrayList<>();
SwiftBlock3 block3 = new SwiftBlock3();
tags.add(new Tag("108", new SimpleDateFormat("yyMMddHHmmssSSSS").format(Calendar.getInstance().getTime())));
block3.setTags(tags);
String block3_103 = (String)context.get(Mx2MtConstants.BLOCK3_103, true);
if (StringUtil.isNotEmpty(block3_103)) {
tags.add(new Tag("103", block3_103));
}
String block3_108 = (String)context.get(Mx2MtConstants.BLOCK3_108, true);
if (StringUtil.isNotEmpty(block3_108)) {
tags.add(new Tag("108", block3_108));
}
String block3_165 = (String)context.get(Mx2MtConstants.BLOCK3_165, true);
if (StringUtil.isNotEmpty(block3_165)) {
tags.add(new Tag("165", block3_165));
}
String block3_433 = (String)context.get(Mx2MtConstants.BLOCK3_433, true);
if (StringUtil.isNotEmpty(block3_433)) {
tags.add(new Tag("433", block3_433));
}
String block3_434 = (String)context.get(Mx2MtConstants.BLOCK3_434, true);
if (StringUtil.isNotEmpty(block3_434)) {
tags.add(new Tag("434", block3_434));
}
context.get(SwiftMessage.class).setBlock3(block3);
}
......@@ -133,7 +180,20 @@ public abstract class AbstractSwiftDto2MtCreator implements SwiftDto2MtCreator{
@Override
public void withBlock5() throws SwiftException {
SwiftBlock5 swiftBlock5 = new SwiftBlock5();
Boolean block5_TNG = (Boolean)context.get(Mx2MtConstants.BLOCK5_TNG, true);
if (Boolean.TRUE.equals(block5_TNG)) {
swiftBlock5.setTag(SwiftBlock5Field.TNG, null);
}
Boolean block5_PDE = (Boolean)context.get(Mx2MtConstants.BLOCK5_PDE, true);
SwiftDto swiftDto = (SwiftDto)context.get(SwiftDto2MtContextIdentifier.SWIFT_DTO_OBJ);
if (!Boolean.TRUE.equals(block5_PDE) && swiftDto.getSwiftHeader() != null) {
block5_PDE = swiftDto.getSwiftHeader().getPossibleDuplicate();
}
if (Boolean.TRUE.equals(block5_PDE)) {
swiftBlock5.setPDE();
}
context.get(SwiftMessage.class).setBlock5(swiftBlock5);
}
@Override
......
......@@ -8,8 +8,6 @@ public class SwiftDto2MtContextIdentifier {
public static final String SWIFT_DTO_OBJ = "swift.dto.obj";
public static final String MT_IO_TYPE = "mt.io.type";
public static final String CD_60F = "PRCD";
public static final String CD_60M = "ITBD";
......
package com.brilliance.swift.swiftdto2mt;
import com.brilliance.swift.constants.Mx2MtConstants;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.swiftdto2mt.mt103.SwiftDto2Mt103Creator;
import com.brilliance.swift.swiftdto2mt.mt202.SwiftDto2Mt202Creator;
......@@ -8,13 +9,28 @@ import com.brilliance.swift.swiftdto2mt.mt940950.SwiftDto2Mt940950Creator;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.vo.SwiftDto;
import java.util.Map;
public class SwiftDto2MtCreatorManager {
public String swiftDto2Mt(SwiftDto swiftDto, String fileOutputPath, String mtIoType) throws SwiftException {
public String swiftDto2Mt(SwiftDto swiftDto, String fileOutputPath, Map<String, Object> extraMap) throws SwiftException {
SwiftDto2MtContext context = new SwiftDto2MtContext();
context.set(SwiftDto2MtContextIdentifier.SWIFT_DTO_OBJ, swiftDto);
context.set(SwiftDto2MtContextIdentifier.MT_OUTPUT_FILE_PATH, fileOutputPath);
context.set(SwiftDto2MtContextIdentifier.MT_IO_TYPE, StringUtil.isEmpty(mtIoType)?"O":mtIoType);
String applicationMode = "O";//代表是收报,默认值
if (extraMap != null) {
applicationMode = (String) extraMap.get(Mx2MtConstants.APPLICATION_MODE);
if (StringUtil.isEmpty(applicationMode)) {
String bnkBic = (String)extraMap.get("bnkBic");//得到本行的bicCode
if (StringUtil.isNotEmpty(bnkBic)) {
if (bnkBic.equals(swiftDto.getSwiftHeader().getSenderBic())) {
applicationMode = "I";//如果发报行和本行bic一样,表示是发报
}
}
}
}
context.putAll(extraMap);
context.set(Mx2MtConstants.APPLICATION_MODE, applicationMode);
AbstractSwiftDto2MtCreator creator = getCreator(swiftDto.getSwiftHeader().getMessageType());
creator.setContext(context);
creator.preProcess();
......
......@@ -7,6 +7,8 @@ import com.brilliance.swift.swiftdto2mt.SwiftDto2MtTagGenerate;
import com.brilliance.swift.swiftdto2mt.mt103.impl.*;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.vo.VoCustomerCreditTransfer;
import com.brilliance.swift.vo.common.ServiceLevel;
import com.brilliance.swift.vo.common.ServiceLevelCode;
import com.brilliance.swift.vo.common.TransactionReference;
import com.prowidesoftware.swift.model.SwiftBlock3;
import com.prowidesoftware.swift.model.SwiftMessage;
......@@ -22,6 +24,15 @@ public class SwiftDto2Mt103Creator extends AbstractSwiftDto2MtCreator {
super.withBlock3();
SwiftBlock3 block3 = context.get(SwiftMessage.class).getBlock3();
VoCustomerCreditTransfer customerCreditTransfer = (VoCustomerCreditTransfer)context.get(SwiftDto2MtContextIdentifier.SWIFT_DTO_OBJ, true);
ServiceLevel serviceLevel = customerCreditTransfer.getServiceLevel();
if (serviceLevel != null) {
if (ServiceLevelCode.G001.equals(serviceLevel.getCode())
|| ServiceLevelCode.G002.equals(serviceLevel.getCode())
|| ServiceLevelCode.G003.equals(serviceLevel.getCode())
|| ServiceLevelCode.G004.equals(serviceLevel.getCode())) {
block3.getTags().add(new Tag("111", serviceLevel.getCode().value().substring(1)));
}
}
TransactionReference txnRef = customerCreditTransfer.getTxnRef();
if (txnRef != null && StringUtil.isNotEmpty(txnRef.getUetr())) {
block3.getTags().add(new Tag("121", txnRef.getUetr()));
......
......@@ -19,7 +19,14 @@ public class Tag36Generate extends AbstractSwiftDto2MtTagGenerate {
SwiftMessage swiftMessage = context.get(SwiftMessage.class);
List<Tag> tags = swiftMessage.getBlock4().getTags();
if (customerCreditTransfer.getExchangeRate() != null) {
String exchangeRateStr = String.valueOf(customerCreditTransfer.getExchangeRate()).replace(".", ",");
String exchangeRateStr = String.valueOf(customerCreditTransfer.getExchangeRate());
if (exchangeRateStr.startsWith("-")) {
exchangeRateStr = exchangeRateStr.replace("-", "");
}
if(exchangeRateStr.indexOf(".") > 0){//去掉小数点后不需要的0
exchangeRateStr = exchangeRateStr.replaceAll("0+?$", "");
}
exchangeRateStr = exchangeRateStr.replace(".", ",");//小数点替换成,
tags.add(new Tag(name, exchangeRateStr));
}
}
......
......@@ -26,6 +26,8 @@ public class SwiftHeader {
private MessagePriority messagePriority;
private Boolean possibleDuplicate;
private List<SwiftHeader> relatedSwiftHeaders;
public String getSenderBic() {
......@@ -110,6 +112,14 @@ public class SwiftHeader {
this.messagePriority = messagePriority;
}
public Boolean getPossibleDuplicate() {
return possibleDuplicate;
}
public void setPossibleDuplicate(Boolean possibleDuplicate) {
this.possibleDuplicate = possibleDuplicate;
}
public List<SwiftHeader> getRelatedSwiftHeaderList() {
if (relatedSwiftHeaders == null) {
relatedSwiftHeaders = new ArrayList<>();
......
......@@ -30,7 +30,7 @@ public class VoCustomerCreditTransfer extends SwiftDto {
protected CcyFormatAmount settledCcyFormatAmount;
protected CcyFormatAmount InstructedCcyFormatAmount;
protected CcyFormatAmount instructedCcyFormatAmount;
protected BigDecimal exchangeRate;
......@@ -115,11 +115,11 @@ public class VoCustomerCreditTransfer extends SwiftDto {
}
public CcyFormatAmount getInstructedCcyFormatAmount() {
return InstructedCcyFormatAmount;
return instructedCcyFormatAmount;
}
public void setInstructedCcyFormatAmount(CcyFormatAmount instructedCcyFormatAmount) {
InstructedCcyFormatAmount = instructedCcyFormatAmount;
instructedCcyFormatAmount = instructedCcyFormatAmount;
}
public BigDecimal getExchangeRate() {
......
......@@ -22,7 +22,7 @@
"ccy": "USD",
"amt": 23453.127
},
"InstructedCcyFormatAmount": {
"instructedCcyFormatAmount": {
"ccy": "EUR",
"amt": 1345.12
},
......
......@@ -18,7 +18,8 @@
<BizMsgIdr>TBEXO12345</BizMsgIdr>
<MsgDefIdr>pacs.008.001.09</MsgDefIdr>
<BizSvc>swift.cbprplus.02</BizSvc>
<CreDt>2020-09-08T10:10:47+08:00</CreDt>
<CreDt>2020-09-08T10:10:47+07:00</CreDt>
<PssblDplct>true</PssblDplct>
</AppHdr>
<Doc:Document xmlns:Doc="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.09">
<Doc:FIToFICstmrCdtTrf>
......@@ -68,7 +69,7 @@
<Doc:CdtDtTm>2022-03-13T22:30:42.112+07:00</Doc:CdtDtTm>
</Doc:SttlmTmIndctn>
<Doc:SttlmTmReq>
<Doc:CLSTm>2022-03-13T21:21:42.112+07:00</Doc:CLSTm>
<Doc:CLSTm>21:21:42.112+07:00</Doc:CLSTm>
</Doc:SttlmTmReq>
<Doc:InstrForCdtrAgt>
<Doc:Cd>TELB</Doc:Cd>
......@@ -95,7 +96,7 @@
<Doc:IntrBkSttlmAmt Ccy="USD">23453.127</Doc:IntrBkSttlmAmt>
<Doc:IntrBkSttlmDt>2022-03-13+08:00</Doc:IntrBkSttlmDt>
<Doc:InstdAmt Ccy="EUR">1345.12</Doc:InstdAmt>
<Doc:XchgRate>17.4357135422</Doc:XchgRate>
<Doc:XchgRate>10.124500</Doc:XchgRate>
<Doc:ChrgBr>DEBT</Doc:ChrgBr>
<Doc:ChrgsInf>
<Doc:Amt Ccy="USD">12.43</Doc:Amt>
......
package com.brilliance.mt2mx.pacs008001;
import com.alibaba.fastjson.JSONObject;
import com.brilliance.swift.SwiftTransfer;
import com.brilliance.swift.util.DateUtil;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.util.XmlUtil;
import com.brilliance.swift.vo.common.CcyFormatAmount;
import com.brilliance.swift.vo.common.ChargeInfo;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
static String appHdr = "Envelope";
static String grpHdr = "Envelope.Document.FIToFICstmrCdtTrf";
static String bodyHdr = "Envelope.Document.FIToFICstmrCdtTrf";
static String defaultFormate = "yyyy-MM-dd HH:mm:ss,SSS";
public static Object getXmlPathValue(Document document, String xmlPath) throws DatatypeConfigurationException, IOException {
xmlPath = xmlPath.replace("#APP", appHdr).replace("#GRP", grpHdr).replace("#BODY", bodyHdr);
Object value = null;
if (xmlPath.indexOf("*") > -1) { //出现多个值
String beginPath = xmlPath.substring(0, xmlPath.indexOf("*"));
String endPath = xmlPath.substring(xmlPath.indexOf("*")+1);
String type = "String";
if (endPath.indexOf("&") > -1) {
type = endPath.substring(endPath.indexOf("&")+1);
endPath = endPath.substring(0, endPath.indexOf("&"));
}
int count = XmlUtil.getChildrenCount(document, beginPath, null);
if (count > 0) {
if ("String".equalsIgnoreCase(type)) {
List<String> list = new ArrayList<>();
for (int i=0; i<count; i++) {
String xmlValue = XmlUtil.getXmlNodeValue(document, beginPath+"("+i+")."+endPath);
list.add(xmlValue);
}
value = list;
} else if ("BigDecimal".equalsIgnoreCase(type)) {
List<BigDecimal> list = new ArrayList<>();
for (int i=0; i<count; i++) {
String xmlValue = XmlUtil.getXmlNodeValue(document, beginPath+"("+i+")."+endPath);
list.add(new BigDecimal(xmlValue));
}
value = list;
}
}
} else if (xmlPath.indexOf("&") > -1) { //数据类型
String type = xmlPath.substring(xmlPath.indexOf("&")+1);
xmlPath = xmlPath.substring(0, xmlPath.indexOf("&"));
String xmlValue = XmlUtil.getXmlNodeValue(document, xmlPath);
if ("DATE".equalsIgnoreCase(type) && StringUtil.isNotEmpty(xmlValue)) {
XMLGregorianCalendar dtTm = DatatypeFactory.newInstance().newXMLGregorianCalendar(xmlValue);
value = DateUtil.format(dtTm.toGregorianCalendar().getTime(), defaultFormate);
} else if ("BigDecimal".equalsIgnoreCase(type) && StringUtil.isNotEmpty(xmlValue)) {
value = new BigDecimal(xmlValue);
} else if ("List<String>".equalsIgnoreCase(type) && StringUtil.isNotEmpty(xmlValue)) {
int count = XmlUtil.getChildrenCount(document, xmlPath, null);
if (count > 0) {
List<String> list = new ArrayList<>();
for (int i=0; i<count; i++) {
xmlValue = XmlUtil.getXmlNodeValue(document, xmlPath+"("+i+")");
list.add(xmlValue);
}
value = list;
}
}
} else {
value = XmlUtil.getXmlNodeValue(document, xmlPath);
}
return value;
}
public static void main(String[] args) throws Exception {
String mt103 = SwiftTransfer.mt2Mx(
/*String mt103 = SwiftTransfer.mt2Mx(
new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftTxt\\Mt103.txt"),
"d:/test/pacs00800109.xml", null);
System.out.println(mt103);
System.out.println(mt103);*/
String xmlStr = FileUtils.readFileToString(new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs00800109.xml"));
Document document = DocumentHelper.parseText(xmlStr);
Map<String, Object> maps = new HashMap<>();
Map<String, Object> swiftHeader = new HashMap<>();
swiftHeader.put("messageType", "pacs.008.001");
swiftHeader.put("version", "09");
maps.put("swiftHeader", swiftHeader);
File propertyFile = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\template\\mx2swiftdto\\pacs008001.Properties");
List<String> properties = FileUtils.readLines(propertyFile);
if (properties.size() > 0) {
for (String property : properties) {
if (property.startsWith("#")) {
continue;//过滤注释
}
String[] strArr = property.split("=");
if (strArr.length != 2) {
continue;//过滤掉不符合规则的配置
}
String key = strArr[0];
String xmlPath = strArr[1];
Object xmlValue = getXmlPathValue(document, xmlPath);
if (xmlValue == null) continue;
String[] keys = key.split("\\.");
if (keys.length == 1) {
maps.put(keys[0], xmlValue); //如果只有一个节点,直接put值
} else { //如果节点值超过1个,先赋值第一个节点,再循环赋值,最后一个节点直接put值。
Map<String, Object> keyMap = null;
if (maps.containsKey(keys[0])) {
keyMap = (Map<String, Object>)maps.get(keys[0]);
} else {
keyMap = new HashMap<>();
maps.put(keys[0], keyMap);
}
for (int i=1; i<keys.length; i++) {
if (i == keys.length-1) {
keyMap.put(keys[i], xmlValue);
break;
}
if (!keyMap.containsKey(keys[i])) {
keyMap.put(keys[i], new HashMap<>());
}
keyMap = (Map<String, Object>)keyMap.get(keys[i]);
}
}
}
}
List<ChargeInfo> chargeInfos = new ArrayList<>();
CcyFormatAmount ccyFormatAmount = new CcyFormatAmount();
ccyFormatAmount.setCcy("USD");
ccyFormatAmount.setAmt(new BigDecimal("13.45"));
ChargeInfo chargeInfo = new ChargeInfo();
chargeInfo.setAmt(ccyFormatAmount);
CcyFormatAmount ccyFormatAmount1 = new CcyFormatAmount();
ccyFormatAmount1.setCcy("USD");
ccyFormatAmount1.setAmt(new BigDecimal("23.56"));
ChargeInfo chargeInfo1 = new ChargeInfo();
chargeInfo1.setAmt(ccyFormatAmount1);
chargeInfos.add(chargeInfo);
chargeInfos.add(chargeInfo1);
maps.put("chargeInfos", chargeInfos);
/*Map<String, Object> resultMap = new HashMap<>();
Map<String, Object> txnRef = new HashMap<>();
txnRef.put("instructionId", "GTBEXO12345");
txnRef.put("endToEndId", "GTBEXO12345");
txnRef.put("uetr", "8a562c67-ca16-48ba-b074-65581be6f001");
resultMap.put("txnRef", txnRef);
Map<String, Object> swiftHeader = new HashMap<>();
swiftHeader.put("messageType", "pacs.008.001");
swiftHeader.put("version", "09");
resultMap.put("swiftHeader", swiftHeader);*/
//SwiftDto swiftDto = SwiftDto.fromJson(new JSONObject(maps).toJSONString());
System.out.println(new JSONObject(maps).toJSONString());
String mt = SwiftTransfer.swiftDto2Mt(new JSONObject(maps).toJSONString(), null, null);
System.out.println(mt);
}
}
......@@ -10,6 +10,7 @@ public class Test {
public static void main(String[] args) {
SwiftDto swiftDto = SwiftTransfer.mx2SwiftDto(new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftXml\\MxPacs00800109.xml"), "d:/test/CustomerCreditTransfer.json", null);
System.out.println(swiftDto);
String mt = SwiftTransfer.swiftDto2Mt(swiftDto.toJson(), null, null);
System.out.println(mt);
}
}
......@@ -8,7 +8,7 @@ public class Test {
public static void main(String[] args) {
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftJson\\CustomerCreditTransfer.json");
String mtStr = SwiftTransfer.swiftDto2Mt(file, null, "I");
String mtStr = SwiftTransfer.swiftDto2Mt(file, null, null);
System.out.println(mtStr);
}
}
......@@ -8,13 +8,13 @@ public class Test {
public static void test202() {
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftJson\\FinancialInstitutionCreditTransfer.json");
String mtStr = SwiftTransfer.swiftDto2Mt(file, null, "O");
String mtStr = SwiftTransfer.swiftDto2Mt(file, null, null);
System.out.println(mtStr);
}
public static void test202Cov() {
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftJson\\FinancialInstitutionCreditTransfer_COV.json");
String mtStr = SwiftTransfer.swiftDto2Mt(file, null, "O");
String mtStr = SwiftTransfer.swiftDto2Mt(file, null, null);
System.out.println(mtStr);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment