Commit 2c15fa67 by chengzhuoshen

增加mx报文转要素的功能

parent fc76d9fb
package com.brilliance.swift.mx2element;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.brilliance.swift.exception.SwiftException;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public abstract class AbstractMx2ElementCreator implements Mx2ElementCreator{
protected AbstractMX abstractMX;
protected String xmlStr;
protected Map<String, Object> maps = new LinkedHashMap<>();
public AbstractMX getAbstractMX() {
return abstractMX;
}
public void setAbstractMX(AbstractMX abstractMX) {
this.abstractMX = abstractMX;
}
public String getXmlStr() {
return xmlStr;
}
public void setXmlStr(String xmlStr) {
this.xmlStr = xmlStr;
}
@Override
public Map<String, Object> buildElement() {
try {
//AbstractMX abstractMX = AbstractMX.parse(xmlStr);
String messageType = abstractMX.getMxId().getBusinessProcess().name()
+ abstractMX.getMxId().getFunctionality()
+ abstractMX.getMxId().getVariant()
+ abstractMX.getMxId().getVersion();
maps.put("messageType", messageType);
String gsonStr = abstractMX.toJson();
Map<String, Object> mxGsonMaps = JSON.parseObject(gsonStr);
String fileName = "/template/"+messageType+".properties";
/*InputStream resourceAsStream = AbstractMx2ElementCreator.class.getResourceAsStream(fileName);
Properties properties = new Properties();
properties.load(resourceAsStream);
Set<Object> keySet = properties.keySet();
for (Object keyObj : keySet) {
String key = (String) keyObj;
String path = properties.getProperty(key);
Object value = getXmlValue(mxGsonMaps, path);
if (value != null) {
maps.put(key, value);
}
}*/
URL url = AbstractMx2ElementCreator.class.getResource(fileName);
File propertyFile = new File(url.getPath());
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 path = strArr[1];
Object value = getXmlValue(mxGsonMaps, path);
if (value != null) {
maps.put(key, value);
}
}
}
} catch (IOException e) {
throw new SwiftException(e.getMessage());
}
return maps;
}
protected Object getXmlValue(Map<String, Object> jsonMaps, String path) {
Object value = null;
if (path.indexOf("?") > 0) {
String[] childPaths = path.split("\\?");
for (String childPath : childPaths) {
value = getXmlValue(jsonMaps, childPath);
if (value != null) {
return value;
}
}
}
//获取数据类型,&分隔,默认是string
String type = "string";
if (path.indexOf("&") > -1) {
type = path.substring(path.indexOf("&")+1);
path = path.substring(0, path.indexOf("&"));
}
//需要用. split,然后循环遍历json获取值
String[] paths = path.split("\\.");
Map<String, Object> tmpMaps = jsonMaps;
for (String p : paths) {
Object obj = tmpMaps.get(p);
if (obj == null) return null;
if (obj instanceof JSONArray) {
JSONArray jsonArray = (JSONArray)obj;
obj = jsonArray.get(0);
if (obj instanceof JSONObject) {
tmpMaps = (Map<String, Object>)obj;
} else {
return jsonArray;
}
} else if (obj instanceof JSONObject) {
tmpMaps = (Map<String, Object>)obj;
} else {
if ("bigdecimal".equals(type)) {
value = new BigDecimal(String.valueOf(obj));
} else if ("boolean".equals(type)) {
value = Boolean.valueOf(String.valueOf(obj));
} else {
value = String.valueOf(obj);
}
break;
}
}
return value;
}
}
package com.brilliance.swift.mx2element;
import java.util.Map;
public interface Mx2ElementCreator {
Map<String, Object> buildElement();
}
package com.brilliance.swift.mx2element;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.mx2element.pacs008001.Mx2ElementPacs008001Creator;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.prowidesoftware.swift.model.mx.AbstractMX;
import java.util.LinkedHashMap;
import java.util.Map;
public class Mx2ElementCreatorManager {
public String mx2Element(String xmlStr) throws SwiftException {
AbstractMX abstractMX = AbstractMX.parse(xmlStr);
String messageType = (abstractMX.getMxId().getBusinessProcess().name()
+ "."
+ abstractMX.getMxId().getFunctionality()
+ "."
+ abstractMX.getMxId().getVariant())
//+ abstractMX.getMxId().getVersion())
.trim();
AbstractMx2ElementCreator creator = getCreator(messageType);
creator.setAbstractMX(abstractMX);
creator.setXmlStr(xmlStr);
Map<String, Object> maps = creator.buildElement();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(maps, LinkedHashMap.class);
}
public AbstractMx2ElementCreator getCreator(String messageType) {
if ("pacs.008.001".equals(messageType)) {
return new Mx2ElementPacs008001Creator();
} else {
throw new SwiftException("Invalid message type");
}
}
}
package com.brilliance.swift.mx2element.pacs008001;
import com.brilliance.swift.mx2element.AbstractMx2ElementCreator;
import java.util.Map;
public class Mx2ElementPacs008001Creator extends AbstractMx2ElementCreator {
@Override
public Map<String, Object> buildElement() {
return super.buildElement();
}
}
frBic=appHdr.fr.fiId.finInstnId.bicfi?appHdr.fr.orgId.id.orgId.anyBIC
toBic=appHdr.to.fiId.finInstnId.bicfi?appHdr.to.orgId.id.orgId.anyBIC
bizMsgIdr=fiToFICstmrCdtTrf.cdtTrfTxInf.pmtId.instrId?fiToFICstmrCdtTrf.grpHdr.msgId?appHdr.bizMsgIdr
msgDefIdr=appHdr.msgDefIdr
bizSvc=appHdr.bizSvc
creDt=fiToFICstmrCdtTrf.grpHdr.creDtTm?appHdr.creDt
priority=appHdr.prty
dbtrAdrLines=fiToFICstmrCdtTrf.cdtTrfTxInf.dbtr.pstlAdr.adrLine
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