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 Bean Shell Hash Variable plugin.
E.g. #beanshell.EnvironmentVariableKey#
But, this may not be enough, we may need some other way to pass in some variable also. We can consider using a URL query parameters syntax to pass in our variables because it is easier to parse later on.
E.g. #beanshell.EnvironmentVariableKey[name=Joget&email=info@joget.org&message={form.sample.message?url}]#
E.g. Display a welcome message for logged in user but display nothing when the user is an anonymous.
We can use getUrlParams method from StringUtil to help us parse parameters passed in with URL query parameters syntax.
The following tutorial is prepared with a Macbook Pro and Joget Source Code version 5.0.0. Please refer to the Guideline for Developing a Plugin article for other platform commands.
Let say our folder directory is 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 beanshell_hash_variable 5.0.0
Then, the shell script will ask us to key in a version number for the plugin and ask us for a confirmation before it generates 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: beanshell_hash_variable version: 5.0.0 package: org.joget.tutorial Y: : y
We should get "BUILD SUCCESS" message shown in our terminal and a "beanshell_hash_variable" folder created in "plugins" folder.
Open the maven project with your favour IDE. We will be using NetBeans.
Then, based on Hash Variable Plugin document, we will have to extends org.joget.apps.app.model.DefaultHashVariablePlugin abstract class.
Now, let's focus on the main method of our Hash Variable plugin which is processHashVariable. We will refer to the source code of Environment Variable Hash Variable plugin on how to retrieve the Environment variable. Then, refer to the source code of Bean Shell Form Binder on how to execute a bean shell script.
public String processHashVariable(String variableKey) { try { String environmentVariableKey = variableKey; //first check and retrieve parameters passed in with URL query parameters syntax wrapped in square bracket [] String queryParams = null; if (variableKey.contains("[") && variableKey.contains("]")) { queryParams = variableKey.substring(variableKey.indexOf("[") + 1, variableKey.indexOf("]")); environmentVariableKey = variableKey.substring(0, variableKey.indexOf("[")); } //Parse the query parameters to a map Map<String, String[]> parameters = null; if (queryParams != null && !queryParams.isEmpty()) { parameters = StringUtil.getUrlParams(queryParams); //put all parameters to plugin properties getProperties().putAll(parameters); } //Retrieve the environment variable based on environmentVariableKey AppDefinition appDef = (AppDefinition) getProperty("appDefinition"); if (appDef != null) { ApplicationContext appContext = AppUtil.getApplicationContext(); EnvironmentVariableDao environmentVariableDao = (EnvironmentVariableDao) appContext.getBean("environmentVariableDao"); EnvironmentVariable env = environmentVariableDao.loadById(environmentVariableKey, appDef); if (env != null) { String script = env.getValue(); //execute the script with all plugin properties return executeScript(script, getProperties()); } else { //environment variable not found, return empty value return ""; } } } catch (Exception e) { //log the exception using LogUtil LogUtil.error(getClassName(), e, "#beanshell."+variableKey+"# fail to parse."); } //return null to by pass the replacing return null; } protected String executeScript(String script, Map properties) throws Exception { Interpreter interpreter = new Interpreter(); interpreter.setClassLoader(getClass().getClassLoader()); for (Object key : properties.keySet()) { interpreter.set(key.toString(), properties.get(key)); } LogUtil.debug(getClass().getName(), "Executing script " + script); return (String) interpreter.eval(script); }
<!-- Change plugin specific dependencies here --> <dependency> <groupId>org.beanshell</groupId> <artifactId>bsh</artifactId> <version>2.0b4</version> </dependency> <!-- End change plugin specific dependencies here -->
In our properties file, we will need to add the key we have used.
org.joget.tutorial.BeanShellHashVariable.pluginLabel=Bean Shell Hash Variable org.joget.tutorial.BeanShellHashVariable.pluginDesc=Using environment variable to execute bean shell script.
public void start(BundleContext context) { registrationList = new ArrayList<ServiceRegistration>(); //Register plugin here registrationList.add(context.registerService(BeanShellHashVariable.class.getName(), new BeanShellHashVariable(), null)); }
Then, let's upload the plugin jar to Manage Plugins. After uploading the jar file, double check that the plugin is uploaded and activated correctly.
Now, let's test our plugin.
Let assume that we have a HTML menu page in a userview that wants to display the following line to logged in user. Normally, we will use "Welcome #currentUser,username#," to display a welcome message.
But, in this use case there is a problem, which shows "Welcome ," without an username when the user is an anonymous.
Now, change the whole message to our Bean Shell Hash Variable and create an environment variable to put our script.
Change:Welcome #currentUser.username#,
to the following. We will need to pass the current user's username as one of our parameters and do not forget to escape it as url.
#beanshell.welcome[username={currentUser.username?url}]?html#
Then, we can create an environment variable with ID "welcome" and use the following script. As we are using getUrlParams method from StringUtil to parse the parameters, all value from parameters are String array.
//all parameters passed in from Beanshell Hash Variable will converted to String array if (username != null && username.length == 1 && !username[0].isEmpty()) { return "Welcome " + username[0] + ","; } else { return ""; }
Let go back to our HTML menu page to see the result.
When user is logged in, it shows the message correctly.
When no user is logged in, the welcome message is not shown.