All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ibizsys.central.plugin.jr.dataentity.print.JRDEPrintRuntime Maven / Gradle / Ivy

There is a newer version: 8.1.0.559
Show newest version
package net.ibizsys.central.plugin.jr.dataentity.print;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import net.ibizsys.central.dataentity.print.DEPrintRuntime;
import net.ibizsys.central.plugin.jr.util.IJasperReportRuntime;
import net.ibizsys.central.plugin.jr.util.JasperReportUtils;
import net.ibizsys.model.dataentity.defield.IPSDEField;
import net.ibizsys.runtime.dataentity.DataEntityRuntimeException;
import net.ibizsys.runtime.dataentity.IDataEntityRuntime;
import net.ibizsys.runtime.dataentity.print.DEPrintRuntimeContext;
import net.ibizsys.runtime.util.Conditions;
import net.ibizsys.runtime.util.IEntityBase;
import net.ibizsys.runtime.util.ISearchContextBase;
import net.ibizsys.runtime.util.model.PSDataEntityUtils;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

/**
 * 实体 JasperReport 打印运行时对象
 * @author lionlau
 *
 */
public class JRDEPrintRuntime extends DEPrintRuntime implements IJasperReportRuntime {

	private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(JRDEPrintRuntime.class);
	
	private JasperReport jasperReport = null;
	
	protected JasperReport getJasperReport() {
		return this.jasperReport;
	}
	
	protected void setJasperReport(JasperReport jasperReport){
		this.jasperReport = jasperReport;
	}

	@Override
	protected void onOutput(OutputStream outputStream, IEntityBase[] list,  String strType) throws Throwable {
		
		if(this.jasperReport == null) {
			String strReportModel = this.getReportModel();
			if(StringUtils.hasLength(strReportModel)) {
				 JasperReport temp = JasperCompileManager.compileReport(new ByteArrayInputStream(strReportModel.getBytes("UTF-8")));
				 if( this.jasperReport == null) {
					 this.jasperReport = temp;
				 }
			}
			else
				if(StringUtils.hasLength(this.getReportFilePath())) {
					String strFilePath = this.getSystemRuntime().getReportFolder();
					if(StringUtils.hasLength(strFilePath)) {
						strFilePath += File.separator;
						strFilePath += this.getReportFilePath();
					}
					else {
						strFilePath = this.getReportFilePath();
					}
					File file = new File(strFilePath);
					if(!file.exists()) {
						throw new DataEntityRuntimeException(this.getDataEntityRuntimeBase(), String.format("打印[%1$s]指定路径[%2$s]不存在",this.getName(),this.getReportFilePath()));
					}
					
					 JasperReport temp = JasperCompileManager.compileReport(new FileInputStream(file));
					 if( this.jasperReport == null) {
						 this.jasperReport = temp;
					 }
				}
				else
					throw new DataEntityRuntimeException(this.getDataEntityRuntimeBase(), String.format("打印[%1$s]没有定义打印模型",this.getName()));
		}
		
		IPSDEField pickupPSDEField = null;
		if(this.getPSDEPrint().getDetailPSDE() != null && this.getPSDEPrint().getDetailPSDEDataSet()!=null) {
			pickupPSDEField = PSDataEntityUtils.getPickupPSDEField(this.getPSDEPrint().getDetailPSDE(), this.getDataEntityRuntimeBase().getPSDataEntity());
		}
		
		List jasperPrintList = new ArrayList();
		
		for(IEntityBase iEntityBase: list) {
			Map params = new HashMap();
			
			DEPrintRuntimeContext dePrintRuntimeContext = new DEPrintRuntimeContext();
			dePrintRuntimeContext.setUserContext(this.getUserContext());
			dePrintRuntimeContext.setDataEntityRuntime(this.getDataEntityRuntime());
			dePrintRuntimeContext.setEntityBase(iEntityBase);
			params.put(IJasperReportRuntime.PARAM_PRINTCONTEXT, dePrintRuntimeContext);
			
			fillParamMap(params);
			
			//进一步查询
			if(this.getPSDEPrint().getDetailPSDE() != null && this.getPSDEPrint().getDetailPSDEDataSet()!=null) {
				
				IDataEntityRuntime detailDataEntityRuntime = this.getSystemRuntime().getDataEntityRuntime(this.getPSDEPrint().getDetailPSDE().getId());
				ISearchContextBase iSearchContextBase = detailDataEntityRuntime.createSearchContext();
				//当前数据对象主键作为上下文参数传入
				Object objKey = this.getDataEntityRuntime().getFieldValue(iEntityBase, this.getDataEntityRuntime().getKeyPSDEField());
				detailDataEntityRuntime.setSearchDataContext(iSearchContextBase, this.getDataEntityRuntime().getKeyPSDEField().getName().toLowerCase(), objKey);
				
				//判断是否有连接到当前实体的属性,如有放入 等于 条件
				if(pickupPSDEField != null) {
					detailDataEntityRuntime.setSearchCondition(iSearchContextBase, pickupPSDEField, Conditions.EQ, objKey);
				}
			}
			
			//
			JasperPrint jprint1 = JasperFillManager.fillReport(this.jasperReport, params, new JREmptyDataSource());
			jasperPrintList.add(jprint1);
		}
		
		
		JasperReportUtils.output(outputStream, jasperPrintList, strType);
		
	}
	
	protected void fillParamMap(Map params) {
		
	}
	


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy