Wednesday, January 23, 2008

More Phobos Data Access

A few months ago I was playing with some data access code and thought I
would publish it. One of the problems some people must solve is data
access to sources without a jdbc driver. I used Phobos to connect to a
Visual Foxpro database. The same technique should be able to be used
for an Access database or other data. The first example just shows a
driver connection. The second example shows a pooled connection using
Apache Commons. The module code for the pooling part is in a previous
blog. This means you can have a pooled data connection from Phobos to
just about any data source. This would not be possible with most Java
application servers, but since we are using our pooling objects provided
by Apache, we can accomplish a much greater variety of data access from
Phobos. Phobos is versatile and flexible.

Driver Example:
library.common.define(controller, "test", function() {
this.Test = function() {
this.onRequest = function() {

// Set up our connection by specifying the driver
java.sql.Class.forName="sun.jdbc.odbc.JdbcOdbcDriver";
// We are using the java odbc bridge with a VFP database
// You need a DSN set up on your machine and for VFP you
// only need to specify the DSN name. User & Pass are blank.
var con = new
java.sql.DriverManager.getConnection("jdbc:odbc:javafox","","");
// create a statement
stmt = con.createStatement();
stmt.setMaxRows(1);
// create the result set by executing the query
// You can specify properties for locking etc if you wish.
rs = stmt.executeQuery("select * from some table");
//Loop through the result set and make a json object

var rsJSON = "{rs: [";
var numcols = rs.getMetaData().getColumnCount();
while (rs.next()) { //Loop through record set
rsJSON += "{"; //resultet row
for (i=1; i<=numcols;i++) {
rsJSON += rs.getMetaData().getColumnName(i) +
": ";
rsJSON += rs.getString(i) + ", ";
} // end for
rsJSON += "},"; //end result set row
} //End while through result set
rsJSON += "]}" model={rs:
rsJSON}
con.close(); //clean up
// render a view with the data
library.view.render("test.ejs");

// Close the connection.
con.close();

}; // End on request function
};
});

Pooled Example:

response.setStatus(200);
response.setContentType("text/html");
writer = response.getWriter();
writer.println("<html><head><title>Phobos</title></head><body>");

var conn = null;
var stmt = null;
var rset = null;
// custom module for pooled connection - start pool (normally done in
startup)
module.vfpDB.startConnectionPool();

writer.println("Pool Started<br>");
writer.println("Creating connection.<br>");
conn =
java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:oConn");
writer.println("Creating statement.<br>");
var stmt = conn.createStatement();
writer.println("Executing statement.<br>");
rset = stmt.executeQuery('select * from some table);
writer.println("Results:<br>");
var numcols = rset.getMetaData().getColumnCount();
while (rset.next()) {
for(i=1;i<=numcols;i++) {
writer.println("<br>" + rset.getString(i));
}
}
writer.println("Closing Pool.<br>");
module.vfpDB.closeConnectionPool();

writer.println("</body></html>");
writer.flush();

Tuesday, January 1, 2008

With the arrival of the holidays and some time off, I was able to spend some time coding on my personal Phobos project. The fourth quarter months were so busy, I was not able to devote much time to coding. I have developed a starter web site, but wanted to develop a simple CMS to make it easy to make additions to the site. I have integrated the open source FCK editor and laid most of the groundwork. I am in the process of working out the bugs now. Instead of posting code samples, I hope to post all of the source code and maybe even a project file. I am pursuing it as time permits.

On another note, on a work related project, I have had to review a large amount of shopping cart software for selection for a new site. The languages covered were Java, PHP and .Net. One of the big requirements was the ability to get data in and out of the system easily in real time for integration into other business systems. After that, there was a need for a large amount of shopping features. Access to source code was also a must have feature whether it be open source or closed source. The system also needed to be somewhat economical for small business.

The two main Java options seemed to be KonaKart and SoftSlate. The KonaKart integration looked promising and the shopping system had plenty of features, but while it is free, they do not release the source code for their server side application. SoftSlate is an economical package with access to source code, but they do not expose web services and from what information I could gather, you would need to write your own integration code for external connections.

There were many php carts out there such as osCommerce, ZenCart and X-Cart. All of these are powerful, feature rich systems. Most of the integration features however consisted of importing data from a text file. While this is nice for total web based applications, it does not fare well interconnecting different business systems. Unfortunately, there wasn't much in the way of documentation as to how to get data in and out of these systems. It would be easy enough to write your own code, but on a complicated system, you wouldn't expect to write data directly to tables without having clear instructions on what data needs to go where. Many of the php carts say that they are working on data and layer abstractions.

I reviewed a few dotnet systems and the one that seemed to shine was AspDotNetStorefront. It was reasonably priced, you have access to source code and external integration in real time is extensive. It is based on proprietary technology, but it does provide advanced features, integration, and source code.

Out of the systems reviewed, it was interesting that depending on the language used, greatly affected the ability of others to work with a complicated system created by someone else. Most Java programmers abstract data access and other tasks. The object oriented nature of Java forces you to do this. The same principle would apply somewhat with dotnet. With PHP and Classic ASP, nothing is abstracted and the code is very free flowing. The nature of these languages make it quick to get stuff done, but as a system gets complicated, maintenance can become difficult. It also makes it much harder for someone who is not familiar with the system to come in and be productive because there isn't a built in blue print. Obviously there are exceptions to all rules because everything is still programmer dependent.

How does this affect Phobos? JavaScript is a very free flowing language and you can get things done very quickly with it. However, the Phobos architecture lends itself to write blue printed code. I think you can have the best of both worlds with Phobos. Of course this can be done in PHP now that it is object oriented as well as other languages, but the only language still that runs in the browser natively is JavaScript.

I think there is still room for a good open source shopping cart out there as well as simple CMS systems. The web is constantly changing and integration is becoming key. I believe Phobos fits the bill for small and large applications. Simplicity backed by the power of the Java infrastructure.