Skip to content

Commit

Permalink
jacksonability
Browse files Browse the repository at this point in the history
  • Loading branch information
jstaerk committed Dec 13, 2021
1 parent dea6d17 commit 88448c6
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 15 deletions.
2 changes: 1 addition & 1 deletion History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

- Jacksonability: Invoice can now be Stringified to / restored from a JSON using Jackson
- Fixed an error validation UBL files
- unknown root elements will now throw type 3 errors
- added some tests
Expand Down
7 changes: 6 additions & 1 deletion doc/development_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

To check if the necessary tools are there, the build is in a stable state and works on your platform, e.g. download and extract https://github.com/ZUGFeRD/mustangproject/archive/master.zip and run ./mvnw clean package

Mvnw is a maven wrapper which will download maven.Maven is the dependency management tool which will download all libraries, their dependencies, and build the whole thing.
Mvnw is a maven wrapper which will download maven.Maven is the dependency management tool which will download all libraries, their dependencies, and build the whole thing.

You will need a Java JDK, e.g. https://www.azul.com/downloads/zulu-community/?architecture=x86-64-bit&package=jdk
Expand Down Expand Up @@ -159,7 +160,11 @@ If you added functionality which you need to test in another project before a ne
install the jar you just generated in your target branch in your local maven cache so it gets picked *instead* of the
maybe not yet even existing new release version:

`mvn install:install-file -Dfile=mustang-1.7.6-SNAPSHOT.jar -DgroupId=org.mustangproject.ZUGFeRD -DartifactId=mustang -Dversion=1.7.6 -Dpackaging=jar -DgeneratePom=true`
```
cd validator/target
mvn install:install-file -Dfile=validator-2.3.2-SNAPSHOT-shaded.jar -DgroupId=org.mustangproject -DartifactId=validator -Dversion=2.3.2 -Dpackaging=jar -DgeneratePom=true
```
This will also work in Gradle given you have the `mavenLocal()` repository activated.


## Release
Expand Down
6 changes: 6 additions & 0 deletions library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.9.0-1</version>
</dependency><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>

