0% found this document useful (0 votes)
1 views

Tutorial 23 - Report

Uploaded by

homadi.99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Tutorial 23 - Report

Uploaded by

homadi.99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

SO CLASS MODULAR VERSION

23 - Report

Subject Advanced tutorial


Version ADK 239
Issue date 06/07/2005
Beneficiaries Developers
File Tutorial 23 - Report.doc
Status Draft
Checked by ASYCUDA World - Lyon competence center
Copyrights United Nations Conference on Trade and Development
Developer guide
Version ADK 239
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

United Nations Conference on Trade and Development Page 1 of 26


Developer guide
Version ADK 239
23 - Report

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

United Nations Conference on Trade and Development Page 2 of 26


Developer guide
Version ADK 239
23 - Report

How to read this document


This symbol indicates advice and recommendations. Information on best
practices and recommended procedures related to the current topic is
contained here.

Keyword Definition

This symbol indicates a warning. Information on common pitfalls or dangers


associated with the current topic is contained here.

This symbol indicates an example to further illustrate the current topic.

Source 1 -
This is an example
of source code Source code.

Phase 1 Phase 2 Phase 3

Figure1 - This is an example of figure

United Nations Conference on Trade and Development Page 3 of 26


Developer guide
Version ADK 239
23 - Report

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

Three different reports will be presented here:


• A detail report giving all the information concerning the documents.
• A simple list of the documents.
• A detail list of the documents, with statistics.

Different ways to use reports will also be presented.

United Nations Conference on Trade and Development Page 4 of 26


Developer guide
Version ADK 239
23 - 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.

United Nations Conference on Trade and Development Page 5 of 26


Developer guide
Version ADK 239
23 - Report

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.

All the fields can be empty.

1.1. Criteria definition (shared)


The definition of the search criteria is done by defining the Data Object Model of the
document.

CRITERIA

CHD

BDA

FST

LST

CIT

LNA

STA

INSTANCE_ID

TYP

GCF

STA

Figure2 - Data Object Model of the search criteria

United Nations Conference on Trade and Development Page 6 of 26


Developer guide
Version ADK 239
23 - Report

