public class ReportBuilder {
static Logger logger = Logger.getLogger(ReportBuilder.class);
private String jrxmlPath = null;
private Map map = null;
private Connection con = null;
private boolean b_db = false;
private JRDataSource getJunefshDS(){
JRDataSource tmp = null;
ObjectInputStream objin = null;
JunefshObj obj = null;
try{
File fin = new File(jrxmlPath+".junefsh");
//
if( !fin.exists())
return null;
// , ,
if(System.currentTimeMillis() - fin.lastModified() > 3600000)
return null;
objin = new ObjectInputStream(
new FileInputStream(fin));
obj = (JunefshObj) objin.readObject();
}catch (Exception e){
return null;
}
finally {
if (objin != null){
try{
objin.close();
}catch(Exception e){
return null;
}
}
}
tmp = new JRBeanCollectionDataSource(obj.getLs());
return tmp;
}
public boolean getReport_PDF(boolean bJasper)
{
try{
long time_b = System.currentTimeMillis();
logger.debug("--start build PDF report--");
if((jrxmlPath==null) || ("".equals(jrxmlPath)))
{
logger.debug("--the config file path is null--");
throw new CustomException(Constants.SERVER_ERROR);
}
JasperReport jr = null;
JasperPrint jasperPrint = null;
JRDataSource jrDS = null;
jrDS = this.getJunefshDS();
jr = JasperCompileManager.compileReport(jrxmlPath + "_PDF.jrxml");
if (Constants.B_EXPORT_TYPE_DB){
jasperPrint = FillReportUseCon(jr);
}
else {
// ,
if (jrDS == null){
jasperPrint = FillReportUseCon(jr);
}
else {
jasperPrint = JasperFillManager.fillReport(jr, map, jrDS);
}
}
// PDF
File fileTmp = new File(jrxmlPath+".pdf");
//
if (fileTmp.canWrite()) {
fileTmp.delete();
}
// ,
BufferedOutputStream bOS = new BufferedOutputStream(new FileOutputStream(fileTmp));
JRPdfExporter exporter = new JRPdfExporter();
try{
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bOS);
exporter.setParameter(JRPdfExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
exporter.setParameter(JRPdfExporterParameter.IS_COMPRESSED, Boolean.TRUE);
exporter.setParameter(JRPdfExporterParameter.FORCE_SVG_SHAPES, Boolean.TRUE);
exporter.exportReport();
} finally{
if(bOS != null){
bOS.close();
}
}
long time_e = System.currentTimeMillis();
logger.debug("total time of using is :\t"+(time_e-time_b)+"ms");
return true;
} catch (JRException e) {
e.printStackTrace();
return false;
} catch (Exception e){
e.printStackTrace();
return false;
}
}
public boolean getReport_HTML(boolean bJasper)
{
try{
long time_b = System.currentTimeMillis();
JasperReport jr = null;
JasperPrint jasperPrint = null;
logger.debug("--start build HTML report--");
if((jrxmlPath==null) || ("".equals(jrxmlPath)))
{
logger.debug("--the config file path is null--");
throw new CustomException(Constants.SERVER_ERROR);
}
//
JRDataSource jrDS = null;
jrDS = this.getJunefshDS();
jr = JasperCompileManager.compileReport(jrxmlPath + ".jrxml");
if (Constants.B_EXPORT_TYPE_DB){
jasperPrint = FillReportUseCon(jr);
}
else {
// ,
if (jrDS == null){
jasperPrint = FillReportUseCon(jr);
}
else {
jasperPrint = JasperFillManager.fillReport(jr, map, jrDS);
}
}
// html
File fileTmp = new File(jrxmlPath+".html");
//
if (fileTmp.canWrite()) {
fileTmp.delete();
}
BufferedOutputStream bOS = new BufferedOutputStream(new FileOutputStream(fileTmp));
JRHtmlExporter exporter = new JRHtmlExporter();
try{
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bOS);
exporter.setParameter(JRHtmlExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.FRAMES_AS_NESTED_TABLES, Boolean.FALSE);
exporter.exportReport();
}finally{
if(bOS != null){
bOS.close();
}
}
long time_e = System.currentTimeMillis();
logger.debug("total time of using is :\t"+(time_e-time_b)+"ms");
return true;
} catch (JRException e) {
e.printStackTrace();
return false;
} catch (Exception e){
e.printStackTrace();
return false;
}
}
public boolean getReport_Excel(boolean bJExcelApiExporter)
{
try{
long time_b = System.currentTimeMillis();
JasperReport jr = null;
JasperPrint jasperPrint = null;
logger.debug("--start build EXCEL report--");
if((jrxmlPath==null) || ("".equals(jrxmlPath)))
{
logger.debug("--the config file path is null--");
throw new CustomException(Constants.SERVER_ERROR);
}
JRDataSource jrDS = null;
jrDS = this.getJunefshDS();
jr = JasperCompileManager.compileReport(jrxmlPath + ".jrxml");
// if (Constants.B_EXPORT_TYPE_DB)
if (b_db){
jasperPrint = FillReportUseCon(jr);
}
else {
// ,
if (jrDS == null){
jasperPrint = FillReportUseCon(jr);
}
else {
jasperPrint = JasperFillManager.fillReport(jr, map, jrDS);
}
}
// excel
File destFile = new File(jrxmlPath+".xls");
//
if (destFile.canWrite()) {
destFile.delete();
}
/*
* , ,
* JExcelApiExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,destFile.toString());
exporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.setParameter(JExcelApiExporterParameter.IS_IMAGE_BORDER_FIX_ENABLED, Boolean.FALSE);
*/
//exporter.exportReport();
BufferedOutputStream bOS = new BufferedOutputStream(new FileOutputStream(destFile));
// BufferedWriter bWr = new BufferedWriter(new FileWriter(destFile));
try{
JRXlsExporter Ex = new JRXlsExporter();
Ex.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
Ex.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, bOS);
Ex.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
Ex.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
Ex.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
Ex.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, Boolean.TRUE);
Ex.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
Ex.setParameter(JRXlsExporterParameter.IS_IMAGE_BORDER_FIX_ENABLED, Boolean.FALSE);
Ex.exportReport();
// }
} finally{
if(bOS != null){
bOS.close();
}
}
long time_e = System.currentTimeMillis();
logger.debug("total time of using is :\t"+(time_e-time_b)+"ms");
// System.out.println("total time of using is :\t"+(time_e-time_b)+"ms");
return true;
} catch (JRException e) {
e.printStackTrace();
return false;
} catch (Exception e){
e.printStackTrace();
return false;
}
}
private JasperPrint FillReportUseCon(JasperReport jr){
JasperPrint jasperPrint = null ;
GetJdbcConnection objTmp = new GetJdbcConnection();
jdbcDAO jdbcdao = objTmp.getJdbcDao();
try{
jasperPrint = JasperFillManager.fillReport(jr, map, jdbcdao.getConJDBC());
}catch(Exception e){
e.printStackTrace();
}finally{
jdbcdao.destroyConJDBC();
}
return jasperPrint;
}
/* private void FillReportUseCon2Stream(JasperReport jr){
GetJdbcConnection objTmp = new GetJdbcConnection();
jdbcDAO jdbcdao = objTmp.getJdbcDao();
try{
JasperFillManager.fillReportToStream(jr, new BufferedOutputStream(new FileOutputStream(jrxmlPath+".out")), map, jdbcdao.getConJDBC());
}catch(Exception e){
e.printStackTrace();
}finally{
jdbcdao.destroyConJDBC();
}
}*/
public String getJrxmlPath() {
return jrxmlPath;
}
public void setJrxmlPath(String jrxmlPath) {
this.jrxmlPath = jrxmlPath;
// linux windows
// System.out.println("old: "+this.jrxmlPath);
if(!this.jrxmlPath.contains(":"))
{
this.jrxmlPath = "/"+this.jrxmlPath;
}
// System.out.println(this.jrxmlPath);
logger.debug(this.jrxmlPath);
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Connection getCon() {
return con;
}
public void setCon(Connection con) {
this.con = con;
}
public void destory(){
if(this.con != null)
this.con = null;
if(this.jrxmlPath != null)
this.jrxmlPath = null;
if(this.map != null)
this.map = null;
}
}