JSReport document generation API
The JsReportDocumentGenerator utility class in Brightspot simplifies the process of generating documents using JSReport. This utility class provides two distinct methods, catering to different use cases, allowing developers to seamlessly integrate JSReport document generation within their Brightspot projects. This topic describes each method and its use cases.
Raw Data Method
createDocument(String template, Object data)
Creates a Document for the specified JSReport template and input data.
-
Parameters:
template: The JSReport template to use, specified by either the template name or short ID.data: The data used to render the JSReport template, typically represented as ajava.util.Mapto convey JSON data.
-
Return:
- A
Documentobject containing the document produced by JSReport, including the document's content and content type.
- A
Usage example
1String templateName = "myTemplate";2Map<String, Object> jsonData = // ... prepare JSON data3Document generatedDocument = JsReportDocumentGenerator.createDocument(templateName, jsonData);
View System Method
createDocument(String template, Class dateEntryView, Object content)
Creates a Document using the specified template, dateEntryView, and content, leveraging the Brightspot view system.
-
Parameters:
template: The entry view used to find an appropriate view model. If null, theDefaultJsReportTemplateannotation will be checked for a default template.dateEntryView: The entry view used to find an appropriate view model.content: The content providing data for document generation.
-
Return:
- A
Documentobject containing the document produced by JSReport, including the document's content and content type.
- A
Usage example
1Object content = Query.from(MyRecord.class).first(); // contrived example for getting cotent from BSP DB2Document generatedDocument = JsReportDocumentGenerator.createDocument(null, JsReportViewModel.class, content);34①@JsonView5②@ViewInterface6③@DefaultJsReportTemplate("myPdf")7④public class JsReportViewModel extends ViewModel<MyRecord> {89⑤public String getTitle() {10return model.getHeadline();11}1213⑥public String getCompany() {14return model.getCompany();15}16}
- ①Specifies that the ViewModel can be converted to JSON, which is the primary format JSReport accepts.
- ②Specifies that the ViewModel is a ViewInterface, meaning any public getter methods are included in data.
- ③Specifies that the default JSReport template to use for this viewmodel has the name "myPdf". This will be used in the API call from line #2 since a null value was passed for the template parameter.
- ④Marks this class as a ViewModel for the "MyRecord" class.
- ⑤This will result in a "title" key in the resulting JSON passed to JSReport.
- ⑥This will result in a "company" key in the resulting JSON passed to JSReport.