public class D_Report extends KDocument implements C_Report {


Source 2 -
D_Report, Data /**
object model * Default constructor
*/
definition
public D_Report() {
super("Report");
}

/**
* Define the Data model
*/
public void define_DataModel() {
// Normal data
DataSet criteria = seg(CRITERIA);

DataSet criteriaChd = criteria.seg(CHD);


DataSet criteriaChdBda = criteriaChd.seg(BDA);
criteriaChdBda.add(FST);
criteriaChdBda.add(LST);
criteria.add(CIT);
criteria.add(LNA);
criteria.add(STA);
criteria.add(INSTANCE_ID);

criteria.add(TYP);

DataSet gcf = seg(GCF);


gcf.addTmp(STA);
}

/**
* 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
}
}

1.2. Visual page for criteria selection


The report visual page is used to fill the information allowing the user to create the report
on the desired documents.
A field for each criterion is needed.
More, three buttons are needed to launch the report generation, one per type of report.
These buttons are declared in the server side. The action associated to each button is
defined in the client document class by adding the rule R_Report for each event
corresponding to a button. The constructor of this rule needs in parameter the middle event
corresponding to the type of report desired. This middle event will be used to fire the
appropriate server rule.

United Nations Conference on Trade and Development Page 7 of 26


Developer guide
Version ADK 239
23 - Report

Figure3 - Report criteria selection visual page

public class DC_Report extends ClientDocument implements C_Report{


Source 3 -
DC_Report client public void initRules(D_Report doc) {
document doc.addRule(new R_Report(DO_DETAIL_REPORT), ACT_SIMPLE_REPORT);
doc.addRule(new R_Report(DO_LIST_FULL_REPORT), ACT_LIST_FULL_REPORT);
doc.addRule(new R_Report(DO_LIST_SIMPLE_REPORT),
ACT_LIST_SIMPLE_REPORT); }
}

public class VP_Report extends ElfVisualPage implements C_Report {


Source 4 -
VP_Report visual //Name
page transient private ElfField fld_name = elfFieldPool.getElfField();
// City
transient private ElfField fld_cit = elfFieldPool.getElfField();
// Start child birthdate
transient private ElfField fld_start_child_birthdate = elfFieldPool.getElfField();
// Status
transient private ElfField fld_status = elfFieldPool.getElfField();
// End child birthdate
transient private ElfField fld_end_child_birthdate = elfFieldPool.getElfField();

/**
* Constructor - initializes the visual components of the page. This is the only page in our
main form.
*/
public VP_Report() {
super();
initVisualPage();

United Nations Conference on Trade and Development Page 8 of 26


Developer guide
Version ADK 239
23 - Report

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() {

// Labels and Titles


addTitleStripe( 0, 0, 500, 24, lng("Main contact criteria"));
add( 40, 40, 175, 24, lng("Last name starts with"));
add( 40, 90, 100, 24, lng("City"));
addTitleStripe( 0, 126, 500, 24, lng("Children criteria"));
add( 40, 184, 175, 24, lng("Child birthdate beetween"));
addTitleStripe( 0, 238, 500, 24, lng("Document criteria"));
add( 40, 290, 100, 24, lng("Status"));

// Editable Visual controls


add( 210, 40, 150, 20, fld_name, lng("Last name starts with"));
add( 210, 90, 150, 20, fld_cit, lng("City"));
add( 210, 184, 100, 20, fld_start_child_birthdate, lng("Start child birthdate "));
add( 333, 184, 100, 20, fld_end_child_birthdate, lng("End child birthdate "));
add( 210, 290, 150, 20, fld_status, lng("Status"));

/**
* Initialization of facets
*/
public void initFacets() {

Document doc = getDocument();


DataSet criteria = ds(CRITERIA);

addFacetText(fld_name, criteria.de(C_Tutorial23.LNA), "X35");


addFacetText(fld_cit, criteria.de(C_Tutorial23.CIT), "X35");

// 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");

String[] choices = (String[]) doc.ds(GCF).de(STA).getContent();


String[] choices1 = new String[choices.length + 1];
choices1[0] = "";
System.arraycopy(choices, 0, choices1, 1, choices.length);

addFacetChoice(fld_status, criteria.de(STA), choices1);


}
public void initRules() { }
}

United Nations Conference on Trade and Development Page 9 of 26


Developer guide
Version ADK 239
23 - Report

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.

Using the JasperPrintManager involve to have the jasperreports libraries


installed on the client side (\client\web\lib\ext)

public class R_Report extends Rule implements C_Report {


Source 5 -
R_Report, asks the private int middleEvent;
server to generate
/**
the report
* Default constructor
*
* @param middleEvent
*/
public R_Report(int middleEvent) {
this.middleEvent = middleEvent;
}

/**
* 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();

// Option for the report destination


JRadioButton chkPrintReport=new JRadioButton(lng("Print the report"), null, true);
JRadioButton chkMailReport=new JRadioButton(lng("Mail the report in pdf format"));
ButtonGroup group = new ButtonGroup();
group.add(chkPrintReport);
group.add(chkMailReport);

// Dialog for the option selection


KEndOfTransactionDialog dlg =
new KEndOfTransactionDialog(DesktopMain.sharedInstance(),
lng("Report destination"),
KEndOfTransactionDialog.CANCEL
| KEndOfTransactionDialog.YES);
dlg.setTitle(lng("Report"));
dlg.addSelectionComponent(chkPrintReport);
dlg.addSelectionComponent(chkMailReport);
// Show the option selection
dlg.show();

United Nations Conference on Trade and Development Page 10 of 26


Developer guide
Version ADK 239
23 - Report

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);
}
}
}
}
}

1.3. Server implementation


On the server side, three main things are needed:
 The server binder class: as for a classical document, it is necessary to
define server rules and the universe of operations.
 The report connector class: this class is very similar to a table connector.
It is used to define the connections between the report fields and the
database fields.
 The server rule for the report generation.

1.3.1. Server binder


The report generation server rule, SR_DoReport, must be added to the server binder.
This rule is added three times: one for each type of report with the corresponding
event.
The universe of operations has only one operation: report. As it will not create or
modify any document, it must be a Nil operation. The visible events corresponding to

United Nations Conference on Trade and Development Page 11 of 26


Developer guide
Version ADK 239
23 - Report

the buttons in the visual page must be added (ACT_SIMPLE_REPORT,


ACT_LIST_SIMPLE_REPORT, ACT_LIST_FULL _REPORT).
More, the middle events used to launch the report generation server rule must be
added to the operation in order to be allowed.

public class S_Report extends ServerBinder implements C_Report {


Source 6 -
S_Report server
class /**
* Default constructor
*/
public S_Report() {
super();
}

/**
* 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");

United Nations Conference on Trade and Development Page 12 of 26


Developer guide
Version ADK 239
23 - Report

op_Report.addVisibleEventID(ACT_LIST_FULL_REPORT, "List full",


lng("List full"), "img/Btn_Print_Normal.gif");
op_Report.setKnownIED(false);
op_Report.setInLibrary(true);
addReferenceModelEvents(op_Report);
addMiddleEvent(op_Report);
ops.add(op_Report);

return ops;
}
}

1.3.2. Report generation server rule


This rule is used to generate the report depending on the parameters chosen by the user. The
first thing to do is to declare a new report connector on the desired binder. In this case, the
report connector must be on the tutorial23 binder.
Then, a GCRReportRunner object must be declared. This object is the one in charge of the
report generation. It needs the report connector and the jrxml file name in parameter.

Jrxml files describe the structure of the report.

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 {

private ReportConnector rc;


private Class cla;
private String jrxml;

/**
* 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;

United Nations Conference on Trade and Development Page 13 of 26


Developer guide
Version ADK 239
23 - Report

/**
* 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) {

protected Object getFieldValue(String name, Object object) {


return object;
}
};
runner.setDebugEnabled(true);

// 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);

DataSet child = criteria.ds(C_Tutorial23.CHD);


Date chd_bda_first = (Date) child.ds(C_Tutorial23.BDA).de(FST).getContent();
Date chd_bda_last = (Date) child.ds(C_Tutorial23.BDA).de(LST).getContent();

// A specific document has been selected


if (instance_id != null) {
runner.setSQLParameter(rc.de(C_Tutorial23.INSTANCE_ID), instance_id);
}

// Searching for person in the specified city


if (city != null && city.length() > 0) {
runner.setSQLParameter(rc.ds(C_Tutorial23.COO).de(C_Tutorial23.CIT), city);
}
// Searching for person with the specified name
if (name != null && name.length() > 0) {
runner.setSQLParameter(rc.ds(C_Tutorial23.IDE).de(C_Tutorial23.LNA),
"%" + name + "%", "(@ like ?)");
}

// Searching for documents in a specific status


if (status > 0) {
runner.addAllowedCurrentStatus(
((GCFServerBinder) binder).getDPP().getAllStatuses()[status - 1]);
}

// Searching for person with child born between 2 dates


if (chd_bda_first != null) {
runner.setSQLParameter(rc.ds(C_Tutorial23.CHD).de(C_Tutorial23.CBD),
new Timestamp(chd_bda_first.getTime()),"(@ >= ?)");
}
if (chd_bda_last != null) {
runner.setSQLParameter(rc.ds(C_Tutorial23.CHD).de(C_Tutorial23.CBD),
new Timestamp(chd_bda_last.getTime()),"(@ <= ?)");
}

// parameters
runner.setImageParameter("logo", "img/un.jpg");

// running and printing


ReportExporter ex = runner.run();

// The usage of the report


Integer type = ((DataSet) e.getSource()).ds(CRITERIA).de(TYP).getContentInteger() ;
if ( type != null && type.intValue() == PRINT_REPORT) {
// Print the report
ServerEvent se = (ServerEvent)e;
se.newDestination(REP);
se.getDestination().tryToSetContent(ex.getJasperPrint());
} else {
// Mail the report
String title = "Tutorial23";

United Nations Conference on Trade and Development Page 14 of 26


Developer guide
Version ADK 239
23 - Report

String userName = null;


if(e instanceof GCFServerEvent){
//Rule launched from S_Report
userName = ((GCFServerEvent) e).getUserEnvironment().getLoginName();
}else{
//Rule launched from S_Tutorial23
userName = criteria.de(USR).getContentString();
}
InnerMail mail = new InnerMail();
mail.setRecipients(InnerMail.TO, new String[] { userName });
mail.setSubject(lng("Report received: ") + title);
if (!ex.getJasperPrint().getPages().isEmpty()) {
byte[] b = ex.exportAsPDF_forMail();
mail.setText(IntlFormat.createMessage("TutorialAdv",
"Please find your requested report ( {0} ) in the attachment of this e-mail.",
new Object[] { title }));
AttachedFile af = new AttachedFile("soplugin/pdf", "tutorial23.pdf", b);
mail.attach(af.getObject(), af.getMime(), af.getFileName());
} else {
mail.setText(IntlFormat.createMessage("TutorialAdv",
"No record found in the requested report."));
}
MailBoxServerBinder.sendMailFromMailBot(mail);
}

}
}

1.3.3. Report connector


The report connector is used to make the link between report fields and database
fields. The link is done with the addSQLField method. It takes two parameters:
 The data element of the document
 The database column name

public class RC_Report extends ReportConnector implements C_Tutorial23 {


Source 8 -
RC_Report report /**
connector * Define the connections between report fields and database fields
*/
public RC_Report(ServerBinder binder) {
super(binder);

addSQLField(INSTANCE_ID, "INSTANCE_ID");

ServerBinder.DataSegment ide = ds(IDE);


addSQLField(ide.de(FNA), "IDE_FNA");
addSQLField(ide.de(LNA), "IDE_LNA");
addSQLField(ide.de(MNA), "IDE_MNA");
addSQLField(ide.de(BDA), "IDE_BDA");
addSQLField(ide.de(PIC), "IDE_PIC");
addSQLField(ide.de(SEX), "IDE_SEX");
addSQLField(ide.de(FLG), "IDE_FLG");

ServerBinder.DataSegment coo = ds(COO);


addSQLField(coo.de(ADR), "COO_ADR");
addSQLField(coo.de(CPO), "COO_CPO");
addSQLField(coo.de(CIT), "COO_CIT");
addSQLField(coo.de(TEL), "COO_TEL");

ServerBinder.DataSegment usr = ds(USR);


addSQLField(usr.de(LOG), "USR_LOG");
addSQLField(usr.de(PWD), "USR_PWD");

ServerBinder.DataSegment job = ds(JOB);


addSQLField(job.de(JBC), "JOB_JBC");

United Nations Conference on Trade and Development Page 15 of 26


Developer guide
Version ADK 239
23 - Report

addSQLField(job.de(JBD), "JOB_JBD");

ServerBinder.DataSegment cjt = ds(CJT);


addSQLField(cjt.de(INSTANCE_ID), "CJT_INSTANCE_ID");

setImageField("IDE_PIC");

ServerBinder.DataSegment chd = ds(CHD);


addSQLField(chd.de(KNumberedSubDocument.RNK), "RNK"); // Child rank
addSQLField(chd.de(CFN), "CHD_NAM"); // Child name
addSQLField(chd.de(CBD), "CHD_BDA"); // Child birth date
}
}

United Nations Conference on Trade and Development Page 16 of 26


Developer guide
Version ADK 239
23 - Report

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.1. Simple list


In this example, all the documents matching the user’s report criteria are listed in a table.
Each line represents a document.

Figure4 - List simple report

United Nations Conference on Trade and Development Page 17 of 26


Developer guide
Version ADK 239
23 - Report

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.

Figure5 - Detailed report

United Nations Conference on Trade and Development Page 18 of 26


Developer guide
Version ADK 239
23 - Report

2.3. Full list


This example presents a new concept: the Scriptlet, used to do some process on data during
the creation of the report.
At the end of the report, statistics can be thus done.

Figure6 - One page of the list full report

United Nations Conference on Trade and Development Page 19 of 26


Developer guide
Version ADK 239
23 - Report

Figure7 - The final page of the report

public class ListFullScriptlet extends it.businesslogic.ireport.IReportScriptlet {


Source 9 -
ListFullScriptlet, private int count = 0;
scriptlet for special
/** Creates a new instance of JRIreportDefaultScriptlet */
process
public ListFullScriptlet() {

/** Begin EVENT_AFTER_COLUMN_INIT */


public void afterColumnInit() throws JRScriptletException {
super.beforeColumnInit();
}
/** End EVENT_AFTER_COLUMN_INIT */

/** Begin EVENT_AFTER_DETAIL_EVAL */


public void afterDetailEval() throws JRScriptletException {
super.afterDetailEval();
}
/** End EVENT_AFTER_DETAIL_EVAL */

/** Begin EVENT_AFTER_GROUP_INIT */


public void afterGroupInit(String groupName) throws JRScriptletException {
super.afterGroupInit(groupName);
}
/** End EVENT_AFTER_GROUP_INIT */

/** Begin EVENT_AFTER_PAGE_INIT */


public void afterPageInit() throws JRScriptletException {
super.afterPageInit();
}
/** End EVENT_AFTER_PAGE_INIT */

/** Begin EVENT_AFTER_REPORT_INIT */


public void afterReportInit() throws JRScriptletException {
}
/** End EVENT_AFTER_REPORT_INIT */

/** Begin EVENT_BEFORE_COLUMN_INIT */


public void beforeColumnInit() throws JRScriptletException {

United Nations Conference on Trade and Development Page 20 of 26


Developer guide
Version ADK 239
23 - Report

}
/** End EVENT_BEFORE_COLUMN_INIT */

/** Begin EVENT_BEFORE_DETAIL_EVAL */


public void beforeDetailEval() throws JRScriptletException {

}
/** end EVENT_BEFORE_DETAIL_EVAL */

/** Begin EVENT_BEFORE_GROUP_INIT */


public void beforeGroupInit(String groupName) throws JRScriptletException {
if (groupName != null && groupName.equals("Person")) {
setVariableValue("pc", new Integer(++count));
}
}
/** End EVENT_BEFORE_GROUP_INIT */

/** Begin EVENT_BEFORE_PAGE_INIT */


public void beforePageInit() throws JRScriptletException {

}
/** End EVENT_BEFORE_PAGE_INIT */

/** Begin EVENT_BEFORE_REPORT_INIT */


public void beforeReportInit() throws JRScriptletException {

}
/** End EVENT_BEFORE_REPORT_INIT */

United Nations Conference on Trade and Development Page 21 of 26


Developer guide
Version ADK 239
23 - Report

3. Report from the original document


In this part, the report generation from the document itself is presented. Obviously, the report
generated will be a detailed report. Two ways are possible:
• reporting from the search result view
• reporting from the opened document
In the two cases a server rule will be used to throw the information to the binder of the report.

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;”

3.1. Report generation server rule


The generation server rule is used to set the information corresponding to the report
wanted and the kind of output (pdf file or printing). The whole information is then sent to
the report binder by firing a server event with the appropriate identification. This event on
the report binder will automatically the report generation server rule (SR_DoReport).
In the case of an output printing, the server rule will get the result and throw it back to the
client.

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();

United Nations Conference on Trade and Development Page 22 of 26


Developer guide
Version ADK 239
23 - Report

// Get the type of report : print or pdf


Integer report_type = kernelevent.getDataSet().ds(DocumentInterface.NORMAL_ID).
ds(CRITERIA).de(TYP).getContentInteger();
report.ds(CRITERIA).de(USR).tryToSetContent(gcf.getUserEnvironment().getLoginName());
report.ds(CRITERIA).de(TYP).tryToSetContent(report_type);
report.ds(CRITERIA).de(C_Tutorial23.INSTANCE_ID).tryToSetContent(
kernelevent.getDataSet().ds(DocumentInterface.KEYS_ID).
de(C_Tutorial23.INSTANCE_ID).getContent());
// Lauch the report generation server rule
ServerEvent se = Server.getBinderInstance(S_Report.class)[0].fire(new
ServerEvent(report, DO_DETAIL_REPORT));
// for a report print, need to forward the result to the original client rule
if (report_type != null && report_type.intValue() == PRINT_REPORT) {
Object rep = se.getDestination().getContent();
gcf.newDestination(REP);
gcf.getDestination().tryToSetContent(rep);
}
} catch (ServerRuleException e) {
((ServerEvent) kernelevent).setError(kernelevent.getData(), "Error while doing report", e);
}
}
}

This rule must be added to the tutorial23 server binder.

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);

}

}

