Thursday August 30, 2007

Oh gosh, this is exciting!
Thanks Eduardo for the tip!
Technorati: blogs rank bsc hotblog
Posted by Arun Gupta in General | Comments[4]
|
|
|
|
| TOTD #5: Loading data from beans in jMaki widgets
The jMaki tutorial from SWDP explained the different approaches to load your own data into a jMaki widget. The jMaki widget models have formalized since then and so the code there no longer works. This TOTD explains how a combo box widget in a JSP page gets it data from a bean.
This TOTD uses NetBeans IDE configured with jMaki plugin and GlassFish.
jMaki Framework" and use all the
defaults. Choose GlassFish as the "Server".index.jsp" page, drag-and-drop "Dojo
Combobox" in the "Main Content Area".<jsp:useBean id="itemBean" scope="session" class="server.ItemValueBean"
/>
<a:widget name="dojo.combobox" value="${itemBean.value}"/>
jsp:useBean tag instantiates the bean "server.ItemValueBean"
in session scope. a:widget tag uses ${itemBean.value}
expression to load the data by invoking getValue() method from
the bean.ItemValueBean"
in the package "server". Replace the entire generated code with
the following:package server;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;public class ItemValueBean {The
public String getValue() {
JSONArray value = new JSONArray();
for (int i=0; i<2; i++) {
try {
JSONObject item = new JSONObject();
item.put("name", "name" + i);
item.put("label", "label" + i);
value.put(item);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
try {
return jsonArrayToString(value, null);
} catch (JSONException ex) {
ex.printStackTrace();
}
return null;
}
/**
* Converts a JSON Object to an Object Literal
*
*/
public String jsonToObjectLibertal(JSONObject jo, StringBuffer buff)
throws JSONException {
if (buff == null)
buff = new StringBuffer("{");
else
buff.append("{");
JSONArray names = jo.names();
for (int l=0; (names != null) && l < names.length(); l++) {
String key = names.getString(l);
String value = null;
if (jo.optJSONObject(key) != null) {
value = key + ":";
buff.append(value);
jsonToObjectLibertal(jo.optJSONObject(key), buff);
} else if (jo.optJSONArray(key) != null) {
value = key + ":";
buff.append(value);
jsonArrayToString(jo.optJSONArray(key),
buff);
} else if (jo.optLong(key, -1) != -1) {
value = key + ":" + jo.get(key) + "";
buff.append(value);
} else if (jo.optDouble(key, -1) != -1) {
value = key + ":" + jo.get(key) + "";
buff.append(value);
} else if (jo.opt(key) != null) {
Object obj = jo.opt(key);
if (obj instanceof Boolean) {
value = key + ":" +
jo.getBoolean(key) + "";
} else {
value = key + ":" + "'" +
jo.get(key) + "'";
}
buff.append(value);
}
if (l < names.length() -1) buff.append(",");
}
buff.append("}");
return buff.toString();
}
public String jsonArrayToString(JSONArray ja, StringBuffer buff) throws
JSONException {
if (buff == null)
buff = new StringBuffer("[");
else
buff.append("[");
for (int key=0; (ja != null) && key < ja.length(); key++) {
String value = null;
if (ja.optJSONObject(key) != null){
jsonToObjectLibertal(ja.optJSONObject(key), buff);
} else if (ja.optJSONArray(key) != null) {
jsonArrayToString(ja.optJSONArray(key),
buff);
} else if (ja.optLong(key, -1) != -1) {
value = ja.get(key) + "";
buff.append(value);
} else if (ja.optDouble(key, -1) != -1) {
value = ja.get(key) + "";
buff.append(value);
} else if (ja.optBoolean(key)) {
value = ja.getBoolean(key) + "";
buff.append(value);
} else if (ja.opt(key) != null) {
Object obj = ja.opt(key);
if (obj instanceof Boolean) {
value = ja.getBoolean(key)
+ "";
} else {
value = "'" + ja.get(key)
+ "'";
}
buff.append(value);
}
if (key < ja.length() -1) buff.append(",");
}
buff.append("]");
return buff.toString();
}
}
getValue methods contains the logic to generate the
business data. In this case, the method generates the
data model
expected by ComboBox using
JSON APIs. This data can very well be generated by
creating a
Persistence Unit and querying a database using JPA or any other
mechanism.
The jsonToObjectLibertal and jsonArrayToString
methods were originally posted
here.
These two methods are required because the JSON parser does not allow you to
create object literals but only JSON objects. By default these contain key :
value pairs where the keys are enclosed in double quotes which does not
match with the expected data model.

Another way to populate jMaki widgets with your data (using JPA) is explained here.
Please leave suggestions on other TOTD that you'd like to see. A complete archive is available here.
Technorati: totd jmaki beans glassfish netbeans
Posted by Arun Gupta in web2.0 | Comments[10]
|
|
|
|
|
Today's Page Hits: 234
Total # blog entries: 994