使用BeanShell存储表单数据

注入的参数

    • element  -  org.joget.apps.form.model.Element
    • rows - org.joget.apps.form.model.FormRowSet
    • formData - org.joget.apps.form.model.FormData

返回参数

    • org.joget.apps.form.model.FormRowSet 对象

例子:

使用Workflow Form Binder存储数据之前进行一些计算。

import java.text.DecimalFormat;
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.apps.form.model.FormStoreBinder;
import org.joget.plugin.base.PluginManager;
  
public FormRowSet store(Element element, FormRowSet rows, FormData formData) {
     
    //check the rows is not empty before store it
    if (rows != null && !rows.isEmpty()) {
        //Get the submitted data
        FormRow row = rows.get(0);
         
        String unitPrice = row.getProperty("unitPrice");
        String unit = row.getProperty("unit");
         
        double total = 0;
        try {
            double price = Double.parseDouble(unitPrice);
            int unit = Integer.parseInt(unit);
             
            total = price * unit;
        } catch (Exception e) {}
         
        //format the total to 2 decimal
        DecimalFormat df = new DecimalFormat("#.00");
        row.setProperty("total", df.format(total));
     
        //Reuse Workflow Form Binder to store data
        PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
        FormStoreBinder binder = (FormStoreBinder) pluginManager.getPlugin("org.joget.apps.form.lib.WorkflowFormBinder");
        binder.store(element, rows, formData);
    }
     
    return rows;
}
 
//call store method with injected variable
return store(element, rows, formData);

使用BeanShell存储表单多行数据

注入的参数

    • element  -  org.joget.apps.form.model.Element
    • rows - org.joget.apps.form.model.FormRowSet
    • formData - org.joget.apps.form.model.FormData

返回参数

    • org.joget.apps.form.model.FormRowSet 对象

例子:

基于多行数据批量创建用户。

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);



  • No labels