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 be following the guideline for developing a plugin to develop our JDBC Options Binder plugin. Please also refer to the very first tutorial How to develop a Bean Shell Hash Variable for more details steps.
Sometime, we may need to write some custom query to populate the options for our multi options field.
Joget Workflow has provided a plugin type called Form Options Binder Plugin. We will develop one to support JDBC connection and custom query.
To develop a JDBC Options binder, we will need the JDBC connection setting and also the custom query to populate the options.
...
The first column of returned JDBC result will be the value of the option and second column is the label of the option. There will be another optional third column for grouping when not using AJAX for cascading drop-down list.
We can refer to the implementation of other available Form Options Binder plugins. Joget default datasource can be retrieve with AppUtil.getApplicationContext().getBean("setupDataSource").
We need to always have our Joget Workflow Source Code ready and built by following this guideline.
...
Open the maven project with your favour IDE. I will be using NetBeans.
Create a "JdbcOptionsBinder" class under "org.joget.tutorial" package. Then, extend the class with org.joget.apps.form.model.FormBinder abstract class.
...
Please refer to Form Options Binder Plugin.
As usual, we have to implement all the abstract methods. We will be using AppPluginUtil.getMessage method to support i18n and using constant variable MESSAGE_PATH for message resource bundle directory.
...
Code Block | ||
---|---|---|
| ||
public FormRowSet loadAjaxOptions(String[] dependencyValues) { FormRowSet rows = new FormRowSet(); rows.setMultiRow(true); //add empty option based on setting if ("true".equals(getPropertyString("addEmpty"))) { FormRow empty = new FormRow(); empty.setProperty(FormUtil.PROPERTY_LABEL, getPropertyString("emptyLabel")); empty.setProperty(FormUtil.PROPERTY_VALUE, ""); rows.add(empty); } //Check the sql. If require dependency value and dependency value is not exist, return empty result. String sql = getPropertyString("sql"); if ((dependencyValues == null || dependencyValues.length == 0) && sql.contains("?")) { return rows; } Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; try { DataSource ds = createDataSource(); con = ds.getConnection(); //support for multiple dependency values if (sql.contains("?") && dependencyValues != null && dependencyValues.length > 1) { String mark = "?"; for (int i = 1; i < dependencyValues.length; i++) { mark += ", ?"; } sql = sql.replace("?", mark); } pstmt = con.prepareStatement(sql); //set query parameters if (sql.contains("?") && dependencyValues != null && dependencyValues.length > 0) { for (int i = 0; i < dependencyValues.length; i++) { pstmt.setObject(i + 1, dependencyValues[i]); } } rs = pstmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); int columnsNumber = rsmd.getColumnCount(); // Set retrieved result to Form Row Set while (rs.next()) { FormRow row = new FormRow(); String value = rs.getString(1); String label = rs.getString(2); row.setProperty(FormUtil.PROPERTY_VALUE, (value != null)?value:""); row.setProperty(FormUtil.PROPERTY_LABEL, (label != null)?label:""); if (columnsNumber > 2) { String grouping = rs.getString(3); row.setProperty(FormUtil.PROPERTY_GROUPING, grouping); } rows.add(row); } } catch (Exception e) { LogUtil.error(getClassName(), e, ""); } finally { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (con != null) { con.close(); } } catch (Exception e) { LogUtil.error(getClassName(), e, ""); } } return rows; } /** * To creates data source based on setting * @return * @throws Exception */ protected DataSource createDataSource() throws Exception { DataSource ds = null; String datasource = getPropertyString("jdbcDatasource"); if ("default".equals(datasource)) { // use current datasource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource"); } else { // use custom datasource Properties dsProps = new Properties(); dsProps.put("driverClassName", getPropertyString("jdbcDriver")); dsProps.put("url", getPropertyString("jdbcUrl")); dsProps.put("username", getPropertyString("jdbcUser")); dsProps.put("password", getPropertyString("jdbcPassword")); ds = BasicDataSourceFactory.createDataSource(dsProps); } return ds; } |
Our plugin is using dbcp, javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse class, so we will need to add jsp-api and commons-dbcp library to our POM file.
Code Block | ||
---|---|---|
| ||
<!-- Change plugin specific dependencies here --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.3</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.
...
Code Block |
---|
org.joget.tutorial.JdbcOptionsBinder.pluginLabel=JDBC Binder org.joget.tutorial.JdbcOptionsBinder.pluginDesc=Used to load field's options using JDBC form.jdbcOptionsBinder.config=Configure JDBC Binder form.jdbcOptionsBinder.datasource=Datasource form.jdbcOptionsBinder.customDatasource=Custom Datasource form.jdbcOptionsBinder.defaultDatasource=Default Datasource form.jdbcOptionsBinder.driver=Custom JDBC Driver form.jdbcOptionsBinder.driver.desc=Eg. com.mysql.jdbc.Driver (MySQL), oracle.jdbc.driver.OracleDriver (Oracle), com.microsoft.sqlserver.jdbc.SQLServerDriver (Microsoft SQL Server) form.jdbcOptionsBinder.url=Custom JDBC URL form.jdbcOptionsBinder.username=Custom JDBC Username form.jdbcOptionsBinder.password=Custom JDBC Password form.jdbcOptionsBinder.useAjax=Use AJAX for cascade options? form.jdbcOptionsBinder.addEmpty=Add Empty Option? form.jdbcOptionsBinder.emptyLabel=Empty Option Label form.jdbcOptionsBinder.sql=SQL SELECT Query form.jdbcOptionsBinder.sql.desc=Use question mark (?) in your query to represent dependency values when using AJAX form.jdbcOptionsBinder.testConnection=Test Connection form.jdbcOptionsBinder.connectionOk=Database connected form.jdbcOptionsBinder.connectionFail=Not able to establish connection. |
We will have to register our plugin class in Activator class (Auto generated in the same class package) to tell the Felix Framework that this is a plugin.
Code Block | ||
---|---|---|
| ||
public void start(BundleContext context) { registrationList = new ArrayList<ServiceRegistration>(); //Register plugin here registrationList.add(context.registerService(JdbcOptionsBinder.class.getName(), new JdbcOptionsBinder(), null)); } |
Let build our plugin. Once the building process is done, we will find that a "jdbc_options_binder-5.0.0.jar" file is created under "jdbc_options_binder/target" directory.
...
Yes, it works as well. Then, we can test the custom configuration and the test connection button.
You can download the source code from jdbc_options_binder_src.zip.
...