| Introduction
|
Aims
| The aim of this unit is to provide you with a solid understanding of COBOL Report Writer. This unit introduces the report writer by -
In the next unit we examine the syntax and semantics of the Report Writer clauses and verbs in detail.
|
Objectives
| By the end of this unit you should -
|
Prerequisites
| You should be familiar with the material covered in the unit;
|
| The Report Writer in Action
|
Introduction
| Producing printed reports is an important aspect of business programming. Unfortunately, report programs are often tedious to code. Report programs are long, and frequently consist of mere repetitions of the tasks and techniques used in other report programs. To simplify the task of writing report programs COBOL contains the Report Writer. The COBOL Report Writer is very large. It includes many new DATA DIVISION entries, including a REPORT SECTION, and a number of new PROCEDURE DIVISION verbs.
|
An example report - with animated commentary.
| The first page from an example report, produced by the Report Writer, is shown below. The report shows the sales made by Bible salespersons in all of the cities of Ireland. In the attached animated commentary we will examine what the program has to do to produce the report.
|
The Procedure Division that produces the sales report
| In a program that did not use the Report Writer, the PROCEDURE DIVISION required to produce the sales report shown above would occupy a hundred lines or so of code. When the REPORT WRITER is used, only the PROCEDURE DIVISION shown below is required. All the work of printing the report is being done in just 10 statements.
|
So much work, so little Procedure Division code
| How can so much work be done in so little PROCEDURE DIVISION code? How does the report writer know that page headings are required or that it is time to print the salesperson or city totals. To achieve so much in so little PROCEDURE DIVISION code, the Report Writer uses a declarative approach to programming rather than the procedural one familiar to most programmers. When the Report Writer is used, the programmer declares what to print when a page heading, page footing, salesman total, city total or final total is required and the Report Writer works out when to print these items. In keeping with the adage that "there is no such thing as free lunch", the PROCEDURE DIVISION of a Report Writer program is short because most of the work is actually done in the (greatly expanded) DATA DIVISION.
| How the Report Writer works
|
Report Groups
| The Report Writer works by recognizing that many reports take (more or less) the same shape.
The Report Writer calls these different report items - Report Groups. Reports are organized around report groups and the Report Writer recognizes seven types of report group (shown below). The indentation shows the importance/order of execution of the report groups.
Report Groups are defined as records in the REPORT SECTION of the DATA DIVISION. Most groups are defined once for each report, but control groups are defined for each control break item. For instance, in the example program, control footings are defined on the SalesPersonNumber, the CityCode and FINAL. FINAL is a special control group that is invoked before the normal control groups (CONTROL HEADING FINAL) and after the normal control groups (CONTROL FOOTING FINAL). An ordinary control group is defined on some control break data-item. The Report Writer monitors the contents of the data-item and when the value in the item changes a control break is automatically initiated and the CONTROL FOOTING group (if there is one) is printed.
|
Report writing made easy
| Writing report programs is time consuming. It may be difficult to get the vertical and horizontal placement of printed items correct. Many lines of code may have to be written merely to move values to their corresponding items in the print line. A line count has to be kept to control when the report prints on a new page and a page count is often required. The Report Writer makes all of this easy by;
| Let's write a Report Writer program!
|
Introduction
| Let's write a Report Writer program! We'll start by writing a simpler version of the program that produces example report shown above and then we'll add to it to create a report program with even more features than the one shown. Let's start by looking the data we have been giving to work with and at what is required in our simple report.
|
Report Specification
| Bible salespersons work in each of the cities of Ireland. Each time a salesperson sells some bibles the value of that sale is recorded in a sales file along with the salesperson number and the city code. The sales file has been validated and sorted on ascending SalesPersonNum within ascending CityCode. We want to write a program that will produce a report that prints -
|
A simple report
| The first page produced by the simple report program we are going to write is shown below. In the section that follows we will examine the program that created the report.
|
Code & Commentary
|
| Let's add some features to our Report Program
|
Adding a city and a final total
| Let's add city totals and a final total to the Report. The city total will be the sum of all the salesperson totals for the city and the final total will be the sum of all the city totals. A city total will be printed when the CityCode changes (i.e. when there is a control break on the CityCode) and the final total will be printed at the end of the report. What changes do we have to make to our program to get it to print the city and final totals?
|
Printing the city total
| If we want to print the city total we must set up a report group for it describing what we want printed. To describe when we want the group printed, we must specify that the group is a CONTROL FOOTING group and we must identify the CityCode as the control break data-item. Since the city control break is senior to the salesperson control break we must indicate this by placing CityCode before SalespersonNumber in the CONTROLS are phrase. The total for the city is automatically accumulated by means of the SUM clause. Every time a salesperson total is printed the phrase - SUM SMS - adds the total to the city total. This is why the item for printing the salesperson total was given a name - so that we could refer to it in the SUM phrase of the city total. The item that prints the city total has also been given a name. This is so that we can refer to it in the final total SUM phrase.
|
Printing a Final Total
| To print the final total we must set up a CONTROL FOOTING FINAL group. Final is a special keyword that indicates the control break that occurs when the end of the report is reached. This is the most senior control break and we indicate this by placing the word FINAL ahead of all the other control break items in the CONTROLS ARE phrase. The final total is automatically accumulated by means of the SUM CS clause. Every time a city total is printed the phrase SUM CS adds the city total to the final total. Note that the final total print item is not named because we never need to refer to it.
|
REPORT SECTION changes
|
|
Changes to the Procedure Division
| What changes do we need to make to the Procedure Division code to print the city and final totals. Why none at all!!! The Procedure Division code remains exactly the same.
| Report Writer Declaratives
|
Introduction
| The Report Writer is a wonderful tool if the structure of the required report fits in to the way the Report Writer does things. But sometimes, the structure or requirements of the report are such that the standard Report Writer alone is an insufficient tool. In these cases, DECLARATIVES may be used to extend the functionality of the Report Writer. The USE BEFORE REPORTING phrase allows code specified in the DECLARATIVES to be executed just before a report group is printed. The code in the DECLARATIVES extends the functionality of the Report Writer by performing tasks or calculations that the Report Writer can not do automatically or by selectively stopping the report group from being printed (using the SUPPRESS PRINTING command).
|
Calculating the salesperson's commission and salary
| Suppose we want to extend our report so that as well as printing the salesperson total it also prints the salesperson's commission and salary. The commission will be a percentage of the saleperson's sales and the salary will be calculated as the commission plus a fixed amount obtained from a two dimension table. Additionally to enhance our understanding of how the control break items are referenced we will show the number of the salesperson for which we have just calculated the totals, commission and salary and the salesperson number currently in the file buffer. Calculating the commission requires more than just simple addition so the Report Writer cannot handle it automatically. To calculate the commission are DECLARATIVES are required. The USE BEFORE REPORTING Salespsn-Line phrase in the DECLARATIVES allows these items to be calculated when the Salespsn-Number control footer group is about to be printed. In the example program, DECLARATIVES are used because the Report Writer cannot automatically calculate a salesperson's commission and salary. The USE BEFORE REPORTING SalesPersonGrp phrase in the DECLARATIVES allows these items to be calculated when the SalesPersonGrp control footer group is about to be printed.
|
Values of control break items
| Have another look at the Report Section above. In particular look at the salesperson control footing group. Notice anything strange? The salesperson number is being printed and the SOURCE clause is used to get its value from the SalesPersonNum in the sales record. But this is a control footing and that means that it is printed when there is a change of salesperson number. So the value in SalesPersonNum should be the number of the next salesperson and not the number of the salesperson whose totals have just been produced. Yet the report produced has the correct salesperson number printed. How can this be? In the control footing any reference to the control break item will be supplied with the previous value not the current value. To help make the relationship between control break items and the Report Section, the Declaratives and the Procedure Division we will show the number of the salesperson for which we have just calculated the totals, commission and salary and the salesperson number currently in the file buffer. In the city footing we will show the city code for the city whose total sales have just been printed and the city code currently in the file buffer.
|
The full program and the report it produces
| Changes from the simple version are shown in red.
|
Referencing control break items - summary
| When a control break item is used in a control footing or in the DECLARATIVES before printing a control footing the value supplied by the Report Writer is the previous value. Referring to the same item in the Procedure Division yields the current value.
| Copyright NoticeThese COBOL course materials are the copyright property of Michael Coughlan. All rights reserved. No part of these course materials may be reproduced in any form or by any means - graphic, electronic, mechanical, photocopying, printing, recording, taping or stored in an information storage and retrieval system - without the written permission of the author. (c) Michael Coughlan Last updated : November
1998 mailto:michael.coughlan@ul.ie | |||||||||||||||||||||||||||||||||||