Tag Archive object

JoGet: Bean Shell Create Json

วิธีดีที่สุดในการให้ข้อมูลจำนวนมากให้ javascript คือรูปแบบ json ไม่เว้นแม้แต่ใน JoGet เพราะถึงแม้จะสามารถดึงข้อมูลจาก input หรือ grid ได้ แต่มันจะถูกบันทึกลง database ไปด้วยแถมดูรก

วิธีที่ดีอีกวิธีคือให้ beanshell ใน section ดึงข้อมูลจาก query แล้วแสดงออกมาใน input ตัวเดียว[code language=”java” title=”Bean Shell Create Json”]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.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.service.FormUtil;
import org.joget.commons.util.LogUtil;
import org.json.simple.JSONObject;

public FormRowSet load(Element element, String username, FormData formData) {

FormRowSet rows = new FormRowSet();

Connection con = null;
try {
DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
con = ds.getConnection();

sql = "SELECT appId, appVersion, name FROM jwdb.app_app WHERE published = 1;";
//LogUtil.info("Bean Shell Create Json", "sql = " + sql);

if (!con.isClosed()) {
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();

/* create json */
JSONObject json = new JSONObject();
while (rs.next()) {
JSONObject item = new JSONObject();

item.put("appVersion", rs.getObject("appVersion"));
item.put("name", rs.getObject("name"));

json.put(rs.getObject("appId"), item);
}

/* add json to input */
FormRow row = new FormRow();
rows.add(row);
row.put("json", json.toString());
}

} catch (Exception e) {
LogUtil.error(getClassName(), e, "Bean Shell Create Json");
} finally {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {}
}

return rows;
}

return load(element, primaryKey, formData);[/code]

ตัวอย่างฟอร์ม[code language=”javascript” title=”Bean Shell Create Json”]{
"className": "org.joget.apps.form.model.Form",
"properties": {
"noPermissionMessage": "",
"loadBinder": {
"className": "org.joget.apps.form.lib.WorkflowFormBinder",
"properties": {}
},
"name": "Bean Shell Create Json",
"description": "",
"postProcessorRunOn": "both",
"permission": {
"className": "",
"properties": {}
},
"id": "BeanShellCreateJson",
"postProcessor": {
"className": "",
"properties": {}
},
"storeBinder": {
"className": "org.joget.apps.form.lib.WorkflowFormBinder",
"properties": {}
},
"tableName": "prototypes"
},
"elements": [
{
"elements": [
{
"elements": [
{
"className": "org.joget.apps.form.lib.TextArea",
"properties": {
"readonly": "",
"validator": {
"className": "",
"properties": {}
},
"workflowVariable": "",
"id": "json",
"label": "JSON",
"placeholder": "",
"rows": "10",
"value": "",
"cols": "100",
"readonlyLabel": ""
}
}
],
"className": "org.joget.apps.form.model.Column",
"properties": {
"width": "100%"
}
}
],
"className": "org.joget.apps.form.model.Section",
"properties": {
"readonly": "",
"loadBinder": {
"className": "org.joget.apps.form.lib.BeanShellFormBinder",
"properties": {
"useAjax": "",
"cacheIdlePause": "120",
"cacheInterval": "",
"script": "import java.sql.Connection;\nimport java.sql.PreparedStatement;\nimport java.sql.ResultSet;\nimport java.sql.SQLException;\nimport javax.sql.DataSource;\nimport org.joget.apps.app.service.AppUtil;\nimport org.joget.apps.form.model.Element;\nimport org.joget.apps.form.model.FormData;\nimport org.joget.apps.form.model.FormRow;\nimport org.joget.apps.form.model.FormRowSet;\nimport org.joget.apps.form.service.FormUtil;\nimport org.joget.commons.util.LogUtil;\nimport org.json.simple.JSONObject;\n\npublic FormRowSet load(Element element, String username, FormData formData) {\n\n FormRowSet rows = new FormRowSet();\n\n Connection con = null;\n try {\n DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean(\"setupDataSource\");\n con = ds.getConnection();\n\n sql = \"SELECT appId, appVersion, name FROM jwdb.app_app WHERE published = 1;\";\n //LogUtil.info(\"Bean Shell Create Json\", \"sql = \" + sql);\n\n if (!con.isClosed()) {\n PreparedStatement stmt = con.prepareStatement(sql);\n ResultSet rs = stmt.executeQuery();\n\n /* create json */\n JSONObject json = new JSONObject();\n while (rs.next()) {\n JSONObject item = new JSONObject();\n\n item.put(\"appVersion\", rs.getObject(\"appVersion\"));\n item.put(\"name\", rs.getObject(\"name\"));\n\n json.put(rs.getObject(\"appId\"), item);\n }\n\n /* add json to input */\n FormRow row = new FormRow();\n rows.add(row);\n row.put(\"json\", json.toString());\n }\n\n } catch (Exception e) {\n LogUtil.error(getClassName(), e, \"Bean Shell Create Json\");\n } finally {\n try {\n if (con != null) {\n con.close();\n }\n } catch (SQLException e) {}\n }\n\n return rows;\n}\n\nreturn load(element, primaryKey, formData);"
}
},
"permissionReadonly": "",
"permission": {
"className": "",
"properties": {}
},
"comment": "",
"id": "section1",
"label": "Bean Shell Create Json",
"storeBinder": {
"className": "",
"properties": {}
},
"readonlyLabel": ""
}
}
]
}[/code]