3.2. Using an operation


To create the report from the search result view, it is necessary to create a new operation
for the S_Tutorial23 server binder. This operation must:
• Terminate automatically
• Be accessible only from the finder
• Have the event associated to the server rule for the report generation

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

United Nations Conference on Trade and Development Page 23 of 26


Developer guide
Version ADK 239
23 - Report

*/
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

protected void apply(KernelEvent kernelevent) {


if (kernelevent instanceof PrepareDocumentToSendEvent &&
((PrepareDocumentToSendEvent) kernelevent).getOperation().getID() ==
C_Tutorial23.OI_REPORT
|| kernelevent instanceof KernelEvent &&
(!(kernelevent instanceof PrepareDocumentToSendEvent))) {

}
}
}

United Nations Conference on Trade and Development Page 24 of 26


Developer guide
Version ADK 239
23 - Report

3.3. Using button


Creating the report can also be done by clicking on a print button in the document. The way to
do so is very similar to a default print:
• Add event identification (DO_PRINT_REPORT) for all the operations needing the
report generation capacity (done in the S_Tutorial23 class).
• Associate the same event identification to a client rule in the client document class.
In this case, the R_Print rule of the report will be used.

public class S_Tutorial23 extends GCFServerBinder implements C_Tutorial23 {


Source 15 -
Modification of the …
server class,
S_Tutorial23 /**
* Create the operations universe
*/
protected Operations createValidOperations() {

Operation op_Update = OperationFactory.makeUpdateOperation(OI_UPDATE,
OP_UPDATE, commitEventsBefore, null);
….
op_Update.addVisibleEventID(DO_PRINT_REPORT, "Report print",
lng("Report print"), "img/Btn_Print_Normal.gif");
addReferenceModelEvents(op_Update);
ops.add(op_Update);

}

}

public class DC_Tutorial23 extends ClientDocument implements C_Tutorial23,


Source 16 - KernelEventConstants {
DC_Tutorial23, void initRules(D_Tutorial23 doc) {
add rule for the …
// Report print
visual event
doc.addRule(new R_Print(), new KernelEvent(DO_PRINT_REPORT));
management …
}

}

United Nations Conference on Trade and Development Page 25 of 26

You might also like