added first support for lists
This commit is contained in:
@ -0,0 +1,35 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class DefaultTableRowTest {
|
||||
|
||||
@Test
|
||||
public void testFromJson() {
|
||||
String jsonString = "{\n" +
|
||||
" \"type\": \"de.muehlencord.shared.pdf.DefaultTableRow\",\n" +
|
||||
" \"data\": {\n" +
|
||||
" \"row\": [\n" +
|
||||
" {\n" +
|
||||
" \"text\": \"Rechnungs-Nr.:\"\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"text\": \"${invoiceNumber}\"\n" +
|
||||
" }\n" +
|
||||
" ],\n" +
|
||||
" \"isList\": false\n" +
|
||||
" }\n" +
|
||||
" }";
|
||||
|
||||
|
||||
TableRow tableRow = GsonUtil.getGsonInstance().fromJson(jsonString, TableRow.class);
|
||||
assertNotNull ("tableRowObject", tableRow);
|
||||
assertEquals ("column count", 2, tableRow.getColumnCount());
|
||||
assertFalse ("isList", tableRow.isList());
|
||||
}
|
||||
}
|
||||
25
pdf/src/test/java/de/muehlencord/shared/pdf/Invoice.java
Normal file
25
pdf/src/test/java/de/muehlencord/shared/pdf/Invoice.java
Normal file
@ -0,0 +1,25 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class Invoice {
|
||||
|
||||
private final List<InvoiceLine> invoiceLines;
|
||||
|
||||
public Invoice() {
|
||||
this.invoiceLines = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addInvoiceLine(InvoiceLine il) {
|
||||
this.invoiceLines.add(il);
|
||||
}
|
||||
|
||||
public List<InvoiceLine> getInvoiceLines() {
|
||||
return invoiceLines;
|
||||
}
|
||||
}
|
||||
58
pdf/src/test/java/de/muehlencord/shared/pdf/InvoiceLine.java
Normal file
58
pdf/src/test/java/de/muehlencord/shared/pdf/InvoiceLine.java
Normal file
@ -0,0 +1,58 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class InvoiceLine {
|
||||
|
||||
private String description;
|
||||
private String price;
|
||||
private String amount;
|
||||
private String total;
|
||||
|
||||
public InvoiceLine(String description, String price, String amount, String total) {
|
||||
this.description = description;
|
||||
this.price = price;
|
||||
this.amount = amount;
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(String price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(String amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public String getTotal() {
|
||||
return total;
|
||||
}
|
||||
|
||||
public void setTotal(String total) {
|
||||
this.total = total;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -1,17 +1,13 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -21,18 +17,11 @@ import org.junit.Test;
|
||||
*/
|
||||
@FixMethodOrder
|
||||
public class PDFDocumentTest {
|
||||
|
||||
private static Gson gson;
|
||||
|
||||
|
||||
private String jsonString = null;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
gson = GsonUtil.getGsonInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToJson() throws FileNotFoundException, IOException, ConfigurationException {
|
||||
public void testToJson() throws FileNotFoundException, IOException, ConfigurationException, TemplateException {
|
||||
System.out.println("testToJson");
|
||||
PDFDocument doc = new PDFDocument();
|
||||
doc.addFont("bold", new Font("Helvetica-Bold", 12, 2));
|
||||
@ -72,19 +61,20 @@ public class PDFDocumentTest {
|
||||
TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold"));
|
||||
invoiceLines.getHeaders()
|
||||
.add ("Menge", 100)
|
||||
.add ("Beschreibung", 100)
|
||||
.add ("Beschreibung", 300)
|
||||
.add ("Einzelpreis", 100)
|
||||
.add ("Summe", 100);
|
||||
doc.addContent(invoiceLines);
|
||||
invoiceLines.addLine("1","Anzeige Hövelhofer Rundschau", "10", "10");
|
||||
invoiceLines.addLine ("${invoiceline.amount}", "${invoiceline.description}", "${invoiceline.price}", "${invoiceline.total}").createList("invoiceLines", "invoiceline");
|
||||
invoiceLines.addLine("2","Anzeige Hövelhofer Rundschau", "10", "20");
|
||||
doc.addContent(invoiceLines);
|
||||
|
||||
TextContent test = new TextContent (doc)
|
||||
.addLine("Das ist ein Test");
|
||||
doc.addContent (test);
|
||||
|
||||
|
||||
jsonString = gson.toJson(doc);
|
||||
System.out.println(jsonString);
|
||||
|
||||
jsonString = doc.getTemplateString();
|
||||
|
||||
File file = new File("c:/temp/test.ftlh");
|
||||
FileUtils.writeStringToFile(file, jsonString, "UTF-8");
|
||||
|
||||
@ -97,9 +87,16 @@ public class PDFDocumentTest {
|
||||
|
||||
Template template = cfg.getTemplate("test.ftlh");
|
||||
PDFTemplate pdfDoc = new PDFTemplate(template);
|
||||
|
||||
Invoice invoice = new Invoice();
|
||||
invoice.addInvoiceLine(new InvoiceLine ("Product 1", "10", "1", "10"));
|
||||
invoice.addInvoiceLine(new InvoiceLine ("Product 2", "5", "10", "50"));
|
||||
invoice.addInvoiceLine(new InvoiceLine ("Product 3", "100", "20", "2000"));
|
||||
|
||||
pdfDoc.addToDatamodel("invoiceDate", new Date());
|
||||
pdfDoc.addToDatamodel("customerNumber", "8755");
|
||||
pdfDoc.addToDatamodel("invoiceNumber", "1234567");
|
||||
pdfDoc.addToDatamodel("invoiceLines", invoice.getInvoiceLines());
|
||||
pdfDoc.create("c:/temp/test.pdf");
|
||||
}
|
||||
|
||||
|
||||
27
pdf/src/test/java/de/muehlencord/shared/pdf/TextTest.java
Normal file
27
pdf/src/test/java/de/muehlencord/shared/pdf/TextTest.java
Normal file
@ -0,0 +1,27 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class TextTest {
|
||||
|
||||
public TextTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromJson() {
|
||||
String jsonString = "{\n"
|
||||
+ "\"text\": \"Rechnungs-Nr.:\"\n"
|
||||
+ "}";
|
||||
|
||||
Text text = GsonUtil.getGsonInstance().fromJson(jsonString, Text.class);
|
||||
assertNotNull ("text object", text);
|
||||
assertEquals ("text value", "Rechnungs-Nr.:", text.getText());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -12,6 +12,10 @@
|
||||
<logger name="org.hibernate">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
|
||||
<logger name="org.gson">
|
||||
<level value="DEBUG" />
|
||||
</logger>
|
||||
|
||||
<category name="de.muehlencord">
|
||||
<priority value="DEBUG"/>
|
||||
@ -26,7 +30,7 @@
|
||||
</category>
|
||||
|
||||
<root>
|
||||
<level value="INFO" />
|
||||
<level value="DEBUG" />
|
||||
<appender-ref ref="consoleAppender" />
|
||||
</root>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user