This example assumes that the
report you are working with contains a table named
Customer that contains a
field named
Last_Years_Sales, and
that the report contains one level of grouping.
You can summarize the fields in
a group. For example, you can group a report by region, and then summarize the
sales for each region.
Get the
DataDefController object.
DataDefController dataDefController = rcd.getDataDefController();
Find the
Customer table from
the list of tables.
Tables tables = rcd.getDatabaseController().getDatabase().getTables();
ITable customerTable = tables.findTableByAlias("Customer"); Find the
Last_Years_Sales
field from the list of fields.
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
validate that summaries can be calculated for the field.
SummaryFieldController summaryFieldController = dataDefController.getSummaryFieldController();
boolean canSummarizeOn = summaryFieldController.canSummarizeOn(salesField);
Get the
Group object
to add the summary to.
In this example, we the innermost group is
retrieved.
Groups groups = dataDefController.getDataDefinition().getGroups();
IGroup group = groups.getGroup(groups.size() - 1);
Create a new
SummaryField
object.
ISummaryField summaryField = new SummaryField();
Call the
setGroup
method to add the summary to a group.
summaryField.setGroup(group);
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);
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 group
The following example shows how to add a summary
field that calculates the sum of the
Customer.Last_Years_Sales field for
each group. The sum of
Customer.Last_Years_Sales for each
group is displayed in the group footer.
public void addGroupSummary(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)
{
Groups groups = dataDefController.getDataDefinition().getGroups();
IGroup group = groups.getGroup(groups.size() - 1);
ISummaryField summaryField = new SummaryField();
summaryField.setGroup(group);
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.Groups
com.crystaldecisions.sdk.occa.report.data.IField
com.crystaldecisions.sdk.occa.report.data.IGroup
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