Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Hash variable is convenient in usedto use, but sometime we want to do some condition check before display displaying a value. But, Hash variable does not provide the ability to do for condition checking.

2.

...

How to solve the problem?

So, how to solve this issue? By looking at the Plugin Types that are currently supported by Joget Workflow, we may need to can develop a Hash Variable Plugin to allow us to write our scripting for condition checking. There are quite a number of Bean Shell plugins provided as default plugin for several plugin types. We can do one for Hash Variable plugin as well.

...

Hash Variable plugin does not provide interface for user to configure, but to develop a Bean Shell Hash Variable plugin, we need somewhere to put our Bean Shell script. We can reuse the Environment Variable to store our script. So the Hash Variable syntax will be a prefix with environment variable key.

eE.g. : #beanshell.EnvironmentVariableKey#

But, this may not be enough, we may need some other way to pass in some variable also. We can consider to use using a URL query parameters syntax to pass in our variables because it is easier to parse later on.

eE.g. : #beanshell.EnvironmentVariableKey[name=Joget&email=info@joget.org&message={form.sample.message?url}]#

4. What is the output and expected outcome of your plugin?

What are do we expected from this Bean Shell Hash variable plugin? The Bean Shell Hash Variable plugin is for admin/developer user to use while when building the /developing an app. Once it is used, the Hash Variable will be replaced by the output return from the Bean Shell interpreter. So that the admin user can do condition check before display something to normal user.

eE.g. : Display a welcome message for logged in user but display nothing when the user is an anonymous.

5.

...

Are there any resources/API that can be

...

reused?

To develop Bean Shell Hash Variable plugin, we can refer to the source code of all the Hash Variable plugin and Bean Shell plugin. Especially, we can refer to the Environment Variable Hash Variable plugin on how to retrieve environment variable using a variable key. We can also refer to the Bean Shell Tool or Bean Shell Form Binder plugin on what to execute the script with Bean Shell interpreter. 

...

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 to the Guideline for developing a plugin for  article for other platform commandcommands.

Let said say our folder directory is as following. 

Code Block
- Home
  - joget
    - plugins
    - jw-community
      -5.0.0

...

Then, the shell script will ask us to key in a version number for your the plugin and ask us for a confirmation before generate it generates the maven project.

Code Block
languagebash
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

...

b. Implement all the abstract methods

Let us 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
languagejava
titleImplementation of all basic abstract methods
collapsetrue
package org.joget.tutorial;
 
import org.joget.apps.app.model.DefaultHashVariablePlugin;
import org.joget.apps.app.service.AppPluginUtil;
 
public class BeanShellHashVariable extends DefaultHashVariablePlugin {
    
    private final static String MESSAGE_PATH = "messages/BeanShellHashVariable";
 
    public String getName() {
        return "BeanShellHashVariable";
    }
 
    public String getVersion() {
        return "5.0.0";
    }
 
    public String getClassName() {
        return getClass().getName();
    }
    
    public String getLabel() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.BeanShellHashVariable.pluginLabel", getClassName(), MESSAGE_PATH);
    }
    
    public String getDescription() {
        //support i18n
        return AppPluginUtil.getMessage("org.joget.tutorial.BeanShellHashVariable.pluginDesc", getClassName(), MESSAGE_PATH);
    }
 
    public String getPropertyOptions() {
        //Hash variable plugin do not support property options
        return "";
    }
    
    public String getPrefix() {
        return "beanshell";
    }
    
    public String processHashVariable(String variableKey) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}

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.

...

In our properties file, we will need to add the key we had have used.

Code Block
languagetext
org.joget.tutorial.BeanShellHashVariable.pluginLabel=Bean Shell Hash Variable
org.joget.tutorial.BeanShellHashVariable.pluginDesc=Using environment variable to execute bean shell script.

e. Register your plugin to the Felix Framework

We will have to register our plugin class in Activator class to tell the Felix Framework that this is a plugin.

...

Code Block
languagejava
public void start(BundleContext context) {
    registrationList = new ArrayList<ServiceRegistration>();

    //Register plugin here
    registrationList.add(context.registerService(BeanShellHashVariable.class.getName(), new BeanShellHashVariable(), null));
}

f. Build it and

...

test

Let build our plugin. Once the building process is done, we will found find that a "beanshell_hash_variable-5.0.0.jar" file is created under "beanshell_hash_variable/target" directory.

 Then, let's upload the plugin jar to Manage Plugins. After upload 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 want that wants to display the following line to logged in user. Normally, we will use "Welcome #currentUser,username#," to display a welcome message.

But, this way has in this use case there is a problem, which is it shown 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 the following:

Code Block
Welcome #currentUser.username#,

to the following. We we will need to pass the current user's username as one of our parameters and do not forget to escape it as url.

...

Then, we can create an environment variable with ID "welcome" and use the following script. Due to As we are using getUrlParams method from StringUtil to parse the parameters, all value from parameters are String array.

...

When user is logged in, it shown shows the message correctly.

When no user is logged in, it did not show the welcome message is not shown.

8. Take a step further, share it or sell it

...