Versions Compared

Key

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

You may want to store certain fields from your form to other tables with the use of the Beanshell Form Binder. Figure 1 shows an example of a form where the first 3 fields are to be stored in another data source in addition to the original form data table.

Thai

คุณอาจต้องการจัดเก็บเขตข้อมูลบางอย่างจากแบบฟอร์มของคุณไปยังตารางอื่นด้วยการใช้ Beanshell Form Binder รูปที่ 1 แสดงตัวอย่างของฟอร์มที่จะเก็บ 3 ฟิลด์แรกในแหล่งข้อมูลอื่นนอกเหนือจากตารางข้อมูลฟอร์มต้นฉบับ


Figure1: Form with Field to Store

Thai

รูปที่ 1: ฟอร์มที่มีฟิลด์เพื่อจัดเก็บ


Then, click on the form "Properties" tab and navigates to "Advanced" page. Choose "Bean Shell Form Binder" as Store Binder.

Thai

จากนั้นคลิกที่แท็บ "คุณสมบัติ" แท็บแล้วไปที่หน้า "ขั้นสูง" เลือก "Bean Shell Form Binder" เป็น Store Binder

Figure 2: Choose Bean Shell Form Binder as the Store Binder

Thai

รูปที่ 2: เลือก Bean Binder ของฟอร์มเป็น Binder ของร้าน


Configure Bean Shell Form Binder with your own coding to store the fields as intended, as shown in the figure below.

Code used in this example:

Thai

กำหนดค่า Bean Binder แบบฟอร์ม Binder ด้วยการเข้ารหัสของคุณเองเพื่อเก็บฟิลด์ตามที่ต้องการดังแสดงในรูปด้านล่าง

รหัสที่ใช้ในตัวอย่างนี้:

Code Block
languagejava
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppService;
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.apps.form.service.FormUtil;
import org.joget.plugin.base.PluginManager;
import org.joget.commons.util.LogUtil;
 
public FormRowSet storeData(Element element, FormRowSet rows, FormData formData) {
    //check for empty data
    if (rows == null || rows.isEmpty()) {
        return rows;
    }
    normalStoring(element, rows, formData);
 
    //store only needed field by create new Form Row Set
    FormRow originalRow = rows.get(0);
    FormRowSet newRows = new FormRowSet();
    FormRow newRow = new FormRow();
 
    newRow.put("firstName", originalRow.getProperty("firstName"));
    newRow.put("lastName", originalRow.getProperty("lastName"));
    newRow.put("email", originalRow.getProperty("email"));
    newRows.add(newRow);
 
    String id = "#currentUser.username#";
 
    //store
    storeToOtherFormDataTable(element, newRows, formData, id);
    storeUsingJDBC(element, newRows, formData, id);
 
    return rows;
}
 
//this function will reuse workflow form binder to store data
public void normalStoring(Element element, FormRowSet rows, FormData formData) {
    PluginManager pluginManager = (PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
    FormStoreBinder binder = (FormStoreBinder) pluginManager.getPlugin("org.joget.apps.form.lib.WorkflowFormBinder");
    binder.store(element, rows, formData);
}
 
//this function will store rows data to a form's data table
public void storeToOtherFormDataTable(Element element, FormRowSet rows, FormData formData, String id) {
    AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
 
    String formId = "user"; // the table of database is configured in the form with id "user"
    AppDefinition appDef = AppUtil.getCurrentAppDefinition();
 
    appService.storeFormData(appDef.getId(), appDef.getVersion().toString(), formId, rows, id);
}
 
//this function will store rows data to external source using JDBC
public void storeUsingJDBC(Element element, FormRowSet rows, FormData formData, String id) {
    Connection con = null;
    try {
        // retrieve connection from the default datasource
        DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
        con = ds.getConnection();
 
        if (!con.isClosed()) {
            //manually handle insert and update by checking the data is exist or not
            String selectQuery = "SELECT username FROM dir_user WHERE username=?";
            PreparedStatement stmt = con.prepareStatement(selectQuery);
            stmt.setString(1, id);
            ResultSet rs = stmt.executeQuery();
 
            Boolean isExist = false;
            if (rs.next()) {
                isExist = true;
            }
 
            FormRow row = rows.get(0);
 
            if (isExist) {
                String updateQuery = "UPDATE dir_user SET firstName = ?, lastName = ?, email = ? WHERE username = ?";
                PreparedStatement ustmt = con.prepareStatement(updateQuery);
                ustmt.setString(1, row.getProperty("firstName"));
                ustmt.setString(2, row.getProperty("lastName"));
                ustmt.setString(3, row.getProperty("email"));
                ustmt.setString(4, id);
                ustmt.executeUpdate();
            } else {
                String insertQuery = "INSERT INTO dir_user (id, username, firstName, lastName, password, email) values (?, ?, ?, ?, 'md5(password)', ?)";
                PreparedStatement istmt = con.prepareStatement(insertQuery);
                istmt.setString(1, id);
                istmt.setString(2, id);
                istmt.setString(3, row.getProperty("firstName"));
                istmt.setString(4, row.getProperty("lastName"));
                istmt.setString(5, row.getProperty("email"));
                istmt.executeUpdate();
            }
        }
    } catch (Exception e) {
        LogUtil.error("Sample app - StoreToMultipleSource form", e, "Error storing using jdbc");
    } finally {
        try {
            if(con != null) {
                con.close();
			}
        } catch (SQLException e) {}
    }
}
 
//call storeData method with injected variables
return storeData(element, rows, formData);


Figure 3: Populate Bean Shell Form Binder with the Necessary Codes

Thai

รูปที่ 3: เติมเครื่องผูกฟอร์มเปลือกถั่วด้วยรหัสที่จำเป็น


If the coding is properly written and tested, you will get this result:

Thai

หากการเขียนโค้ดและการทดสอบถูกต้องคุณจะได้รับผลลัพธ์นี้:

Figure 4: Fill and Submit Form for Testing

Thai

รูปที่ 4: กรอกและส่งแบบฟอร์มสำหรับการทดสอบ


Check the data in the database.

Thai

ตรวจสอบข้อมูลในฐานข้อมูล

Figure 5: Data Stored Correctly in the Tables

Thai

รูปที่ 5: ข้อมูลที่เก็บไว้อย่างถูกต้องในตาราง