XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Developing an OpenLaszlo App
by Sreekumar Parameswaran Pillai | Pages: 1, 2, 3, 4

newcontact.lzx

The addNewContact() method caches the values from the form fields in local variables for reuse and makes a call to the application layer to update the database.

<method name="addNewContact">
setAttribute('emailid',hb_1.email_in.getText());
setAttribute('firstnm',hb_2.fname_in.getText());
setAttribute('lastnm',hb_2.lname_in.getText());
setAttribute('phonenmbr',hb_4.pnumber_in.getText());

var ds=canvas.datasets.addnewDS;
var p=new LzParam();
p.addValue("action","addnewcontact",true);
p.addValue("emailid",emailid, true);
p.addValue("firstname",firstnm,true);
p.addValue("lastname",lastnm,true);
p.addValue("phonenumber",phonenmbr,true);
p.addValue("housename",hb_3.hname_in.getText(),true);
p.addValue("streetname",hb_3.sname_in.getText(),true);
p.addValue("cityname",hb_4.cname_in.getText(),true);
ds.setQueryString(p);
ds.doRequest();
</method>

Then resets the form components:

<method name="resetForm">
hb_1.email_in.setText("");
hb_2.fname_in.setText("");
hb_2.lname_in.setText("");
hb_4.pnumber_in.setText("");
hb_3.hname_in.setText("");
hb_3.sname_in.setText("");
hb_4.cname_in.setText("");
</method>

The same functionality is provided on other datapointers. Here again, the errors checked are for the add new contact action. If the message is successful, this gets a pointer to the root of the dataset, traverses through the child elements and sets the text to the value it was given in the form components. The datapath of the grid is then updated so that the changed data is visible in the grid.

<datapointer xpath="addnewDS:/addressbook">
<method event="ondata">
if (this.xpathQuery("result[@action='addnewcontact']/text()") == "success") {
messagetext.setText("Contact details successfully Inserted");
var dp = canvas.datasets.listcontactsDS.getPointer();
dp.selectChild(2);
dp.addNodeFromPointer(dp);
dp.setNodeAttribute('email',classroot.emailid);
dp.selectChild();
dp.setNodeText(classroot.firstnm);
dp.selectNext();
dp.setNodeText(classroot.lastnm);
dp.selectNext();
dp.setNodeText(classroot.phonenmbr);
Debug.write('classroot.emailidentifier',classroot.emailid);
parent.setVisible(false);
canvas.mainwindow.listAllContacts();
phonelist.setVisible(true);
contactsgrid.datapath.updateData();
} else if (this.xpathQuery("result[@action='addnewcontact']/text()") == "failure") {
messagetext.setText("Update failed !");
}else if (this.xpathQuery("result[@action='exception']/text()") == "failure") {
messagetext.setText("There is an exception!");
}else{
messagetext.setText("");
}
</method>
</datapointer>

datasets.lzx

This declares all the datasets required for the application

<library>
<!-- dataset for the entire list of contacts in the database -->
<dataset type="http" name="listcontactsDS" src="http://localhost:8080/laszlotutorial/xmlfetcher.jsp?" request="false"/>

<!-- dataset for the entire details of a single contact -->
<dataset type="http" name="contactdetailsDS" src="http://localhost:8080/laszlotutorial/xmlfetcher.jsp?" request="false"/>

<!-- dataset to add a new contact to the database -->
<dataset type="http" name="addnewDS" src="http://localhost:8080/laszlotutorial/xmlfetcher.jsp?" request="false"/>
</library>

xmlfetcher.jsp

The following XQuery returns the email ID, first name, last name, and phone. Note the strings <addressbook> before and after the XQuery expression. This is required to wrap the <person> element within the root element <addressbook>.

