package com.jasper.Exception;
public class ParameterException extends Exception
{
public ParameterException()
{
}
public ParameterException(String arg0)
{
super(arg0);
}
public ParameterException(Throwable arg0)
{
super(arg0);
}
public ParameterException(String arg0, Throwable arg1)
{
super(arg0, arg1);
}
public static void main(String[] args)
{
}
}
------------------------------------------------------------------------------------------------------------
package com.jasper.report;
public enum JasperCsvMapTypes
{
ColumnNames, DateFormat, NumberFormat, RecordDelimiter, UseFirstRowAsHeader, GroupDataUsed;
}
------------------------------------------------------------------------------------------------------------
package com.jasper.report;
public enum JasperGenerateFormatType
{
pdfFormat, csvFormat, excelFormat, xmlFormat, htmlFormat, xlsxFormat;
}
-------------------------------------------------------------------------------------------------------------
package com.jasper.report;
import com.jasper.Exception.ParameterException;
import java.io.File;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRCsvDataSource;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JasperReportCsvDataSource
{
private String jasperRptCsvFileName = "";
private String jasperTemplateName = "";
private String jasperPrintFileName = "";
private HashMap<String, Object> jasperReportParameters = null;
private JasperPrint jasperPrintObject = null;
private List<String> columnNamesList = null;
private String[] columnNames = null;
private String dateFormat = "";
private String recordDelimiter = "";
private String useFirstRowAsHeader = "";
private SimpleDateFormat dt = null;
private JRCsvDataSource jasperDataSource = null;
private JRCsvDataSource jasperDataSourceRewind = null;
private static final Log logger = LogFactory.getLog(JasperReportCsvDataSource.class);
private static String generateJasperPrintObjectSynch = "blockOff";
private static Map generateJasperPrintObjectMap = Collections.synchronizedMap(new HashMap());
public JasperReportCsvDataSource(String jasperTemplateName, String jasperCsvDataFileName, HashMap<String, Object> jasperReportParameters)
{
this.jasperRptCsvFileName = jasperCsvDataFileName;
this.jasperTemplateName = jasperTemplateName;
this.jasperReportParameters = jasperReportParameters;
}
private void exportCsv(OutputStream outputStream) throws ParameterException
{
long start = System.currentTimeMillis();
String MN = "exportCsv()";
logger.info("Entering method:" + MN);
JRCsvExporter exporter = new JRCsvExporter();
JasperPrint jp = getJasperPrintObject();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleWriterExporterOutput(outputStream));
try
{
exporter.exportReport();
} catch (JRException ex) {
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportCsv() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void generateJasperPrintObject(String saveFileDir)
throws ParameterException
{
String MN = "generateJasperPrintObject()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
long Tname = Thread.currentThread().getId();
logger.info("generateJasperPrintObject -Block 1- Thread: " + Tname);
try
{
setDataSource();
String uuid = UUID.randomUUID().toString();
String jasperPrintFileName = saveFileDir + uuid + ".jrprint";
logger.info(MN + " -- jasperPrintFileName: " + jasperPrintFileName);
setJasperPrintFileName(jasperPrintFileName);
JasperFillManager.fillReportToFile(getJasperTemplateName(), jasperPrintFileName, getReportParameters(), this.jasperDataSource);
this.jasperDataSource.close();
setJasperPrintObject();
}
catch (ParameterException ex) {
logger.error(MN + " ParameterException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + Tname + " 1 Exception " + getJasperTemplateName());
logger.error(MN + Tname + " 2 Exception " + getJasperPrintFileName());
logger.error(MN + Tname + " 3 Exception " + getReportParameters().size());
logger.error(MN + Tname + " 4 Exception " + this.jasperDataSource);
logger.error(MN + " Exception -- " + Tname + " -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Throwable t) {
logger.error(MN + " -- Throwable " + t.getMessage() + " Id: " + Thread.currentThread().getName());
throw new ParameterException(t.getMessage(), t);
}
logger.info(MN + " creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("generateJasperPrintObject -Block 2- Thread: " + Tname);
logger.info("Leaving method:" + MN);
}
private HashMap<String, Object> getReportParameters()
{
return this.jasperReportParameters;
}
private String getJasperPrintFileName()
{
return this.jasperPrintFileName;
}
public void disposeJapserReport()
{
new File(getJasperPrintFileName()).delete();
logger.info("disposeJapserReport : " + getJasperPrintFileName());
}
public void createJasperReportType(String saveFileDir, JasperGenerateFormatType jasperGenerateFormatType, OutputStream outputStream)
throws ParameterException
{
long start = System.currentTimeMillis();
String MN = "createJasperReportType()";
logger.info("Entering method:" + MN);
logger.info(MN + " " + jasperGenerateFormatType.name());
try
{
if (getJasperPrintObject() == null)
generateJasperPrintObject(saveFileDir);
else
logger.info("getJasperPrintObject() -- is not NULL");
switch (jasperGenerateFormatType.ordinal())
{
case 1:
exportPdf(outputStream);
break;
case 2:
exportCsv(outputStream);
break;
case 3:
exportXls(outputStream);
break;
case 4:
exportXml(outputStream);
break;
case 5:
exportXlsx(outputStream);
break;
case 6:
exportHtml(outputStream);
break;
default:
logger.error("Error");
}
}
catch (Exception ex) {
logger.error(MN + " " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("createJasperReportType() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private String getJasperCsvDataFileName()
{
return this.jasperRptCsvFileName;
}
private String getJasperTemplateName()
{
return this.jasperTemplateName;
}
private JasperPrint getJasperPrintObject()
{
return this.jasperPrintObject;
}
private void exportPdf(OutputStream outputStream) throws ParameterException
{
String MN = "exportPdf()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
try {
JasperExportManager.exportReportToPdfStream(jp, outputStream);
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportPdf() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void exportXml(OutputStream outputStream)
throws ParameterException
{
String MN = "exportXml()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
try {
JasperExportManager.exportReportToXmlStream(jp, outputStream);
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportXml() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Entering method:" + MN);
}
private void exportXls(OutputStream outputStream) throws ParameterException
{
String MN = "exportXls()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(Boolean.valueOf(true));
configuration.setDetectCellType(Boolean.valueOf(true));
exporter.setConfiguration(configuration);
try
{
exporter.exportReport();
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportXls() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void setJasperPrintObject()
throws ParameterException
{
String MN = "setJasperPrintObject()";
logger.info("Entering method:" + MN);
File jasperPrintFile = new File(getJasperPrintFileName());
long Tname = Thread.currentThread().getId();
logger.info(MN + " -- jasperPrintFile: " + Tname + " -- " + jasperPrintFile);
try
{
this.jasperPrintObject = ((JasperPrint)JRLoader.loadObject(jasperPrintFile));
}
catch (JRException ex)
{
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("Leaving method:" + MN);
}
private void setJasperPrintFileName(String jasperPrintFileName) {
this.jasperPrintFileName = jasperPrintFileName;
}
private void setDataSource() throws ParameterException
{
String MN = "setDataSource()";
logger.info("Entering method:" + MN);
try
{
this.jasperDataSource = new JRCsvDataSource(JRLoader.getLocationInputStream(getJasperCsvDataFileName()));
}
catch (JRException ex)
{
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
HashMap reportParameters = getReportParameters();
this.useFirstRowAsHeader = ((String)reportParameters.get(JasperCsvMapTypes.UseFirstRowAsHeader.toString()));
if (this.useFirstRowAsHeader == null) {
this.columnNamesList = ((List)reportParameters.get(JasperCsvMapTypes.ColumnNames.toString()));
this.columnNames = ((String[])this.columnNamesList.toArray(new String[this.columnNamesList.size()]));
this.jasperDataSource.setColumnNames(this.columnNames);
} else {
this.jasperDataSource.setUseFirstRowAsHeader(true);
}
this.dateFormat = ((String)reportParameters.get(JasperCsvMapTypes.DateFormat.toString()));
if (this.dateFormat != null) {
this.dt = new SimpleDateFormat(this.dateFormat);
this.jasperDataSource.setDateFormat(this.dt);
}
this.recordDelimiter = ((String)reportParameters.get(JasperCsvMapTypes.RecordDelimiter.toString()));
this.jasperDataSource.setRecordDelimiter(this.recordDelimiter);
String strTmp = (String)reportParameters.get(JasperCsvMapTypes.GroupDataUsed.toString());
Boolean bGroupDataUsed = new Boolean(strTmp);
logger.info(MN + " bGroupDataUsed is: " + bGroupDataUsed);
if (bGroupDataUsed.booleanValue()) {
setDataSourceRewind();
}
logger.info("Leaving method:" + MN);
}
private void setDataSourceRewind()
throws ParameterException
{
String MN = "setDataSourceRewind()";
logger.info("Entering method:" + MN);
try
{
this.jasperDataSourceRewind = new JRCsvDataSource(JRLoader.getLocationInputStream(getJasperCsvDataFileName()));
}
catch (JRException ex)
{
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
HashMap reportParameters = getReportParameters();
this.useFirstRowAsHeader = ((String)reportParameters.get(JasperCsvMapTypes.UseFirstRowAsHeader.toString()));
if (this.useFirstRowAsHeader == null) {
this.columnNamesList = ((List)reportParameters.get(JasperCsvMapTypes.ColumnNames.toString()));
this.columnNames = ((String[])this.columnNamesList.toArray(new String[this.columnNamesList.size()]));
this.jasperDataSourceRewind.setColumnNames(this.columnNames);
} else {
this.jasperDataSourceRewind.setUseFirstRowAsHeader(true);
}
this.dateFormat = ((String)reportParameters.get(JasperCsvMapTypes.DateFormat.toString()));
if (this.dateFormat != null) {
this.dt = new SimpleDateFormat(this.dateFormat);
this.jasperDataSourceRewind.setDateFormat(this.dt);
}
this.recordDelimiter = ((String)reportParameters.get(JasperCsvMapTypes.RecordDelimiter.toString()));
this.jasperDataSourceRewind.setRecordDelimiter(this.recordDelimiter);
this.jasperReportParameters.put("TABLE_DATA_SET", this.jasperDataSourceRewind);
logger.info("Leaving method:" + MN);
}
private void exportHtml(OutputStream outputStream)
throws ParameterException
{
long start = System.currentTimeMillis();
String MN = "exportHtml()";
logger.info("Entering method:" + MN);
HtmlExporter exporter = new HtmlExporter();
JasperPrint jp = getJasperPrintObject();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(outputStream));
try
{
exporter.exportReport();
}
catch (JRException ex) {
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info(MN = " -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void exportXlsx(OutputStream outputStream) throws ParameterException
{
String MN = "exportXlsx()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setDetectCellType(Boolean.valueOf(true));
exporter.setConfiguration(configuration);
try
{
exporter.exportReport();
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info(MN + " -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
}
--------------------------------------------------------------------------------------------------------------
package com.jasper.utility;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Config
{
private static Config instance = null;
private static Properties properties = null;
private static final Log logger = LogFactory.getLog(Config.class);
private static final String JASPER_UTILITIES_CONFIG_FILE = "jasperUtilities.properties";
public static Config getInstance()
throws Exception
{
String MN = "getInstance";
logger.info("Entering method:" + MN);
if (instance == null) {
try
{
instance = new Config();
properties = new Properties();
Hashtable systemProps = System.getProperties();
if (systemProps.containsKey("config.dir"))
{
FileInputStream is = new FileInputStream(systemProps.get("config.dir") + "/" + "jasperUtilities.properties");
logger.info(MN + " Loading the config file: " + "jasperUtilities.properties");
properties.load(is);
}
else
{
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jasperUtilities.properties");
if (is == null) throw new Exception("InputStream is null");
logger.info(MN + " Loading the config file from: /" + "jasperUtilities.properties");
properties.load(is);
}
logger.info("End method:" + MN);
}
catch (Exception ex) {
logger.error("Unable to find the config file " + ex.getMessage());
instance = null;
throw new Exception(ex);
}
}
logger.info("Leaving method:" + MN);
return instance;
}
public String getProperty(String propertyName)
{
String propertyValue = null;
propertyValue = properties.getProperty(propertyName);
return propertyValue;
}
}
--------------------------------------------------------------------------------------------------------------------
package com.jasper.utility;
import com.jasper.Exception.ParameterException;
import com.jasper.report.JasperCsvMapTypes;
import com.jasper.report.JasperGenerateFormatType;
import com.jasper.report.JasperReportCsvDataSource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JasperApiUtility
{
private static final String JASPER_UTILITY_OUTPUT_DIR = "jasper.utility.outputdir";
private static final String JASPER_UTILITY_RECORD_DELIMITER = "jasper.utility.recordDelimiter";
private HashMap<String, Object> jasperReportParameters;
private File file;
private FileOutputStream fos;
private JasperReportCsvDataSource ct;
private JasperGenerateFormatType jasperGenerateFormatType;
private ByteArrayOutputStream bos;
private String recordDelimiter;
private Map<String, Object> parameters;
private String jasperTemplateName;
private String rptCsvFileName;
private String saveFileDir;
private List<String> columnNamesList;
private String[] rptCsvColumnmName;
private String dateFormat;
private Map optionalParms;
private static Map jasperAPIObjectMap = Collections.synchronizedMap(new HashMap());
private static final Log logger = LogFactory.getLog(JasperApiUtility.class);
public JasperApiUtility()
{
this.jasperReportParameters = null;
this.file = null;
this.fos = null;
this.ct = null;
this.jasperGenerateFormatType = null;
this.bos = null;
this.recordDelimiter = "";
this.parameters = null;
this.jasperTemplateName = "";
this.rptCsvFileName = "";
this.saveFileDir = "";
this.columnNamesList = null;
this.rptCsvColumnmName = null;
this.dateFormat = "";
this.optionalParms = null;
}
public static void main(String[] args)
{
String MN = "main[]";
logger.info("Entering method:" + MN);
String rptCsvFileName = "";
String jasperTemplateName = "";
String outputFileLocation = "";
String outputFileName = "";
String[] rptCsvColumnmName = null;
Map optionalParms = null;
String dateFormat = "";
String reportOutputFormat = "pdf";
outputFileLocation = "C:\\Temp\\Jasper\\Test\\output\\";
outputFileName = "madhup." + reportOutputFormat;
rptCsvFileName = "c:\\Temp\\Jasper\\Test\\csv\\V2_Data_File.csv";
jasperTemplateName = "c:\\Temp\\Jasper\\Test\\template\\AEReport_1d.jasper";
optionalParms = new HashMap();
optionalParms.put("dateFrom", "2013-05-08");
optionalParms.put("dateTo", "2013-28-08");
optionalParms.put("reportUser", "Test Parms User2");
try
{
JasperApiUtility ju = new JasperApiUtility();
String reportName = ju.getReportLocation(jasperTemplateName, rptCsvFileName, rptCsvColumnmName, dateFormat, reportOutputFormat, outputFileLocation, outputFileName, optionalParms);
logger.info(MN + " Report generated is " + reportName);
}
catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
}
}
private boolean isFilePresent(String fileName, String fileType)
throws ParameterException
{
String MN = "isFilePresent ";
File file = new File(fileName);
if (!file.exists()) {
throw new ParameterException(MN + " -- File: " + fileName + " -- File type: " + fileType + " is not found");
}
logger.info(MN + " -- Using File " + fileName + " -- File type: " + fileType);
return true;
}
public String getReportLocation(String jasperTemplateName, String rptCsvFileName, String[] rptCsvColumnmName, String dateFormat, String reportOutputFormat, String saveFileDir, String outputFileName, Map optionalParms)
throws ParameterException
{
String MN = "getReportLocation ";
logger.info("Entering method:" + MN);
this.jasperTemplateName = jasperTemplateName;
this.rptCsvFileName = rptCsvFileName;
this.saveFileDir = saveFileDir;
this.rptCsvColumnmName = rptCsvColumnmName;
this.dateFormat = dateFormat;
this.optionalParms = optionalParms;
try
{
synchronized (jasperAPIObjectMap) {
isFilePresent(rptCsvFileName, "Data File");
isFilePresent(jasperTemplateName, "Jasper Template");
this.jasperReportParameters = setJasperReportParameters(this.rptCsvColumnmName, this.dateFormat, this.optionalParms);
this.jasperGenerateFormatType = JasperGenerateFormatType.valueOf(reportOutputFormat.toLowerCase() + "Format");
this.file = new File(saveFileDir + outputFileName);
this.fos = new FileOutputStream(this.file);
this.ct = new JasperReportCsvDataSource(this.jasperTemplateName, this.rptCsvFileName, this.jasperReportParameters);
this.ct.createJasperReportType(this.saveFileDir, this.jasperGenerateFormatType, this.fos);
this.fos.close();
logger.info(MN + " File: " + this.file.toString() + " created");
this.ct.disposeJapserReport();
}
} catch (FileNotFoundException e) {
logger.error(MN + " FileNotFoundException " + e.getMessage(), e);
throw new ParameterException(MN + " FileNotFoundException " + e.getMessage());
}
catch (IOException e) {
logger.error(MN + " IOException " + e.getMessage(), e);
throw new ParameterException(MN + " IOException " + e.getMessage());
} catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
throw new ParameterException(MN + "Exception " + e.getMessage());
}
logger.info("Leaving method:" + MN);
return this.file.toString();
}
private HashMap<String, Object> setJasperReportParameters(String[] columnNames, String dateTemplate, Map reportParms)
throws Exception
{
String MN = "setJasperReportParameters ";
logger.info("Entering method:" + MN);
if (reportParms != null) {
this.parameters = reportParms;
}
this.recordDelimiter = Config.getInstance().getProperty("jasper.utility.recordDelimiter");
this.parameters.put(JasperCsvMapTypes.RecordDelimiter.toString(), this.recordDelimiter);
if (columnNames == null) {
this.parameters.put(JasperCsvMapTypes.UseFirstRowAsHeader.toString(), "true");
}
else {
this.columnNamesList = Arrays.asList(columnNames);
this.parameters.put(JasperCsvMapTypes.ColumnNames.toString(), this.columnNamesList);
}
this.parameters.put(JasperCsvMapTypes.DateFormat.toString(), dateTemplate);
logger.info("Leaving method:" + MN);
return (HashMap)this.parameters;
}
public ByteArrayOutputStream getReportOutputStream(String jasperTemplateName, String rptCsvFileName, String[] rptCsvColumnmName, String dateFormat, String reportOutputFormat)
throws ParameterException
{
String MN = "getReportOutputStream ";
logger.info("Entering method:" + MN);
try
{
isFilePresent(rptCsvFileName, "Data File");
isFilePresent(jasperTemplateName, "Jasper Template");
this.jasperReportParameters = setJasperReportParameters(rptCsvColumnmName, dateFormat, null);
this.jasperGenerateFormatType = JasperGenerateFormatType.valueOf(reportOutputFormat.toLowerCase() + "Format");
this.bos = new ByteArrayOutputStream();
this.ct = new JasperReportCsvDataSource(jasperTemplateName, rptCsvFileName, this.jasperReportParameters);
this.ct.createJasperReportType("", this.jasperGenerateFormatType, this.bos);
logger.info(MN + " bos size: " + this.bos.size() + " created");
this.bos.close();
this.ct.disposeJapserReport();
} catch (FileNotFoundException e) {
logger.error(MN + " FileNotFoundException " + e.getMessage(), e);
throw new ParameterException(MN + " FileNotFoundException " + e.getMessage());
}
catch (IOException e) {
logger.error(MN + " IOException " + e.getMessage(), e);
throw new ParameterException(MN + " IOException " + e.getMessage());
} catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
throw new ParameterException(MN + "Exception " + e.getMessage());
}
logger.info("Leaving method:" + MN);
return this.bos;
}
public String getReportURL(String jasperTemplateName, String rptCsvFileName, String[] rptCsvColumnmName, String dateFormat, String reportOutputFormat, String outputFileName)
throws ParameterException
{
String MN = "getReportURL ";
logger.info("Entering method:" + MN);
String url = "";
boolean implemented = false;
if (implemented)
try {
isFilePresent(rptCsvFileName, "Data File");
isFilePresent(jasperTemplateName, "Jasper Template");
this.jasperReportParameters = setJasperReportParameters(rptCsvColumnmName, dateFormat, null);
this.jasperGenerateFormatType = JasperGenerateFormatType.valueOf(reportOutputFormat.toLowerCase() + "Format");
String saveFileDir = "C:\\Temp\\Jasper\\Test\\output\\";
this.file = new File(saveFileDir + outputFileName);
FileOutputStream fos = new FileOutputStream(this.file);
this.ct = new JasperReportCsvDataSource(jasperTemplateName, rptCsvFileName, this.jasperReportParameters);
this.ct.createJasperReportType("", this.jasperGenerateFormatType, fos);
this.fos.close();
logger.info(MN + " File: " + this.file.toString() + " created");
this.ct.disposeJapserReport();
} catch (FileNotFoundException e) {
logger.error(MN + " FileNotFoundException " + e.getMessage(), e);
throw new ParameterException(MN + " FileNotFoundException " + e.getMessage());
}
catch (IOException e) {
logger.error(MN + " IOException " + e.getMessage(), e);
throw new ParameterException(MN + " IOException " + e.getMessage());
}
catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
throw new ParameterException(MN + "Exception " + e.getMessage());
}
else {
url = "Not yet implemented";
}
logger.info(MN + "url --" + url);
logger.info("Leaving method:" + MN);
return url;
}
}
public class ParameterException extends Exception
{
public ParameterException()
{
}
public ParameterException(String arg0)
{
super(arg0);
}
public ParameterException(Throwable arg0)
{
super(arg0);
}
public ParameterException(String arg0, Throwable arg1)
{
super(arg0, arg1);
}
public static void main(String[] args)
{
}
}
------------------------------------------------------------------------------------------------------------
package com.jasper.report;
public enum JasperCsvMapTypes
{
ColumnNames, DateFormat, NumberFormat, RecordDelimiter, UseFirstRowAsHeader, GroupDataUsed;
}
------------------------------------------------------------------------------------------------------------
package com.jasper.report;
public enum JasperGenerateFormatType
{
pdfFormat, csvFormat, excelFormat, xmlFormat, htmlFormat, xlsxFormat;
}
-------------------------------------------------------------------------------------------------------------
package com.jasper.report;
import com.jasper.Exception.ParameterException;
import java.io.File;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRCsvDataSource;
import net.sf.jasperreports.engine.export.HtmlExporter;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleHtmlExporterOutput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JasperReportCsvDataSource
{
private String jasperRptCsvFileName = "";
private String jasperTemplateName = "";
private String jasperPrintFileName = "";
private HashMap<String, Object> jasperReportParameters = null;
private JasperPrint jasperPrintObject = null;
private List<String> columnNamesList = null;
private String[] columnNames = null;
private String dateFormat = "";
private String recordDelimiter = "";
private String useFirstRowAsHeader = "";
private SimpleDateFormat dt = null;
private JRCsvDataSource jasperDataSource = null;
private JRCsvDataSource jasperDataSourceRewind = null;
private static final Log logger = LogFactory.getLog(JasperReportCsvDataSource.class);
private static String generateJasperPrintObjectSynch = "blockOff";
private static Map generateJasperPrintObjectMap = Collections.synchronizedMap(new HashMap());
public JasperReportCsvDataSource(String jasperTemplateName, String jasperCsvDataFileName, HashMap<String, Object> jasperReportParameters)
{
this.jasperRptCsvFileName = jasperCsvDataFileName;
this.jasperTemplateName = jasperTemplateName;
this.jasperReportParameters = jasperReportParameters;
}
private void exportCsv(OutputStream outputStream) throws ParameterException
{
long start = System.currentTimeMillis();
String MN = "exportCsv()";
logger.info("Entering method:" + MN);
JRCsvExporter exporter = new JRCsvExporter();
JasperPrint jp = getJasperPrintObject();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleWriterExporterOutput(outputStream));
try
{
exporter.exportReport();
} catch (JRException ex) {
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportCsv() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void generateJasperPrintObject(String saveFileDir)
throws ParameterException
{
String MN = "generateJasperPrintObject()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
long Tname = Thread.currentThread().getId();
logger.info("generateJasperPrintObject -Block 1- Thread: " + Tname);
try
{
setDataSource();
String uuid = UUID.randomUUID().toString();
String jasperPrintFileName = saveFileDir + uuid + ".jrprint";
logger.info(MN + " -- jasperPrintFileName: " + jasperPrintFileName);
setJasperPrintFileName(jasperPrintFileName);
JasperFillManager.fillReportToFile(getJasperTemplateName(), jasperPrintFileName, getReportParameters(), this.jasperDataSource);
this.jasperDataSource.close();
setJasperPrintObject();
}
catch (ParameterException ex) {
logger.error(MN + " ParameterException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + Tname + " 1 Exception " + getJasperTemplateName());
logger.error(MN + Tname + " 2 Exception " + getJasperPrintFileName());
logger.error(MN + Tname + " 3 Exception " + getReportParameters().size());
logger.error(MN + Tname + " 4 Exception " + this.jasperDataSource);
logger.error(MN + " Exception -- " + Tname + " -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Throwable t) {
logger.error(MN + " -- Throwable " + t.getMessage() + " Id: " + Thread.currentThread().getName());
throw new ParameterException(t.getMessage(), t);
}
logger.info(MN + " creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("generateJasperPrintObject -Block 2- Thread: " + Tname);
logger.info("Leaving method:" + MN);
}
private HashMap<String, Object> getReportParameters()
{
return this.jasperReportParameters;
}
private String getJasperPrintFileName()
{
return this.jasperPrintFileName;
}
public void disposeJapserReport()
{
new File(getJasperPrintFileName()).delete();
logger.info("disposeJapserReport : " + getJasperPrintFileName());
}
public void createJasperReportType(String saveFileDir, JasperGenerateFormatType jasperGenerateFormatType, OutputStream outputStream)
throws ParameterException
{
long start = System.currentTimeMillis();
String MN = "createJasperReportType()";
logger.info("Entering method:" + MN);
logger.info(MN + " " + jasperGenerateFormatType.name());
try
{
if (getJasperPrintObject() == null)
generateJasperPrintObject(saveFileDir);
else
logger.info("getJasperPrintObject() -- is not NULL");
switch (jasperGenerateFormatType.ordinal())
{
case 1:
exportPdf(outputStream);
break;
case 2:
exportCsv(outputStream);
break;
case 3:
exportXls(outputStream);
break;
case 4:
exportXml(outputStream);
break;
case 5:
exportXlsx(outputStream);
break;
case 6:
exportHtml(outputStream);
break;
default:
logger.error("Error");
}
}
catch (Exception ex) {
logger.error(MN + " " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("createJasperReportType() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private String getJasperCsvDataFileName()
{
return this.jasperRptCsvFileName;
}
private String getJasperTemplateName()
{
return this.jasperTemplateName;
}
private JasperPrint getJasperPrintObject()
{
return this.jasperPrintObject;
}
private void exportPdf(OutputStream outputStream) throws ParameterException
{
String MN = "exportPdf()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
try {
JasperExportManager.exportReportToPdfStream(jp, outputStream);
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportPdf() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void exportXml(OutputStream outputStream)
throws ParameterException
{
String MN = "exportXml()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
try {
JasperExportManager.exportReportToXmlStream(jp, outputStream);
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportXml() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Entering method:" + MN);
}
private void exportXls(OutputStream outputStream) throws ParameterException
{
String MN = "exportXls()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
configuration.setOnePagePerSheet(Boolean.valueOf(true));
configuration.setDetectCellType(Boolean.valueOf(true));
exporter.setConfiguration(configuration);
try
{
exporter.exportReport();
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("exportXls() -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void setJasperPrintObject()
throws ParameterException
{
String MN = "setJasperPrintObject()";
logger.info("Entering method:" + MN);
File jasperPrintFile = new File(getJasperPrintFileName());
long Tname = Thread.currentThread().getId();
logger.info(MN + " -- jasperPrintFile: " + Tname + " -- " + jasperPrintFile);
try
{
this.jasperPrintObject = ((JasperPrint)JRLoader.loadObject(jasperPrintFile));
}
catch (JRException ex)
{
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info("Leaving method:" + MN);
}
private void setJasperPrintFileName(String jasperPrintFileName) {
this.jasperPrintFileName = jasperPrintFileName;
}
private void setDataSource() throws ParameterException
{
String MN = "setDataSource()";
logger.info("Entering method:" + MN);
try
{
this.jasperDataSource = new JRCsvDataSource(JRLoader.getLocationInputStream(getJasperCsvDataFileName()));
}
catch (JRException ex)
{
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
HashMap reportParameters = getReportParameters();
this.useFirstRowAsHeader = ((String)reportParameters.get(JasperCsvMapTypes.UseFirstRowAsHeader.toString()));
if (this.useFirstRowAsHeader == null) {
this.columnNamesList = ((List)reportParameters.get(JasperCsvMapTypes.ColumnNames.toString()));
this.columnNames = ((String[])this.columnNamesList.toArray(new String[this.columnNamesList.size()]));
this.jasperDataSource.setColumnNames(this.columnNames);
} else {
this.jasperDataSource.setUseFirstRowAsHeader(true);
}
this.dateFormat = ((String)reportParameters.get(JasperCsvMapTypes.DateFormat.toString()));
if (this.dateFormat != null) {
this.dt = new SimpleDateFormat(this.dateFormat);
this.jasperDataSource.setDateFormat(this.dt);
}
this.recordDelimiter = ((String)reportParameters.get(JasperCsvMapTypes.RecordDelimiter.toString()));
this.jasperDataSource.setRecordDelimiter(this.recordDelimiter);
String strTmp = (String)reportParameters.get(JasperCsvMapTypes.GroupDataUsed.toString());
Boolean bGroupDataUsed = new Boolean(strTmp);
logger.info(MN + " bGroupDataUsed is: " + bGroupDataUsed);
if (bGroupDataUsed.booleanValue()) {
setDataSourceRewind();
}
logger.info("Leaving method:" + MN);
}
private void setDataSourceRewind()
throws ParameterException
{
String MN = "setDataSourceRewind()";
logger.info("Entering method:" + MN);
try
{
this.jasperDataSourceRewind = new JRCsvDataSource(JRLoader.getLocationInputStream(getJasperCsvDataFileName()));
}
catch (JRException ex)
{
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
HashMap reportParameters = getReportParameters();
this.useFirstRowAsHeader = ((String)reportParameters.get(JasperCsvMapTypes.UseFirstRowAsHeader.toString()));
if (this.useFirstRowAsHeader == null) {
this.columnNamesList = ((List)reportParameters.get(JasperCsvMapTypes.ColumnNames.toString()));
this.columnNames = ((String[])this.columnNamesList.toArray(new String[this.columnNamesList.size()]));
this.jasperDataSourceRewind.setColumnNames(this.columnNames);
} else {
this.jasperDataSourceRewind.setUseFirstRowAsHeader(true);
}
this.dateFormat = ((String)reportParameters.get(JasperCsvMapTypes.DateFormat.toString()));
if (this.dateFormat != null) {
this.dt = new SimpleDateFormat(this.dateFormat);
this.jasperDataSourceRewind.setDateFormat(this.dt);
}
this.recordDelimiter = ((String)reportParameters.get(JasperCsvMapTypes.RecordDelimiter.toString()));
this.jasperDataSourceRewind.setRecordDelimiter(this.recordDelimiter);
this.jasperReportParameters.put("TABLE_DATA_SET", this.jasperDataSourceRewind);
logger.info("Leaving method:" + MN);
}
private void exportHtml(OutputStream outputStream)
throws ParameterException
{
long start = System.currentTimeMillis();
String MN = "exportHtml()";
logger.info("Entering method:" + MN);
HtmlExporter exporter = new HtmlExporter();
JasperPrint jp = getJasperPrintObject();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleHtmlExporterOutput(outputStream));
try
{
exporter.exportReport();
}
catch (JRException ex) {
logger.error(MN + " JRException -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info(MN = " -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
private void exportXlsx(OutputStream outputStream) throws ParameterException
{
String MN = "exportXlsx()";
logger.info("Entering method:" + MN);
long start = System.currentTimeMillis();
JasperPrint jp = getJasperPrintObject();
JRXlsxExporter exporter = new JRXlsxExporter();
exporter.setExporterInput(new SimpleExporterInput(jp));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();
configuration.setDetectCellType(Boolean.valueOf(true));
exporter.setConfiguration(configuration);
try
{
exporter.exportReport();
}
catch (JRException ex) {
logger.error(MN + " JRException " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
catch (Exception ex) {
logger.error(MN + " Exception -- " + ex.getMessage(), ex);
throw new ParameterException(ex.getMessage(), ex);
}
logger.info(MN + " -- creating: creation time : " + (System.currentTimeMillis() - start));
logger.info("Leaving method:" + MN);
}
}
--------------------------------------------------------------------------------------------------------------
package com.jasper.utility;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Config
{
private static Config instance = null;
private static Properties properties = null;
private static final Log logger = LogFactory.getLog(Config.class);
private static final String JASPER_UTILITIES_CONFIG_FILE = "jasperUtilities.properties";
public static Config getInstance()
throws Exception
{
String MN = "getInstance";
logger.info("Entering method:" + MN);
if (instance == null) {
try
{
instance = new Config();
properties = new Properties();
Hashtable systemProps = System.getProperties();
if (systemProps.containsKey("config.dir"))
{
FileInputStream is = new FileInputStream(systemProps.get("config.dir") + "/" + "jasperUtilities.properties");
logger.info(MN + " Loading the config file: " + "jasperUtilities.properties");
properties.load(is);
}
else
{
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jasperUtilities.properties");
if (is == null) throw new Exception("InputStream is null");
logger.info(MN + " Loading the config file from: /" + "jasperUtilities.properties");
properties.load(is);
}
logger.info("End method:" + MN);
}
catch (Exception ex) {
logger.error("Unable to find the config file " + ex.getMessage());
instance = null;
throw new Exception(ex);
}
}
logger.info("Leaving method:" + MN);
return instance;
}
public String getProperty(String propertyName)
{
String propertyValue = null;
propertyValue = properties.getProperty(propertyName);
return propertyValue;
}
}
--------------------------------------------------------------------------------------------------------------------
package com.jasper.utility;
import com.jasper.Exception.ParameterException;
import com.jasper.report.JasperCsvMapTypes;
import com.jasper.report.JasperGenerateFormatType;
import com.jasper.report.JasperReportCsvDataSource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JasperApiUtility
{
private static final String JASPER_UTILITY_OUTPUT_DIR = "jasper.utility.outputdir";
private static final String JASPER_UTILITY_RECORD_DELIMITER = "jasper.utility.recordDelimiter";
private HashMap<String, Object> jasperReportParameters;
private File file;
private FileOutputStream fos;
private JasperReportCsvDataSource ct;
private JasperGenerateFormatType jasperGenerateFormatType;
private ByteArrayOutputStream bos;
private String recordDelimiter;
private Map<String, Object> parameters;
private String jasperTemplateName;
private String rptCsvFileName;
private String saveFileDir;
private List<String> columnNamesList;
private String[] rptCsvColumnmName;
private String dateFormat;
private Map optionalParms;
private static Map jasperAPIObjectMap = Collections.synchronizedMap(new HashMap());
private static final Log logger = LogFactory.getLog(JasperApiUtility.class);
public JasperApiUtility()
{
this.jasperReportParameters = null;
this.file = null;
this.fos = null;
this.ct = null;
this.jasperGenerateFormatType = null;
this.bos = null;
this.recordDelimiter = "";
this.parameters = null;
this.jasperTemplateName = "";
this.rptCsvFileName = "";
this.saveFileDir = "";
this.columnNamesList = null;
this.rptCsvColumnmName = null;
this.dateFormat = "";
this.optionalParms = null;
}
public static void main(String[] args)
{
String MN = "main[]";
logger.info("Entering method:" + MN);
String rptCsvFileName = "";
String jasperTemplateName = "";
String outputFileLocation = "";
String outputFileName = "";
String[] rptCsvColumnmName = null;
Map optionalParms = null;
String dateFormat = "";
String reportOutputFormat = "pdf";
outputFileLocation = "C:\\Temp\\Jasper\\Test\\output\\";
outputFileName = "madhup." + reportOutputFormat;
rptCsvFileName = "c:\\Temp\\Jasper\\Test\\csv\\V2_Data_File.csv";
jasperTemplateName = "c:\\Temp\\Jasper\\Test\\template\\AEReport_1d.jasper";
optionalParms = new HashMap();
optionalParms.put("dateFrom", "2013-05-08");
optionalParms.put("dateTo", "2013-28-08");
optionalParms.put("reportUser", "Test Parms User2");
try
{
JasperApiUtility ju = new JasperApiUtility();
String reportName = ju.getReportLocation(jasperTemplateName, rptCsvFileName, rptCsvColumnmName, dateFormat, reportOutputFormat, outputFileLocation, outputFileName, optionalParms);
logger.info(MN + " Report generated is " + reportName);
}
catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
}
}
private boolean isFilePresent(String fileName, String fileType)
throws ParameterException
{
String MN = "isFilePresent ";
File file = new File(fileName);
if (!file.exists()) {
throw new ParameterException(MN + " -- File: " + fileName + " -- File type: " + fileType + " is not found");
}
logger.info(MN + " -- Using File " + fileName + " -- File type: " + fileType);
return true;
}
public String getReportLocation(String jasperTemplateName, String rptCsvFileName, String[] rptCsvColumnmName, String dateFormat, String reportOutputFormat, String saveFileDir, String outputFileName, Map optionalParms)
throws ParameterException
{
String MN = "getReportLocation ";
logger.info("Entering method:" + MN);
this.jasperTemplateName = jasperTemplateName;
this.rptCsvFileName = rptCsvFileName;
this.saveFileDir = saveFileDir;
this.rptCsvColumnmName = rptCsvColumnmName;
this.dateFormat = dateFormat;
this.optionalParms = optionalParms;
try
{
synchronized (jasperAPIObjectMap) {
isFilePresent(rptCsvFileName, "Data File");
isFilePresent(jasperTemplateName, "Jasper Template");
this.jasperReportParameters = setJasperReportParameters(this.rptCsvColumnmName, this.dateFormat, this.optionalParms);
this.jasperGenerateFormatType = JasperGenerateFormatType.valueOf(reportOutputFormat.toLowerCase() + "Format");
this.file = new File(saveFileDir + outputFileName);
this.fos = new FileOutputStream(this.file);
this.ct = new JasperReportCsvDataSource(this.jasperTemplateName, this.rptCsvFileName, this.jasperReportParameters);
this.ct.createJasperReportType(this.saveFileDir, this.jasperGenerateFormatType, this.fos);
this.fos.close();
logger.info(MN + " File: " + this.file.toString() + " created");
this.ct.disposeJapserReport();
}
} catch (FileNotFoundException e) {
logger.error(MN + " FileNotFoundException " + e.getMessage(), e);
throw new ParameterException(MN + " FileNotFoundException " + e.getMessage());
}
catch (IOException e) {
logger.error(MN + " IOException " + e.getMessage(), e);
throw new ParameterException(MN + " IOException " + e.getMessage());
} catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
throw new ParameterException(MN + "Exception " + e.getMessage());
}
logger.info("Leaving method:" + MN);
return this.file.toString();
}
private HashMap<String, Object> setJasperReportParameters(String[] columnNames, String dateTemplate, Map reportParms)
throws Exception
{
String MN = "setJasperReportParameters ";
logger.info("Entering method:" + MN);
if (reportParms != null) {
this.parameters = reportParms;
}
this.recordDelimiter = Config.getInstance().getProperty("jasper.utility.recordDelimiter");
this.parameters.put(JasperCsvMapTypes.RecordDelimiter.toString(), this.recordDelimiter);
if (columnNames == null) {
this.parameters.put(JasperCsvMapTypes.UseFirstRowAsHeader.toString(), "true");
}
else {
this.columnNamesList = Arrays.asList(columnNames);
this.parameters.put(JasperCsvMapTypes.ColumnNames.toString(), this.columnNamesList);
}
this.parameters.put(JasperCsvMapTypes.DateFormat.toString(), dateTemplate);
logger.info("Leaving method:" + MN);
return (HashMap)this.parameters;
}
public ByteArrayOutputStream getReportOutputStream(String jasperTemplateName, String rptCsvFileName, String[] rptCsvColumnmName, String dateFormat, String reportOutputFormat)
throws ParameterException
{
String MN = "getReportOutputStream ";
logger.info("Entering method:" + MN);
try
{
isFilePresent(rptCsvFileName, "Data File");
isFilePresent(jasperTemplateName, "Jasper Template");
this.jasperReportParameters = setJasperReportParameters(rptCsvColumnmName, dateFormat, null);
this.jasperGenerateFormatType = JasperGenerateFormatType.valueOf(reportOutputFormat.toLowerCase() + "Format");
this.bos = new ByteArrayOutputStream();
this.ct = new JasperReportCsvDataSource(jasperTemplateName, rptCsvFileName, this.jasperReportParameters);
this.ct.createJasperReportType("", this.jasperGenerateFormatType, this.bos);
logger.info(MN + " bos size: " + this.bos.size() + " created");
this.bos.close();
this.ct.disposeJapserReport();
} catch (FileNotFoundException e) {
logger.error(MN + " FileNotFoundException " + e.getMessage(), e);
throw new ParameterException(MN + " FileNotFoundException " + e.getMessage());
}
catch (IOException e) {
logger.error(MN + " IOException " + e.getMessage(), e);
throw new ParameterException(MN + " IOException " + e.getMessage());
} catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
throw new ParameterException(MN + "Exception " + e.getMessage());
}
logger.info("Leaving method:" + MN);
return this.bos;
}
public String getReportURL(String jasperTemplateName, String rptCsvFileName, String[] rptCsvColumnmName, String dateFormat, String reportOutputFormat, String outputFileName)
throws ParameterException
{
String MN = "getReportURL ";
logger.info("Entering method:" + MN);
String url = "";
boolean implemented = false;
if (implemented)
try {
isFilePresent(rptCsvFileName, "Data File");
isFilePresent(jasperTemplateName, "Jasper Template");
this.jasperReportParameters = setJasperReportParameters(rptCsvColumnmName, dateFormat, null);
this.jasperGenerateFormatType = JasperGenerateFormatType.valueOf(reportOutputFormat.toLowerCase() + "Format");
String saveFileDir = "C:\\Temp\\Jasper\\Test\\output\\";
this.file = new File(saveFileDir + outputFileName);
FileOutputStream fos = new FileOutputStream(this.file);
this.ct = new JasperReportCsvDataSource(jasperTemplateName, rptCsvFileName, this.jasperReportParameters);
this.ct.createJasperReportType("", this.jasperGenerateFormatType, fos);
this.fos.close();
logger.info(MN + " File: " + this.file.toString() + " created");
this.ct.disposeJapserReport();
} catch (FileNotFoundException e) {
logger.error(MN + " FileNotFoundException " + e.getMessage(), e);
throw new ParameterException(MN + " FileNotFoundException " + e.getMessage());
}
catch (IOException e) {
logger.error(MN + " IOException " + e.getMessage(), e);
throw new ParameterException(MN + " IOException " + e.getMessage());
}
catch (Exception e) {
logger.error(MN + " Exception " + e.getMessage(), e);
throw new ParameterException(MN + "Exception " + e.getMessage());
}
else {
url = "Not yet implemented";
}
logger.info(MN + "url --" + url);
logger.info("Leaving method:" + MN);
return url;
}
}