Return error message from store binder(beanshell) to display back to the forms
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.DriverManager;
import javax.sql.DataSource;
import java.lang.Exception;
import org.apache.axis.encoding.Base64;
import org.joget.commons.util.LogUtil;
import org.joget.apps.form.service.FormUtil;
import org.joget.apps.form.service.FileUtil;
import org.apache.commons.io.FileUtils;
import java.sql.Blob;
import javax.sql.rowset.serial.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.Form;
import org.joget.apps.app.service.AppService;
import org.joget.apps.form.model.Element;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.joget.apps.form.dao.FormDataDao;
String userId = "#form.dbos_stao_form.userId#";
String productSeq = "#form.dbos_stao_form.productSeq#";
String groupForStaffSeq = "#form.dbos_stao_form.groupForStaffSeq#";
String productSummary = "#form.dbos_stao_form.productSummary#";
String pdsLink = "#form.dbos_stao_form.pdsLink#";
String effectiveDate = "#form.dbos_stao_form.effectiveDate#";
String productCategory = "#form.dbos_stao_form.productCategory#";
String foreignShareTradeChk = "#form.dbos_stao_form.foreignShareTradeChk#";
String termAndConditions = "#form.dbos_stao_form.termAndConditions#";
String declarationApplicant = "#form.dbos_stao_form.declarationApplicant#";
String productFeatures1 = "#form.dbos_stao_form.productFeatures1#";
String productFeatures2 = "#form.dbos_stao_form.productFeatures2#";
String productFeatures3 = "#form.dbos_stao_form.productFeatures3#";
String productTypeIsNotDisplayList = "#form.dbos_stao_form.productTypeIsNotDisplayList#";
String fileId = "#form.dbos_stao_form.fileId#";
public String getJdbcConnectionUrl() {
return "#envVariable.stao_jdbc_url#";
}
long productIconSeq;
public void insertProductIconAtt(Connection conn){
PreparedStatement stmt = null, ps2 = null, ps3 = null;
ResultSet rs = null;
try{
String tableName = "dbos_stao_form";
String recordId = "#form.dbos_stao_form.id#";
String fileNameList = "#form.dbos_stao_form.productIconDoc#";
LogUtil.info("jogetFormId", recordId);
LogUtil.info("jogetUploadFileNameList", fileNameList);
String[] fileNameArray = fileNameList.split("\\;");
for (String fileName : fileNameArray){
File jogetUploadFile = FileUtil.getFile(fileName, tableName, recordId);
String jogetUploadPath = FileUtil.getUploadPath(tableName, recordId);
LogUtil.info("jogetUploadPath", jogetUploadPath);
LogUtil.info("Product Maintenance Details", ">>>>Checkpoint");
LogUtil.info("File Name", jogetUploadFile.getName());
LogUtil.info("File Path", jogetUploadFile.getAbsolutePath());
LogUtil.info("File Size", String.valueOf(jogetUploadFile.length()));
LogUtil.info("Product Maintenance Details", "Converting file to blob...");
byte[] bytes = FileUtils.readFileToByteArray(jogetUploadFile);
Blob blob = new SerialBlob(bytes);
LogUtil.info("Product Maintenance Details", "Converted Successfully.");
// the db2 insert statement
String query = " insert into STSM.APP_ATTACHMENT_HDR (APP_ATTACHMENT_FILENAME, APP_ATTACHMENT_CONTENT, APP_ATTACHMENT_SIZE)"
+ " values (?, ?, ?)";
String[] generatedColumns = { "APP_ATTACHMENT_SEQ" };
stmt = conn.prepareStatement(query,generatedColumns);
stmt.setString (1, jogetUploadFile.getName());
stmt.setBlob (2, blob);
stmt.setLong (3, jogetUploadFile.length());
// execute the preparedstatement
stmt.execute();
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
long id = rs.getLong(1);
System.out.println("Inserted ID -" + id); // display inserted record
productIconSeq = id;
}
}
} catch (Exception e1) {
LogUtil.error(">>>>>>ERROR", null, e1.getMessage());
} finally {
try{
if(rs != null) {
rs.close();
}
}catch (SQLException e2) {}
try{
if(stmt != null) {
stmt.close();
}
}catch (SQLException e3) {}
try{
if(ps3 != null) {
ps3.close();
}
}catch (SQLException e4) {}
try{
if(ps2 != null) {
ps2.close();
}
}catch (SQLException e5) {}
}
}
public void callBackendApi(productIconSeq, row){
LogUtil.info("DBOS Product Details - callBackendApi","called!");
String staoApi = "#envVariable.stao_api#";
String urlStr = staoApi +"/application/saveProductInfo";
URL url = (URL)new URL(urlStr.trim());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setRequestProperty("Accept", "application/json");
conn.setDoOutput(true);
LogUtil.info("!check","check");
String jsonInputString = "{\"productTypeId\": \""+productSeq+"\", \"accountTypeId\": \""+ productCategory +
"\", \"productFeatures1\": \""+ productFeatures1 +"\", \"productFeatures2\": \""+ productFeatures2 +"\", \"productFeatures3\": \""+ productFeatures3 +
"\", \"productTypeIsNotDisplayList\": \"["+ productTypeIsNotDisplayList +
"]\", \"productTypeDesc\": \""+ productSummary +
"\", \"pdsLink\": \""+ pdsLink +
"\", \"fstChk\": \""+ foreignShareTradeChk +"\", \"selectedDeclaAppId\": \""+ declarationApplicant +
"\", \"selectedTncId\": \""+ termAndConditions +"\", \"effectiveDate\": \""+ effectiveDate +
"\", \"userId\": \""+ userId +
// "\", \"activeStatusId\": \""+ brokerageRate +
"\", \"groupForStaffSeq\": \""+ groupForStaffSeq +
"\", \"productIconSeq\": \""+ productIconSeq +
"\"}";
LogUtil.info(" - jsonInputString",jsonInputString);
try{
OutputStream os = conn.getOutputStream();
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
conn.connect();
//Getting the response code
int responsecode = conn.getResponseCode();
LogUtil.info("response ",responsecode.toString());
//Getting the response data
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String responseData = response.toString();
LogUtil.info("responseData ",response.toString());
JSONObject jsonObject = new JSONObject(responseData);
String dataValue = jsonObject.getString("data");
LogUtil.info("data now is ",dataValue);
row.put("dataValue", dataValue != null ? dataValue : "");
} catch(Exception e){
e.printStackTrace();
}finally {
try {
if(conn != null)
conn.disconnect();
} catch(SQLException e2) {}
}
}
public FormRowSet saveJogetDocToDB2() {
FormRowSet results = new FormRowSet();
results.setMultiRow(true);
Connection con = null;
try {
java.util.Properties connectionProps = new java.util.Properties();
connectionProps.put("user", "#envVariable.stao_jdbc_user#");
connectionProps.put("password", "#envVariable.stao_jdbc_pwd#");
connectionProps.put("connectionTimeout","20");
connectionProps.put("loginTimeout","20");
connectionProps.put("maxStatements","50");
connectionProps.put("progressiveStreaming","2");
connectionProps.put("retrieveMessagesFromServerOnGetMessage","true");
Class.forName("#envVariable.stao_jdbc_driver#").newInstance();
con = DriverManager.getConnection(getJdbcConnectionUrl(), connectionProps);
if(!con.isClosed()){
// callBackendApi();
String attachmentList = "#form.dbos_stao_form.productIconDoc#";
FormRow row = new FormRow();
if(attachmentList != ""){
insertProductIconAtt(con);
System.out.println("productIconSeq ID -" + productIconSeq); // display inserted record
callBackendApi(productIconSeq,row);
}else{
callBackendApi(fileId,row);
}
results.add(row);
LogUtil.info("results formset is ", results.toString());
}
} catch(Exception ex) {
ex.printStackTrace();
LogUtil.error("Product Maintenance", null, ex.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e2) {}
}
return results;
}
return saveJogetDocToDB2();
This is my code in store binder. I want to get back the dataValue and return the data to the screen display what is the message that the user hit during the api call (message like: add product successfully or edit product successfully or fail to add product).
I have tried to direct to a page called maintenanceCompletePage after submit and pass over the dataValue field to the form but it is not working because of the message did not get to return during store binder. Can anyone help me on this?