Join us on May 14-15, 2019 at Forrester Digital Transformation & Innovation 2019 in Chicago, USA.
In this tutorial, we will following the guideline of developing a plugin to develop our Form Submission Statistics Generator. Please also refer to the very first tutorial How to develop a Bean Shell Hash Variable for more details steps.
1. What is the problem?
We have 2 SQL Chart menus (the queries used are dependent on MySQL) to show form data submission statistics and this 2 menus always need to replicate for different form.
2. What is your idea to solve the problem?
We can develop a Generator Plugin to ease the process to replicate the 2 SQL Chart menus for other forms.
3. What is the input needed for your plugin?
To develop a Generator Plugin for our 2 SQL Chart menus, we can consider to provide the following as input.
- Userview ID : Which userview to add this 2 SQL menus
- Some option for label change in Category label, Menu label, and Chart label
4. What is the output and expected outcome of your plugin?
2 SQL Chart menus will be added to the selected userview under a new category. One of the menu will show the monthly submission chart and another one will show the daily submission chart based on the year and month filter.
5. Is there any resources/API that can be reuse?
First, we can do build our 2 SQL Chart menus in one of the existing userview. Then, copy the JSON definition of the category which contains our 2 SQL Chart menus form the "ADVANCED: JSON Definition" on the bottom of the Userview Builder.
We will get our JSON definition of the category as following. Please note that the queries used in the 2 SQL Chart menus are dependent on MySQL database.
After that, we can utilize the GeneratorUtil to add the category JSON definition to our selected userview JSON definition.
6. Prepare your development environment
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 for developing a plugin for other platform command.
Let said our folder directory as following.
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.
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.
We should get "BUILD SUCCESS" message shown in our terminal and a "form_submission_statistics_generator" folder created in "plugins" folder.
Open the maven project with your favour IDE. I will be using NetBeans.
7. Just code it!
a. Extending the abstract class of a plugin type
Create a "FormSubmissionStatisticsGenerator" class under "org.joget.tutorial" package. Then, extend the class with org.joget.apps.generator.model.GeneratorPlugin abstract class. Please refer to Generator Plugin.
b. Implement all the abstract methods
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/formSubmissionStatisticsGenerator.json". Let us create a directory "resources/properties" under "form_submission_statistics_generator/src/main" directory. After create the directory, create a file named "formSubmissionStatisticsGenerator.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.
In the plugin properties option, you may notice we are using "[default_userviewId]" in "userviewId" property. We will use the "[formName]" and "[formId]" in some properties as well. So, we will need to modify the getPropertyOptions method to cater for this values.
Once we done the properties option to collect input, we can work on the main method of the plugin which is format method.
In the getCategoryJson method, we will retrieve the category JSON definition form "/resources/category.json" file. Let us create a directory "resources/resources" under "form_submission_statistics_generator/src/main" directory. After create the directory, create a file named "category.json" in the "resources" folder. Then, copy the category JSON definition we created previously and paste it inside this file. We will need to replace some of the hardcoded value as variable and remember to escape those existing "%" to "%%" as the AppUtil.readPluginResource using String.format for injects value into the file.
c. Manage the dependency libraries of your plugin
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.
d. Make your plugin internationalization (i18n) ready
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 "form_submission_statistics_generator/src/main" directory. Then, create a "FormSubmissionStatisticsGenerator.properties" file in the folder. In the properties file, let add all the message keys and its label as below.
e. Register your plugin to Felix Framework
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.
f. Build it and testing
Let build our plugin. Once the building process is done, we will found a "form_submission_statistics_generator-5.0.0.jar" file is created under "form_submission_statistics_generator/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.
Let us check the properties page for "Advanced Generator".
After the generation process is done.
A new category is added to our userview.
The SQL chart menus will shown as following.
8. Take a step further, share it or sell it
You can download the source code from form_submission_statistics_generator.zip.
To download the ready-to-use plugin jar, please find it in http://marketplace.joget.org/.