Navigating a CompoundCursor for Different Displays of Data

With methods on a CompoundCursor you can easily move through, or navigate, its structure and get the values from its ValueCursor descendents. Data from a multidimensional OLAP query is often displayed in a crosstab format, or as a table or a graph.

To display the data for multiple rows and columns, you loop through the positions at different levels of the CompoundCursor depending on the needs of your display. For some displays, such as a table, you loop through the positions of the parent CompoundCursor. For other displays, such as a crosstab, you loop through the positions of the child Cursor objects.

To display the results of a query in a table view, in which each row contains a value from each output ValueCursor and from the base ValueCursor, you determine the position of the top-level, or root, CompoundCursor and then iterate through its positions. Example 9-6 displays only a portion of the result set at one time. It creates a Cursor for a Source that represents a query that is based on a measure that has unit cost values. The dimensions of the measure are the product and time dimensions. The creation of the primary Source objects and the derived selections of the dimensions is not shown.

The example joins the Source objects representing the dimension value selections to the Source representing the measure. It prepares and commits the current Transaction and then creates a Cursor. It casts the Cursor to a CompoundCursor. The example sets the position of the CompoundCursor, iterates through twelve positions of the CompoundCursor, and prints out the values specified at those positions. The TransactionProvider is tp and the DataProvider is dp. The output object is a PrintWriter.

If the time selection for the query has eight values, such as the first day of each calendar quarter for the years 1999 and 2000, and the product selection has three values, then the result set of the unitPriceByDay query has twenty-four positions. Example 9-6 displays something like the following table, which has the values specified by positions 7 through 18 of the CompoundCursor.

Example 9-7 uses the same query as Example 9-6. In a crosstab view, the first row is column headings, which are the values from timeSel in this example. The output for timeSel is the faster varying output because the timeSel dimension selection was joined to the measure first. The remaining rows begin with a row heading. The row headings are values from the slower varying output, which is productSel. The remaining positions of the rows, under the column headings, contain the unitPrice values specified by the set of the dimension values.

To display the results of a query in a crosstab view, you specify the positions of the children of the top-level CompoundCursor and then iterate through their positions. Example 9-7 gets the values but does not include code for putting the values in the appropriate cells of the crosstab display.

Figure 9-1 Crosstab View of the Result Set Specified by unitPriceByDay

Example 9-8 creates a Source that is based on a sales amount measure. The dimensions of the measure are the customer, product, time, channel, and promotion dimensions. The Source objects for the dimensions represent selections of the dimension values. The creation of those Source objects is not shown.

The query that results from joining the dimension selections to the measure Source represents total sales amount values as specified by the values of its outputs.

The example creates a Cursor for the query and then sends the Cursor to the printAsCrosstab method, which prints the values from the Cursor in a crosstab. That method calls other methods that print page, column, and row values.

The fastest varying output of the Cursor is the selection of customers, which has three values that specify all of the customers from France, the UK, and the USA. The customer values are the column headings of the crosstab. The next fastest varying output is the selection of products, which has four values that specify types of products. The page dimensions are selections of two time values, which are the first and second calendar quarters of the year 2000, one channel value, which is the direct channel, and one promotion value, which is all promotions.

The TransactionProvider is tp and the DataProvider is dp. The output object is a PrintWriter.