Commit f9e08bdc by WeiCong

完成swift报文的组报功能

parent 599cf024
...@@ -27,6 +27,10 @@ public class MessageFormat { ...@@ -27,6 +27,10 @@ public class MessageFormat {
if (swfmsg == null && mtType.charAt(1) == '9') if (swfmsg == null && mtType.charAt(1) == '9')
swfmsg = mtCache.get('n' + mtType.substring(1)); swfmsg = mtCache.get('n' + mtType.substring(1));
if (swfmsg == null) {
mtType = mtType.toUpperCase();
swfmsg = mtCache.get(mtType);
}
if (swfmsg != null) { if (swfmsg != null) {
swfmsg.setMtType(mtType); swfmsg.setMtType(mtType);
return swfmsg.clone(); return swfmsg.clone();
......
...@@ -6,6 +6,10 @@ import com.brilliace.swifteditor.tag.TagLine; ...@@ -6,6 +6,10 @@ import com.brilliace.swifteditor.tag.TagLine;
import com.brilliace.swifteditor.util.Assert; import com.brilliace.swifteditor.util.Assert;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.*; import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -17,6 +21,9 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -17,6 +21,9 @@ public class SWFMessage extends AbstractMessageArea {
private Map<Integer, TagLine> flatModel = new HashMap<>(); private Map<Integer, TagLine> flatModel = new HashMap<>();
private Map<String, Integer> tag2No = new HashMap<>(); private Map<String, Integer> tag2No = new HashMap<>();
private Map<String, String> bscInfo; private Map<String, String> bscInfo;
private volatile String body;
private String sndbic;
private String rcvbic;
protected SWFMessage() { protected SWFMessage() {
} }
...@@ -81,6 +88,14 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -81,6 +88,14 @@ public class SWFMessage extends AbstractMessageArea {
sb.append("]"); sb.append("]");
} }
public void setSndbic(String sndbic) {
this.sndbic = sndbic;
}
public void setRcvbic(String rcvbic) {
this.rcvbic = rcvbic;
}
public Map<Integer, String> getHeaders() { public Map<Integer, String> getHeaders() {
return headers; return headers;
} }
...@@ -231,7 +246,7 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -231,7 +246,7 @@ public class SWFMessage extends AbstractMessageArea {
throw new IllegalArgumentException("域" + tagLine.getName() + "需要填充List集合类型的数据"); throw new IllegalArgumentException("域" + tagLine.getName() + "需要填充List集合类型的数据");
} }
} }
setGenericRepetitive((AbstractMessageArea) tagLine.parent, index, tagLine.getTno(), (List<Object>) val, 0); setGenericRepetitive((AbstractMessageArea) tagLine.parent, index, tagLine.getTno(), (List<Object>) val, 0, tagName);
} else { } else {
tagLine.setValue((String) val); tagLine.setValue((String) val);
} }
...@@ -241,7 +256,14 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -241,7 +256,14 @@ public class SWFMessage extends AbstractMessageArea {
int part = Integer.parseInt(path.substring(index + 1)) + 1; int part = Integer.parseInt(path.substring(index + 1)) + 1;
Assert.state(part > 0, "索引[" + path + "]指定的域块不存在"); Assert.state(part > 0, "索引[" + path + "]指定的域块不存在");
if (tagLine.parent != null) { if (tagLine.parent != null) {
setGenericRepetitive((AbstractMessageArea) tagLine.parent, part, tagLine.getTno(), (List<Object>) val, 0); if (val instanceof String && tagLine.onlyOne) {
if (tagLine.onlyOne) {
val = Collections.singletonList(val);
} else {
throw new IllegalArgumentException("域" + tagLine.getName() + "需要填充List集合类型的数据");
}
}
setGenericRepetitive((AbstractMessageArea) tagLine.parent, part, tagLine.getTno(), (List<Object>) val, 0, tagName);
} else { } else {
for (TagCell cell : tagLine.cellList) { for (TagCell cell : tagLine.cellList) {
if (cell.cnt == part) { if (cell.cnt == part) {
...@@ -270,12 +292,13 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -270,12 +292,13 @@ public class SWFMessage extends AbstractMessageArea {
} }
private boolean setGenericRepetitive(AbstractMessageArea parent, int index, int ano, List<Object> rs, int cnt) { private boolean setGenericRepetitive(AbstractMessageArea parent, int index, int ano, List<Object> rs, int cnt, String tagName) {
if (parent.getType() == MessageArea.CYC || parent.getType() == MessageArea.SEQ) { if (parent.getType() == MessageArea.CYC || parent.getType() == MessageArea.SEQ) {
for (int i = 0; i < parent.getFields().size(); i++) { for (int i = 0; i < parent.getFields().size(); i++) {
MessageArea item = parent.getFields().get(i); MessageArea item = parent.getFields().get(i);
if (item.getType() == MessageArea.TAG) { if (item.getType() == MessageArea.TAG) {
if (((TagLine) item).getTno() == ano) { if (((TagLine) item).getTno() == ano) {
checkTagLine((TagLine) item, tagName);
if (index == -1) { if (index == -1) {
((TagLine) item).setValue((String) rs.get(cnt)); ((TagLine) item).setValue((String) rs.get(cnt));
} else { } else {
...@@ -293,8 +316,8 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -293,8 +316,8 @@ public class SWFMessage extends AbstractMessageArea {
continue; continue;
} }
if (parent.getType() == MessageArea.SEQ ? if (parent.getType() == MessageArea.SEQ ?
setGenericRepetitive((AbstractMessageArea) item, index, ano, (List) rs.get(cnt), 0) setGenericRepetitive((AbstractMessageArea) item, index, ano, (List) rs.get(cnt), 0, tagName)
: setGenericRepetitive((AbstractMessageArea) item, index, ano, rs, cnt)) { : setGenericRepetitive((AbstractMessageArea) item, index, ano, rs, cnt, tagName)) {
return true; return true;
} }
} }
...@@ -303,7 +326,7 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -303,7 +326,7 @@ public class SWFMessage extends AbstractMessageArea {
if (((AbstractMessageArea) parent).withIn(ano)) { if (((AbstractMessageArea) parent).withIn(ano)) {
MessageArea cp = ((AbstractMessageArea) parent).getFields().get(0);//默认有一个组项 MessageArea cp = ((AbstractMessageArea) parent).getFields().get(0);//默认有一个组项
do { do {
setGenericRepetitive((AbstractMessageArea) cp, index, ano, rs, cnt++); setGenericRepetitive((AbstractMessageArea) cp, index, ano, rs, cnt++, tagName);
if (rs.size() <= cnt) if (rs.size() <= cnt)
return true; return true;
try { try {
...@@ -380,7 +403,7 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -380,7 +403,7 @@ public class SWFMessage extends AbstractMessageArea {
rs = new LinkedList<Object>(); rs = new LinkedList<Object>();
getGenericRepetitive((AbstractMessageArea) tagLine.parent, index, tagLine.getTno(), (List<Object>) rs); getGenericRepetitive((AbstractMessageArea) tagLine.parent, index, tagLine.getTno(), (List<Object>) rs);
} else { } else {
rs = tagLine.getSourceValue(); rs = tagLine.getValueForHuman();
} }
} else { } else {
tagLine = this.flatModel.get(Integer.valueOf(path.substring(0, index))); tagLine = this.flatModel.get(Integer.valueOf(path.substring(0, index)));
...@@ -429,7 +452,7 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -429,7 +452,7 @@ public class SWFMessage extends AbstractMessageArea {
if (item.getType() == MessageArea.TAG) { if (item.getType() == MessageArea.TAG) {
if (((TagLine) item).getTno() == ano) { if (((TagLine) item).getTno() == ano) {
if (index == -1) { if (index == -1) {
rs.add(((TagLine) item).getSourceValue()); rs.add(((TagLine) item).getValueForHuman());
} else { } else {
for (TagCell cell : ((TagLine) item).cellList) { for (TagCell cell : ((TagLine) item).cellList) {
if (cell.cnt == index) { if (cell.cnt == index) {
...@@ -509,4 +532,110 @@ public class SWFMessage extends AbstractMessageArea { ...@@ -509,4 +532,110 @@ public class SWFMessage extends AbstractMessageArea {
} }
} }
/**
* 获取swift报文体
*/
public String getMsg() {
if (body != null) {
return body;
}
StringWriter sw = null;
SwtBufferedWriter sb = null;
try {
Assert.notNull(sndbic, "发报行不能为空");
Assert.notNull(rcvbic, "收报行不能为空");
sw = new StringWriter();
sb = this.new SwtBufferedWriter(sw);
sb.write("{1:F01");
sb.write(getLogicalTerminalAddress(sndbic, "1") + "0000000000");
sb.write("}");
sb.write("{2:I" + mtType.substring(0, 3));
sb.write(getLogicalTerminalAddress(rcvbic, "2"));
sb.write("N");
sb.write("}");
if(mtType.endsWith("COV")){
sb.write("{3:{119:COV}}");
}
sb.write("{4:");
sb.newLine();
printMsgBody(this, sb);
sb.write("-}");
sb.flush();
body = sw.toString();
} catch (Exception e) {
return "获取报文体异常:" + e.getMessage();
} finally {
try {
if (sb != null)
sb.close();
} catch (IOException ioe) {
}
}
return body;
}
private String getLogicalTerminalAddress(String bic, String field) {
if (bic == null) {
return null;
}
if ((bic.length() == 8) || (bic.length() == 11) || (bic.length() == 12)) {
if (bic.length() == 11) {
if ("1".equals(field)) {
return bic.substring(0, 8) + "A" + bic.substring(8, 11);
}
return bic.substring(0, 8) + "X" + bic.substring(8, 11);
}
if (bic.length() == 8) {
if ("1".equals(field)) {
return bic.substring(0, 8) + "AXXX";
}
return bic.substring(0, 8) + "XXXX";
}
return bic;
}
throw new IllegalStateException("第"+field+"部分的bic码["+bic+"]不符合规范,需满足8、11或12位长度");
}
private void printMsgBody(AbstractMessageArea ma, SwtBufferedWriter sb) throws IOException {
for (MessageArea sma : ma.getFields()) {
if (sma.getType() == TAG) {
sb.write((TagLine) sma);
} else {
for (MessageArea ssma : ((AbstractMessageArea) sma).getFields()) {
AbstractMessageArea temp = (AbstractMessageArea) ssma;
printMsgBody(temp, sb);
}
}
}
}
class SwtBufferedWriter extends BufferedWriter {
public static final String NEW_LINE = "\r\n";
public SwtBufferedWriter(Writer arg0) {
super(arg0);
}
public void newLine() throws IOException {
write(NEW_LINE);
}
public void write(TagLine tagLine) throws IOException {
checkNullForNeedField(tagLine);
String val = tagLine.getValue();
if (!val.equals("")) {
write(":" + tagLine.getName() + ":");
write(val);
write(NEW_LINE);
}
}
private void checkNullForNeedField(TagLine tagLine) {
boolean flag = "M".equals(tagLine.getStatus()) && "".equals(tagLine.getValue()) ? true : false;
Assert.state(!flag, "域[" + tagLine.getName() + "]是必输项,不能为空");
}
}
} }
{1:F01NYCBCNSHAXXX0000000000}{2:I103BKCHHKHHAXXXN}{4:
:20:NYCBCNSH1
:23B:CRED
:32A:180608USD80020,12
:33B:USD80070,12
:36:5,11
:50K:/110000000
FOSHAN HUAXI IRON PRINTED CAN CO.,L
TD
HEJIAO INDUSTRY AREA NANZHUANG CHAN
CHENG FOSHAN CHINA
:52A:NYCBCNSH2
:53D:STRIX LIMITED
:54A:WYCBCNSH
:55D:HU LU WA
:56A:/8655221555
MdlBnkBIC
:57A:/6659823266333
AcctBnkBIC
:59:/22000000
STRIX LIMITED
FORREST HOUSE RONALDSWAY BALLASALLA
ISLE OF MAN UK
:70:RmtInfo
:71A:OUR
:71G:USD50,
:72:PstcrptDscInfo
-}
\ No newline at end of file
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