Class 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
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
FieldsModifier and TypeFieldDescriptionprotected byte[]
The array of bytes of the PDF document. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected com.fasterxml.jackson.databind.ObjectMapper
Returns the JacksonObjectMapper
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 byPdfTemplate.generate()
as an array of bytesprotected 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
setPayloadJson(String json)
Takes a JSON string and creates the payload for the template from it.void
setPayloadMap(Map<String,Object> map)
Takes a Java map and creates the payload for the template from it.toString()
boolean
validate()
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[] pdfContentThe 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 thegetPdfContent()
method once generation is complete.
-
-
Constructor Details
-
AbstractJacksonPdfTemplate
public AbstractJacksonPdfTemplate()
-
-
Method Details
-
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 byPdfTemplate.getPayloadClass()
.- Specified by:
getPayload
in interfacePdfTemplate<T>
- Returns:
- the payload
-
setPayload
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 byPdfTemplate.getPayloadClass()
.- Specified by:
setPayload
in interfacePdfTemplate<T>
- Parameters:
payload
- the payload
-
validate
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 interfacePdfTemplate<T>
- Returns:
true
if the payload is valid and can be safely used to generate PDFs,false
otherwise- Throws:
MissingPayloadException
-
initPayload
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 overridesetPayload
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 byvalidate()
after it check that the payload is not null. There is therefore no need to overridevalidate
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 byPdfTemplate.generate()
as an array of bytes- Specified by:
getPdfContent
in interfacePdfTemplate<T>
- Returns:
- the bytes of the generated PDF document
-
getJsonMapper
protected com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()Returns the JacksonObjectMapper
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
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 JacksonObjectMapper
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
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 JacksonObjectMapper
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
-