Versions Compared

Key

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

...

Usages

Use as Form Load Binder
Anchor
Form Load Binder
Form Load Binder

Injected Variables:

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)

    • primaryKey - The primary key provided by the element to load data. (java.lang.String)

    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected

...

Return Object:

    • An org.joget.apps.form.model.FormRowSet object which contains one org.joget.apps.form.model.FormRow object. 

...

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)
    • primaryKey - The primary key provided by the element to load data. (java.lang.String)
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected

...

Return Object:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. All FormRow objects are expected to have "value" and "label" property.

...

    • values - Dependency values of the controlling field. (java.lang.String[])

Expected

...

Return Object:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. All FormRow objects are expected to have "value" and "label" property.

...

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)
    • rows - Data to be store. Contains only one org.joget.apps.form.model.FormRow object. (org.joget.apps.form.model.FormRowSet)
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected

...

Return Object:

    • Same org.joget.apps.form.model.FormRowSet object which stored.

...

    • element - Element that this validator is tie to. (org.joget.apps.form.model.Element)
    • values - The submitted values of the element. (java.lang.String[])
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected

...

Return Object:

    • A boolean value to indicate the validation pass or fail.

...

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)
    • primaryKey - The primary key provided by the element to load data. (java.lang.String)
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected Return Object:

    • An org.joget.apps.form.model.FormRowSet object which contains one or more org.joget.apps.form.model.FormRow object. 

...

Code Block
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FormUtil;
import org.joget.plugin.base.PluginManager;
import org.joget.apps.form.model.FormLoadBinder;

public FormRowSet load(Element element, String primaryKey, FormData formData) {
    String defaultFormDefId = "default_grid_entry"; //change this to the form id used to store default grid data
    String formDefId = "grid_entry";  //change this to the form id used to store grid data
    String foreignKey = "fk"; //change this to the foreign key 
    
    FormRowSet f = new FormRowSet();
    f.setMultiRow(true);

    // Reuse Multi Row Binder to load data
    PluginManager pluginManager = (PluginManager) FormUtil.getApplicationContext().getBean("pluginManager");
    FormLoadBinder binder = (FormLoadBinder) pluginManager.getPlugin("org.joget.plugin.enterprise.MultirowFormBinder");
    
    //Load from the grid table
    binder.setProperty("formDefId", formDefId);
    binder.setProperty("foreignKey", foreignKey);
    f = binder.load(element, primaryKey, formData);
    
    //if no grid data is retrieved, get from default table
    if (f == null || f.isEmpty()) {
        binder.setProperty("formDefId", defaultFormDefId);
        
        //set the foreign key value to empty
        f = binder.load(element, "", formData);
    }
    return f;
}

//call load method with injected variable
return load(element, primaryKey, formData);

...

    • element - Element that this binder is tie to. (org.joget.apps.form.model.Element)
    • rows - Data to be store. Contains one or more org.joget.apps.form.model.FormRow object. (org.joget.apps.form.model.FormRowSet)
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected Return Object:

    • Same org.joget.apps.form.model.FormRowSet object which stored.

...

Code Block
import java.util.HashSet;
import java.util.Set;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.commons.util.LogUtil;
import org.joget.commons.util.StringUtil;
import org.joget.directory.model.Role;
import org.joget.directory.model.User;
import org.joget.directory.dao.RoleDao;
import org.joget.directory.dao.UserDao;
import org.joget.directory.model.service.DirectoryUtil;
import org.joget.directory.model.service.UserSecurity;
 
public FormRowSet store(Element element, FormRowSet rows, FormData formData) {
    try {
        UserSecurity us = DirectoryUtil.getUserSecurity();
        RoleDao roleDao = (RoleDao) AppUtil.getApplicationContext().getBean("roleDao");
        UserDao userDao = (UserDao) AppUtil.getApplicationContext().getBean("userDao");
        
        for (FormRow row : rows) {
            //Get the submitted data for each grid row
            String username = row.getProperty("username");
            String firstName = row.getProperty("firstName");
            String lastName = row.getProperty("lastName");
            String email = row.getProperty("email");
            String password = row.getProperty("password");
            
            User user = new User();
            user.setId(username);
            user.setUsername(username);
            user.setTimeZone("0");
            user.setActive(1);
            user.setFirstName(firstName);
            user.setLastName(lastName);
            user.setEmail(email);
            
            //Check if there is user security implementation, using it to encrypt password
            if (us != null) {
                user.setPassword(us.encryptPassword(username, password));
            } else {
                user.setPassword(StringUtil.md5Base16(password));
            }
            user.setConfirmPassword(password);
            
            //set user role
            Set roleSet = new HashSet();
            roleSet.add(roleDao.getRole("ROLE_USER"));
            user.setRoles(roleSet);
            
            userDao.addUser(user);
            if (us != null) {
                us.insertUserPostProcessing(user);
            }
        }
    } catch (Exception e) {
        LogUtil.error("Sample app - Bulk Create Users form", e, "Store user error!!");
    }
    return rows;
}
 
