Jump to content

Auto Report generation


gr4nt4

Recommended Posts

Hi All

 

I've have the following code (which is not mine) which should be able to create pdf and xl output from a command line.

 

Code:
/**
* This module will allow Jasper Reports, desinged via iReport, to be executed
* in batch mode.
* The module takes the following arguments:
* 1) Run Mode - (V)iew Report, (F)ormat Report or (B«»)oth
* 2) Report Name (Full Path)
* 3) Output File Name (Full Path) Suffixc is used as File Format
* (PDF, Excel ...etc)
* *) The database connection strings are set using the -D (sets the
* properties dbase.x (driver, url,user, password)
* Also, the promptable paramaters can now be placed on the command
* line - so typically the args become
* [V|F|B] [Full Report Path] [Full Output Formatted Report Name]
* [-DParamName1=Paramvalue -DParamName2=ParamValue ...etc]
* ...Be wary here as the ParamNames need to have embedded spaces replaced
* with ^ (as space in a -D property appears to mess up) - so to pass
* a parameter of This Is The First Number and This Is The Last Number
* you would enter:
* -DThis^Is^The^First^Number=1 -DThis^Is^The^Last^Number=999
* The reason for doing this is that it means the reports can be run
* completely unattended. IE, assume that you have a report that is to
* be scheduled to run every day with the first day of the month (but can
* have any date passed to it). typically the report would be designed to
* have a parameter of date that is promptable, but in reality you just
* want to schedule it to run with a 'calculated' date. So a run script
* can be set up that calculates the first day of the month, and then
* passes that as a -D property to this runner - this runner will detect
* that the date parameter has been passed on the command line, and will
* not try to prompt for it. So you can have unattended running of
* 'parameterised' reports.


// Include the main Jasper Reports Driver.....
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.*;
import java.sql.*;
import java.util.*;
import java.text.*;
import javax.swing.*;

public class JReportRunner {

public static void main(String[] args) {
int i = 0;

String sourceFileName = null;
JasperReport compiledReport = null;
JRParameter[] prompts = null;
String reportPath = null;
String outputFileName = null;
String outputFormat = null;
String runMode = null;
Map parameters = new HashMap();
OpenDatabase openDatabase = null;
JasperPrint outputReport = null;
String commandLineParamName = null;
String paramName = null;
Class paramClass = null;
String paramClassName = null;
String paramValue = null;
int paramInt = 0;
long paramLong = 0;
short paramShort = 0;
double paramDouble = 0;
float paramFloat = 0;
byte paramByte = 0;
boolean commandLine = false;
java.util.Date paramDate = null;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);

// By default date entry is lenient - that means the 0/0/01 is valid
// as is 99/99/9999 - the days get mapped to existing days ie - 0 in
// a day is the last day of the preceding month and 99 would be the
// date in circa 3 months time ..... (why would anyone want
// that? - anyway, that's how it does it) - so make sure lenient is
// turned off
dateFormat.setLenient(false);

System.out.println("In Runner With Date Format ("+dateFormat+"«»)"«»);
while (i < args.length) {
System.out.print("Arg ("+i+"«») Is ("+args+"«») "«»);
// Move the arguments into named variables
if (i == 0) {
runMode = args;
}
if (i == 1) {
reportPath = args;
}
if (i == 2) {
outputFileName = args;
}
i++;
}
System.out.println();

// Split the outputfile name to get the suffix - as we'll use this
// to decide which methods to use to export the file....
i = outputFileName.lastIndexOf("."«»);
outputFormat = outputFileName.substring(i+1,outputFileName.length());

// We need to know the parameters in the report, I can't see any other
// way of getting to this info apart from 'dummy' compiling the report
// - but to do that we'll need a pointer to the source .jrxml file
i = reportPath.lastIndexOf("."«»);
sourceFileName = reportPath.substring(0,i+1)+"jrxml";

try {
compiledReport = JasperCompileManager.compileReport(sourceFileName);
}
catch (JRException e) {
System.err.println("JReportRunner: Cannot Compile Report ("+
sourceFileName+"«») "+e);
}

prompts = compiledReport.getParameters();

// Now we need to loop through the paramaters and prompt (if promptable)
// for the values - we'll put the parameters into a separate hash map
// The prog can also be run with the parameters passed on the command
// line (via -D) - to enable a true batch process......
JFrame jframe = new JFrame("Dialog"«»);
for (i = 0;i < prompts.length;i++) {
if (!prompts.isSystemDefined()) {
paramName = prompts.getName();
// If passed on the command line, the parameter has spaces
// replaced with ^, so From Claim No becomes From^Claim^No
// (as spaces messes up the System Properties) - so create
// a command line parameter from the main parameter, with ^
// instead of embedded space.......
commandLineParamName= paramName.trim();
commandLineParamName= commandLineParamName.replaceAll(" ","^"«»);
paramClass = prompts.getValueClass();
paramClassName = prompts.getValueClassName();
commandLine = false;
while (true) {
if (commandLine) {
// can't do owt else apart from abort here, as we're
// in 'unattended' mode (in theory)
System.err.println(
"JReportRunner: Command Line Parameter ("+
paramName+"«») Failed Validationn"+
"Value Set To ("+paramValue+"«»)"«»);
System.exit(1);
}
paramValue = (System.getProperty(commandLineParamName));
if (paramValue == null) {
paramValue =
JOptionPane.showInputDialog
(jframe,
"Please Enter Value For "+
paramName+" n "+paramClass,
paramName+" (Type "+paramClassName+"«»)",
JOptionPane.QUESTION_MESSAGE);
}
else {
// This will be detected at the start of the loop,
// and if set means that a command line parameter
// failed validation - so we'll have to abort
commandLine = true;
}
if (paramClassName.endsWith("Integer"«»)) {
try {
paramInt = Integer.parseInt(paramValue);
}
catch (NumberFormatException e) {
if (!commandLine){
JOptionPane.showMessageDialog
(jframe,
"Invalid Input ("+
paramValue+"«») "+e,
"Invalid Integer Value",
JOptionPane.ERROR_MESSAGE);
}
continue;
}
parameters.put(paramName,new Integer(paramInt));
}
if (paramClassName.endsWith("Short"«»)) {
try {
paramShort = Short.parseShort(paramValue);
}
catch (NumberFormatException e) {
if (!commandLine) {
JOptionPane.showMessageDialog
(jframe,
"Invalid Input ("+
paramValue+"«») "+e,
"Invalid Short Value",
JOptionPane.ERROR_MESSAGE );
}
continue;
}
parameters.put(paramName,new Short(paramShort));
}
if (paramClassName.endsWith("Long"«»)) {
try {
paramLong = Long.parseLong(paramValue);
}
catch (NumberFormatException e) {
if (!commandLine) {
JOptionPane.showMessageDialog
(jframe,
"Invalid Input ("+
paramValue+"«») "+e,
"Invalid Long Value",
JOptionPane.ERROR_MESSAGE );
}
continue;
}
parameters.put(paramName,new Long(paramLong));
}
if (paramClassName.endsWith("Float"«»)) {
try {
paramFloat = Float.parseFloat(paramValue);
}
catch (NumberFormatException e) {
if (!commandLine) {
JOptionPane.showMessageDialog
(jframe,
"Invalid Input ("+
paramValue+"«») "+e,
"Invalid Float Value",
JOptionPane.ERROR_MESSAGE );
}
continue;
}
parameters.put(paramName,new Float(paramFloat));
}
if (paramClassName.endsWith("Double"«»)) {
try {
paramDouble = Double.parseDouble(paramValue);
}
catch (NumberFormatException e) {
if (!commandLine){
JOptionPane.showMessageDialog
(jframe,
"Invalid Input ("+
paramValue+"«») "+e,
"Invalid Double Value",
JOptionPane.ERROR_MESSAGE );
}
continue;
}
parameters.put(paramName,new Double(paramDouble));
}
if (paramClassName.endsWith("Byte"«»)) {
try {
paramByte = Byte.parseByte(paramValue);
}
catch (NumberFormatException e) {
if (!commandLine){
JOptionPane.showMessageDialog
(jframe,
"Invalid Input ("+
paramValue+"«») "+e,
"Invalid Byte Value",
JOptionPane.ERROR_MESSAGE );
}
continue;
}
parameters.put(paramName,new Byte(paramByte));
}
if (paramClassName.endsWith("Date"«»)) {
try {
paramDate = (java.util.Date)dateFormat.parse(paramValue);
}
catch (ParseException e) {
if (!commandLine){
JOptionPane.showMessageDialog
(jframe,
"Invalid Input ("+
paramValue+"«»)n "+
"Please Use Format (dd/mm/yy[yy])n"+e,
"Invalid Date Value",
JOptionPane.ERROR_MESSAGE );
}
continue;
}
parameters.put(paramName,paramDate);
}
if (paramClassName.endsWith("String"«»)) {
parameters.put(paramName,paramValue);
}
if (paramClassName.endsWith("Text"«»)) {
parameters.put(paramName,paramValue);
}
break;
}
}
}

// Set up the SQL connection via our old database open class (hope this
// was left in a working state!)
openDatabase = new OpenDatabase("","","",""«»);

// Fill the report.....
try {
outputReport = JasperFillManager.fillReport(reportPath,
parameters,
openDatabase.connection);
}
catch (JRException e) {
System.err.println("JReportRunner: Cannot Fill Report ("+
reportPath+"«») "+e);
}

if (runMode.equalsIgnoreCase("V"«») || runMode.equalsIgnoreCase("B"«»)){
// Now preview the report using the printmanager class....
try {
JasperViewer.viewReport(outputReport);
}
// Did not compile, because viewReport does not throw this: catch (JRException e) {
catch (Exception e) {
System.err.println("JReportRunner: Cannot View Report ("+
reportPath+"«») "+e);
}
}

// Finally DO something - we have the file format, so case the format
// and call the respective processor......
if (runMode.equalsIgnoreCase("F"«») || runMode.equalsIgnoreCase("B"«»)){
try {
if (outputFormat.equalsIgnoreCase("pdf"«»)) {
JasperExportManager.exportReportToPdfFile(outputReport,outputFileName);
}
else if (outputFormat.equalsIgnoreCase("html"«»)) {
JasperExportManager.exportReportToHtmlFile(outputReport,outputFileName);
}
else if (outputFormat.equalsIgnoreCase("xml"«»)) {
JasperExportManager.exportReportToXmlFile(outputReport,outputFileName,1==1);
}
else {
System.err.println("Invalid File Format: ("+outputFormat+"«»)"«»);
}
}
catch (JRException e) {
System.err.println("JReportRunner: Cannot Create Export ("+
outputFileName+"«») Type ("+outputFormat+"«») "+e);
}
}
}
}

 

However I get the error

 

JReportRunner: Cannot Compile Report (C:DatajrunnerTest.jrxml) net.sf.

jasperreports.engine.JRException: Error compiling report java source files : C:

DatajrunnerUnnamed_1189149333642_675773.java

Exception in thread "main" java.lang.NullPointerException

at JReportRunner.main(JReportRunner.java:127)

 

When Running the command line

 

"java -classpath C:DataiReportsiReport-1.3.1libcommons-beanutils-1.7.jar;C:

DataiReportsiReport-1.3.1libcommons-collections-2.1.jar;C:DataiReportsiR

eport-1.3.1libcommons-digester-1.7.jar;C:DataiReportsiReport-1.3.1libcomm

ons-logging-1.0.2.jar;C:DataiReportsiReport-1.3.1libjasperreports-1.3.1.jar

;C:datajrunner JReportRunner F C:DatajrunnerTest.jasper c:anoutfile

.pdf -Ddbase.driversun.odbc.JdbcOdbcDrive -Ddbase.urlBBR -Ddbase.user

 

I've little knowlage of java and would appriciate any pointers you can give.

 

Many thanks in advance

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...