Writing Business Logic in Java

Many reasons exist why you would want to write business logic for your non-PeopleSoft applications (and even your PeopleSoft applications) in Java. Perhaps you have licensed a third-party set of Java classes to do some very specific processing (tax calculation, for example). Or perhaps you have developed some internal Enterprise JavaBeans for your specific business processes. Or perhaps you simply like to code in Java. Whatever the reason, you can easily integrate Java code with your applications through the Java PeopleCode functions.

Invoking Java from PeopleCode

Three primary Java PeopleCode functions are used to invoke Java from PeopleCode:

  • GetJavaClass

  • CreateJavaObject

  • CreateJavaArray

Java PeopleCode Function Usage Example

GetJavaClass

Finds a Java class that you can manipulate in PeopleCode. This is used for those classes that have static members for which instantiating an object of the class isn't appropriate. You can call only static methods, that is, class methods, with the object created with this function.

In Java, you access such static members of a class by using the class name:

result = java.class.name.SomeStaticMethod();

In PeopleCode, you use the following code:

&Result = GetJavaClass("java.class.name").
SomeStaticMethod();

The following example is simple PeopleCode that uses GetJavaClass to get a system class:

&Sys = GetJavaClass("java.lang.System");
&Sys.setProperty("java.security.policy", "C:\java\policy");
WinMessage("The security property is: " | 
&Sys.getProperty("java.security.policy"));
&Props = &Sys.getProperties();
&Props.put("java.security.policy", "C:\java\policy");
&Sys.setProperties(&Props);
WinMessage("The security property is: " | 
&Sys.getProperty("java.security.policy"));

CreateJavaObject

Creates a Java object that can be manipulated in your PeopleCode. You can use the CreateJavaObject function to create a Java array. If ClassName is the name of an array class (ending with [ ]), ConstructorParams are used to initialize the array.

In Java, use the following code to initialize an array:

intArray = new int[]{1, 2, 3, 5, 8, 13};

In PeopleCode, you use the following code to initialize a Java array:

&IntArray = CreateJavaObject("int[]", 1, 2, 3, 5, 8, 13);

If you want to initialize a Java array without knowing the number of parameters until runtime, use the CreateJavaArray function.

The following example is a simple PeopleCode program that creates a Java object from a sample program that generates a random password:

/* Example to return Random Passwords from a Java class */
Local JavaObject &oGpw;
/* Create an instance of the object */
&oGpw = CreateJavaObject("com.PeopleSoft.Random.Gpw_Demo");
&Q = "1";
/* Call the method within the class */
&NEW_VALUE = &oGpw.getNewPassword(&Q, PSRNDMPSWD.LENGTH);
/* This is just returning one value for now */
PSRNDMPSWD.PSWD = &NEW_VALUE;

CreateJavaArray

Enables you to create a Java array without knowing the number or value of the elements.

When you create an array in Java, you already know the number of elements in the array. If you don't know the number of elements in the array, but you want to use a Java array, use the CreateJavaArray function in PeopleCode. This will create a Java object that is a Java array, and you can pass in the number of elements that are to be in the array.

The following PeopleCode example passes a PeopleCode array of strings (&Parms) to a Java method xyz of class Abc. This example assumes that when you are writing the code, you don't know how many parameters you will have.

Local JavaObject &Abc, &RefArray;
Local array of String &Parms;
&Parms = CreateArray();
/* Populate array how ever you want to populate it */
&Abc = GetJavaObject("com.peoplesoft.def.Abc");
/* Create the java array object. */
&JavaParms = CreateJavaArray("java.lang.String[]",  &Parms.Len);
/* Populate the java array from the PeopleCode array. */
&RefArray = GetJavaClass("java.lang.reflect.Array");
For &i = 1 to &Parms.Len
	&RefArray.set(&JavaParms, &i - 1, &Parms[&i]);
End-For;
/* Call the method. */
&Abc.xyz(&JavaParms);

Accessing the PeopleSoft Runtime System from Java

After a Java class has been invoked, the class can access the PeopleSoft runtime system. Java classes delivered with PeopleTools enable you to call PeopleCode from your Java program and access contextual information from the runtime system, such as the current user’s role and language preference. By importing the PeopleTools-delivered Java classes in your Java program, you can have access to PeopleCode objects and methods. Hundreds of PeopleSoft system variables, constants, and built-in functions are available for use with this approach. Discussions of the various methods follow.

Accessing the runtime system works only from a Java program that was initially called from PeopleCode. You must call PeopleCode facilities only from the same thread that was used for the call into Java. You cannot call any PeopleCode facility that would cause the server to return to the browser for an end-user action because the state of the Java computation cannot be saved and restored when the action is complete.

Java Class Usage

SysVar

Use to refer to PeopleSoft system variables, such as %Language or %Oprid.

For example, %Session, becomes SysVar.Session().

See PeopleCode Developer’s Guide: System Variables.

SysCon

Use to refer to system constants, such as %SQLStatus_OK or %FilePath_Absolute.

For example, %CharType_Matched becomes SysCon.CharType_Matched.

See PeopleCode Developer’s Guide: Constants.

Name

Enables you to use the PeopleSoft-reserved item references. This enables you to reference pages, components, records, fields, and so forth.

For example, you can refer to a record field using the following PeopleCode:

REC_NAME.FIELD_NAME

With the Name class, you can use a similar construct:

new PeopleSoft.PeopleCode.Name("REC_NAME", "FIELD_NAME");

As another example, you can refer to a PeopleSoft page using the following PeopleCode:

Page.PAGE_NAME

In Java, it would be:

new PeopleSoft.PeopleCode.Name("Page", "PAGE_NAME");

See PeopleCode Developer’s Guide: Definition Name References.

Func

Use to refer to built-in PeopleCode functions, such as CreateRowset or GetFile.

For example, SetLanguage(LANG_CD) becomes Func.SetLanguage(LANG_CD).

The existing PeopleCode classes (Array, Rowset, and so forth) have properties and methods that you can access from Java. PeopleCode classes have the same name, so Record becomes Record, SQL becomes SQL, and so forth. Methods are accessed by the method name. The name of a property is prefixed with either get or set, depending on whether you're reading or writing to the property. For example, to get the IsChanged property would be getIsChanged. To set the value for a field would be &MyField.setValue.

Func (continued)

Here is an example of a Java program that uses PeopleCode objects to access the database:

/* This code is used to test the Java/PeopleCode interface. */
import PeopleSoft.PeopleCode.*;
public class Test {
/*
* Add up and return the length of all the
* item labels on the UTILITIES menu,
* found two different ways.
*/
public static int Test() {
/* Get a Rowset to hold all the menu item records. */
Rowset rs = Func.CreateRowset(new Name("RECORD", "PSMENUITEM"), new Object[]{});
String menuName = "UTILITIES";
int nRecs = rs.Fill(new Object[]{"WHERE FILL.MENUNAME = :1", menuName});
int i;
int nFillChars = 0;
for (i = 1; i <= rs.getActiveRowCount(); i++) {
String itemLabel = (String)rs.GetRow(i)
.GetRecord(new Name("RECORD", "PSMENUITEM"))
.GetField(new Name("FIELD", "ITEMLABEL"))
.getValue();
nFillChars += itemLabel.length();
}
/* Do this a different way - use the SQL object to read each menu
item record. */
int nSQLChars = 0;
Record menuRec = Func.CreateRecord(new Name("RECORD", "PSMENUITEM"));
SQL menuSQL = Func.CreateSQL("%SelectAll(:1) WHERE MENUNAME = :2",
new Object[]{menuRec, menuName});
while (menuSQL.Fetch(new Object[]{menuRec})) {
String itemLabel = (String)menuRec
.GetField(new Name("FIELD", "ITEMLABEL"))
.getValue();
nSQLChars += itemLabel.length();
}
return nFillChars + 100000 * nSQLChars;
}
}

Func (continued)

This can be run from PeopleCode in the following way:

Local JavaObject &Test;
Local number &chars;
&Test = GetJavaClass("Test");
&chars = &Test.Test();
&Test = Null;
WinMessage("The character counts found are: " | &chars, 0);

See PeopleCode API Reference and PeopleCode Language Reference.