From fdf7211184b347067d14b9e494f49790fa355112 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Fri, 13 Sep 2024 14:16:15 +0200 Subject: [PATCH] improvements Signed-off-by: Jan N. Klug --- .../main/java/org/mustangproject/Item.java | 28 +++++++------- .../java/org/mustangproject/XMLTools.java | 37 ------------------- .../java/org/mustangproject/util/NodeMap.java | 13 +++++++ 3 files changed, 26 insertions(+), 52 deletions(-) diff --git a/library/src/main/java/org/mustangproject/Item.java b/library/src/main/java/org/mustangproject/Item.java index 1057f617..9f568a6d 100644 --- a/library/src/main/java/org/mustangproject/Item.java +++ b/library/src/main/java/org/mustangproject/Item.java @@ -64,15 +64,15 @@ public class Item implements IZUGFeRDExportableItem { //and we additionally have vat% setProduct(new Product()); icnm.getAsString("Name").ifPresent(product::setName); - icnm.getAsNodeMap("ClassifiedTaxCategory").flatMap(m -> m.getAsString("Percent")) - .ifPresent(vatPercent -> product.setVATPercent(new BigDecimal(vatPercent.trim()))); + icnm.getAsNodeMap("ClassifiedTaxCategory").flatMap(m -> m.getAsBigDecimal("Percent")) + .ifPresent(product::setVATPercent); }); itemMap.getAsNodeMap("Price").ifPresent(icnm -> { // ubl // PriceAmount with currencyID and BaseQuantity with unitCode - icnm.getAsString("PriceAmount").ifPresent(amount -> setPrice(new BigDecimal(amount.trim()))); - icnm.getAsString("BaseQuantity").ifPresent(baseQuantity -> setQuantity(new BigDecimal(baseQuantity.trim()))); + icnm.getAsBigDecimal("PriceAmount").ifPresent(this::setPrice); + icnm.getAsBigDecimal("BaseQuantity").ifPresent(this::setBasisQuantity); }); itemMap.getNode("InvoicedQuantity").ifPresent(icn -> { @@ -82,19 +82,17 @@ public class Item implements IZUGFeRDExportableItem { }); itemMap.getAsNodeMap("SpecifiedLineTradeAgreement", "SpecifiedSupplyChainTradeAgreement").ifPresent(icnm -> { - icnm.getAllNodes("AdditionalReferencedDocument").map(ReferencedDocument::fromNode) - .forEach(this::addReferencedDocument); - icnm.getAsNodeMap("BuyerOrderReferencedDocument") .flatMap(bordNodes -> bordNodes.getAsString("LineID")) .ifPresent(this::addReferencedLineID); icnm.getAsNodeMap("NetPriceProductTradePrice").ifPresent(npptpNodes -> { - npptpNodes.getAsString("ChargeAmount") - .ifPresent(amount -> setPrice(new BigDecimal(amount.trim()))); - npptpNodes.getAsString("BasisQuantity") - .ifPresent(bq -> setBasisQuantity(new BigDecimal(bq.trim()))); + npptpNodes.getAsBigDecimal("ChargeAmount").ifPresent(this::setPrice); + npptpNodes.getAsBigDecimal("BasisQuantity").ifPresent(this::setBasisQuantity); }); + + icnm.getAllNodes("AdditionalReferencedDocument").map(ReferencedDocument::fromNode) + .forEach(this::addReferencedDocument); }); itemMap.getNode("SpecifiedTradeProduct").map(Product::new).ifPresent(this::setProduct); @@ -114,13 +112,13 @@ public class Item implements IZUGFeRDExportableItem { itemMap.getAsNodeMap("SpecifiedLineTradeSettlement", "SpecifiedSupplyChainTradeSettlement").ifPresent(icnm -> { icnm.getAsNodeMap("ApplicableTradeTax") - .map(cnm -> cnm.getAsStringOrNull("RateApplicablePercent", "ApplicablePercent")) - .ifPresent(vatPercent -> product.setVATPercent(new BigDecimal(vatPercent.trim()))); + .flatMap(cnm -> cnm.getAsBigDecimal("RateApplicablePercent", "ApplicablePercent")) + .ifPresent(product::setVATPercent); if (recalcPrice && !BigDecimal.ZERO.equals(quantity)) { icnm.getAsNodeMap("SpecifiedTradeSettlementLineMonetarySummation") - .map(cnm -> cnm.getAsStringOrNull("LineTotalAmount")) - .ifPresent(lineTotal -> setPrice(new BigDecimal(lineTotal.trim()).divide(quantity, 4, RoundingMode.HALF_UP))); + .flatMap(cnm -> cnm.getAsBigDecimal("LineTotalAmount")) + .ifPresent(lineTotal -> setPrice(lineTotal.divide(quantity, 4, RoundingMode.HALF_UP))); } icnm.getAllNodes("AdditionalReferencedDocument").map(ReferencedDocument::fromNode).forEach(this::addAdditionalReference); diff --git a/library/src/main/java/org/mustangproject/XMLTools.java b/library/src/main/java/org/mustangproject/XMLTools.java index c882e573..8ee9fe5a 100644 --- a/library/src/main/java/org/mustangproject/XMLTools.java +++ b/library/src/main/java/org/mustangproject/XMLTools.java @@ -4,21 +4,9 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.math.RoundingMode; -import java.util.AbstractList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.RandomAccess; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.Stream; import org.apache.commons.io.IOUtils; import org.dom4j.io.XMLWriter; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; public class XMLTools extends XMLWriter { @Override @@ -31,30 +19,6 @@ public class XMLTools extends XMLWriter { return super.escapeElementEntities(s); } - public static List asList(NodeList n) { - return n.getLength() == 0 ? - Collections.emptyList() : new NodeListWrapper(n); - } - - static final class NodeListWrapper extends AbstractList - implements RandomAccess { - private final NodeList list; - - NodeListWrapper(NodeList l) { - list = l; - } - - @Override - public Node get(int index) { - return list.item(index); - } - - @Override - public int size() { - return list.getLength(); - } - } - public static String nDigitFormat(BigDecimal value, int scale) { /* * I needed 123,45, locale independent.I tried @@ -75,7 +39,6 @@ public class XMLTools extends XMLWriter { } - public static String encodeXML(CharSequence s) { if (s == null) { return ""; diff --git a/library/src/main/java/org/mustangproject/util/NodeMap.java b/library/src/main/java/org/mustangproject/util/NodeMap.java index abe73d12..373781f7 100644 --- a/library/src/main/java/org/mustangproject/util/NodeMap.java +++ b/library/src/main/java/org/mustangproject/util/NodeMap.java @@ -23,6 +23,7 @@ package org.mustangproject.util; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -102,6 +103,18 @@ public class NodeMap { return getNode(localNames).map(Node::getTextContent); } + /** + * Get the text content of a matching node + *

+ * In case more than one node matches, it is not guaranteed that the first match is selected + * + * @param localNames one or more {@code LocalName}s + * @return the text content of the matching node, converted to BigDecimal + */ + public Optional getAsBigDecimal(String... localNames) { + return getNode(localNames).map(Node::getTextContent).map(s->new BigDecimal(s.trim())); + } + /** * Get the text content of a matching node *