Versions Compared

Key

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

By using Bean Shell Form

...

Data Store, we are able to load and update our Environment Variable values by using a form.

Let's create a form with each Text Field to represent a Environment Variable in our app and set all the field id to the ID of the Environment Variable.

Thai

โดยใช้ Bean Binder Data Store แบบฟอร์มเราสามารถโหลดและปรับปรุงค่า Environment Variable ของเราโดยใช้แบบฟอร์ม

มาสร้างแบบฟอร์มพร้อมฟิลด์ข้อความแต่ละฟิลด์เพื่อเป็นตัวแทนของตัวแปรสภาพแวดล้อมในแอพของเราและตั้งค่า ID ฟิลด์ทั้งหมดเป็น ID ของตัวแปรสภาพแวดล้อม

...

Then, click on "Settings" tab then set the "Load BinderData Store" and "Store BinderData Store" in "Advanced" page to "Bean Shell Form BinderData Store".

Thai

จากนั้นคลิกที่แท็บ "Settings" จากนั้นตั้งค่า "Load BinderData Store" และ "Store BinderData Store" ในหน้า "Advanced" เป็น "Bean Shell Form BinderData Store"



Warning

Be advised the following BeanShell codes are not compatible in Joget Cloud multi-tenant environment.

...

Using the following script for "Load BinderData Store".

Thai

การใช้สคริปต์ต่อไปนี้สำหรับ "Load BinderData Store"

Code Block
languagejava
import java.util.Collection;
import org.joget.apps.app.dao.EnvironmentVariableDao;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.model.EnvironmentVariable;
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;
 
public FormRowSet load(Element element, String primaryKey, FormData formData) {
    FormRowSet rows = new FormRowSet();
    AppDefinition appDef = AppUtil.getCurrentAppDefinition();
 
    if (appDef != null) {
        EnvironmentVariableDao environmentVariableDao = (EnvironmentVariableDao) AppUtil.getApplicationContext().getBean("environmentVariableDao");
        Collection environmentVariableList = environmentVariableDao.getEnvironmentVariableList(null, appDef, null, null, null, null);
        
        //loop the result and add to FormRow
        if (environmentVariableList != null && environmentVariableList.size() > 0) {
            FormRow r = new FormRow();
            
            for (EnvironmentVariable e : environmentVariableList) {
                if (e.getValue() != null) {
                    r.setProperty(e.getId(), e.getValue());
                }
            }
            
            rows.add(r);
        }
    }
    
    return rows;
}
 
//call load method with injected variable
return load(element, primaryKey, formData);

...

Using the following script for "Store BinderData Store".

Thai

การใช้สคริปต์ต่อไปนี้สำหรับ "Store Data Store Binder"

Code Block
languagejava
import org.joget.apps.app.dao.EnvironmentVariableDao;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.model.EnvironmentVariable;
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()) {
        AppDefinition appDef = AppUtil.getCurrentAppDefinition();
        EnvironmentVariableDao environmentVariableDao = (EnvironmentVariableDao) AppUtil.getApplicationContext().getBean("environmentVariableDao");
        
        //Get the submitted data
        FormRow row = rows.get(0);
        
        //Add / Update each Environment Variable
        for (Object key : row.keySet()) {
            String envId = (String) key;
            String value = row.getProperty(envId);
            
            if (!envId.isEmpty()){
                EnvironmentVariable e = environmentVariableDao.loadById(envId, appDef);
                if (e != null) {
                    e.setValue(value);
                    environmentVariableDao.update(e);
                } else {
                    EnvironmentVariable e = new EnvironmentVariable();
                    e.setAppDefinition(appDef);
                    e.setId(envId);
                    e.setValue(value);
                    environmentVariableDao.add(e);
                }
            }
        }
    }
    
    return rows;
}
 
//call store method with injected variable
return store(element, rows, formData);

...