Class AbstractJacksonPdfTemplate<T>

java.lang.Object
org.nekosoft.pdffer.template.AbstractJacksonPdfTemplate<T>
Type Parameters:
T - the type of the payload that this PDF template can handle
All Implemented Interfaces:
PdfTemplate<T>
Direct Known Subclasses:
DefaultPdfTemplate, PdfInvoiceTemplate

public abstract class AbstractJacksonPdfTemplate<T> extends Object implements PdfTemplate<T>

An abstract template that works with JSON based payloads and tries to turn them into the payload class that the template can handle. The following PdfferProducerBean methods require that the template extend this class

  • generatePdfDocumentByPathFromJsonMap
  • generatePdfDocumentByPathFromJsonString
  • generatePdfDocumentFromJsonMap
  • generatePdfDocumentFromJsonMap
  • generatePdfDocumentFromJsonString
  • generatePdfDocumentFromJsonString
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    The array of bytes of the PDF document.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected com.fasterxml.jackson.databind.ObjectMapper
    Returns the Jackson ObjectMapper that is used for converting payloads from JSON strings and Java maps to the payload type of the template.
    Returns the payload used by this instance to generate a PDF document.
    byte[]
    Returns the PDF document that was generated by PdfTemplate.generate() as an array of bytes
    protected T
    initPayload​(T payload)
    Override this method to initialize the template and its parts once a payload has been set.
    void
    setPayload​(T payload)
    Sets the payload that should be used by this instance to generate a PDF document.
    void
    Takes a JSON string and creates the payload for the template from it.
    void
    Takes a Java map and creates the payload for the template from it.
     
    boolean
    Checks that the payload is consistent with the requirements of the template.
    protected boolean
    Provide your validation logic for the template.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface org.nekosoft.pdffer.template.PdfTemplate

    generate, getPayloadClass
  • Field Details

    • pdfContent

      protected byte[] pdfContent

      The array of bytes of the PDF document.

      The PdfTemplate.generate() method must create this array and populate it with the bytes of the PDF document that it creates. It must be done before the method returns, so that it is available to clients via the getPdfContent() method once generation is complete.

  • Constructor Details

    • AbstractJacksonPdfTemplate

      public AbstractJacksonPdfTemplate()
  • Method Details

    • getPayload

      public T getPayload()
      Description copied from interface: PdfTemplate
      Returns the payload used by this instance to generate a PDF document. It must be of the type indicated by PdfTemplate.getPayloadClass().
      Specified by:
      getPayload in interface PdfTemplate<T>
      Returns:
      the payload
    • setPayload

      public void setPayload(T payload)
      Description copied from interface: PdfTemplate
      Sets the payload that should be used by this instance to generate a PDF document. It must be of the type indicated by PdfTemplate.getPayloadClass().
      Specified by:
      setPayload in interface PdfTemplate<T>
      Parameters:
      payload - the payload
    • validate

      public boolean validate() throws MissingPayloadException
      Description copied from interface: PdfTemplate

      Checks that the payload is consistent with the requirements of the template.

      The concept of "valid payload" is entirely up to each PdfTemplate instance.

      Specified by:
      validate in interface PdfTemplate<T>
      Returns:
      true if the payload is valid and can be safely used to generate PDFs, false otherwise
      Throws:
      MissingPayloadException
    • initPayload

      protected T initPayload(T payload)

      Override this method to initialize the template and its parts once a payload has been set. For example, you could configure the JSON mapper or any formatters your template might use based on information in the payload. The method also allows you to completely change the payload if you need to and return a different one to be set into the template.

      This method is called by setPayload(Object), so there is no need to ever override setPayload itself.

      The default implementation does nothing and then returns the same value it receives.

      Parameters:
      payload - the payload that is being set into the template
      Returns:
      the payload to be set into the template (in most cases you will simply return the same value you receive)
    • validatePayload

      protected boolean validatePayload()

      Provide your validation logic for the template. Return true if the payload is valid and can be used for PDF generation. This method is invoked by validate() after it check that the payload is not null. There is therefore no need to override validate itself.

      The default implementation returns true unconditionally.

      Returns:
      true if the payload is valid and can be safely used to generate PDFs, false otherwise
    • getPdfContent

      public byte[] getPdfContent()
      Description copied from interface: PdfTemplate
      Returns the PDF document that was generated by PdfTemplate.generate() as an array of bytes
      Specified by:
      getPdfContent in interface PdfTemplate<T>
      Returns:
      the bytes of the generated PDF document
    • getJsonMapper

      protected com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()
      Returns the Jackson ObjectMapper that is used for converting payloads from JSON strings and Java maps to the payload type of the template.
      Returns:
      the Jackson ObjectMapper used by this template
    • setPayloadMap

      public void setPayloadMap(Map<String,​Object> map) throws PayloadFormatException

      Takes a Java map and creates the payload for the template from it. The map must contain all the attributes required by the payload class (as specified by the PdfTemplate.getPayloadClass() method), and a Jackson ObjectMapper will be used for the conversion.

      This will trigger a call to initPayload(Object)

      .
      Parameters:
      map - the Java map representation of the payload
      Throws:
      PayloadFormatException - if the map could not successfully be converted to a payload class instance
    • setPayloadJson

      public void setPayloadJson(String json) throws PayloadFormatException

      Takes a JSON string and creates the payload for the template from it. The JSON string must contain all the attributes required by the payload class (as specified by the PdfTemplate.getPayloadClass() method), and a Jackson ObjectMapper will be used for the conversion.

      This will trigger a call to initPayload(Object)

      .
      Parameters:
      json - the JSON representation of the payload
      Throws:
      PayloadFormatException - if the JSON string could not successfully be converted to a payload class instance
    • toString

      public String toString()
      Overrides:
      toString in class Object