Tuesday, November 1, 2011

Large Excel JasperReports

When generating large Excel report by using JasperReports and DynamicJasper, have to take note the following configuration.

  • Java Heap Size
  • VIRTUALIZER
  • IS_IGNORE_PAGINATION
If you miss 1 of the configuration, you might encounter OutOfMemoryError exception.

Check out the following example code.

public class JasperExcelReport {

protected JasperPrint jp;

protected JasperReport jr;

protected Map params = new HashMap();

protected DynamicReport dr;

public void buildReport() throws Exception {

DynamicReportBuilder drb = new DynamicReportBuilder();

for (int i = 1; i <= 10; i++) {

AbstractColumn column = ColumnBuilder.getInstance()

.setColumnProperty("Column" + i, String.class.getName())

.setTitle("Column " + i + " Title").setWidth(200).build();

drb.addColumn(column);

}

drb.setUseFullPageWidth(true);

dr = drb.build();

JRDataSource ds = getDataSource();

// Make sure Java Heap Size is set.

// E.g. "-Xms256m -Xmx1024m"

// Virtualizer convert the page and store into a physical files on harddisk.

JRFileVirtualizer virtualizer = new JRFileVirtualizer(100, "C://tmp");

params.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

System.out.println("Generating...");

jr = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), params);

System.out.println("Filling...");

if (ds != null) {

// JRParameter.IS_IGNORE_PAGINATION default is false,

// if you set to true, virtualizer will not work.

params.put(JRParameter.IS_IGNORE_PAGINATION, new Boolean(false));

jp = JasperFillManager.fillReport(jr, params, ds);

} else {

jp = JasperFillManager.fillReport(jr, params);

}

// If column width more then page width, some column will merge automatically.

// Set the page width to resolve this issue.

//jp.setPageWidth(0);

//jp.setPageHeight(0);

FileOutputStream fileOut = new FileOutputStream("C:/tmp/ExcelReport.xls");

JRExporter exporter = new JExcelApiExporter();

exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, fileOut);

exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jp);

System.out.println("Exporting...");

exporter.exportReport();

fileOut.flush();

fileOut.close();

}

protected JRDataSource getDataSource() {

// Generate dummy data to show in the report.

List records = new ArrayList();

for (int i = 1; i < 500000; i++) {

Map columns = new HashMap();

for (int j = 1; j <= 10; j++) {

System.out.println("Column" + j);

// The HashMap Key must save with ColumnProperty Name

columns.put("Column" + j, "Record " + i + " Column " + j + " data.");

}

records.add(columns);

}

JRDataSource ds = new JRMapCollectionDataSource(records);

return ds;

}

public static void main(String[] args) throws Exception {

JasperExcelReport jer = new JasperExcelReport();

jer.buildReport();

System.out.println("Done");

}

}

Tuesday, September 27, 2011

How to create large dummy file in Windows Command-Line

Sometime you might need to create a dummy file for testing or other purpose. How can you create a file with the file size you want? For example, how can you create a 1GB file for your testing?

Windows have a command line utility called Fsutil that can help you to create the respective file size you want.

The following command create a dummy file called “test.txt” with 1 GB file size. Just copy and paste into your cmd.

fsutil file createnew test.txt 1000000000

Thursday, September 15, 2011

Manually Generate Castor UUID Key

I am using Castor (Data binding framework) in my company. Most of the tables in our database have an UUID column to make sure all the records are unique. UUID column value is generated by Castor when we insert the record into that table. Of course you have to specific which table and column to store the UUID in the mapping file.

Sometimes you might want to manually insert the records into database. If you insert 1 or 2 records, you can easily specific the UUID value when insert. But if you have 100 or 1000 records to insert. How can you generate the UUID?

I have spent few hours to find out.

You can just copy generateKey method and write a java class to generate the insert statements you want.

Hope the below example can help you to kick start.

