Joget DX 8 Stable Released
The stable release for Joget DX 8 is now available, with a focus on UX and Governance.
In this tutorial, we will following the guideline of developing a plugin to develop our Download PDF Datalist Action plugin. Please also refer to the very first tutorial How to develop a Bean Shell Hash Variable for more details steps.
We need to provide a PDF download feature when user click on a link/button on a datalist. The downloaded PDF file should be a form with data populated.
We can develop a Datalist Action Plugin to display a button for generate a form PDF file.
To develop a PDF Download Datalist Action plugin, we can consider to provide the following as input.
When PDF Download Datalist Action is used as datalist row action or column action, a normal user will see a link to download the PDF file in every rows of a datalist. Once the link is clicked, a PDF will be prompt to download.
When the plugin is used as whole datalist action, a zip file containing all the generated PDF of every selected rows will be prompt to download when the button is clicked.
To develop the PDF Download Datalist Action plugin, we can reuse the methods in FormPdfUtil to generate a form as PDF. We can also refer to the source code of the Datalist Form Data Delete Action plugin as well. Other than that, we can refer to the Export Form Email Tool on what kind of plugin properties options we can provide in the plugin as the Export Form Email Tool are using the methods in FormPdfUtil as well.
We need to always have our Joget Workflow Source Code ready and builded by following this guideline.
The following of this tutorial is prepared with a Macbook Pro and Joget Source Code version 5.0.0. Please refer to Guideline of developing a plugin for other platform command.
Let said our folder directory as following.
- Home - joget - plugins - jw-community -5.0.0
The "plugins" directory is the folder we will create and store all our plugins and the "jw-community" directory is where the Joget Workflow Source code stored.
Run the following command to create a maven project in "plugins" directory.
cd joget/plugins/ ~/joget/jw-community/5.0.0/wflow-plugin-archetype/create-plugin.sh org.joget.tutorial download_pdf_datalist_action 5.0.0
Then, the shell script will ask us to key in a version for your plugin and ask us for confirmation before generate the maven project.
Define value for property 'version': 1.0-SNAPSHOT: : 5.0.0 [INFO] Using property: package = org.joget.tutorial Confirm properties configuration: groupId: org.joget.tutorial artifactId: download_pdf_datalist_action version: 5.0.0 package: org.joget.tutorial Y: : y
We should get "BUILD SUCCESS" message shown in our terminal and a "download_pdf_datalist_action" folder created in "plugins" folder.
Open the maven project with your favour IDE. I will be using NetBeans.
Create a "DownloadPdfDatalistAction" class under "org.joget.tutorial" package. Then, extend the class with org.joget.apps.datalist.model.DataListActionDefault abstract class. Please refer to Datalist Action Plugin.
As usual, we have to implement all the abstract methods. We will using AppPluginUtil.getMessage method to support i18n and using constant variable MESSAGE_PATH for message resource bundle directory.
Then, we have to do a UI for admin user to provide inputs for our plugin. In getPropertyOptions method, we already specify our Plugin Properties Options definition file is locate at "/properties/downloadPdfDatalistAction.json". Let us create a directory "resources/properties" under "download_pdf_datalist_action/src/main" directory. After create the directory, create a file named "downloadPdfDatalistAction.json" in the "properties" folder.
In the properties definition options file, we will need to provide options as below. Please note that we can use "@@message.key@@" syntax to support i18n in our properties options.
[{ title : '@@datalist.downloadPdf.config@@', properties : [{ name : 'label', label : '@@datalist.downloadPdf.label@@', type : 'textfield', value : '@@datalist.downloadPdf.download@@' }, { name : 'formDefId', label : '@@datalist.downloadPdf.form@@', type : 'selectbox', options_ajax : '[CONTEXT_PATH]/web/json/console/app[APP_PATH]/forms/options', required : 'True' }, { name : 'recordIdColumn', label : '@@datalist.downloadPdf.recordIdColumn@@', description : '@@datalist.downloadPdf.recordIdColumn.desc@@', type : 'textfield' }, { name : 'confirmation', label : '@@datalist.downloadPdf.confirmationMessage@@', type : 'textfield' }] }, { title : '@@datalist.downloadPdf.advanced@@', properties : [{ name : 'formatting', label : '@@datalist.downloadPdf.formatting@@', type : 'codeeditor', mode : 'css' }, { name : 'headerHtml', label : '@@datalist.downloadPdf.headerHtml@@', type : 'codeeditor', mode : 'html' }, { name : 'repeatHeader', label : '@@datalist.downloadPdf.repeatHeader@@', type : 'checkbox', options : [{ value : 'true', label : '' }] }, { name : 'footerHtml', label : '@@datalist.downloadPdf.footerHtml@@', type : 'codeeditor', mode : 'html' }, { name : 'repeatFooter', label : '@@datalist.downloadPdf.repeatFooter@@', type : 'checkbox', options : [{ value : 'true', label : '' }] }, { name : 'hideEmptyValueField', label : '@@datalist.downloadPdf.hideEmptyValueField@@', type : 'checkbox', options : [{ value : 'true', label : '' }] }, { name : 'showNotSelectedOptions', label : '@@datalist.downloadPdf.showNotSelectedOptions@@', type : 'checkbox', options : [{ value : 'true', label : '' }] }] }]
After done the properties option to collect input, we can work on the main method of the plugin which is executeAction method.
public DataListActionResult executeAction(DataList dataList, String[] rowKeys) { // only allow POST HttpServletRequest request = WorkflowUtil.getHttpServletRequest(); if (request != null && !"POST".equalsIgnoreCase(request.getMethod())) { return null; } // check for submited rows if (rowKeys != null && rowKeys.length > 0) { try { //get the HTTP Response HttpServletResponse response = WorkflowUtil.getHttpServletResponse(); if (rowKeys.length == 1) { //generate a pdf for download singlePdf(request, response, rowKeys[0]); } else { //generate a zip of all pdfs multiplePdfs(request, response, rowKeys); } } catch (Exception e) { LogUtil.error(getClassName(), e, "Fail to generate PDF for " + ArrayUtils.toString(rowKeys)); } } //return null to do nothing return null; } /** * Handles for single pdf file * @param request * @param response * @param rowKey * @throws IOException * @throws javax.servlet.ServletException */ protected void singlePdf(HttpServletRequest request, HttpServletResponse response, String rowKey) throws IOException, ServletException { byte[] pdf = getPdf(rowKey); writeResponse(request, response, pdf, rowKey+".pdf", "application/pdf"); } /** * Handles for multiple files download. Put all pdfs in zip. * @param request * @param response * @param rowKeys * @throws java.io.IOException * @throws javax.servlet.ServletException */ protected void multiplePdfs(HttpServletRequest request, HttpServletResponse response, String[] rowKeys) throws IOException, ServletException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(baos); try { //create pdf and put in zip for (String id : rowKeys) { byte[] pdf = getPdf(id); zip.putNextEntry(new ZipEntry(id+".pdf")); zip.write(pdf); zip.closeEntry(); } zip.finish(); writeResponse(request, response, baos.toByteArray(), getLinkLabel() +".zip", "application/zip"); } finally { baos.close(); zip.flush(); } } /** * Generate PDF using FormPdfUtil * @param id * @return */ protected byte[] getPdf(String id) { AppDefinition appDef = AppUtil.getCurrentAppDefinition(); String formDefId = getPropertyString("formDefId"); Boolean hideEmptyValueField = null; if (getPropertyString("hideEmptyValueField").equals("true")) { hideEmptyValueField = true; } Boolean showNotSelectedOptions = null; if (getPropertyString("showNotSelectedOptions").equals("true")) { showNotSelectedOptions = true; } Boolean repeatHeader = null; if ("true".equals(getPropertyString("repeatHeader"))) { repeatHeader = true; } Boolean repeatFooter = null; if ("true".equals(getPropertyString("repeatFooter"))) { repeatFooter = true; } String css = null; if (!getPropertyString("formatting").isEmpty()) { css = getPropertyString("formatting"); } String header = null; if (!getPropertyString("headerHtml").isEmpty()) { header = getPropertyString("headerHtml"); header = AppUtil.processHashVariable(header, null, null, null); } String footer = null; if (!getPropertyString("footerHtml").isEmpty()) { footer = getPropertyString("footerHtml"); footer = AppUtil.processHashVariable(footer, null, null, null); } return FormPdfUtil.createPdf(formDefId, id, appDef, null, hideEmptyValueField, header, footer, css, showNotSelectedOptions, repeatHeader, repeatFooter); } /** * Write to response for download * @param response * @param bytes * @param filename * @param contentType * @throws IOException */ protected void writeResponse(HttpServletRequest request, HttpServletResponse response, byte[] bytes, String filename, String contentType) throws IOException, ServletException { OutputStream out = response.getOutputStream(); try { String name = URLEncoder.encode(filename, "UTF8").replaceAll("\\+", "%20"); response.setHeader("Content-Disposition", "attachment; filename="+name+"; filename*=UTF-8''" + name); response.setContentType(contentType+"; charset=UTF-8"); if (bytes.length > 0) { response.setContentLength(bytes.length); out.write(bytes); } } finally { out.flush(); out.close(); //simply foward to a request.getRequestDispatcher(filename).forward(request, response); } }
Our plugin is using javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse class, so we will need to add jsp-api library to our POM file.
<!-- Change plugin specific dependencies here --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <!-- End change plugin specific dependencies here -->
We are using i18n message key in getLabel and getDescription method. We also used i18n message key in our properties options definition as well. So, we will need to create a message resource bundle properties file for our plugin.
Create directory "resources/messages" under "download_pdf_datalist_action/src/main" directory. Then, create a "DownloadPdfDatalistAction.properties" file in the folder. In the properties file, let add all the message keys and its label as below.
org.joget.tutorial.DownloadPdfDatalistAction.pluginLabel=Download PDF org.joget.tutorial.DownloadPdfDatalistAction.pluginDesc=Support to download form PDF from datalist datalist.downloadPdf.download=Download datalist.downloadPdf.config=Configure Download PDF Action datalist.downloadPdf.label=Label datalist.downloadPdf.form=Form datalist.downloadPdf.recordIdColumn=Record Id Column datalist.downloadPdf.recordIdColumn.desc=Default to the primary key of the configured binder datalist.downloadPdf.confirmationMessage=Confirmation Message datalist.downloadPdf.hideEmptyValueField=Hide field that without value datalist.downloadPdf.showNotSelectedOptions=Show unselected options for multi options field datalist.downloadPdf.advanced=Advanced datalist.downloadPdf.formatting=Formatting (CSS) datalist.downloadPdf.headerHtml=Header (HTML) datalist.downloadPdf.repeatHeader=Repeat header on every page? datalist.downloadPdf.footerHtml=Footer (HTML) datalist.downloadPdf.repeatFooter=Repeat footer on every page?
We will have to register our plugin class in Activator class (Auto generated in the same class package) to tell Felix Framework that this is a plugin.
public void start(BundleContext context) { registrationList = new ArrayList<ServiceRegistration>(); //Register plugin here registrationList.add(context.registerService(DownloadPdfDatalistAction.class.getName(), new DownloadPdfDatalistAction(), null)); }
Let build our plugin. Once the building process is done, we will found a "download_pdf_datalist_action-5.0.0.jar" file is created under "download_pdf_datalist_action/target" directory.
Then, let upload the plugin jar to Manage Plugins. After upload the jar file, double check the plugin is uploaded and activated correctly.
Then, Let try it in one of the datalist. You can find our new plugin is shown under "Actions" in Datalist Builder.
Once we dragged and dropped the "Download PDF" action into the datalist builder canvas, we can edit the action. The following configuration page will shown based on our properties option definition.
Let add the "Download PDF" action as row action and also the whole list action for testing. We can see the "Download" button shown correctly in userview as image below.
When row action is clicked, a pdf is downloaded.
When the whole list action is clicked, a zip is downloaded.
You can download the source code from download_pdf_datalist_action.zip.
To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/.