Joget DX 8 Stable Released
The stable release for Joget DX 8 is now available, with a focus on UX and Governance.
...
Store Binder -> Bean Shell Form Binder
Code Block |
---|
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.service.AppUtil; import org.joget.apps.form.model.*Form; import org.joget.apps.form.libmodel.*Element; import org.joget.apps.form.servicemodel.*FormData; import java.sql.*org.joget.apps.form.model.FormRow; import org.joget.apacheapps.commonsform.collectionsmodel.SequencedHashMapFormRowSet; import java.util.*org.joget.apps.form.service.FormUtil; import org.joget.commons.util.UuidGenerator; public saveGridRows(Element element, FormRowSet getGridRows(rows, FormData formData) { return rows ; // this will return the grid rows } public saveGridRows(FormRowSet rows) { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql://localhost:3307/jwdb?characterEncoding=UTF-8", "database username", "database password"); String recordId = null; Connection con = null; try { //Get Joget's current datasource configs DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource"); con = ds.getConnection(); if(!con.isClosed()) { String recordId = "#requestParam.id#"; // If you need to query data from different table(s) and process it before saving it to database, // you can do it here //To generate new record IDs for storing into child table UuidGenerator uuid = UuidGenerator.getInstance(); //Iterate to add new records Iterator i= rows.iterator(); // Iterating grid rows while (i.hasNext()) { FormRow row = (FormRow) i.next(); String pId = uuid.getUuid(); String gridColumn1 = row.get("gridColumn1"); // reading grid column value String gridColumn2 = row.get("gridColumn2"); String gridColumn3 = row.get("gridColumn3"); String pId = uuid.getUuid(); // generating Primary Key String insertSql = "INSERT INTO your_table_name (id,col1gridColumn1,col2gridColumn2,col3gridColumn3) VALUES (?,?,?,?);"; PreparedStatement stmtInsert = con.prepareStatement(insertSql); stmtInsert.setString(1, pId); stmtInsert.setString(2, gridColumn1); stmtInsert.setString(3, gridColumn2); stmtInsert.setString(4, gridColumn3); //Execute SQL statement stmtInsert.executeUpdate(); } } else { System.out.println("Connection Problem"); } } } } catch (Exception ex) { LogUtil.error("Your App/Plugin Name", ex, "Error storing using jdbc"); } finally { try { if (con != null) { con.close(); } FormRowSet rows = getGridRows(); // getting the grid rows saveGridRows(rows); // processing & storing the grid rows } catch (Exception ex) { LogUtil.error("Your App/Plugin Name", ex, "Error closing the jdbc connection"); } } } //Process and store grid rows saveGridRows(element, rows, formData); |
Grid Design
Correct Grid design should look like this:
...
part of load binder beanshell script :
Code Block | ||
---|---|---|
| ||
import org.joget.apps.form.model.FormRow; import org.joget.apps.form.model.FormRowSet; FormRowSet f = new FormRowSet(); f.setMultiRow(true); FormRow r1 = new FormRow(); r1.put("gridColumn1", your_value); r1.put("gridColumn2", your_value); r1.put("gridColumn3", your_value); f.add(r1); FormRow r2 = new FormRow(); r2.put("gridColumn1", your_value); r2.put("gridColumn2", your_value); r2.put("gridColumn3", your_value); f.add(r2); return f; |