Developing Web Applications
with OpenRules and Tomcat

Part 4 – Moving Presentation Logic to OpenRules

This is the fourth part of a step-by-step account of how to develop web applications using Tomcat and OpenRules. The first part described how to setup a Tomcat-based development environment and create a trivial web application. The second part added a JSP-based GUI and Java classes to implement a HelloCustomer application that greets a customer based on his/her personal data and time of the day. The third part moved business logic of greeting generation from Java to Business Rules using OpenRules decision tables. Now we want to replace JSP-based GUI with OpenRules Forms.
 

Step 13 – Creating a new project with OpenRules jars

Let's copy previously created project 'hello.openrules.tomcat' to a new project 'hello.openrules.forms.tomcat'. Before making any serious changes, we have to replace the word  'hello.openrules.tomcat' to 'hello.openrules.forms.tomcat' in the files build.properties and run.html. Rebuild the project and make sure that it still works.

Now we have to make sure that subdirectory 'lib' inside 'war/WEB-INF' contains additional files from 'openrules.config' that is a part of OpenRules standard installation:

Step 14 – Adding OpenRules Forms

In the Part 3, we externalized business logic inside into OpenRules decision tables presented in the file HelloCustomer.xls.  Now we also want to use OpenRules layout tables to represent  our GUI.  Instead of using a developer-oriented JSP code presented in index.jsp file, we will define a layout of the same web page using the following Excel table:

This layout table will allow us to dynamically create the proper html page and receive a user's input in the properties of the object "c" of our Java type Customer. It uses a method "generatedGreeting" to display a generated greeting:

This greeting is generated every time when a user clicks the button "Update Greeting" and our server executes the following method "body":

The method 'body' represents a standard way to run OpenRules-based web applications. The only parameter of this method is "dialog" that contains all business objects passed from a Java application. In our case, we expect to receive two objects: customer and response. The method "body' takes the current 'hour' of the day and executes our old decision tables "defineGreeting" and "defineSalutation" saving results in the 'response'.

Now let's reorganize our Excel tables in a way that will allow you to extend them in the future.  Instead of one file HelloCustomer.xls we will create the following files:

File Tables Comments
war/rules/include/HelloRules.xls defineGreeting old decision table from HelloCustomer.xls
  defineSalutation old decision table from HelloCustomer.xls
war/rules/include/HelloForm.xls helloLayout a new TableLayout defined above
war/rules/main/HelloWeb.xls body a new method defined above
  generatedGreeting a new method defined above
  Environment a table that describes the fact that files HelloRules.xls and HelloForms.xls are included into HelloWeb.xls

Here is the Environment table:

Step 15 – New index.jsp

We use a standard OpenRules index.jsp file instead of our own defined earlier. Here it is:

 

hello.tomcat/war/index.jsp
<%@ page import="com.openrules.forms.gui.jsp.*" %>
<%@ page import="com.openrules.forms.*" %>
<%@ page import="hello.*" %>

<%
  String xlsMain = "file:../webapps/hello.openrules.forms.tomcat/rules/main/HelloWeb.xls";
  String s_attr = "openrules_session"; 
  // check session
  OpenRulesSession openrulesSession = (OpenRulesSession) session.getAttribute(session_attr);
  if (openrulesSession == null ) {
	openrulesSession = new OpenRulesSession(xlsMain);
	openrulesSession.setAttribute(s_attr, openrulesSession);

	Customer customer = new Customer();
	customer.setName("Robinson");
	customer.setGender("Female");
	customer.setMaritalStatus("Married");
	EngineResponse engineResponse = new EngineResponse();

	Dialog dialog = openrulesSession.getDialog();
	dialog.put("customer",customer);
	dialog.put("response",engineResponse);
  }
%> 

<HTML>
  <HEAD><TITLE> hello.openrules.forms.tomcat </TITLE></HEAD>
  <body>
   <%
	openrulesSession.processRequest(session, request, out);
   %>
  </body>
</HTML>
 

Out application specific code is highlighted by red.  When a web session is created for the very first time we create OpenRules internal OpenRulesSession. We also create an instance of the object Customer and put it into a standard dialog object under the name "customer". Dialog is a standard placeholder (a HashMap) for different application-specific objects, which states we want to save between different Web client requests during one web interaction session.

The code between body-tags will be executed upon every HTTP requests. The default "processRequest" method will call an underlying OpenRulesEngine to be used with our Excel file HelloWeb.xls. After its completion, "processRequest" will produce an html-page as it was specified by the method "body" of the file HelloWeb.xls.

Thus, we do not need our old GreetingEngine anymore - just delete files IGreetingEngine.java and GreetingEngine.java from the package "src/hello".

Step 16 – Deploy and Execute

Now we are ready to build, deploy and test our hello.openrules.forms.tomcat application. Run Ant target 'deploy' with a Tomcat running. After running a browser with to http://localhost:8080/hello.openrules.forms.tomcat/index.jsp (or from file run.html) we should see the same web client:

Summary

  1. The first part described how to setup a Tomcat-based development environment and create a trivial web application

  2. The second part added a JSP-based GUI and Java classes to implement a HelloCustomer application that greets a customer based on his/her personal data and time of the day (project hello.tomcat)

  3. The third part moved business logic of greeting generation from Java to Business Rules using OpenRules decision tables (project hello.openrules.tomcat)

  4. The fourth part moved GUI to OpenRules (project hello.openrules.forms.tomcat)

The actual implementations of projects hello.tomcat, hello.openrules.tomcat, and hello.openrules.form.tomcat are available as a part of OpenRules InsideTrack.

Back    


Copyright © 2005-2006, OpenRules, Inc.