Jasperreports in Jmatter
Wednesday, October 31st, 2007This is my answer to one of the question in groups.google.com/group/jmatter
here is how i used jasperreports + ireport + Jmatter
i have kept it step wise so that it is easy to follow
please visit my blog at http://CodeWithMe.com/blog for the screen shots
i will post it after few days and email me at justniraj at hotmail dot com if you think i can answer you
Step 1) Make a EO Class
in my case the EO class is
package com.quickresource.models;
import com.u2d.element.CommandInfo;
import com.u2d.model.AbstractComplexEObject;
import com.u2d.model.ComplexType;
import com.u2d.model.Title;
import com.u2d.persist.Persist;
import com.u2d.reflection.Cmd;
import com.u2d.type.atom.StringEO;
@Persist
public class SampleEO extends AbstractComplexEObject {
private final StringEO name = new StringEO();
private final StringEO age = new StringEO();
@Override
public Title title() {
return new Title(”TEst”);
}
public StringEO getAge() {
return age;
}
public StringEO getName() {
return name;
}
}
Step 2)
build your jmatter project
what i mean is generate the class files for your EO classes
Step 3)
open ireport set the classpath by following menu options
add
build/classes directory of your jmatter application to the classpath of ireport
add
jmatter.jar , jibx-extras.jar, jibx-run.jar of your lib/runtime directory to the classpath of ireport
Step 3)
Open a new Report
Follow the menu
data->report query
Step 4)
Instead of Report Query where you write the sql choose Java bean datasource
Step 5)
give fully qualified name of your EO object
in my case :
com.quickresource.models.SampleEO
Step 6)
now you will get the filed name and age in the property box of ireport you can design the report as normal ireport report
but remember the class of the field(s) is com.u2d.type.atom.StringEO
Step 7)
Compile the report you cannot preview the report with live data as you are not using any live datbase
Step ![]()
copy the generated .jaseper file to you application directory in my case i copied the file sampleReport.jasper to
/com/quickresource/models/ of my src dir
Step 9)
Copy the libs(jars) needed for jasper report
now here is the easy way to do
although you can get the jasper report from the jasper report project what i like to do is
copy following folders from lib of springframe work to /lib/runtime of my application
folders i copied
1) jakarta-commons
Required jar files by jasper report
2) jasperreports
Jasper report jar files
you do not need to change the build.xml of jmatter as it is set to include all the sub directories of /lib/runtime
Step 10)
now add the following code to you EO class
@Cmd
public static Object printAllEmployee(CommandInfo info) {
try {
List list = ComplexType.forClass(SampleEO.class).list().getItems();
JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(
list);
JasperPrint print = JasperFillManager
.fillReport(SampleEO.class.getResourceAsStream(”/com/quickresource/models/sampleReport.jasper”),null, datasource);
JRViewer viewer = new JRViewer(print);
return viewer;
} catch (Exception ms) {
ms.printStackTrace();
return null;
}
}
Step 11)
add SampleEo to your classbar
now compile and run right click on the SampleEOs of Class bar
you will be able to see jasper report preview in jmatter screen
Thats it
Have fun jmatter matters a lot
Here is the complete code listing
1) com/quickresource/models/SampleEO.java
package com.quickresource.models;
import java.util.List;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JRViewer;
import com.u2d.element.CommandInfo;
import com.u2d.model.AbstractComplexEObject;
import com.u2d.model.ComplexType;
import com.u2d.model.Title;
import com.u2d.persist.Persist;
import com.u2d.reflection.Cmd;
import com.u2d.type.atom.StringEO;
@Persist
public class SampleEO extends AbstractComplexEObject {
private final StringEO name = new StringEO();
private final StringEO age = new StringEO();
@Override
public Title title() {
return new Title(”TEst”);
}
public StringEO getAge() {
return age;
}
public StringEO getName() {
return name;
}
@Cmd
public static Object printAllEmployee(CommandInfo info) {
try {
List list = ComplexType.forClass(SampleEO.class).list().getItems();
JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(
list);
JasperPrint print = JasperFillManager
.fillReport(
SampleEO.class
.getResourceAsStream(”/com/quickresource/models/sampleReport.jasper”),
null, datasource);
JRViewer viewer = new JRViewer(print);
return viewer;
} catch (Exception ms) {
ms.printStackTrace();
return null;
}
}
}
2) SampleReport.jxml(as genereated by jasper report)
<?xml version=”1.0″ encoding=”UTF-8″ ?>
<!– Created with iReport - A designer for JasperReports –>
<!DOCTYPE jasperReport PUBLIC “//JasperReports//DTD Report Design//EN” “http://jasperreports.sourceforge.net/dtds/jasperreport.dtd”>
<jasperReport
name=”sampleReport”
columnCount=”1″
printOrder=”Vertical”
orientation=”Portrait”
pageWidth=”595″
pageHeight=”842″
columnWidth=”535″
columnSpacing=”0″
leftMargin=”30″
rightMargin=”30″
topMargin=”20″
bottomMargin=”20″
whenNoDataType=”NoPages”
isTitleNewPage=”false”
isSummaryNewPage=”false”>
<property name=”ireport.scriptlethandling” value=”0″ />
<property name=”ireport.encoding” value=”UTF-8″ />
<import value=”java.util.*” />
<import value=”net.sf.jasperreports.engine.*” />
<import value=”net.sf.jasperreports.engine.data.*” />
<field name=”age” class=”com.u2d.type.atom.StringEO”>
<fieldDescription><![CDATA[age]]></fieldDescription>
</field>
<field name=”name” class=”com.u2d.type.atom.StringEO”>
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<background>
<band height=”0″ isSplitAllowed=”true” >
</band>
</background>
<title>
<band height=”50″ isSplitAllowed=”true” >
</band>
</title>
<pageHeader>
<band height=”50″ isSplitAllowed=”true” >
<staticText>
<reportElement
x=”208″
y=”18″
width=”131″
height=”18″
key=”staticText-1″/>
<box topBorder=”None” topBorderColor=”#000000″ leftBorder=”None” leftBorderColor=”#000000″ rightBorder=”None” rightBorderColor=”#000000″ bottomBorder=”None” bottomBorderColor=”#000000″/>
<textElement>
<font/>
</textElement>
<text><![CDATA[Page Header of Sample]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height=”30″ isSplitAllowed=”true” >
<staticText>
<reportElement
x=”6″
y=”6″
width=”97″
height=”17″
key=”staticText-2″/>
<box topBorder=”None” topBorderColor=”#000000″ leftBorder=”None” leftBorderColor=”#000000″ rightBorder=”None” rightBorderColor=”#000000″ bottomBorder=”None” bottomBorderColor=”#000000″/>
<textElement>
<font/>
</textElement>
<text><![CDATA[age]]></text>
</staticText>
<staticText>
<reportElement
x=”232″
y=”8″
width=”106″
height=”18″
key=”staticText-3″/>
<box topBorder=”None” topBorderColor=”#000000″ leftBorder=”None” leftBorderColor=”#000000″ rightBorder=”None” rightBorderColor=”#000000″ bottomBorder=”None” bottomBorderColor=”#000000″/>
<textElement>
<font/>
</textElement>
<text><![CDATA[Name]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height=”18″ isSplitAllowed=”true” >
<textField isStretchWithOverflow=”false” isBlankWhenNull=”false” evaluationTime=”Now” hyperlinkType=”None” hyperlinkTarget=”Self” >
<reportElement
x=”2″
y=”0″
width=”100″
height=”18″
key=”textField”/>
<box topBorder=”None” topBorderColor=”#000000″ leftBorder=”None” leftBorderColor=”#000000″ rightBorder=”None” rightBorderColor=”#000000″ bottomBorder=”None” bottomBorderColor=”#000000″/>
<textElement>
<font/>
</textElement>
<textFieldExpression class=”java.lang.String”><![CDATA[”"+$F{age}]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow=”false” isBlankWhenNull=”false” evaluationTime=”Now” hyperlinkType=”None” hyperlinkTarget=”Self” >
<reportElement
x=”234″
y=”0″
width=”100″
height=”18″
key=”textField”/>
<box topBorder=”None” topBorderColor=”#000000″ leftBorder=”None” leftBorderColor=”#000000″ rightBorder=”None” rightBorderColor=”#000000″ bottomBorder=”None” bottomBorderColor=”#000000″/>
<textElement>
<font/>
</textElement>
<textFieldExpression class=”java.lang.String”><![CDATA[”"+$F{name}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height=”30″ isSplitAllowed=”true” >
</band>
</columnFooter>
<pageFooter>
<band height=”50″ isSplitAllowed=”true” >
</band>
</pageFooter>
<lastPageFooter>
<band height=”50″ isSplitAllowed=”true” >
</band>
</lastPageFooter>
<summary>
<band height=”50″ isSplitAllowed=”true” >
</band>
</summary>
</jasperReport>