JAVA: Create Json

การสร้าง json ใน java ทั้งแบบ array [“aaa”, “bbb”, “ccc”] และ object {“aaa”:”bbb”, “ccc”:”ddd”, “eee”:”fff”} ทำได้ง่ายๆ

สร้าง json array[code language=”java” title=”json array”]import org.json.simple.JSONArray;

JSONArray json = new JSONArray();
while (rs.next()) {
JSONObject item = new JSONArray();

item.add(rs.getObject("appId"));
item.add(rs.getObject("appVersion"));
item.add(rs.getObject("name"));

json.add(item);
}[/code]

รวมร่าง[code language=”java” title=”add json array”]json.add( { JSONArray ที่ต้องการรวม } );[/code]

สร้าง json object[code language=”java” title=”json object”]import org.json.simple.JSONObject;

JSONObject json = new JSONObject();
while (rs.next()) {
JSONObject item = new JSONObject();

item.put("appVersion", rs.getObject("appVersion"));
item.put("name", rs.getObject("name"));

json.put(rs.getObject("appId"), item);
}[/code]

รวมร่าง[code language=”java” title=”json to string”]json.put( {index ที่ต้องการ}, { JSONArray ที่ต้องการรวม } );[/code]

แปลงเป็น string เพื่อใช้อย่างอื่น[code language=”java” title=”json to string”]json.toString()[/code]

อ่านเพิ่มเติม JoGet: Bean Shell Create Json

PHP: ดูการทำงานสร้าง flowchart (backtrace)

ถ้าเรียนเขียน program มาคงจะคุ้นเคยกับ flowchart การทำงานของของโปรแกรม แต่ในชีวิตการทำงานจริงๆ โดยเฉพาะถ้าเขียน code โดยใช้ framework ต่างๆ จะไม่เป็นเหมือนที่ได้ออกแบบไว้เสมอไป เพราะว่าบางครั้งโปรแกรมจะทำงานให้เราเองโดยที่ไม่ได้สั่ง สาเหตุคือ มีการเรียกใช้ hook หรือ tricker ทำให้เกิด process ที่อาจจะไม่ทราบที่มา หรือต้องมาแก้งานของคนอื่นโดนที่ไม่มีเอกสารให้

นอกจากการใช้วิธี PHP: list included หรือ required ไฟล์ที่ใช้ ถ้าเราใช้ตัว framework ต่างๆ มักจะมีการเตรียม function ในการสร้าง backtrace ไว้ให้ แต่ถ้าไม่มีหรือเป็น code ที่เขียนด้วยตัวเอง ยังสามารถใช้ debug_backtrace ในการค้นหาที่มาที่ไปได้เช่น[code language=”php” title=”debug.debug_backtrace.php”]<?php

function getBacktrace()
{
$backtrace = debug_backtrace();
echo ‘<pre>’, print_r($backtrace, true), ‘</pre>’;
fwrite(fopen(‘logs_debug_backtrace.txt’, ‘a+’), "\n\n" . __FILE__ . ‘ :’ . __LINE__ . "\n\n" . print_r($backtrace, true));
}

function getPhpInfo($what)
{
phpinfo($what);

/* จะดูว่า ทำไม่ getPhpInfo ถึงทำงาน */
getBacktrace();
}

function index()
{
getPhpInfo(INFO_ENVIRONMENT);
}

index();[/code]เมื่อทดสอบดูจะได้ผลลัพธ์[code language=”text” title=”logs_debug_backtrace.txt”]Array
(
[0] => Array
(
[file] => D:\xampp\htdocs\snippets\PHP\debug.debug_backtrace.php
[line] => 13
[function] => getBacktrace
[args] => Array
(
)

)

[1] => Array
(
[file] => D:\xampp\htdocs\snippets\PHP\debug.debug_backtrace.php
[line] => 18
[function] => getPhpInfo
[args] => Array
(
[0] => 16
)

)

[2] => Array
(
[file] => D:\xampp\htdocs\snippets\PHP\debug.debug_backtrace.php
[line] => 21
[function] => index
[args] => Array
(
)

)

)[/code]เพราะว่าเราเขียน function debug_backtrace() ไว้ใน function getBacktrace() อีกชั้น เพื่อที่ความง่ายในการค้นหา flow เมื่อต้องการดูว่า function หรือ class ที่เราสนใจมันมี flow มายังไงก็แค่ไปเรียกใน function ที่สงสัย เราก็จะได้ลำดับการทำงานย้อนกลับไป เราจะได้ทราบทั้งฟังก์ชั้น คลาส บรรทัด ไฟล์ แม้แต่ตัวแปรที่ส่งเข้าไปใน function นั้นๆ