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
generatePdfDocumentByPathFromJsonMapgeneratePdfDocumentByPathFromJsonStringgeneratePdfDocumentFromJsonMapgeneratePdfDocumentFromJsonMapgeneratePdfDocumentFromJsonStringgeneratePdfDocumentFromJsonString
-
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.ObjectMapperReturns the JacksonObjectMapperthat 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 TinitPayload(T payload)Override this method to initialize the template and its parts once a payload has been set.voidsetPayload(T payload)Sets the payload that should be used by this instance to generate a PDF document.voidsetPayloadJson(String json)Takes a JSON string and creates the payload for the template from it.voidsetPayloadMap(Map<String,Object> map)Takes a Java map and creates the payload for the template from it.toString()booleanvalidate()Checks that the payload is consistent with the requirements of the template.protected booleanProvide your validation logic for the template.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods 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:PdfTemplateReturns the payload used by this instance to generate a PDF document. It must be of the type indicated byPdfTemplate.getPayloadClass().- Specified by:
getPayloadin interfacePdfTemplate<T>- Returns:
- the payload
-
setPayload
Description copied from interface:PdfTemplateSets 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:
setPayloadin interfacePdfTemplate<T>- Parameters:
payload- the payload
-
validate
Description copied from interface:PdfTemplateChecks that the payload is consistent with the requirements of the template.
The concept of "valid payload" is entirely up to each
PdfTemplateinstance.- Specified by:
validatein interfacePdfTemplate<T>- Returns:
trueif the payload is valid and can be safely used to generate PDFs,falseotherwise- 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 mapperor 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 overridesetPayloaditself.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
trueif 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 overridevalidateitself.The default implementation returns true unconditionally.
- Returns:
trueif the payload is valid and can be safely used to generate PDFs,falseotherwise
-
getPdfContent
public byte[] getPdfContent()Description copied from interface:PdfTemplateReturns the PDF document that was generated byPdfTemplate.generate()as an array of bytes- Specified by:
getPdfContentin interfacePdfTemplate<T>- Returns:
- the bytes of the generated PDF document
-
getJsonMapper
protected com.fasterxml.jackson.databind.ObjectMapper getJsonMapper()Returns the JacksonObjectMapperthat is used for converting payloads from JSON strings and Java maps to the payload type of the template.- Returns:
- the Jackson
ObjectMapperused 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 JacksonObjectMapperwill 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 JacksonObjectMapperwill 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
-