//call store method with injected variable
return store(element, rows, formData);

...

    • element - Element that this validator is tie to. (org.joget.apps.form.model.Element)
    • rows - Submitted data. Contains one or more org.joget.apps.form.model.FormRow object. (org.joget.apps.form.model.FormRowSet)
    • formData - The data holder of the whole form. (org.joget.apps.form.model.FormData)

Expected Return Object:

    • A boolean value to indicate the validation pass or fail.

...

    • user - User object of current logged in user (org.joget.directory.model.User)
    • requestParams - Request parameters map of current HTTP Request (java.util.Map)

Expected Return Object:

    • A boolean value to indicate the user is authorized.

...

    • workflowAssignment - The workflow activity assignment object of the saving form. Null when the form is not an assignment form. (org.joget.workflow.model.WorkflowAssignment)
    • pluginManager - Plugin Manager service bean for convenient usage. (org.joget.plugin.base.PluginManager)
    • appDef - App definition of the process. (org.joget.apps.app.model.AppDefinition)
    • request - Http Request object of current Http Request. (javax.servlet.http.HttpServletRequest)

Expected Return Object:

    • None

Samples:

Reuse Email tool to send separate email to each users. The following script is for a form not mapped to workflow assignment, therefore workflowAssignment is not available.

...

    • pluginManager - Plugin Manager service bean for convenient usage. (org.joget.plugin.base.PluginManager)
    • workflowActivity - Workflow Activity that trying to retrieves assignee. (org.joget.workflow.model.WorkflowActivity)

Expected Return Object:

    • A java.util.Collection of username in java.lang.String to be assign to the Workflow Activity.

...

Code Block
languagejava
import java.util.ArrayList;
import java.util.Collection;
import org.joget.apps.app.service.AppUtil;
import org.joget.directory.model.User;
import org.joget.directory.model.service.ExtDirectoryManager;
import org.joget.workflow.model.WorkflowActivity;

public Collection getAssignees(WorkflowActivity activity) { 
    Collection assignees = new ArrayList();
    
    ExtDirectoryManager directoryManager = (ExtDirectoryManager) pluginManager.getBean("directoryManager");
    
    String deptId = "D-005";
    
    //Get total user in department
    Long total = directoryManager.getTotalUsers(null, null, deptId, null, null, null, null);
    
    //Get random number from 0 to the total number of users in department
    int random = (int) (Math.random() * total);
    
    //Get users using directory manager
    Collection userList = directoryManager.getUsers(null, null, deptId, null, null, null, null, "firstName", false, random, 1);
    for(Object u : userList){
        User user = (User) u;
        assignees.add(user.getUsername());
    }
    
    return assignees;
}

//call getAssignees method with injected variable
return getAssignees(workflowActivity);

Use as Process Tool 
Anchor
Use as Process Tool
Use as Process Tool

Injected Variables:

    • workflowAssignment - The workflow tool activity assignment object. (org.joget.workflow.model.WorkflowAssignment)
    • pluginManager - Plugin Manager service bean for convenient usage. (org.joget.plugin.base.PluginManager)
    • appDef - App definition of the process. (org.joget.apps.app.model.AppDefinition)
    • request - Http Request object of current HTTP Request. Not available if the tool is trigger by Deadline. (javax.servlet.http.HttpServletRequest)

Expected Return Object:

    • None

Samples:

Start a new process in the same app with current record id.

...

    • user - User object of current logged in user (org.joget.directory.model.User)
    • requestParams - Request parameters map of current HTTP Request (java.util.Map)

Expected Return Object:

    • A boolean value to indicate the user is authorized.

...

If your script need to reuse for multiple times in an app or can be used by others app in future development, please consider to make your Bean Shell script as a plugin instead of copy and paste it multiple times across your app. Bean Shell script is harder to maintain in this case. Imagine that you want to make a change, you will have to update all the places that are using the same script as well. Beside that, a normal plugin implementation will have better performance than a script running by a Bean Shell interpreter.

8. Reuse existing plugins in your code to make it cleaner and easy to maintain

If partial of your script can be done by existing plugins in Joget Workflow, you can reuse the plugin in stead of writting it again. 

To reuse a plugin, you can retrieve the plugin using PluginManager, then set it properties and execute it.

Example: 

  1. Reuse Multi Row Binder plugin to load data
  2. Reuse Email Tool to send email

 

More samples

Children Display