first trial
This commit is contained in:
@ -24,15 +24,25 @@ import de.jottyfan.csv2camt.csv.CsvLine;
|
||||
public class CamtWriter {
|
||||
|
||||
protected Document generateXml(CsvBean bean) throws Exception {
|
||||
if (bean.getIban() == null || bean.getIban().length() < 10){
|
||||
if (bean.getIban() == null || bean.getIban().length() < 10) {
|
||||
throw new Exception(String.format("invalid iban: %s", bean.getIban()));
|
||||
} else if (bean.getKontostandFinal() == null) {
|
||||
throw new Exception("no kontostandFinal");
|
||||
}
|
||||
String receiver = "Evangelische-Freikirchliche Gemeinde Dresden Süd-Ost";
|
||||
StringBuilder buf = new StringBuilder("KtoNr");
|
||||
buf.append(bean.getIban().substring(bean.getIban().length() - 10));
|
||||
buf.append("_");
|
||||
buf.append(bean.getKontostandDatum().replace(".", "-"));
|
||||
buf.append("_0000"); // no creation time in csv
|
||||
String MsgIdText = buf.toString();
|
||||
buf = new StringBuilder();
|
||||
buf.append(bean.getKontostandDatum().replace(".", "-"));
|
||||
buf.append("T00:00:00.000+02:00"); // no creation time in csv
|
||||
String kontostandzeitpunkt = buf.toString();
|
||||
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
|
||||
Document xml = builder.newDocument();
|
||||
xml.setXmlStandalone(true);
|
||||
|
||||
@ -40,151 +50,58 @@ public class CamtWriter {
|
||||
document.setAttribute("xmlns", "urn:iso:std:iso:20022:tech:xsd:camt.052.001.06");
|
||||
xml.appendChild(document);
|
||||
|
||||
Element BkToCstmrAcctRpt = xml.createElement("BkToCstmrAcctRpt");
|
||||
document.appendChild(BkToCstmrAcctRpt);
|
||||
|
||||
Element GrpHdr = xml.createElement("GrpHdr");
|
||||
BkToCstmrAcctRpt.appendChild(GrpHdr);
|
||||
|
||||
Element MsgId = xml.createElement("MsgId");
|
||||
StringBuilder buf = new StringBuilder("KtoNr");
|
||||
buf.append(bean.getIban().substring(bean.getIban().length() - 10));
|
||||
buf.append("_");
|
||||
buf.append(bean.getKontostandDatum().replace(".", "-"));
|
||||
buf.append("_0000"); // no creation time in csv
|
||||
MsgId.setTextContent(buf.toString());
|
||||
GrpHdr.appendChild(MsgId);
|
||||
|
||||
Element CreDtTm = xml.createElement("CreDtTm");
|
||||
buf = new StringBuilder();
|
||||
buf.append(bean.getKontostandDatum().replace(".", "-"));
|
||||
buf.append("T00:00:00.000+02:00"); // no creation time in csv
|
||||
CreDtTm.setTextContent(buf.toString());
|
||||
GrpHdr.appendChild(CreDtTm);
|
||||
|
||||
Element MsgRcpt = xml.createElement("MsgRcpt");
|
||||
GrpHdr.appendChild(MsgRcpt);
|
||||
|
||||
Element Nm = xml.createElement("Nm");
|
||||
Nm.setTextContent("Evangelische-Freikirchliche Gemeinde Dresden Süd-Ost");
|
||||
MsgRcpt.appendChild(Nm);
|
||||
|
||||
Element Rpt = xml.createElement("Rpt");
|
||||
BkToCstmrAcctRpt.appendChild(Rpt);
|
||||
|
||||
Element Id = xml.createElement("Id");
|
||||
Id.setTextContent(MsgId.getTextContent());
|
||||
Rpt.appendChild(Id);
|
||||
|
||||
Element BkToCstmrAcctRpt = new XmlTag(xml, "BkToCstmrAcctRpt", document).get();
|
||||
Element GrpHdr = new XmlTag(xml, "GrpHdr", BkToCstmrAcctRpt).get();
|
||||
new XmlTag(xml, "MsgId", GrpHdr).withText(MsgIdText);
|
||||
Element CreDtTm = new XmlTag(xml, "CreDtTm", GrpHdr).withText(kontostandzeitpunkt).get();
|
||||
new XmlTag(xml, "Nm", new XmlTag(xml, "MsgRcpt", GrpHdr).get()).withText(receiver);
|
||||
Element Rpt = new XmlTag(xml, "Rpt", BkToCstmrAcctRpt).get();
|
||||
new XmlTag(xml, "Id", Rpt).withText(MsgIdText);
|
||||
Rpt.appendChild(CreDtTm.cloneNode(true));
|
||||
|
||||
Element Acct = xml.createElement("Acct");
|
||||
Rpt.appendChild(Acct);
|
||||
|
||||
Element AcctId = xml.createElement("Id");
|
||||
Acct.appendChild(AcctId);
|
||||
|
||||
Element IBAN = xml.createElement("IBAN");
|
||||
IBAN.setTextContent(bean.getIban());
|
||||
AcctId.appendChild(IBAN);
|
||||
|
||||
Element Bal = xml.createElement("Bal");
|
||||
Rpt.appendChild(Bal);
|
||||
|
||||
Element Tp = xml.createElement("Tp");
|
||||
Bal.appendChild(Tp);
|
||||
|
||||
Element CdOrPrtry = xml.createElement("CdOrPrtry");
|
||||
Tp.appendChild(CdOrPrtry);
|
||||
|
||||
Element Cd = xml.createElement("Cd");
|
||||
Cd.setTextContent("CLAV");
|
||||
CdOrPrtry.appendChild(Cd);
|
||||
|
||||
Element Amt = xml.createElement("Amt");
|
||||
Amt.setAttribute("Ccy", bean.getWaehrung());
|
||||
Amt.setTextContent(bean.getKontostandFinal().replace(".", "").replace(",", ".")); // not sure if correct...
|
||||
Bal.appendChild(Amt);
|
||||
|
||||
Element CdtDbtInd = xml.createElement("CdtDbtInd");
|
||||
CdtDbtInd.setTextContent("CRDT");
|
||||
Bal.appendChild(CdtDbtInd);
|
||||
|
||||
Element Dt = xml.createElement("Dt");
|
||||
Bal.appendChild(Dt);
|
||||
|
||||
Element DtTm = xml.createElement("DtTm");
|
||||
DtTm.setTextContent(CreDtTm.getTextContent());
|
||||
Dt.appendChild(DtTm);
|
||||
new XmlTag(xml, "IBAN", new XmlTag(xml, "Id", new XmlTag(xml, "Acct", Rpt).get()).get()).withText(bean.getIban());
|
||||
Element Bal = new XmlTag(xml, "Bal", Rpt).get();
|
||||
new XmlTag(xml, "Cd", new XmlTag(xml, "CdOrPrtry", new XmlTag(xml, "Tp", Bal).get()).get()).withText("CLAV");
|
||||
new XmlTag(xml, "Amt", Bal).withAttr("Ccy", bean.getWaehrung())
|
||||
.withText(bean.getKontostandFinal().replace(".", "").replace(",", ".")); // not sure if correct...
|
||||
new XmlTag(xml, "CdtDbtInd", Bal).withText("CRDT");
|
||||
new XmlTag(xml, "DtTm", new XmlTag(xml, "Dt", Bal).get()).withText(CreDtTm.getTextContent());
|
||||
|
||||
for (CsvLine trans : bean.getList()) {
|
||||
Element Ntry = xml.createElement("Ntry");
|
||||
Rpt.appendChild(Ntry);
|
||||
|
||||
Element NtryAmt = xml.createElement("Amt");
|
||||
NtryAmt.setAttribute("Ccy", trans.Waehrung());
|
||||
NtryAmt.setTextContent(trans.Betrag().replace(".", "").replace(",", "."));
|
||||
Ntry.appendChild(NtryAmt);
|
||||
|
||||
Element Ntry = new XmlTag(xml, "Ntry", Rpt).get();
|
||||
new XmlTag(xml, "Amt", Ntry).withAttr("Ccy", trans.Waehrung())
|
||||
.withText(trans.Betrag().replace(".", "").replace(",", "."));
|
||||
new XmlTag(xml, "CdtDbtInd", Ntry).withText("CRDT"); // not sure if this is right
|
||||
new XmlTag(xml, "Sts", Ntry).withText("BOOK"); // not sure if this is right
|
||||
new XmlTag(xml, "Dt", new XmlTag(xml, "BookgDt", Ntry).get()).withText(XmlTag.yyyyMMdd(trans.Buchungstag()));
|
||||
new XmlTag(xml, "Dt", new XmlTag(xml, "ValDt", Ntry).get()).withText(XmlTag.yyyyMMdd(trans.Wert()));
|
||||
Element BkTxCd = new XmlTag(xml, "BkTxCd", Ntry).get();
|
||||
Element Domn = new XmlTag(xml, "Domn", BkTxCd).get();
|
||||
new XmlTag(xml, "Cd", Domn).withText("PMNT"); // not sure if this is right
|
||||
Element Fmly = new XmlTag(xml, "Fmly", Domn).get();
|
||||
new XmlTag(xml, "Cd", Fmly).withText("RCDT"); // not sure if this is right
|
||||
new XmlTag(xml, "SubFmlyCd", Fmly).withText("ESCT"); // not sure if this is right
|
||||
Element Prtry = new XmlTag(xml, "Prtry", BkTxCd).get();
|
||||
new XmlTag(xml, "Cd", Prtry).withText("NTRF+166"); // not sure if this is right
|
||||
new XmlTag(xml, "Issr", Prtry).withText("DK"); // not sure if this is right
|
||||
Element TxDtls = new XmlTag(xml, "TxDtls", new XmlTag(xml, "NtryDtls", Ntry).get()).get();
|
||||
new XmlTag(xml, "Amt", TxDtls).withAttr("Ccy", trans.Waehrung()).withText(trans.Betrag());
|
||||
new XmlTag(xml, "CdtDbtInd", TxDtls).withText("CRDT"); // not sure if this is right
|
||||
Element BkTxCd_Ntry = new XmlTag(xml, "BkTxCd", TxDtls).get();
|
||||
Element Domn_Ntry = new XmlTag(xml, "Domn", BkTxCd_Ntry).get();
|
||||
new XmlTag(xml, "Cd", Domn_Ntry).withText("PMNT"); // not sure if this is right
|
||||
Element Fmly_Ntry = new XmlTag(xml, "Fmly", Domn_Ntry).get();
|
||||
new XmlTag(xml, "Cd", Fmly_Ntry).withText("RCDT"); // not sure if this is right
|
||||
new XmlTag(xml, "SubFmlyCd", Fmly_Ntry).withText("ESCT"); // not sure if this is right
|
||||
Element Prtry_Ntry = new XmlTag(xml, "Prtry", BkTxCd_Ntry).get();
|
||||
new XmlTag(xml, "Cd", Prtry_Ntry).withText("NTRF+166"); // not sure if this is right
|
||||
new XmlTag(xml, "Issr", Prtry_Ntry).withText("DK"); // not sure if this is right
|
||||
Element RltdPties = new XmlTag(xml, "RltdPties", TxDtls).get();
|
||||
new XmlTag(xml, "Nm", new XmlTag(xml, "Dbtr", RltdPties).get()).withText(trans.Beguenstigter_Auftraggeber());
|
||||
new XmlTag(xml, "Nm", new XmlTag(xml, "Cdtr", RltdPties).get()).withText(receiver);
|
||||
Element RmtInf = new XmlTag(xml, "RmtInf", TxDtls).get();
|
||||
new XmlTag(xml, "Ustrd", RmtInf).withText(String.format("Referenz %s", trans.Kundenreferenz()));
|
||||
new XmlTag(xml, "Ustrd", RmtInf).withText(trans.Verwendungszweck());
|
||||
}
|
||||
|
||||
// TODO: add the rest, the Ntry elements, as children of Rpt
|
||||
// <Ntry>
|
||||
// <Amt Ccy="EUR">128.00</Amt>
|
||||
// <CdtDbtInd>CRDT</CdtDbtInd>
|
||||
// <Sts>BOOK</Sts>
|
||||
// <BookgDt>
|
||||
// <Dt>2023-03-31</Dt>
|
||||
// </BookgDt>
|
||||
// <ValDt>
|
||||
// <Dt>2023-03-31</Dt>
|
||||
// </ValDt>
|
||||
// <BkTxCd>
|
||||
// <Domn>
|
||||
// <Cd>PMNT</Cd>
|
||||
// <Fmly>
|
||||
// <Cd>RCDT</Cd>
|
||||
// <SubFmlyCd>ESCT</SubFmlyCd>
|
||||
// </Fmly>
|
||||
// </Domn>
|
||||
// <Prtry>
|
||||
// <Cd>NTRF+166</Cd>
|
||||
// <Issr>DK</Issr>
|
||||
// </Prtry>
|
||||
// </BkTxCd>
|
||||
// <NtryDtls>
|
||||
// <TxDtls>
|
||||
// <Amt Ccy="EUR">128.00</Amt>
|
||||
// <CdtDbtInd>CRDT</CdtDbtInd>
|
||||
// <BkTxCd>
|
||||
// <Domn>
|
||||
// <Cd>PMNT</Cd>
|
||||
// <Fmly>
|
||||
// <Cd>RCDT</Cd>
|
||||
// <SubFmlyCd>ESCT</SubFmlyCd>
|
||||
// </Fmly>
|
||||
// </Domn>
|
||||
// <Prtry>
|
||||
// <Cd>NTRF+166</Cd>
|
||||
// <Issr>DK</Issr>
|
||||
// </Prtry>
|
||||
// </BkTxCd>
|
||||
// <RltdPties>
|
||||
// <Dbtr>
|
||||
// <Nm>Mickey Mouse</Nm>
|
||||
// </Dbtr>
|
||||
// <Cdtr>
|
||||
// <Nm>Evangelische-Freikirchliche Gemeinde Dresden Süd-Ost</Nm>
|
||||
// </Cdtr>
|
||||
// </RltdPties>
|
||||
// <RmtInf>
|
||||
// <Ustrd>Referenz NOTPROVIDED</Ustrd>
|
||||
// <Ustrd>Gemeindefreizeit</Ustrd>
|
||||
// </RmtInf>
|
||||
// </TxDtls>
|
||||
// </NtryDtls>
|
||||
// </Ntry>
|
||||
|
||||
return xml;
|
||||
}
|
||||
|
||||
|
43
src/main/java/de/jottyfan/csv2camt/xml/XmlTag.java
Normal file
43
src/main/java/de/jottyfan/csv2camt/xml/XmlTag.java
Normal file
@ -0,0 +1,43 @@
|
||||
package de.jottyfan.csv2camt.xml;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jotty
|
||||
*
|
||||
*/
|
||||
public class XmlTag {
|
||||
private Element element;
|
||||
|
||||
public XmlTag(Document xml, String tagName) {
|
||||
this.element = xml.createElement(tagName);
|
||||
}
|
||||
|
||||
public XmlTag(Document xml, String tagName, Element parent) {
|
||||
this.element = xml.createElement(tagName);
|
||||
parent.appendChild(element);
|
||||
}
|
||||
|
||||
public XmlTag withAttr(String name, String value) {
|
||||
this.element.setAttribute(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public XmlTag withText(String text) {
|
||||
this.element.setTextContent(text);
|
||||
return this;
|
||||
}
|
||||
|
||||
public final Element get() {
|
||||
return this.element;
|
||||
}
|
||||
|
||||
public static final String yyyyMMdd(String date) {
|
||||
return LocalDate.parse(date, DateTimeFormatter.ofPattern("d.M.yyyy")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user