if (action.equals("getall")) {
pstmt = connection.prepareStatement("XQuery <addressbook> {
( for $person in db2-fn:xmlcolumn('DB2ADMIN.ADDRESSBOOK.CONTACTINFO')/person"+
"return <person  email=\"{$person/@email}\"> {($person/firstname), ($person/lastname),($person/phone)} </person>)}
</addressbook>");
rs = pstmt.executeQuery();

while (rs.next()) {
System.out.println(rs.getString(1));
out.print(rs.getString(1));
}
}

In this action, the entire information about a person is retrieved and returned to the client. The email ID of the person comes to the JSP as a value of the request parameter--email ID.

if (action.equals("getcontactdetails")) {
String pk = request.getParameter("emailid");

String sql="values(xmlquery(' for $person in db2-fn:xmlcolumn(\"DB2ADMIN.ADDRESSBOOK.CONTACTINFO\")/person"+
" where $person/@email eq $emailid " +
" return <addressbook>{($person)}</addressbook> ' "+
" passing cast(? AS varchar(50) ) as \"emailid\" ))";

pstmt = connection.prepareStatement(sql);

pstmt.setString(1,pk);
rs= pstmt.executeQuery();

while (rs.next()) {
System.out.println(rs.getString(1));
out.print(rs.getString(1));
}

In this action, the new contact information is persisted to the database. The information is parsed from the request parameters and converted into an XML string before it goes into the CONTACTINFO column. In a production environment, this will be taken care of by an XML generator utility within the application framework.


if (action.equals("addnewcontact")) {
String pk = request.getParameter("emailid");
String fname = request.getParameter("firstname");
String lname = request.getParameter("lastname");
String hname = request.getParameter("housename");
String sname = request.getParameter("streetname");
String cname = request.getParameter("cityname");
String phnumber = request.getParameter("phonenumber");

String finalString="<person email="+"\""+ pk +"\""+"><firstname>"+fname+"</firstname><lastname>"+lname+"</lastname><phone>"+
phnumber+"</phone><housename>"+hname+"</housename><street>"+
sname+"</street><city>"+cname+"</city></person>";

String sql="INSERT INTO DB2ADMIN.ADDRESSBOOK(EMAILID ,CONTACTINFO)values('"+pk+"','"+finalString+"')";

stmt = connection.createStatement();
stmt.executeUpdate(sql);

out.print("<addressbook><result action=\"addnewcontact\">success</result></addressbook>");

}

In the updatecontact action, the changed information on a user is updated to the database. As in the earlier case, the XML string is generated and the DB XML column updated with the new string. In this context, we should also consider that the same XML can be produced at the presentation layer and sent as the value of a parameter to achieve the same effect. However, this may not be desirable since in most cases, business logic would need to be executed on the data from the presentation layer before persisting it to the database.

if (action.equals("updatecontact")) {
String pk = request.getParameter("emailid");
String fname = request.getParameter("firstname");
String lname = request.getParameter("lastname");
String hname = request.getParameter("housename");
String sname = request.getParameter("streetname");
String cname = request.getParameter("cityname");
String phnumber = request.getParameter("phonenumber");

String finalString="<person email="+"\""+ pk +"\""+"><firstname>"+fname
+" </firstname><lastname>"+lname+"</lastname><phone>"+
phnumber+"</phone><housename>"+hname+"</housename><street>"+
sname+"</street><city>"+cname+"</city></person>";

String sql="UPDATE DB2ADMIN.ADDRESSBOOK SET CONTACTINFO='"+ finalString+ "' WHERE EMAILID='"+pk +"'";

stmt = connection.createStatement();
stmt.executeUpdate(sql);

out.print("<addressbook><result action=\"updatecontact\">success</result></addressbook>");
System.out.println("<addressbook><result>success</result></addressbook>");

}

The catch block where the exceptions are caught and registered to an XML string and sent to the client. The exception message is available at the <result> </result> node.

} catch (Exception e) {
e.printStackTrace();
out.print("<addressbook><result action=\"exception\">failure</result></addressbook>");
} finally {
System.out.println("executing finally ");
try {
System.out.println("Closing the statement and connection");
if(null!=rs){
rs.close();
}
if(null != pstmt){
pstmt.close();
}
connection.close();
System.out.println("closing the connection");
} catch (Exception e) {
System.out.println("There is exception in the finally block");
e.printStackTrace();
}
}

Conclusion

In this article we explain how to develop an application with DB2 Express-C Edition, OpenLaszlo, and Java technologies. We have explored DB2 XQuery functions to a very limited extent. We have also achieved the ideal 100 percent separation between the view and the application code as expected in a tiered web application, as well as seamless integration between the two using XML. We have also seen a solution to increase system response speed to user actions by manipulating only the changed data in any situation.

References

  1. Software Engineer's Guide to Developing OpenLaszlo Applications
  2. LZX Reference Manual
  3. IBM DB2 Database for Linux, Unix, and Windows information center


1 to 1 of 1
  1. OpenLaszlo is free and open source
    2006-11-11 00:46:29 Arab
1 to 1 of 1