<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/java/org/mustangproject/Contact.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mustangproject;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.mustangproject.ZUGFeRD.IZUGFeRDExportableContact;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -9,6 +10,7 @@
* for the organisation/company itsel please
* @see TradeParty
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Contact implements IZUGFeRDExportableContact {

protected String name, phone, email, zip, street, location, country;
Expand All @@ -27,6 +29,13 @@ public Contact(String name, String phone, String email) {
this.email = email;
}

/***
* empty constructor.
* as always, not recommended, for jackson...
*/
public Contact() {
}

/***
* complete specification of a named contact with a different address
* @param name full name
Expand Down
19 changes: 12 additions & 7 deletions library/src/main/java/org/mustangproject/Invoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package org.mustangproject;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.mustangproject.ZUGFeRD.*;
import org.mustangproject.ZUGFeRD.model.DocumentCodeTypeConstants;

Expand All @@ -31,15 +33,17 @@
* An invoice, with fluent setters
* @see IExportableTransaction if you want to implement an interface instead
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Invoice implements IExportableTransaction {

protected String documentName = null, documentCode = null, number = null, ownOrganisationFullPlaintextInfo = null, referenceNumber = null, shipToOrganisationID = null, shipToOrganisationName = null, shipToStreet = null, shipToZIP = null, shipToLocation = null, shipToCountry = null, buyerOrderReferencedDocumentID = null, invoiceReferencedDocumentID = null, buyerOrderReferencedDocumentIssueDateTime = null, ownForeignOrganisationID = null, ownOrganisationName = null, currency = null, paymentTermDescription = null;
protected Date issueDate = null, dueDate = null, deliveryDate = null;
protected BigDecimal totalPrepaidAmount = null;
protected TradeParty sender = null, recipient = null, deliveryAddress = null;
@JsonDeserialize(contentAs=Item.class)
protected ArrayList<IZUGFeRDExportableItem> ZFItems = null;
protected ArrayList<String> notes = null;
private String sellerOrderReferencedDocumentID;
protected String sellerOrderReferencedDocumentID;
protected String contractReferencedDocument = null;
protected ArrayList<FileAttachment> xmlEmbeddedFiles=null;

Expand All @@ -49,12 +53,9 @@ public class Invoice implements IExportableTransaction {
protected ArrayList<IZUGFeRDAllowanceCharge> Allowances = new ArrayList<>(),
Charges = new ArrayList<>(), LogisticsServiceCharges = new ArrayList<>();
protected IZUGFeRDPaymentTerms paymentTerms = null;
private Date invoiceReferencedIssueDate;
private String specifiedProcuringProjectID = null;



private String specifiedProcuringProjectName = null;
protected Date invoiceReferencedIssueDate;
protected String specifiedProcuringProjectID = null;
protected String specifiedProcuringProjectName = null;

public Invoice() {
ZFItems = new ArrayList<>();
Expand Down Expand Up @@ -551,6 +552,10 @@ public IZUGFeRDExportableItem[] getZFItems() {
return ZFItems.toArray(new IZUGFeRDExportableItem[0]);
}

public void setZFItems(ArrayList<IZUGFeRDExportableItem> ims) {
ZFItems=ims;
}

/**
* required
* adds invoice "lines" :-)
Expand Down
10 changes: 10 additions & 0 deletions library/src/main/java/org/mustangproject/Item.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mustangproject;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.mustangproject.ZUGFeRD.IReferencedDocument;
import org.mustangproject.ZUGFeRD.IZUGFeRDAllowanceCharge;
import org.mustangproject.ZUGFeRD.IZUGFeRDExportableItem;
Expand All @@ -11,6 +12,8 @@
/***
* describes any invoice line
*/

@JsonIgnoreProperties(ignoreUnknown = true)
public class Item implements IZUGFeRDExportableItem {
protected BigDecimal price, quantity, tax, grossPrice, lineTotalAmount;
protected Date detailedDeliveryPeriodFrom=null, detailedDeliveryPeriodTo=null;
Expand All @@ -35,6 +38,13 @@ public Item(Product product, BigDecimal price, BigDecimal quantity) {
}


/***
* empty constructor
* do not use, but might be used e.g. by jackson
* */
public Item() {
}

public Item addReferencedLineID(String s) {
referencedLineID=s;
return this;
Expand Down
11 changes: 11 additions & 0 deletions library/src/main/java/org/mustangproject/Product.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.mustangproject;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.mustangproject.ZUGFeRD.IZUGFeRDExportableProduct;

import java.math.BigDecimal;

/***
* describes a product, good or service used in an invoice item line
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product implements IZUGFeRDExportableProduct {
protected String unit, name, description, sellerAssignedID, buyerAssignedID;
protected BigDecimal VATPercent;
Expand All @@ -28,6 +30,15 @@ public Product(String name, String description, String unit, BigDecimal VATPerce
}


/***
* empty constructor
* just for jackson etc
*/
public Product() {

}


public String getSellerAssignedID() {
return sellerAssignedID;
}
Expand Down
19 changes: 14 additions & 5 deletions library/src/main/java/org/mustangproject/TradeParty.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.mustangproject.ZUGFeRD.IZUGFeRDExportableContact;
import org.mustangproject.ZUGFeRD.IZUGFeRDExportableTradeParty;
import org.mustangproject.ZUGFeRD.IZUGFeRDTradeSettlement;
Expand All @@ -15,6 +16,7 @@
/***
* A organisation, i.e. usually a company
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class TradeParty implements IZUGFeRDExportableTradeParty {

protected String name, zip, street, location, country;
Expand All @@ -25,6 +27,15 @@ public class TradeParty implements IZUGFeRDExportableTradeParty {
protected List<IZUGFeRDTradeSettlementDebit> debitDetails = new ArrayList<>();
protected Contact contact = null;

/**
* Default constructor.
* Probably a bad idea but might be needed by jackson or similar
*/
public TradeParty() {

}


/***
*
* @param name of the company
Expand Down Expand Up @@ -118,13 +129,11 @@ public TradeParty(NodeList nodes) {
for (int taxChildIndex = 0; taxChildIndex < taxChilds.getLength(); taxChildIndex++) {
if (taxChilds.item(taxChildIndex).getLocalName() != null) {
if ((taxChilds.item(taxChildIndex).getLocalName().equals("ID"))) {
if (taxChilds.item(taxChildIndex).getAttributes().getNamedItem("schemeID")!=null) {
if (taxChilds.item(taxChildIndex).getAttributes().getNamedItem("schemeID").getNodeValue().equals("VA"))
{
if (taxChilds.item(taxChildIndex).getAttributes().getNamedItem("schemeID") != null) {
if (taxChilds.item(taxChildIndex).getAttributes().getNamedItem("schemeID").getNodeValue().equals("VA")) {
setVATID(taxChilds.item(taxChildIndex).getFirstChild().getNodeValue());
}
if (taxChilds.item(taxChildIndex).getAttributes().getNamedItem("schemeID").getNodeValue().equals("FC"))
{
if (taxChilds.item(taxChildIndex).getAttributes().getNamedItem("schemeID").getNodeValue().equals("FC")) {
setTaxID(taxChilds.item(taxChildIndex).getFirstChild().getNodeValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
* @author jstaerk
* */

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.mustangproject.Item;

import java.math.BigDecimal;
import java.util.Date;

@JsonDeserialize(as = Item.class)
public interface IZUGFeRDExportableItem extends IAbsoluteValueProvider{

IZUGFeRDExportableProduct getProduct();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

/**
* *********************************************************************
* <p>
* Copyright 2019 Jochen Staerk
* <p>
* Use is subject to license terms.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0.
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* <p>
* See the License for the specific language governing permissions and
* limitations under the License.
* <p>
* **********************************************************************
*/
package org.mustangproject.ZUGFeRD;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.TestCase;
import org.assertj.core.util.Lists;
import org.junit.FixMethodOrder;
import org.junit.runners.MethodSorters;
import org.mustangproject.*;

import java.math.BigDecimal;
import java.util.Date;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DeSerializationTest extends TestCase {
public void testJackson() throws JsonProcessingException {

ObjectMapper mapper = new ObjectMapper();
Invoice i = new Invoice().setDueDate(new Date()).setIssueDate(new Date()).setDeliveryDate(new Date()).setSender(new TradeParty("some org", "teststr", "55232", "teststadt", "DE").addTaxID("taxID")).setOwnVATID("DE0815").setRecipient(new TradeParty("Franz Müller", "teststr.12", "55232", "Entenhausen", "DE").addVATID("DE4711").setContact(new Contact("Franz Müller", "01779999999", "franz@mueller.de", "teststr. 12", "55232", "Entenhausen", "DE"))).setNumber("0185").addItem(new Item(new Product("Testprodukt", "", "C62", new BigDecimal(19)), new BigDecimal("1"), new BigDecimal(1.0)));
String jsonArray = mapper.writeValueAsString(i);

// [{"stringValue":"a","intValue":1,"booleanValue":true},
// {"stringValue":"bc","intValue":3,"booleanValue":false}]

Invoice fromJSON = mapper.readValue(jsonArray, Invoice.class);
assertEquals(fromJSON.getNumber(), i.getNumber());
assertEquals(fromJSON.getZFItems().length, i.getZFItems().length);

}
}

0 comments on commit 88448c6

Please sign in to comment.