텍스트 보고서 인쇄를 위한 코드 작성
이 주제에서는 샘플 텍스트 보고서 프로그램 및 핸들러를 제공합니다.
전제조건
- EGL 프로젝트
- 데이터베이스 및 해당 데이터베이스에 대한 작동하는 연결
개요
이 프로그램은 SQL 데이터베이스에 있는 모든 고객의 목록과 이러한 고객의 현재 잔고를 인쇄합니다.
이 프로그램이 실행되면서 다음 이벤트가 발생합니다.
- 보고서 생성기 프로그램이 CustomerRecord 및 textReportHandler를 기반으로 하는 변수를 작성합니다.
- 보고서 생성기가 핸들러로부터 start() 함수를 호출합니다.
- 핸들러 start() 함수가 TextReport ExternalType 파트를 기반으로 하는 변수를 작성하고 두 이벤트 onEveryRow 및 onLastRow에 대한 함수를 지정합니다. 이 함수는 그 후 myReport.startReport() 함수를 통해 보고서의 제어를 다음 보고서 엔진에 전달합니다.
- myReport.startReport() 함수는 보고서 형식을 변경하지 않으므로 보고서 엔진은 제어를 핸들러에 반환하고, 핸들러는 제어를 보고서 생성기에 반환합니다.
- 보고서 생성기가 EGL open 문을 사용하여 결과 세트를 작성합니다. 이 결과 세트는 현재 데이터베이스에 있는 모든 고객을 포함합니다.
- 보고서 생성기가 결과 세트 전체에 걸쳐 루프를 수행합니다. 생성기는 각 고객에 대해 output() 함수에 고객 이름 및 잔고를 전송합니다.
- 핸들러가 핸들러에 있는 모든 함수에서 액세스할 수 있는 레코드에 고객 이름 및 잔고를 저장하고 현재 총 잔고를 업데이트합니다.
- 그 후 핸들러가 outputToReport() 함수를 사용하여 보고서 엔진에 제어를 반환합니다.
- 보고서 엔진은 몇 가지 하우스키핑 태스크를 수행한 후 핸들러에 있는 onEveryRow() 함수를 호출합니다.
- 핸들러의 onEveryRow() 함수는 현재 인쇄 위치에 있는 고객 이름을 전송하고, 페이지의 40번째 열로 이동하여 고객 잔고를 인쇄한 후 캐리지 리턴 및 용지 넘김을 보고서 파일에 전송합니다.
- 제어가 다시 보고서 생성기에 전송되며, 생성기는 처리할 다음 고객 레코드를 검색합니다. 생성기는 모든 고객을 처리한 후 핸들러의 finish() 함수를 호출합니다.
- 핸들러의 finish() 함수는 finishReport() 함수를 통해 제어를 보고서 엔진에 전달합니다.
- 보고서 엔진이 마지막 행 이벤트에 대한 함수를 찾고 핸들러로부터 onLastRow()를 호출합니다.
- 핸들러의 onLastRow() 함수가 핸들러가 업데이트하고 있던 현재 총계를 인쇄합니다.
- 보고서 엔진이 보고서 파일을 닫으며 제어가 핸들러에 다시 전달되고, 핸들러는 최종적으로 실행 단위를 종료하는 생성기에 제어를 전달합니다.
고객 레코드
이 프로그램은 다음 레코드를 사용하여 고객 SQL 데이터베이스에 액세스합니다.
record CustomerRecord type SQLRecord
{tableNames = [["ADMINISTRATOR.CUSTOMER", "L1"]],
keyItems = [customerNumber]}
customerNumber STRING {column="C_NUMBER", maxLen=6};
customerName STRING {column="C_NAME", isSQLNullable=yes, maxLen=25};
customerAddr1 STRING {column="C_ADDR1", isSQLNullable=yes, maxLen=25};
customerAddr2 STRING {column="C_ADDR2", isSQLNullable=yes, maxLen=25};
customerAddr3 STRING {column="C_ADDR3", isSQLNullable=yes, maxLen=25};
customerBalance MONEY {column="C_BALANCE", isSQLNullable=yes};
end
보고서 생성기
실행은 보고서 생성기에서 시작됩니다.
package com.companyb.customer;
program reportGenerator type BasicProgram
myCustomer CustomerRecord;
myHandler textReportHandler{};
function main()
myHandler.start(); // passes control to handler
// get customer list from database
open myResults scroll for myCustomer;
forEach (from myResults) // loop through results
myHandler.output(myCustomer.customerName, myCustomer.customerBalance);
end // forEach
myHandler.finish(); // close report
endend
일반 핸들러
보고서 생성기와 보고서 엔진은 둘 다 핸들러로부터 함수를 호출합니다.
package com.companyb.customer;
record reportRecord type BasicRecord
customerName STRING;
customerBalance MONEY;
end
handler textReportHandler type BasicHandler
myReport TextReport{}; // creates instance
currentReportRecord reportRecord;
runningTotal MONEY = 0;
function start()
myReport.onEveryRowListener = onEveryRow;
myReport.onLastRowListener = onLastRow;
// accept defaults on everything but destination file
myReport.startReport("D:/temp/customerReport.txt",
null,null,null,null,null,null);
end
function output(cName STRING in, cBal MONEY in)
// this information comes from the forEach loop in the main program
currentReportRecord.customerName = cName;
currentReportRecord.customerBalance = cBal;
runningTotal = runningTotal + cBal;
// pass control to the report engine
// onEveryRow is called
myReport.outputToReport();
end
function finish()
// pass control to the report engine again
// onLastRow is called
myReport.finishReport();
end
function onEveryRow(myEvent TextReportEvent in)
myReport.printText(currentReportRecord.customerName);
myReport.column(40);
myReport.printText(currentReportRecord.customerBalance);
myReport.println();
end
function onLastRow(myEvent TextReportEvent in)
myReport.println(); // skip a line
myReport.printText("All customers:");
myReport.column(40);
myReport.printText(runningTotal);
endend