public final class ManuallyCastorGeneratorKeyUUID {

private DecimalFormat _df = new DecimalFormat();

private String _sHost = null;

private static long _staticCounter = 0;

// Method extracted from UUIDKeyGenerator

public String generateKey() {

String sUUID = null;

try {

// getting IP (fixed length: 12 character)

if(_sHost == null)

_sHost = InetAddress.getLocalHost().getHostAddress();

StringTokenizer st = new StringTokenizer(_sHost, ".");

_df.applyPattern("000");

while(st.hasMoreTokens()) {

if(sUUID == null)

sUUID = _df.format(new Integer(st.nextToken()));

else

sUUID += _df.format(new Integer(st.nextToken()));

}

// getting currentTimeMillis (fixed length: 13 character)

_df.applyPattern("0000000000000");

sUUID += _df.format(System.currentTimeMillis());

// getting static counter (fixed length: 15 character)

if(_staticCounter >= 99999) // 99999 generated keys in one timer interval? no...

_staticCounter = 0;

_staticCounter++;

_df.applyPattern("00000");

sUUID += _df.format(_staticCounter);

} catch ( Exception ex ) {

ex.printStackTrace();

}

return sUUID;

}

public static void main(String args[]) {

ManuallyCastorGeneratorKeyUUID g = new ManuallyCastorGeneratorKeyUUID();

for(int i=0; i<10; i++) {

System.out.println("insert into table (uuid, column1, column2) values ('" + g.generateKey() +"', value1, value2);");

}

}

}

Wednesday, September 14, 2011

Dynamic Column in JasperReports

If you are looking for a dynamic column for your JasperReports, this is the right place. In this post, I will show you how to generate a dynamic column report with a JasperReports template file.

This is a basic dynamic column report that help you to kick start.

We are using DynamicJasper (3.0.6), JasperReports (2.0.5) and iReport (2.0.5) for this post. I know the version is out of date but the steps should be quite similar.

Create a JasperReports template for your report by using iReport.


You can see the columnHeader band is empty, without columns. We will use DynamicJasper to populate the columns into that band.

The following is the sample java code to generate dynamic columns report.

public class DynamicJasperTemplate {

protected JasperPrint jp;

protected JasperReport jr;

protected Map params = new HashMap();

protected DynamicReport dr;

public void buildReport() throws Exception {

params.put("ReportTitle1", "Report Title");

DynamicReportBuilder drb = new DynamicReportBuilder();

Font font = new Font(10,"Sarif",true);

Style headerStyle = new Style();

headerStyle.setFont(font);

headerStyle.setHorizontalAlign(HorizontalAlign.LEFT);

headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);

font = new Font(8,"Sarif",false);

Style detailStyle = new Style();

detailStyle.setFont(font);

detailStyle.setHorizontalAlign(HorizontalAlign.LEFT);

detailStyle.setVerticalAlign(VerticalAlign.MIDDLE);

for(int i=1; i<=10; i++) {

AbstractColumn column = ColumnBuilder.getInstance()

.setColumnProperty("Column"+i, String.class.getName())

.setTitle("Column "+i+" Title").setWidth(200)

.setStyle(detailStyle).setHeaderStyle(headerStyle).build();

drb.addColumn(column);

}

drb.setUseFullPageWidth(true);

// Pass the JasperReport Template to DynamicJasper

drb.setTemplateFile("C:/DynamicJasperTemplate.jrxml");

DynamicReport dr = drb.build();

JRDataSource ds = getDataSource();

jr = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), params);

if (ds != null) {

jp = JasperFillManager.fillReport(jr, params, ds);

} else {

jp = JasperFillManager.fillReport(jr, params);

}

JasperExportManager.exportReportToPdfFile(jp,"C:/report-out.pdf");

}

protected JRDataSource getDataSource() {

// Generate dummy data to show in the report.

List records = new ArrayList();

for(int i=1; i<10; i++) {

Map columns = new HashMap();

for (int j=1; j<=10; j++) {

// The HashMap Key must save with ColumnProperty Name

columns.put("Column"+j, "Record "+i+" Column "+j+" data.");

}

records.add(columns);

}

JRDataSource ds = new JRMapCollectionDataSource(records);

return ds;

}

public static void main(String[] args) throws Exception {

DynamicJasperTemplate djt = new DynamicJasperTemplate();

djt.buildReport();

}

}



This is the output PDF file.