This example assumes that you
have a report with a table named
Customer that contains a
field named
Last_Years_Sales.
This example shows how to create a summary field
that finds the largest value for the
Customer.Last_Years_Sales field.
Get the
DataDefController object from a
ReportClientDocument object.
DataDefController dataDefController = rcd.getDataDefController();
Get the list of database tables, and
find theCustomer table.
Tables tables = rcd.getDatabaseController().getDatabase().getTables();
ITable customerTable = tables.findTableByAlias("Customer"); Get the list of database fields, and
find the
Last_Years_Sales
field.
Fields<IField> fields = customerTable.getDataFields();
IField salesField = fields.findField("{Customer.Last_Years_Sales}", FieldDisplayNameType.formulaName, java.util.Locale.US); Call the
canSummarizeOn method of the
SummaryFieldController class to
test whether summaries can be calculated for the field.
SummaryFieldController summaryFieldController = dataDefController.getSummaryFieldController();
boolean canSummarizeOn = summaryFieldController.canSummarizeOn(salesField);
Create a new
SummaryField
object.
ISummaryField summaryField = new SummaryField();
Call the
setSummarizedField method to
set the field that is being summarized.
summaryField.setSummarizedField(salesField);
Call the
setOperation
method to define the type of summary operation.
summaryField.setOperation(SummaryOperation.sum);
Note: Call the method
setOperationParameter if the
summary operation requires a parameter.
Call the
add method
of
SummaryFieldController to add
the summary to the report.
The value -1 adds the
summary to the end of the summary list.
summaryFieldController.add(-1, summaryField);
Example: Adding a summary to a report
This example shows how to calculate the sum of the
Customer.Last_Years_Sales field. The
summary is added to the report footer.
public void addSummary(ReportClientDocument rcd) throws ReportSDKException
{
DataDefController dataDefController = rcd.getDataDefController();
Tables tables = rcd.getDatabaseController().getDatabase().getTables();
ITable customerTable = tables.findTableByAlias("Customer");
Fields<IField> fields = customerTable.getDataFields();
IField salesField = fields.findField("{Customer.Last_Years_Sales}", FieldDisplayNameType.formulaName, java.util.Locale.US);
SummaryFieldController summaryFieldController = dataDefController.getSummaryFieldController();
boolean canSummarizeOn = summaryFieldController.canSummarizeOn(salesField);
if (canSummarizeOn)
{
ISummaryField summaryField = new SummaryField();
summaryField.setSummarizedField(salesField);
summaryField.setOperation(SummaryOperation.sum);
summaryFieldController.add(-1, summaryField);
}
}This list includes the classes used by the sample
code:
com.crystaldecisions.sdk.occa.report.application.DataDefController
com.crystaldecisions.sdk.occa.report.application.ReportClientDocument
com.crystaldecisions.sdk.occa.report.application.SummaryFieldController
com.crystaldecisions.sdk.occa.report.data.FieldDisplayNameType
com.crystaldecisions.sdk.occa.report.data.Fields
com.crystaldecisions.sdk.occa.report.data.IField
com.crystaldecisions.sdk.occa.report.data.ISummaryField
com.crystaldecisions.sdk.occa.report.data.ITable
com.crystaldecisions.sdk.occa.report.data.SummaryField
com.crystaldecisions.sdk.occa.report.data.SummaryOperation
com.crystaldecisions.sdk.occa.report.data.Tables
com.crystaldecisions.sdk.occa.report.lib.ReportSDKException