How to invoke Java classes from a Page Flow

From OpenQuote

Jump to: navigation, search

Contents

Note: This page is under construction

Through the use of JavaServices and the embedding of Java code within within Drools rules, OpenQuote already provides easy access to all of the facilities and power of Java. However, there are times when these types of access are still to limiting and you really need to be able to invoke classes developed completely outside of OpenQuote. This HOWTO explains how this can be achieved.

The approach described here applies to OpenQuote release 1.2 and later.

[edit] Outline of the steps

The simplest way to access your own classes from within a PageFlow is to use them from within either a JavaService, or from within Drools rules. This approach offers the potential to avoid introducing a dependency on any OpenQuote classes into your code, though it does have the downside of requiring a little extra plumbing.

In principal, it is a simple as two steps:

  1. Place the jar containing your classes into <OpenQuote Home>/jboss/server/default/deploy/openquote.ear/lib
  2. Import and use the classes from JavaServices, or Drools based rules.

Alternatively, you can implement a page flow service in your own Java code - and therefore invoke your classes directly from the PageFlow.

Either way, the whole of the Quotation model - including all of the data associated with the quote currently being processed is available to you from within the Java code. Naturally, accessing the Quotation object directly will mean that your code has a dependency on some OpenQuote classes.

We'll work though two examples. The first will access a Java class without introducing any dependency between that code and OpenQuote. In the second, a new java service will be developed and invoked directly from the PageFlow. In this case the service will also have complete access to the Quotation being processed.

The scenario for both is the same: adding external registration number validation to the MotorPlus product. The validate here will be very simple, but could easily for the basis for accessing 3rd party motor vehicle databases.

[edit] Example 1 Invoking Java code from a JavaService

In this example we'll add a JavaService to the product and use it as a wrapper to invoke a simple validation method in an external class. That method will accept a String representing the registration number to be checked, and return a boolean valid/invalid flag. As the method can't see the quotation object itself, the wrapper will also take care of placing an error indicator on the registration number attribute if the value returned in false.

[edit] Creating the RegistrationNumberValidation class

The first job is to create the validation class itself. Using an IDE of your liking, enter the following code and compile it.

package com.acme;
 
public class VehicleValidator {
	static public boolean isRegistrationNumberValid(String registrationNumber) {
		return registrationNumber.matches("^[A-Z0-9 ]*$");
	}
}

The validation shown here could be much more easily achieved using normal Attribute field validation in OpenQuote. However, this code could obviously be extended easily to invoke external services instead.

[edit] Depoloying the RegistrationNumberValidation class

Package the newly generated .class file into a jar, and copy that jar into [OpenQuote Home]/jboss/server/default/deploy/openquote.ear/lib.

If you have OpenQuote running already, you'll see messages scrolling past in it's console window as JBoss redeploys the application in response to the newly added jar. You may see some exception messages related to Java Server Faces, you can safely ignore these.

If you don't have OpenQuote running, start it now.

[edit] Adding a new JavaService to the MotorPlus PageFlow

In this example we are going to wrap the call to the new validation class in a JavaService to make it accessible to the to the PageFlow.

In this step we will just add a simple "Hello World" JavaService to the product, and invoke it from the PageFlow. We will populate that service with more meaningful code in the next step.

  • Login to OpenQuote as a product developer, and from Alfresco window on the Product Development tab navigate into the MotorPlus product's space.
  • Use "Create->Create Content" to add a new file with the name "VehicleValidatorService.java" of type "Plain Text". Set the content of the new file to the following:
import com.ail.core.product.executepageaction.ExecutePageActionArgImp;
 
public class VehicleValidatorService {
    public static void invoke(ExecutePageActionArgImp args) {
        System.out.println("Hello World!");
    }
}
  • To make your new VehicleValidatorService available to the PageFlow, edit Registry.xml in the MotorPlus folder in Alfresco, and add the following:
<service name="VehicleValidatorService" builder="CachingClassBuilder" key="com.ail.core.command.JaninoAccessor">
    <parameter name="Url">~/VehicleValidatorService.java</parameter>
</service>
 
<command name="VehicleValidator" builder="ClassBuilder" key="com.ail.core.product.executepageaction.ExecutePageActionCommand">
    <parameter name="Service">VehicleValidatorService</parameter>
</command>

[edit] Using RegistrationNumberValidation from the JavaService

[edit] Testing

[edit] Debugging

[edit] Example 2 Implementing a PageFlow service

essays