ReportEngine.java 6.68 KB
Newer Older
Naung1 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
package com.csc.library.report;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRTextExporter;
import net.sf.jasperreports.engine.export.JRTextExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.oasis.JROdtExporter;
import net.sf.jasperreports.engine.util.JRLoader;

import com.csc.library.session.InitialEnvironment;
import com.csc.library.utilities.MyLog;
import com.csc.library.utilities.UProfile;


public class ReportEngine  {
	private java.util.HashMap condition;
	private MyReport myrep;
	private TemplateFormula template;
	private UProfile ufmaster=null;	
	private InitialEnvironment env;
	private boolean first = true;
	private JRDataSource dataSource;
	private String templateDir;
	private String reportId="";
	private String CharSet="UTF-8";

	public ReportEngine(HashMap condition,String reportId,UProfile upf) {
		this.reportId=reportId;
		this.condition = condition;
		this.ufmaster=upf;
	}

	
	
	public void run() {
		try {
			this.setData();
			String exportType=(String)this.condition.get("EXPORT_TYPE");
			//JasperReport jasperReport = JasperManager.loadReport(this.getMainRep());
            JasperReport jasperReport = (JasperReport)JRLoader.loadObject(this.getMainRep());
			JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, this.condition, dataSource);
			if(exportType.equals("EXPORT_PDF"))
				JasperExportManager.exportReportToPdfFile(jasperPrint, this.getExportPath());
			else if(exportType.equals("EXPORT_HTML"))
				JasperExportManager.exportReportToHtmlFile(jasperPrint, this.getExportPath());
			else if(exportType.equals("EXPORT_XML"))
				JasperExportManager.exportReportToXmlFile(jasperPrint, this.getExportPath(),false);
			else if (exportType.equals("EXPORT_CSV_COM")) {
				JRExporter exporter = new JRCsvExporter();
				exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"TIS-620");// UTF-8,UTF-16 ö export 
				exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
				OutputStream writer =new FileOutputStream(this.getExportPath());
				exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,writer);
				exporter.exportReport();
			} else if (exportType.equals("EXPORT_XLS")) {
				JRExporter exporter = new JRXlsExporter();
				exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
				FileOutputStream fos = new FileOutputStream(this.getExportPath());
				exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,fos);
				exporter.exportReport();
			}else if (exportType.equals("EXPORT_TXT")) {
				JRExporter exporter = new JRTextExporter();
				exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
				exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH,jasperPrint.getPageWidth());
				exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT,jasperPrint.getPageHeight());
				FileOutputStream fos = new FileOutputStream(this.getExportPath());
				exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,fos);
				exporter.exportReport();
			}else if (exportType.equals("EXPORT_RTF")) {
				JRRtfExporter exporter = new JRRtfExporter();
				exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
				FileOutputStream fos = new FileOutputStream(this.getExportPath());
				exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,fos);
				exporter.exportReport();
			}else if (exportType.equals("EXPORT_ODT")) {
				JROdtExporter exporter = new JROdtExporter();
	            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	            FileOutputStream fos = new FileOutputStream(this.getExportPath());
	            exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fos);
//	            exporter.setParameter(JRExporterParameter.OFFSET_X, new Integer(0));
//	            exporter.setParameter(JRExporterParameter.OFFSET_Y, new Integer(0));
	            exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
	            exporter.exportReport();
			}
		} catch (Throwable t) {
			MyLog.error(this, this.ufmaster, t.toString());
		}finally{
			this.condition.clear();
		}
	}

	
	private String getExportPath(){
		String exportPath=env.getValue("export-dir");
		String appName = ufmaster.get("appname");
//		if (appName != null && !appName.equals("")) {
//			exportPath +="/"+ appName + "/Report/"
//					+ this.reportId+"."+this.condition.get("FILE_TYPE");
//		} else {
			exportPath+="/REPORT/"+this.reportId+"."+this.condition.get("FILE_TYPE");
//		}
		
		return exportPath;
	}
	

	private void setData() {
		System.out.println("PRINTLN CHECK******************************");
		//this.setCondition();
		this.initReport();
		this.condition.put("REPORTOBJ", this.myrep);
		dataSource = new JRResultSetDataSource(myrep);
		templateDir = this.getMainRep();
		MyLog.info(this, this.ufmaster,"----------------- Template " + templateDir);
		try {
			MyLog.info(this, this.ufmaster,"----------------- Row " + this.myrep.getRow());
		} catch (SQLException ex) {
			MyLog.error(this, this.ufmaster, ex);
		}
	}

	private void initReport() {
		try {
			myrep = new ReportResultSet();
			System.out.println("CONDITION " + condition);
			myrep.process(condition);
		} catch (Exception e) {
			MyLog.error(this, this.ufmaster, e);
		}
	}

	public String getMainRep() {
		this.initEnv();
		String mainrep = "";
		String template = (String) this.condition.get("TEMPLATE");
		if (template != null){
			template.toLowerCase();
			if(template.indexOf("$")!=-1)
				template = template.substring(0, template.indexOf("$"));
		}	
		try {
			int row = this.myrep.getRow();
			if (row > 0) {
				String pathName = "";
				String appName = ufmaster.get("appname");
				if (appName != null && !appName.equals("")) {
					mainrep = env.getValue("report-template") + appName + "/"
							+ template;
				} else {
					mainrep = env.getValue("report-template") + template;
				}
			} else{
				mainrep = env.getValue("report-template")+ env.getValue("report-btemplate");
			}	
		} catch (Exception e) {
			MyLog.error(this, this.ufmaster, e);
		}
		return mainrep;
	}

	private void initEnv() {
		env = new InitialEnvironment("GLOBAL");
	}	
	
}