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

net.ibizsys.central.plugin.jr.util.EntitiesJRDataSource Maven / Gradle / Ivy

There is a newer version: 8.1.0.564.6
Show newest version
package net.ibizsys.central.plugin.jr.util;

import java.util.List;

import net.ibizsys.model.dataentity.defield.IPSDEField;
import net.ibizsys.runtime.dataentity.IDataEntityRuntime;
import net.ibizsys.runtime.util.IEntity;
import net.ibizsys.runtime.util.IEntityBase;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;




/**
 * 报表数据源对象 (数据对象列表)
 * 
 * @author Administrator
 *
 */
public class EntitiesJRDataSource implements JRDataSource {

	private List entityList = null;
	private int nCurIndex = -1;
	private int nColumnCount = -1;
	private int nMaxRowCount = -1;
	private IDataEntityRuntime iDataEntityRuntime = null;

	public EntitiesJRDataSource(List entityList) {
		this.entityList = entityList;
	}
	
	public EntitiesJRDataSource(List entityList, IDataEntityRuntime iDataEntityRuntime) {
		this.entityList = entityList;
		this.iDataEntityRuntime = iDataEntityRuntime;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.sf.jasperreports.engine.JRDataSource#getFieldValue(net.sf.jasperreports.engine.JRField)
	 */
	@Override
	public Object getFieldValue(JRField arg0) throws JRException {
		try {
			if (getColumnCount() > 0) {
				int nColumnIndex = 0;
				// 判断列名称是否有列表时
				String strFieldName = arg0.getName();
				if (strFieldName.indexOf("_SRF") == 0) {
					// 虚拟列
					strFieldName = strFieldName.substring(4);
					int nPos = strFieldName.indexOf("_");
					if (nPos != -1) {
						String strIndex = strFieldName.substring(0, nPos);
						strFieldName = strFieldName.substring(nPos + 1);
						nColumnIndex = Integer.parseInt(strIndex);
						nColumnIndex--;
					}
				}

				int nRealIndex = -1;
				if (getMaxRowCount() > 0) {
					nRealIndex = nColumnIndex * getMaxRowCount() + nCurIndex;
				} else {
					int nRowCount = this.entityList.size() / getColumnCount();
					if (this.entityList.size() % getColumnCount() != 0) {
						nRowCount += 1;
					}
					nRealIndex = nColumnIndex * nRowCount + nCurIndex;
				}

				if (nRealIndex >= this.entityList.size())
					return null;
				else
				{
					return getFieldValue(this.entityList.get(nRealIndex),strFieldName);
				}
			} else {
				return getFieldValue(this.entityList.get(nCurIndex),arg0.getName());
			}
		} catch (Exception e) {
			throw new JRException(e);
		}
	}
	
	/**
	 * 获取数据对象的指定属性值
	 * @param iEntityBase
	 * @param strName
	 * @return
	 * @throws Exception
	 */
	protected Object getFieldValue(IEntityBase iEntityBase, String strName) throws Exception{
		IPSDEField iPSDEField = null;
		if(this.getDataEntityRuntime()!=null) {
			iPSDEField = this.getDataEntityRuntime().getPSDEField(strName);
			
		}
		if(iPSDEField != null) {
			return this.getDataEntityRuntime().getFieldValue(iEntityBase, iPSDEField);
		}
		
		if(iEntityBase instanceof IEntity) {
			return ((IEntity)iEntityBase).get(strName);
		}
		
		
		throw new Exception(String.format("无法从数据对象[%1$s]获取指定属性",iEntityBase));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.sf.jasperreports.engine.JRDataSource#next()
	 */
	@Override
	public boolean next() throws JRException {
		if (this.entityList == null) return false;

		nCurIndex++;
		if (getColumnCount() > 0) {
			if (getMaxRowCount() > 0) {
				if (nCurIndex >= this.entityList.size()) return false;

				if (nCurIndex >= getMaxRowCount()) return false;

				return true;
			} else {
				// 自动计算行数
				int nRowCount = this.entityList.size() / getColumnCount();
				if (this.entityList.size() % getColumnCount() != 0) {
					nRowCount += 1;
				}

				if (nCurIndex >= nRowCount) return false;

				return true;
			}
		} else {
			if (nCurIndex >= this.entityList.size())
				return false;
			else
				return true;
		}
	}

	/**
	 * 获取数据的分区列显示数量
	 * 
	 * @return the nColumnCount
	 */
	public int getColumnCount() {
		return nColumnCount;
	}

	/**
	 * 获取数据的最大行记录数
	 * 
	 * @return the nMaxRowCount
	 */
	public int getMaxRowCount() {
		return nMaxRowCount;
	}

	/**
	 * 设置数据的分区列显示数量
	 * 
	 * @param nColumnCount the nColumnCount to set
	 */
	public void setColumnCount(int nColumnCount) {
		this.nColumnCount = nColumnCount;
	}

	/**
	 * 设置数据的最大行记录数
	 * 
	 * @param nMaxRowCount the nMaxRowCount to set
	 */
	public void setMaxRowCount(int nMaxRowCount) {
		this.nMaxRowCount = nMaxRowCount;
	}

	
	/**
	 * 获取当前相关的实体运行时对象
	 * @return
	 */
	public IDataEntityRuntime getDataEntityRuntime() {
		return this.iDataEntityRuntime;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy