Using @OP tags with grid rules  Multiple co-brands

Chapter 1: Developer Features

Creating user-defined grid-rule functions

This section describes how to use the new grid-rule function, which you can invoke within the new Edit Rules section in the Filter window.

The new edit grid rule allows you to add a text value into a specified set of grid cells, determining the value to be added explicitly (as a literal string), or by executing a command or function, with this syntax

	=command(arg[, arg[…]])

For example, this calculates the sum of all the cells specified in the array $R4F5:R6F8:

=sum($R4F5:R6F8)

For each new grid rule function, you must create a corresponding Java class, which must exist inside the com.onepage.ccl.execute package. The class must implement the com.onepage.ccl.execute.Function interface, and must be named <something>Function; for example, MaxFunction, or SumFunction. For ease of development, there is a base class that you can extend—com.onepage.ccl.execute.FunctionBase—that implements the necessary interface, and provides some useful support functions.

To illustrate the steps necessary, use the sample code below to create a real function called count, which counts the number of entries in an array.

Create a new Java class using this input:

package com.onepage.ccl.execute;
include java.util.Vector;
public class CountFunction extends FunctionBase
{
	/**
	* This is the main constructor.  We delegate to the base
	* constructor
	*
	* @param args Vector of arguments provided to the function
	* @param array Two-dimensional array of GridCell elements. This represents the array of data we have access to.
	* @param row Current row number (1-based)
	* @param col Current col number (1-based)
	*/
	public CountFunction(Vector args,
 GridCell[][] array,
 int row,
 int col)
{
 super(args, array, row, col);
 // perform any necessary initialisation steps here
}
/**
* This routine does the work: it has access to the array of
* data, and using current row and col details, and the
* arguments provided to the function, it can evaluate a return
* string to be inserted at the current cell position.
*/
public String process()
{
 // write code here ...
}
}

Use the new function by entering =count(arg[, arg[, …]]) when you edit the grid rule.

The main work is done within the process method, which creates a result based on the cell data, along with the current row and column information. The method can use the arguments provided by the user to determine the steps to take.

There are two useful support functions available to the class, which are provided in FunctionBase:

For example, to return a count of the elements specified by an array specifier, use extractSubArray to return the targeted subarray, and then calculate the number of elements, which is the number of columns time the number of rows.The parent class FunctionBase provides a number of instance variables that you can use to determine the arguments that are passed in, and the current row and column:

For the example above, you can do something similar to this:

public String process()
{
     GridCell[][] cells =
extractSubArray((String)_args.elementAt(0));
     int count = cells.length * cells[0].length;
     return "" + count;
}

This code handles the case =count($R1F2:R4F6). You can also make handling of the count’s arguments more extensive; for example, allow any number of array-specifiers to be provided. For example:

public String process()
{
     int count = 0;
     for (int i = 0; i < _args.size(); i++)
     {
          GridCell[][] cells = 

extractSubArray((String)_args.elementAt(i));
     count += cells.length * cells[0].length;
     }
     return "" + count;
}

This code handles the case =count($R1F2:R4F6, $R5F8:R8F9), and any number of arguments.To compile the class file, you must have onepage.jar in your CLASSPATH. Place the resulting class file in the WEB-INF\classes hierarchy, creating the necessary subdirectories. For example, on Windows, enter:

javac -classpath %ONEPAGE%\WEB-INF\lib\onepage.jar
-d %ONEPAGE%\WEB-INF\classes CountFunction.java

where %ONEPAGE% points to the onepage Web application installation directory in the application server’s directory; for example, x:\infoedition\tomcat\webapps\onepage\.

After compiling the class file, you must restart the application server.





Copyright © 2004. Sybase Inc. All rights reserved. Multiple co-brands

View this book as PDF