Skip to content

Commit

Permalink
added setter methods to update the xml tree (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Dec 15, 2024
1 parent 985284a commit da17627
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.imixs.archive.documents.einvoice;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.time.LocalDate;
Expand All @@ -11,6 +13,13 @@
import java.util.Set;
import java.util.logging.Logger;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -107,6 +116,12 @@ public BigDecimal getNetTotalAmount() {
return netTotalAmount;
}

public abstract void setNetTotalAmount(BigDecimal value);

public void setNetTotalAmount(Double value) {
setNetTotalAmount(BigDecimal.valueOf(value));
}

/**
* Returns all trade parties
*
Expand Down Expand Up @@ -268,4 +283,38 @@ public void setPrefix(EInvoiceNS ns, String prefix) {

}

/**
* Returns the XML representation of the current document as a byte array
*
* @return byte array containing the XML data
* @throws TransformerException
*/
public byte[] getContent() throws TransformerException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
// Setup transformer
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

// Configure output properties
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");

// Transform DOM to byte array
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(outputStream);
transformer.transform(source, result);

return outputStream.toByteArray();

} finally {
try {
outputStream.close();
} catch (IOException e) {
logger.warning("Failed to close output stream: " + e.getMessage());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ public void setNameSpaces() {
for (int j = 0; j < defAttributes.getLength(); j++) {
Node node = defAttributes.item(j);

// if (getPrefix(EInvoiceNS.A).equals(node.getLocalName())
// && !getUri(EInvoiceNS.A).equals(node.getNodeValue())) {
// logger.fine("...set A namespace URI: " + node.getNodeValue());
// setUri(EInvoiceNS.A, node.getNodeValue());
// }
if (getPrefix(EInvoiceNS.RSM).equals(node.getLocalName())
&& !getUri(EInvoiceNS.RSM).equals(node.getNodeValue())) {
logger.fine("...set RSM namespace URI: " + node.getNodeValue());
Expand Down Expand Up @@ -227,4 +222,31 @@ public TradeParty parseTradeParty(Element tradePartyElement, String type) {
}
return tradeParty;
}

@Override
public void setNetTotalAmount(BigDecimal value) {
// Finde das SpecifiedTradeSettlementHeaderMonetarySummation Element
Element element = findChildNodeByName(supplyChainTradeTransaction, EInvoiceNS.RAM,
"ApplicableHeaderTradeSettlement");
if (element != null) {
Element tradeSettlementElement = findChildNodeByName(element, EInvoiceNS.RAM,
"SpecifiedTradeSettlementHeaderMonetarySummation");
if (tradeSettlementElement != null) {
// Update LineTotalAmount
Element lineTotalElement = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM,
"LineTotalAmount");
if (lineTotalElement != null) {
lineTotalElement.setTextContent(value.toPlainString());
// Update auch den internen Wert
netTotalAmount = value;
}
// Update TaxBasisTotalAmount
Element taxBasisElement = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM,
"TaxBasisTotalAmount");
if (taxBasisElement != null) {
taxBasisElement.setTextContent(value.toPlainString());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,30 +102,6 @@ private void loadDocumentCoreData() {

parseTotal();

// // read Total amount
// element = findChildNodeByName(supplyChainTradeTransaction, EInvoiceNS.RAM,
// "ApplicableHeaderTradeSettlement");
// if (element != null) {
// Element tradeSettlementElement = findChildNodeByName(element, EInvoiceNS.RAM,
// "SpecifiedTradeSettlementHeaderMonetarySummation");
// if (tradeSettlementElement != null) {
// Element child = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM,
// "GrandTotalAmount");
// if (child != null) {
// grandTotalAmount = new BigDecimal(child.getTextContent());
// }
// child = findChildNodeByName(tradeSettlementElement, EInvoiceNS.RAM,
// "TaxTotalAmount");
// if (child != null) {
// taxTotalAmount = new BigDecimal(child.getTextContent());
// }
// netTotalAmount = grandTotalAmount.subtract(taxTotalAmount).setScale(2,
// RoundingMode.HALF_UP);

// }

// }

}

/**
Expand Down Expand Up @@ -184,43 +160,13 @@ public TradeParty parseTradeParty(Element tradePartyElement, String type) {

}

// Element postalAddress = findChildNodeByName(tradePartyElement,
// EInvoiceNS.RAM,
// "PostalTradeAddress");
// if (postalAddress != null) {
// partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM,
// "PostcodeCode");
// if (partyElement != null) {
// tradeParty.setPostcodeCode(partyElement.getTextContent());
// }
// partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM,
// "CityName");
// if (partyElement != null) {
// tradeParty.setCityName(partyElement.getTextContent());
// }
// partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM,
// "CountryID");
// if (partyElement != null) {
// tradeParty.setCountryId(partyElement.getTextContent());
// }
// partyElement = findChildNodeByName(postalAddress, EInvoiceNS.RAM,
// "LineOne");
// if (partyElement != null) {
// tradeParty.setStreetAddress(partyElement.getTextContent());
// }
// }

// Element specifiedTaxRegistration = findChildNodeByName(tradePartyElement,
// EInvoiceNS.RAM,
// "SpecifiedTaxRegistration");
// if (specifiedTaxRegistration != null) {
// partyElement = findChildNodeByName(specifiedTaxRegistration, EInvoiceNS.RAM,
// "ID");
// if (partyElement != null) {
// tradeParty.setVatNumber(partyElement.getTextContent());
// }
// }
return tradeParty;
}

@Override
public void setNetTotalAmount(BigDecimal value) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'setNetTotalAmount'");
}

}

0 comments on commit da17627

Please sign in to comment.