Commit c316053e by chengzhuoshen

增加VoCustomerCreditTransfer gson相关代码,支持gson转xml

parent 71aea669
......@@ -27,6 +27,7 @@ public class Mt2MxPacs00801Creator extends AbstractMt2MxCreator {
customerCreditTransfer.setCreateDate(swiftDto.getCreateDate());
customerCreditTransfer.setMessagePriority(swiftDto.getMessagePriority());
customerCreditTransfer.setUetr(swiftDto.getUetr());
customerCreditTransfer.setMessageType("pacs.008.001");
context.set(customerCreditTransfer);
} catch (Exception e) {
throw new SwiftException("ERROR", e.getMessage());
......
......@@ -8,7 +8,6 @@ import com.prowidesoftware.swift.model.field.Field23E;
import com.prowidesoftware.swift.model.mt.AbstractMT;
import com.prowidesoftware.swift.model.mt1xx.MT103;
import java.util.ArrayList;
import java.util.List;
public class Pacs00801Parse23EField extends AbstractMt2MxParseFields {
......@@ -23,7 +22,7 @@ public class Pacs00801Parse23EField extends AbstractMt2MxParseFields {
Field23E field23E = field23EList.get(i);
InstructionCode instructionCode = new InstructionCode();
instructionCode.setCode(field23E.getCode());
instructionCode.setCode(field23E.getAdditionalInformation());
instructionCode.setAdditionalInformation(field23E.getAdditionalInformation());
customerCreditTransfer.addInstructionCode(instructionCode);
}
}
......
......@@ -3,9 +3,23 @@ package com.brilliance.swift.mx;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.mx.pacs00801.SwiftDto2MxPacs008001Creator;
import com.brilliance.swift.vo.SwiftDto;
import org.apache.commons.io.FileUtils;
import java.io.File;
public class SwiftDto2MxCreatorManager {
public String swiftDto2Mx(File file, String fileOutputPath) throws Exception {
String gsonStr = FileUtils.readFileToString(file);
SwiftDto swiftDto = SwiftDto.fromJson(gsonStr);
return swiftDto2Mx(swiftDto, fileOutputPath);
}
public String swiftDto2Mx(String gsonStr, String fileOutputPath) {
SwiftDto swiftDto = SwiftDto.fromJson(gsonStr);
return swiftDto2Mx(swiftDto, fileOutputPath);
}
public String swiftDto2Mx(SwiftDto swiftDto, String fileOutputPath) throws SwiftException {
try {
SwiftDto2MxContext context = new SwiftDto2MxContext();
......
package com.brilliance.swift.vo;
import com.brilliance.swift.util.DateUtil;
import com.google.gson.*;
import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.Date;
public class DateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS";
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return DateUtil.parseDate(json.getAsString(), DATE_FORMAT);
} catch (ParseException e) {
throw new JsonParseException(e.getMessage());
}
}
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
String dateStr = DateUtil.format(src, DATE_FORMAT);
return new JsonPrimitive(dateStr);
}
}
package com.brilliance.swift.vo;
import com.brilliance.swift.vo.common.MessagePriority;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Date;
......@@ -75,4 +77,22 @@ public class SwiftDto {
public void setUetr(String uetr) {
this.uetr = uetr;
}
public String toJson() {
final Gson gson = new GsonBuilder()
.registerTypeAdapter(SwiftDto.class, new SwiftDtoAdapter())
.registerTypeAdapter(Date.class, new DateAdapter())
.setPrettyPrinting()
.create();
return gson.toJson(this, this.getClass());
}
public static SwiftDto fromJson(String json) {
final Gson gson = new GsonBuilder()
.registerTypeAdapter(SwiftDto.class, new SwiftDtoAdapter())
.registerTypeAdapter(Date.class, new DateAdapter())
.create();
return gson.fromJson(json, SwiftDto.class);
}
}
package com.brilliance.swift.vo;
import com.google.gson.*;
import java.lang.reflect.Type;
public class SwiftDtoAdapter implements JsonSerializer<SwiftDto>, JsonDeserializer<SwiftDto> {
private static final String MESSAGETYPE = "messageType";
@Override
public SwiftDto deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
JsonPrimitive prim = (JsonPrimitive) jsonObject.get(MESSAGETYPE);
if (prim == null) {
throw new JsonParseException("Missing " + MESSAGETYPE + " in JSON structure");
}
Class<?> klass = null;
try {
String className = "";
if ("pacs.008.001".equals(prim.getAsString())) {
className = "com.brilliance.swift.vo.VoCustomerCreditTransfer";
}
klass = Class.forName(className);
} catch (ClassNotFoundException e) {
throw new JsonParseException("Cannot find MX implementation for " + e.getMessage());
}
return context.deserialize(json, klass);
}
@Override
public JsonElement serialize(SwiftDto src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject object = context.serialize(src).getAsJsonObject();
return object;
}
}
......@@ -191,9 +191,4 @@ public class VoCustomerCreditTransfer extends SwiftDto {
getRegulatoryReportingList().add(regulatoryReporting);
return this;
}
@Override
public String getMessageType() {
return "pacs.008.001";
}
}
{
"debitDate": "2022-04-21 22:24:00,000",
"creditDate": "2022-04-22 00:30:00,000",
"clsDate": "2022-04-21 23:21:00,000",
"bussinessCode": "SDVA",
"instructionCodes": [
{
"code": "SDVA"
},
{
"code": "BNKK"
},
{
"code": "GHJK",
"additionalInformation": "789556"
}
],
"txnTypeCode": "GCY",
"settledDate": "2022-03-13 00:00:00,000",
"settledCcyFormatAmount": {
"ccy": "USD",
"amt": 23453.13
},
"InstructedCcyFormatAmount": {
"ccy": "EUR",
"amt": 1345.12
},
"exchangeRate": 17.4357135422,
"partyDtos": [
{
"partyType": "OC",
"partyBic": "GCYXXXXXX12",
"partyAcct": "01111001759234567890",
"addressList": []
},
{
"partyType": "OI",
"partyBic": "FOOBARC0",
"addressList": []
},
{
"partyType": "SC",
"partyBic": "FOOBARC1",
"addressList": []
},
{
"partyType": "RC",
"partyBic": "FOOBARC3",
"addressList": []
},
{
"partyType": "RI",
"partyBic": "FOOBARC4",
"addressList": []
},
{
"partyType": "II",
"partyBic": "FOOBARC6",
"addressList": []
},
{
"partyType": "AI",
"partyBic": "BANKANC7",
"addressList": []
},
{
"partyType": "BC",
"partyName": "TEST CORP",
"partyIBANAcct": "00013500510020179998",
"addressList": [
"Nellis ABC, NV"
]
}
],
"remittanceInfos": [
"gechengyang"
],
"chargeFor": "DEBT",
"chargeCcyFormatAmount": {
"ccy": "USD",
"amt": 12.43
},
"senderToReceiverInformations": [
{
"type": "PIA",
"InstructionInformation": "FOOBARC7"
},
{
"type": "IFCA",
"InstructionInformation": "789556"
}
],
"regulatoryReportings": [
"/ORDERRES/BE//MEILAAN 1, 9000 GENT",
"//JIANGXIAQU",
"//WENHUADADAO"
],
"messageType": "pacs.008.001",
"senderBic": "BANKANC0",
"receiverBic": "FOOBARC0",
"createDate": "2022-04-21 17:41:30,705",
"messagePriority": "NORM",
"messageId": "TBEXO12345",
"uetr": "8a562c67-ca16-48ba-b074-65581be6f001"
}
\ No newline at end of file
package com.brilliance.mt2mx.pacs008001;
import com.brilliance.swift.mt2mx.Mt2MxCreateManager;
import org.apache.commons.io.FileUtils;
import java.io.File;
......@@ -9,7 +8,6 @@ public class TestPacs008001 {
public static void main(String[] args) throws Exception {
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftTxt\\MT103.txt");
String mtStr = FileUtils.readFileToString(file);
String xmlStr = new Mt2MxCreateManager().mt2mx(file, "d:/test/pacs008.xml", null);
System.out.println(xmlStr);
}
......
package com.brilliance.mx.pacs008001;
import com.brilliance.swift.mx.SwiftDto2MxCreatorManager;
import java.io.File;
public class TestPacs008001_GSON {
public static void main(String[] args) throws Exception {
File file = new File(System.getProperty("user.dir")+"\\swiftCore\\src\\main\\resources\\swiftJson\\customercredittransfer.txt");
String xmlStr = new SwiftDto2MxCreatorManager().swiftDto2Mx(file, "d:/test/pacs008_json.xml");
System.out.println(xmlStr);
}
}
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