Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PaymentComponents/ded189c834a093c722b2965724033a3e to your computer and use it in GitHub Desktop.
Save PaymentComponents/ded189c834a093c722b2965724033a3e to your computer and use it in GitHub Desktop.
ParseAndValidateBahtnetMessage
import gr.datamation.iso20022.bahtnet.BahtnetMessage;
import gr.datamation.mx.message.head.BusinessApplicationHeader02;
import gr.datamation.mx.message.pacs.FinancialInstitutionCreditTransfer08;
import gr.datamation.validation.error.ValidationError;
import gr.datamation.validation.error.ValidationErrorList;
import java.io.ByteArrayInputStream;
public class ParseAndValidateBahtnetMessage {
public static void main(String... args) {
execute();
}
public static void execute() {
// constructBahtnetMessage();
// parseAndValidateBahtnetMessage();
autoParseAndValidateBahtnetMessage();
}
public static void constructBahtnetMessage() {
try {
//Initialize the header object
BusinessApplicationHeader02 businessApplicationHeader = new BusinessApplicationHeader02();
businessApplicationHeader.parseXML(validBahtnetPacs009HeaderString);
//Initialize the document object
FinancialInstitutionCreditTransfer08 financialInstitutionCreditTransfer08 = new FinancialInstitutionCreditTransfer08();
financialInstitutionCreditTransfer08.parseXML(validBahtnetPacs009DocumentString);
//We fill the elements of the message object using setters
//financialInstitutionCreditTransfer08.getMessage().setGrpHdr(new GroupHeader93());
//financialInstitutionCreditTransfer08.getMessage().getGrpHdr().setMsgId("1234");
//or setElement()
//financialInstitutionCreditTransfer08.setElement("GrpHdr/MsgId", "1234");
//Construct the Bahtnet message object using two separate objects, header, document
BahtnetMessage<BusinessApplicationHeader02, FinancialInstitutionCreditTransfer08> bahtnetMessage = new BahtnetMessage<>(businessApplicationHeader, financialInstitutionCreditTransfer08);
//Perform validation in both header and message object using bahtnetMessage
//Use BahtnetMessage.BahtnetMsgType enumeration object to select the matching schema (check the table of supported Bahtnet messages below
//bahtnetMessage.extractBahtnetMsgType() can also be used
ValidationErrorList validationErrorList = bahtnetMessage.validate(BahtnetMessage.BahtnetMsgType.PACS_009_CORE);
if (validationErrorList.isEmpty()) {
System.out.println("Message is valid");
System.out.println(bahtnetMessage.convertToXML()); //Get the generated xmls for head and document
} else {
handleValidationError(validationErrorList);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
public static void parseAndValidateBahtnetMessage() {
try {
//Initialize the bahtnetMessage
BahtnetMessage<BusinessApplicationHeader02, FinancialInstitutionCreditTransfer08> bahtnetMessage = new BahtnetMessage<>(new BusinessApplicationHeader02(), new FinancialInstitutionCreditTransfer08());
//Fill the bahtnetMessage with data from xml and validate Bahtnet against the xml schema
ValidationErrorList validationErrorList = bahtnetMessage.autoParseAndValidateXml(new ByteArrayInputStream(validBahtnetPacs009String.getBytes()));
if (!validationErrorList.isEmpty()) {
handleValidationError(validationErrorList);
return;
}
//Perform validation in both header and message object using bahtnetMessage
//Use BahtnetMessage.BahtnetMsgType enumeration object to select the matching schema (check the table of supported Bahtnet messages below
//BahtnetMessage.extractBahtnetMsgType() can also be used
validationErrorList.addAll(bahtnetMessage.validate(BahtnetMessage.BahtnetMsgType.PACS_009_CORE));
if (validationErrorList.isEmpty()) {
System.out.println("Message is valid");
System.out.println(bahtnetMessage.convertToXML()); //Get the generated xmls for head and document
} else {
handleValidationError(validationErrorList);
}
//Extract the header and the core message from bahtnetMessage object
BusinessApplicationHeader02 businessApplicationHeader = bahtnetMessage.getAppHdr();
FinancialInstitutionCreditTransfer08 financialInstitutionCreditTransfer08 = bahtnetMessage.getDocument();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
public static void autoParseAndValidateBahtnetMessage() {
try {
//Initialize the bahtnetMessage
BahtnetMessage<BusinessApplicationHeader02, FinancialInstitutionCreditTransfer08> bahtnetMessage = new BahtnetMessage<>();
//Fill the bahtnetMessage with data from xml and validate Bahtnet against the xml schema
ValidationErrorList validationErrorList = bahtnetMessage.autoParseAndValidateXml(new ByteArrayInputStream(validBahtnetPacs009String.getBytes()));
if (!validationErrorList.isEmpty()) {
handleValidationError(validationErrorList);
return;
}
//Perform validation in both header and message object using bahtnetMessage
validationErrorList.addAll(bahtnetMessage.autoValidate());
if (validationErrorList.isEmpty()) {
System.out.println("Message is valid");
System.out.println(bahtnetMessage.convertToXML()); //Get the generated xmls for head and document
} else {
handleValidationError(validationErrorList);
}
//Extract the header and the core message from bahtnetMessage object
BusinessApplicationHeader02 businessApplicationHeader = bahtnetMessage.getAppHdr();
FinancialInstitutionCreditTransfer08 financialInstitutionCreditTransfer08 = bahtnetMessage.getDocument();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
private static void handleValidationError(ValidationErrorList validationErrorList) {
System.err.println("Message is invalid, and the errors are the following:");
for (ValidationError error : validationErrorList) {
System.err.println(error.toString());
System.err.println(
"Error Code: " + error.getErrorCode() + "\n" +
"Error Description: " + error.getDescription() + "\n" +
"Line number in error inside the tag: " + error.getLine() + "\n"
);
}
}
private static final String validBahtnetPacs009String = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<AppHdr xmlns=\"urn:iso:std:iso:20022:tech:xsd:head.001.001.02\">\n" +
" <Fr>\n" +
" <OrgId>\n" +
" <Id>\n" +
" <OrgId>\n" +
" <AnyBIC>BBBBUS33</AnyBIC>\n" +
" </OrgId>\n" +
" </Id>\n" +
" </OrgId>\n" +
" </Fr>\n" +
" <To>\n" +
" <OrgId>\n" +
" <Id>\n" +
" <OrgId>\n" +
" <AnyBIC>AAAAGB2L</AnyBIC>\n" +
" </OrgId>\n" +
" </Id>\n" +
" </OrgId>\n" +
" </To>\n" +
" <BizMsgIdr>BBBB/120928-CCT/JPY/123</BizMsgIdr>\n" +
" <MsgDefIdr>pacs.009.001.08</MsgDefIdr>\n" +
" <BizSvc>bot.bahtnet.01</BizSvc>\n" +
" <CreDt>2008-09-29T04:49:45+03:00</CreDt>\n" +
"</AppHdr>\n" +
"\n" +
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pacs.009.001.08\">\n" +
" <FICdtTrf>\n" +
" <GrpHdr>\n" +
" <MsgId>BBBB/120928-FICT/JPY/430</MsgId>\n" +
" <CreDtTm>2012-09-28T16:00:00</CreDtTm>\n" +
" <NbOfTxs>1</NbOfTxs>\n" +
" <SttlmInf>\n" +
" <SttlmMtd>CLRG</SttlmMtd>\n" +
" <ClrSys>\n" +
" <Cd>SEU</Cd>\n" +
" </ClrSys>\n" +
" </SttlmInf>\n" +
" </GrpHdr>\n" +
" <CdtTrfTxInf>\n" +
" <PmtId>\n" +
" <InstrId>BBBB/120928-FICT/JPY/430/1</InstrId>\n" +
" <EndToEndId>ABC/4562/2012-09-08</EndToEndId>\n" +
" <TxId>BBBB/120928-CCT/123/1</TxId>\n" +
" <UETR>00000000-0000-4000-8000-000000000000</UETR>\n" +
" </PmtId>\n" +
" <PmtTpInf>\n" +
" <CtgyPurp>\n" +
" <Prtry>CONS</Prtry>\n" +
" </CtgyPurp>\n" +
" </PmtTpInf>\n" +
" <IntrBkSttlmAmt Ccy=\"THB\">10000000</IntrBkSttlmAmt>\n" +
" <IntrBkSttlmDt>2012-09-29</IntrBkSttlmDt>\n" +
" <SttlmPrty>URGT</SttlmPrty>\n" +
" <InstgAgt>\n" +
" <FinInstnId>\n" +
" <BICFI>ABABUS23</BICFI>\n" +
" </FinInstnId>\n" +
" </InstgAgt>\n" +
" <Dbtr>\n" +
" <FinInstnId>\n" +
" <BICFI>BBBBUS33</BICFI>\n" +
" </FinInstnId>\n" +
" </Dbtr>\n" +
" <CdtrAgt>\n" +
" <FinInstnId>\n" +
" <BICFI>AAAAJPJT</BICFI>\n" +
" </FinInstnId>\n" +
" </CdtrAgt>\n" +
" <Cdtr>\n" +
" <FinInstnId>\n" +
" <BICFI>AAAAGB2L</BICFI>\n" +
" </FinInstnId>\n" +
" </Cdtr>\n" +
" </CdtTrfTxInf>\n" +
" </FICdtTrf>\n" +
"</Document>";
private static final String validBahtnetPacs009HeaderString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<AppHdr xmlns=\"urn:iso:std:iso:20022:tech:xsd:head.001.001.02\">\n" +
" <Fr>\n" +
" <OrgId>\n" +
" <Id>\n" +
" <OrgId>\n" +
" <AnyBIC>BBBBUS33</AnyBIC>\n" +
" </OrgId>\n" +
" </Id>\n" +
" </OrgId>\n" +
" </Fr>\n" +
" <To>\n" +
" <OrgId>\n" +
" <Id>\n" +
" <OrgId>\n" +
" <AnyBIC>AAAAGB2L</AnyBIC>\n" +
" </OrgId>\n" +
" </Id>\n" +
" </OrgId>\n" +
" </To>\n" +
" <BizMsgIdr>BBBB/120928-CCT/JPY/123</BizMsgIdr>\n" +
" <MsgDefIdr>pacs.009.001.08</MsgDefIdr>\n" +
" <BizSvc>bot.bahtnet.01</BizSvc>\n" +
" <CreDt>2008-09-29T04:49:45+03:00</CreDt>\n" +
"</AppHdr>";
private static final String validBahtnetPacs009DocumentString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pacs.009.001.08\">\n" +
" <FICdtTrf>\n" +
" <GrpHdr>\n" +
" <MsgId>BBBB/120928-FICT/JPY/430</MsgId>\n" +
" <CreDtTm>2012-09-28T16:00:00</CreDtTm>\n" +
" <NbOfTxs>1</NbOfTxs>\n" +
" <SttlmInf>\n" +
" <SttlmMtd>CLRG</SttlmMtd>\n" +
" <ClrSys>\n" +
" <Cd>SEU</Cd>\n" +
" </ClrSys>\n" +
" </SttlmInf>\n" +
" </GrpHdr>\n" +
" <CdtTrfTxInf>\n" +
" <PmtId>\n" +
" <InstrId>BBBB/120928-FICT/JPY/430/1</InstrId>\n" +
" <EndToEndId>ABC/4562/2012-09-08</EndToEndId>\n" +
" <TxId>BBBB/120928-CCT/123/1</TxId>\n" +
" <UETR>00000000-0000-4000-8000-000000000000</UETR>\n" +
" </PmtId>\n" +
" <PmtTpInf>\n" +
" <CtgyPurp>\n" +
" <Prtry>CONS</Prtry>\n" +
" </CtgyPurp>\n" +
" </PmtTpInf>\n" +
" <IntrBkSttlmAmt Ccy=\"THB\">10000000</IntrBkSttlmAmt>\n" +
" <IntrBkSttlmDt>2012-09-29</IntrBkSttlmDt>\n" +
" <SttlmPrty>URGT</SttlmPrty>\n" +
" <InstgAgt>\n" +
" <FinInstnId>\n" +
" <BICFI>ABABUS23</BICFI>\n" +
" </FinInstnId>\n" +
" </InstgAgt>\n" +
" <Dbtr>\n" +
" <FinInstnId>\n" +
" <BICFI>BBBBUS33</BICFI>\n" +
" </FinInstnId>\n" +
" </Dbtr>\n" +
" <CdtrAgt>\n" +
" <FinInstnId>\n" +
" <BICFI>AAAAJPJT</BICFI>\n" +
" </FinInstnId>\n" +
" </CdtrAgt>\n" +
" <Cdtr>\n" +
" <FinInstnId>\n" +
" <BICFI>AAAAGB2L</BICFI>\n" +
" </FinInstnId>\n" +
" </Cdtr>\n" +
" </CdtTrfTxInf>\n" +
" </FICdtTrf>\n" +
"</Document>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment