/*
 * Created on Jul 28, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package jreport.uds.javabean;

import java.util.Properties;

import jreport.uds.javabean.exception.RptDataInitializerException;
import jreport.uds.javabean.api.RptDataInitializer;
import jreport.uds.javabean.beans.PutData;

/**
 * @author jennyw
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SubRptCollectionDataInitializer implements RptDataInitializer {
	protected String runtimekey =  null;
	/* (non-Javadoc)
	 * @see jreport.uds.javabean.RptDataInitializer#getDataCollection(java.util.Properties)
	 */
	
	public Object getDataCollection(Properties props)
			throws RptDataInitializerException {
		
		PutData putdata = new PutData();
		putdata.initPersonData();			
		
		runtimekey =  (String)props.get("JavaBeanDS_RuntimeDataID");
//		System.out.println("getDataCollection>>>"+this);
		if( runtimekey!=null && runtimekey.trim().length()>0){
			if(DataCenter.containsData(runtimekey)){
				try {
//					System.out.println("lock data>>>"+runtimekey+","+this);
					return DataCenter.lockData(runtimekey, this);
				} catch (DataCenterException e) {
					throw new RptDataInitializerException(e, "Subreport Data Initializer cannot LOCK data by the key:"+runtimekey+e.getMessage());
				}
			}else{
				throw new RptDataInitializerException(null, "Subreport Data Initializer cannot FIND data object from Data Center by the key:"+runtimekey);
			}
		}else{
			
			RptDataInitializerException e = new RptDataInitializerException(null, "Subreport Data Initializer get NULL runtime data ID. Please make sure you pass the property vlaue for runtime data id.");
			e.printStackTrace();
			throw e;
		}
	
	}

	/* (non-Javadoc)
	 * @see jreport.uds.javabean.RptDataInitializer#close()
	 */
	public void close() {
//		System.out.println("close sub-report");
		DataCenter.unlockData(runtimekey, this);
		UDSForJavaBeanLogger.debug("Number of Datas in center:"+DataCenter.size());
//		DataCenter.dump(System.out);
	}

}
