Tutorial 23 - Report
Tutorial 23 - Report
23 - Report
Table of contents
HOW TO READ THIS DOCUMENT ....................................................................... 3
OBJECTIVE ................................................................................................................. 4
INTRODUCTION ........................................................................................................ 5
1. REPORT DOCUMENT ...................................................................................... 6
1.1. CRITERIA DEFINITION (SHARED)...................................................................... 6
1.2. VISUAL PAGE FOR CRITERIA SELECTION .......................................................... 7
1.3. SERVER IMPLEMENTATION ............................................................................ 11
1.3.1. Server binder ........................................................................................... 11
1.3.2. Report generation server rule ................................................................. 13
1.3.3. Report connector ..................................................................................... 15
2. JRXML REPORTS............................................................................................ 17
2.1. SIMPLE LIST ................................................................................................... 17
2.2. DETAIL .......................................................................................................... 18
2.3. FULL LIST ...................................................................................................... 19
3. REPORT FROM THE ORIGINAL DOCUMENT ........................................ 22
3.1. REPORT GENERATION SERVER RULE .............................................................. 22
3.2. USING AN OPERATION ................................................................................... 23
3.3. USING BUTTON .............................................................................................. 25
Figures
FIGURE1 - THIS IS AN EXAMPLE OF FIGURE........................................................................... 3
FIGURE2 - DATA OBJECT MODEL OF THE SEARCH CRITERIA ................................................ 6
FIGURE3 - REPORT CRITERIA SELECTION VISUAL PAGE ........................................................ 8
FIGURE4 - LIST SIMPLE REPORT .......................................................................................... 17
FIGURE5 - DETAILED REPORT ............................................................................................. 18
FIGURE6 - ONE PAGE OF THE LIST FULL REPORT ................................................................. 19
FIGURE7 - THE FINAL PAGE OF THE REPORT ....................................................................... 20
Source code
SOURCE 1 - THIS IS AN EXAMPLE OF SOURCE CODE ................................................................ 3
SOURCE 2 - D_REPORT, DATA OBJECT MODEL DEFINITION .................................................... 7
SOURCE 3 - DC_REPORT CLIENT DOCUMENT ........................................................................ 8
SOURCE 4 - VP_REPORT VISUAL PAGE................................................................................... 8
SOURCE 5 - R_REPORT, ASKS THE SERVER TO GENERATE THE REPORT ................................ 10
SOURCE 6 - S_REPORT SERVER CLASS ................................................................................. 12
SOURCE 7 - SR_DOREPORT REPORT GENERATION SERVER RULE ......................................... 13
SOURCE 8 - RC_REPORT REPORT CONNECTOR ..................................................................... 15
SOURCE 9 - LISTFULLSCRIPTLET, SCRIPTLET FOR SPECIAL PROCESS .................................... 20
SOURCE 10 - SR_REPORT, SERVER RULE FOR REPORT GENERATION ...................................... 22
SOURCE 11 - S_TUTORIAL23, ADD THE SR_REPORT RULE .................................................... 23
SOURCE 12 - S_TUTORIAL23, ADD AN OPERATION FOR A DIRECT PRINTING IN THE FINDER
RESULT PAGE ..................................................................................................................... 23
SOURCE 13 - DC_TUTORIAL23, CLIENT DOCUMENT MODIFICATION FOR THE REPORT
TRIGGERING....................................................................................................................... 24
SOURCE 14 - R_REPORT, RULE MODIFICATION ...................................................................... 24
SOURCE 15 - MODIFICATION OF THE SERVER CLASS, S_TUTORIAL23 .................................... 25
SOURCE 16 - DC_TUTORIAL23, ADD RULE FOR THE VISUAL EVENT MANAGEMENT ............... 25
Keyword Definition
Source 1 -
This is an example
of source code Source code.
Objective
In this document you will learn
How to create a report
Required element
Knowledge on rules.
Previous tutorial.
The objective of this tutorial is to explain how to create a report for a smart printing. The
definition of a report is done in 3 steps:
• Define the criteria for the creation of the report
• Create the visual document for the criteria selection
• Define the report
Introduction
A report is creating depending on user criteria. These criteria define which document should
be used for the report. The criteria are filled in using a visual page.
A report will be created in the same way as a classical document. However, this kind of
document has its own specificities. For example, instead of a table connector, a report
connector is needed.
1. Report document
The objective is to report persons corresponding to the following criteria:
• The last name: report persons with a specific name (can be the entire name or a part
of the name).
• The city: report all the persons living in a specific city.
• The birth date of his children: report all the persons who have a child born between
2 dates.
• The status of the document.
CRITERIA
CHD
BDA
FST
LST
CIT
LNA
STA
INSTANCE_ID
TYP
GCF
STA
/**
* Define the Data model
*/
public void define_DataModel() {
// Normal data
DataSet criteria = seg(CRITERIA);
criteria.add(TYP);
/**
* Define client business rules
*/
public void define_ClientBusinessRule() {
addRule(new R_Init_Document(), DOCUMENT_INIT);
}
/**
* Define reference tables
*/
public void define_ReferenceModel() {
define_Ref(JOB_TAB); // Reference Models - Country
}
}
/**
* Constructor - initializes the visual components of the page. This is the only page in our
main form.
*/
public VP_Report() {
super();
initVisualPage();
initVisualControls();
}
/**
* Initialize page properties
*/
public void initVisualPage() {
setSize(550, 475);
setBackgroundImage(so.swing.IconResourcer.getIcon("img/aw2t.gif"),KPanel.STRETCH);
setDefaultKTitleStripeFont(new Font("Arial Black", Font.PLAIN, 15));
setDefaultJLabelFont(new Font("Tahoma", Font.PLAIN, 14));
setDefaultJLabelColor(new Color(0, 0, 0, 255), new Color(204, 204, 255, 65), false);
setDefaultJLabelBorder(KVisualPage.borders[0]);
setDefaultJTextFieldFont(new Font("Dialog", Font.BOLD, 12));
setDefaultJTextFieldColor(new Color(0, 0, 51, 255), new Color(235, 233, 237, 255), true);
setDefaultJTextFieldBorder(KVisualPage.borders[6]);
setDefaultFocusedJTextFieldColor(new Color(0, 0, 51, 255),
new Color(222, 222, 253, 255), true);
setDefaultFocusedJTextFieldBorder(KVisualPage.borders[6]);
}
/**
* Initialize visual controls of the page
*
*/
public void initVisualControls() {
/**
* Initialization of facets
*/
public void initFacets() {
// Children criteria
DataSet child = criteria.ds(C_Tutorial23.CHD);
addFacetDate(fld_start_child_birthdate, child.ds(C_Tutorial23.BDA).de(FST), "Date");
addFacetDate(fld_end_child_birthdate, child.ds(C_Tutorial23.BDA).de(LST), "Date");
The R_Report rule is a client rule used to ask server to generate the report corresponding to
the user demand.
First, this rule creates a dialog box to propose the user tow choices:
• print the report
• get the report as a pdf file in his mailbox
Then, the rule fires the middle event given in parameter of the constructor with the document
containing:
• the type of output for the report (selected in the dialog box before)
• the criteria filled in the visual page
Finally, in the case of a report print, the rule gets the result of the server rule and uses the
JasperPrintManager to print the report.
/**
* Application of the rule
*/
protected void apply(KernelEvent kernelevent) {
if (kernelevent instanceof PrepareDocumentToSendEvent &&
((PrepareDocumentToSendEvent) kernelevent).getOperation().getID() ==
C_Tutorial23.OI_REPORT
|| kernelevent instanceof KernelEvent
&& (!(kernelevent instanceof PrepareDocumentToSendEvent))) {
Document doc = (Document) kernelevent.getData();
if (dlg.getOption() == KEndOfTransactionDialog.YES) {
// Test the option selected
if (chkPrintReport.isSelected()) {
// Print
doc.ds(CRITERIA).de(TYP).tryToSetContent(PRINT_REPORT);
TransactionEvent te = doc.applyMiddleEvent(middleEvent, doc);
if (te.getException() != null || te.getResult() == null ||
te.getResult().getContent() == null) {
KOptionPane.showMessageDialog(DesktopMain.sharedInstance(),
lng("Could not get the report"),
lng("Server Error"),
KOptionPane.ERROR_MESSAGE, null);
} else {
try {
JasperPrint jp = (JasperPrint) te.getResult().getContent();
if(jp.getPages().isEmpty()){
KOptionPane.showMessageDialog(DesktopMain.sharedInstance(),
lng("The report is empty"),
lng("Report"),
KOptionPane.INFORMATION_MESSAGE, null);
}else{
JasperPrintManager.printReport(jp, true);
}
} catch (JRException jre) {
System.out.println(lng("Could not print the report, ") + jre.toString());
}
}
} else {
// Mail
doc.ds(CRITERIA).de(TYP).tryToSetContent(MAIL_REPORT);
doc.applyMiddleEvent(middleEvent, doc);
}
}
}
}
}
/**
* Define server rules
*/
public void defineBinder() {
addServerRule(new SR_SendAllStatuses(this, S_Tutorial23.class), LOAD_DPP);
addServerRule(new ReferenceServerRule(this, new int[] {}, C_Tutorial23.JOB_TAB));
addServerRule(new SR_DoReport(this, S_Tutorial23.class, "detail.jrxml"),
DO_DETAIL_REPORT);
addServerRule(new SR_DoReport(this, S_Tutorial23.class, "list_simple.jrxml"),
DO_LIST_SIMPLE_REPORT);
addServerRule(new SR_DoReport(this, S_Tutorial23.class, "list_full.jrxml"),
DO_LIST_FULL_REPORT);
}
/**
* Database initialization
*/
public void initializeDatabase() {}
/**
* Add reference model events to the abstractOperation passed in parameter
* @param abstractOperation
*/
protected void addReferenceModelEvents(AbstractOperation abstractOperation) {
abstractOperation.addEventID(JOB_LOAD, C_Tutorial23.JOB_TAB);
// the start event which activates the LOAD DPP rule:
abstractOperation.addEventID(LOAD_DPP);
abstractOperation.setStartEvent(LOAD_DPP);
}
/**
* Add middle events to the abstractOperation passed in parameter
* Events will be allowed for this operation
* @param abstractOperation
*/
protected void addMiddleEvent(AbstractOperation abstractOperation) {
abstractOperation.addEventID(DO_DETAIL_REPORT);
abstractOperation.addEventID(DO_LIST_FULL_REPORT);
abstractOperation.addEventID(DO_LIST_SIMPLE_REPORT);
}
/**
* Define the universe of operations
*/
protected Operations createValidOperations() {
Operations ops = super.createValidOperations();
// Report operation
Operation op_Report = OperationFactory.makeNilOperation(OI_REPORT,
OP_REPORT,
lng("Report"),
"img/Btn_Print_Normal.gif");
op_Report.addVisibleEventID(ACT_SIMPLE_REPORT, "Detail",
lng("Detail"), "img/Btn_Print_Normal.gif");
op_Report.addVisibleEventID(ACT_LIST_SIMPLE_REPORT, "List simple",
lng("List simple"), "img/Btn_Print_Normal.gif");
return ops;
}
}
Next step is to set the parameters. This is done with the setSQLParameter() method of the
GCFReportRunner object. This will construct the SQL query, corresponding to the desired
documents to report.
When all the parameters are set, the query must be executed with the run() method of the
GCFReportRunner object. The result is a ReportExporter object. This object will be used to
get the report in a specific format:
• The method getJasperPrint() is the default way to get the report.
• The method exportAsPDF() will return the report as a pdf file.
/**
Source 7 - * Server rule to generate the report depending on the parameters selected
SR_DoReport * @author UNCTAD
report generation * @version
*/
server rule
public class SR_DoReport extends ServerRule implements C_Report {
/**
* Default constructor
* @param s
* @param clazz
* @param jrxml
*/
public SR_DoReport(ServerBinder s, Class clazz, String jrxml) {
super(s);
this.cla = clazz;
this.jrxml = jrxml;
/**
* Application of the rule : generate the report
*/
protected void apply(KernelEvent e) {
ServerBinder binder = Server.getBinderInstance(cla)[0];
rc = new RC_Report(binder);
GCFReportRunner runner = new GCFReportRunner(rc, jrxml) {
// Basic criteria
DataSet finalSet = e.getDataSet();
DataSet criteria = finalSet.ds(DocumentInterface.NORMAL_ID).ds(CRITERIA);
String city = criteria.de(C_Tutorial23.CIT).getContentString();
String name = criteria.de(C_Tutorial23.LNA).getContentString();
Object instance_id = criteria.de(C_Tutorial23.INSTANCE_ID).getContent();
int status = criteria.de(STA).getInt(0);
// parameters
runner.setImageParameter("logo", "img/un.jpg");
}
}
addSQLField(INSTANCE_ID, "INSTANCE_ID");
addSQLField(job.de(JBD), "JOB_JBD");
setImageField("IDE_PIC");
2. JRXML reports
A “jrxml” file defines how the report will looks like. It is a kind of xml file. Different kinds
of presentation can be done. Some possibilities are presented here. The way to create these
files will not be presented here because it is not relative to the use of the SO Class. For
explications, see the “jasper” documentation. Tools are generally used to simplify the creation
(iReport …).
2.2. Detail
For this one, each document found in the report query is detailed on a page. All the
information concerning a person are presented.
}
/** End EVENT_BEFORE_COLUMN_INIT */
}
/** end EVENT_BEFORE_DETAIL_EVAL */
}
/** End EVENT_BEFORE_PAGE_INIT */
}
/** End EVENT_BEFORE_REPORT_INIT */
The source codes presented below are not all defined in the same package.
Two packages are used:
• the one of the original document:
“package un.asytutorialAdv.tutorial23;”
• the one of the report document:
“package un.asytutorialAdv.tutorial23.report;”
package un.asytutorialAdv.tutorial23.server;
Source 10 -
SR_Report, server public class SR_Report extends ServerRule implements C_Report {
rule for report
/**
generation
* Constructor
* @param s
*/
public SR_Report(ServerBinder s) {
super(s);
}
/**
* Application of the rule
*/
protected void apply(KernelEvent kernelevent) {
try {
GCFServerEvent gcf = (GCFServerEvent)kernelevent;
D_Report report = new D_Report();
// Define the DOM
report.define();
package un.asytutorialAdv.tutorial23.server;
Source 11 -
S_Tutorial23, add public class S_Tutorial23 extends GCFServerBinder implements C_Tutorial23 {
the SR_Report rule
…
/**
* Define server rule
*/
public void defineBinder() {
…
// Server rule for report generation
addServerRule(new SR_Report(this), DO_REPORT);
…
}
…
}
package un.asytutorialAdv.tutorial23.server;
Source 12 -
S_Tutorial23, add public class S_Tutorial23 extends GCFServerBinder implements C_Tutorial23 {
an operation for a
direct printing in …
the finder result /**
page * Create the operations universe
*/
protected Operations createValidOperations() {
…
Operation op_Report = OperationFactory.makeNilOperation(OI_REPORT,
OP_REPORT, lng("Report"), "img/Btn_Print_Normal.gif");
op_Report.setAutoFinish(true);
op_Report.setInLibrary(false);
addMiddleEvents(op_Report);
addReferenceModelEvents(op_Report);
ops.add(op_Report);
…
}
…
/**
* Add middle events, allow events for an operation
*/
private void addMiddleEvents(AbstractOperation abstractOperation) {
…
abstractOperation.addEventID(DO_REPORT);
}
}
It is now necessary to make the server rule SR_Report launched automatically on the Report
operation. To do so, the R_Report rule (of the un.asytutorialAdv.tutorial23.report.client.rules
package) is added on the client document with the FINAL_ACTION_REQUESTED event.
package un.asytutorialAdv.tutorial23.client;
Source 13 -
DC_Tutorial23, public class DC_Tutorial23 extends ClientDocument implements C_Tutorial23,
client document KernelEventConstants {
void initRules(D_Tutorial23 doc) {
modification for the
…
report triggering // Report print
doc.addRule(new R_Report(DO_REPORT), FINAL_ACTION_REQUESTED);
…
}
…
}
The R_Report rule must be modified to take into account events coming from the tutorial23
binder.
package un.asytutorialAdv.tutorial23.report.client.rules;
Source 14 -
R_Report, rule public class R_Report extends Rule implements C_Report {
modification
…
}
}
}