Created
July 18, 2023 15:36
-
-
Save PaymentComponents/bcb63721ba2a598a2312c88d2939dc8c to your computer and use it in GitHub Desktop.
ParseAndValidateCgiMpMessage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gr.datamation.iso20022.cgi.mp.pacs.CustomerCreditTransferInitiation09RelayServiceCgiMp; | |
import gr.datamation.validation.error.ValidationError; | |
import gr.datamation.validation.error.ValidationErrorList; | |
import java.io.ByteArrayInputStream; | |
public class ParseAndValidateCgiMpMessage { | |
public static void main(String... args) { | |
execute(); | |
} | |
public static void execute() { | |
constructCgiMpMessage(); | |
parseAndValidateCgiMpMessage(); | |
} | |
public static void constructCgiMpMessage() { | |
try { | |
//Initialize the message object | |
CustomerCreditTransferInitiation09RelayServiceCgiMp customerCreditTransfer = new CustomerCreditTransferInitiation09RelayServiceCgiMp(); | |
//Optionally use an existing message if we do not want to create the object from scratch | |
customerCreditTransfer.parseXML(validCgiMpPain001RelayServiceString); | |
//We fill the elements of the message object using setters | |
//customerCreditTransfer.getMessage().setGrpHdr(new GroupHeader85()); | |
//customerCreditTransfer.getMessage().getGrpHdr().setMsgId("1234"); | |
//or setElement() | |
//customerCreditTransfer.setElement("GrpHdr/MsgId", "1234"); | |
//Perform validation | |
ValidationErrorList validationErrorList = customerCreditTransfer.validate(); | |
if (validationErrorList.isEmpty()) { | |
System.out.println("Message is valid"); | |
System.out.println(customerCreditTransfer.convertToXML()); //Get the generated xml | |
} else { | |
handleValidationError(validationErrorList); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.err.println(e.getMessage()); | |
} | |
} | |
public static void parseAndValidateCgiMpMessage() { | |
try { | |
//Initialize the message object | |
CustomerCreditTransferInitiation09RelayServiceCgiMp customerCreditTransfer = new CustomerCreditTransferInitiation09RelayServiceCgiMp(); | |
//Validate against the xml schema | |
ValidationErrorList validationErrorList = customerCreditTransfer.validateXML(new ByteArrayInputStream(validCgiMpPain001RelayServiceString.getBytes())); | |
if (!validationErrorList.isEmpty()) { | |
handleValidationError(validationErrorList); | |
return; | |
} | |
//Fill the message with data from xml | |
customerCreditTransfer.parseXML(validCgiMpPain001RelayServiceString); | |
//Validate both the xml schema and rules | |
validationErrorList.addAll(customerCreditTransfer.validate()); | |
if (validationErrorList.isEmpty()) { | |
System.out.println("Message is valid"); | |
System.out.println(customerCreditTransfer.convertToXML()); //Get the generated xml | |
} else { | |
handleValidationError(validationErrorList); | |
} | |
} 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 validCgiMpPain001RelayServiceString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | |
"<Document xmlns=\"urn:iso:std:iso:20022:tech:xsd:pain.001.001.09\">\n" + | |
" <CstmrCdtTrfInitn>\n" + | |
" <GrpHdr>\n" + | |
" <MsgId>ABC/120928/CCT001</MsgId>\n" + | |
" <CreDtTm>2012-09-28T14:07:00</CreDtTm>\n" + | |
" <NbOfTxs>1</NbOfTxs>\n" + | |
" <CtrlSum>10000000</CtrlSum>\n" + | |
" <InitgPty>\n" + | |
" <Nm>ABC Corporation</Nm>\n" + | |
" <PstlAdr>\n" + | |
" <StrtNm>Times Square</StrtNm>\n" + | |
" <BldgNb>7</BldgNb>\n" + | |
" <PstCd>NY 10036</PstCd>\n" + | |
" <TwnNm>New York</TwnNm>\n" + | |
" <Ctry>US</Ctry>\n" + | |
" </PstlAdr>\n" + | |
" </InitgPty>\n" + | |
" </GrpHdr>\n" + | |
" <PmtInf>\n" + | |
" <PmtInfId>ABC/086</PmtInfId>\n" + | |
" <PmtMtd>TRF</PmtMtd>\n" + | |
" <BtchBookg>false</BtchBookg>\n" + | |
" <ReqdExctnDt>\n" + | |
" <Dt>2012-09-29</Dt>\n" + | |
" </ReqdExctnDt>\n" + | |
" <Dbtr>\n" + | |
" <Nm>ABC Corporation</Nm>\n" + | |
" <PstlAdr>\n" + | |
" <StrtNm>Times Square</StrtNm>\n" + | |
" <BldgNb>7</BldgNb>\n" + | |
" <PstCd>NY 10036</PstCd>\n" + | |
" <TwnNm>New York</TwnNm>\n" + | |
" <Ctry>US</Ctry>\n" + | |
" </PstlAdr>\n" + | |
" </Dbtr>\n" + | |
" <DbtrAcct>\n" + | |
" <Id>\n" + | |
" <Othr>\n" + | |
" <Id>00125574999</Id>\n" + | |
" </Othr>\n" + | |
" </Id>\n" + | |
" <Ccy>EUR</Ccy>\n" + | |
" </DbtrAcct>\n" + | |
" <DbtrAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>BBBBUS33</BICFI>\n" + | |
" <PstlAdr>\n" + | |
" <AdrLine>AAA</AdrLine>\n" + | |
" </PstlAdr>\n" + | |
" </FinInstnId>\n" + | |
" </DbtrAgt>\n" + | |
" <CdtTrfTxInf>\n" + | |
" <PmtId>\n" + | |
" <InstrId>ABC/120928/CCT001/01</InstrId>\n" + | |
" <EndToEndId>ABC/4562/2012-09-08</EndToEndId>\n" + | |
" </PmtId>\n" + | |
" <Amt>\n" + | |
" <InstdAmt Ccy=\"JPY\">10000000</InstdAmt>\n" + | |
" </Amt>\n" + | |
" <ChrgBr>SHAR</ChrgBr>\n" + | |
" <CdtrAgt>\n" + | |
" <FinInstnId>\n" + | |
" <BICFI>AAAAGB2L</BICFI>\n" + | |
" </FinInstnId>\n" + | |
" </CdtrAgt>\n" + | |
" <Cdtr>\n" + | |
" <Nm>DEF Electronics</Nm>\n" + | |
" <PstlAdr>\n" + | |
" <AdrLine>Corn Exchange 5th Floor</AdrLine>\n" + | |
" <AdrLine>Mark Lane 55</AdrLine>\n" + | |
" <AdrLine>EC3R7NE London</AdrLine>\n" + | |
" <AdrLine>GB</AdrLine>\n" + | |
" </PstlAdr>\n" + | |
" </Cdtr>\n" + | |
" <CdtrAcct>\n" + | |
" <Id>\n" + | |
" <Othr>\n" + | |
" <Id>23683707994125</Id>\n" + | |
" </Othr>\n" + | |
" </Id>\n" + | |
" </CdtrAcct>\n" + | |
" <Purp>\n" + | |
" <Cd>GDDS</Cd>\n" + | |
" </Purp>\n" + | |
" <RmtInf>\n" + | |
" <Strd>\n" + | |
" <RfrdDocInf>\n" + | |
" <Tp>\n" + | |
" <CdOrPrtry>\n" + | |
" <Cd>CINV</Cd>\n" + | |
" </CdOrPrtry>\n" + | |
" </Tp>\n" + | |
" <Nb>4562</Nb>\n" + | |
" <RltdDt>2012-09-08</RltdDt>\n" + | |
" </RfrdDocInf>\n" + | |
" </Strd>\n" + | |
" </RmtInf>\n" + | |
" </CdtTrfTxInf>\n" + | |
" </PmtInf>\n" + | |
" </CstmrCdtTrfInitn>\n" + | |
"</Document>\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment