package com.independenthealth.edwdma.crosswalks.beans.views;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.independenthealth.edwdma.util.Constants;

// added paging functionality

import com.independenthealth.edwdma.crosswalks.beans.PagedObjectList;
import java.util.List;
import java.util.ArrayList;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

// paigng functionality

/**
 * A view bean that represents information to be displayed when viewing
 * crosswalk records of a crosswalk table.  This bean is ultimately
 * transferred to a JSP page for view.
 * 
 * @version 1.0
 * 
 */
public class DispCwRecView 
{
	private String objectName = null;
	private String tableDesc = null;

	private Map cwTableParamMap = new HashMap();
	private List cwTableRecords = null;
	private String viewDatePattern = null;
	private Vector viewProps = null;

	// Default colspans
	private int colSpan = 2;
	private int leftColSpan = 1;
	private int rightColSpan = 1;
	
	
	//adding paging func.
	//private List pList=null;
	//private int pPageSize=0;
	//private int pCurrentRow;
	
//	Logger
   protected Logger log = LogManager.getLogger(getClass());
  
   // mList  object
   private List        mList;
  
   //  (pagesize)  objectmList
   private int         mPageSize;
  
   //  resultset 
   private int           mCurrentRow;
	// adding paging functionality
	
	

	/**
	 * Constructs the View Crosswalk Record view bean object
	 * @param tableDesc			The crosswalk table description
	 * @param objectName		The crosswalk table object name
	 * @param viewProps			A collection of FieldProperty objects representing
	 * 							the fields that are viewable for the crosswalk record
	 * 							being expired
	 * @param viewDatePattern	Date pattern to format date fields when viewing
	 */
	
//	added paging functionality
	
	
	
	 // paging functionlity
	
//	added paging functionality
	
	 
	
	 // paging functionlity
	
	
	public DispCwRecView(
		String tableDesc,
		String objectName,
		Vector viewProps,
		String viewDatePattern,
		List mList,
		int mPageSize,
		int mCurrentRow
		)
	{
		this.tableDesc = tableDesc;
		this.objectName = objectName;
		this.viewProps = viewProps;
		this.viewDatePattern = viewDatePattern;
		this.mList = mList;
		this.mPageSize = mPageSize;
		this.mCurrentRow = mCurrentRow;

		// calculate and set the colspans
		setColSpans();
		
					

		cwTableParamMap.put(Constants.CW_REQ_PARAM_OBJECT_NAME, objectName);
		cwTableParamMap.put(Constants.CW_REQ_PARAM_TABLE_DESC, tableDesc);
		
		// adding paging functionality
		//cwTableParamMap.put(Constants.CW_REQ_PARAM_PAGE_SIZE, new Integer(mPageSize));
		//cwTableParamMap.put(Constants.CW_REQ_PARAM_TABLE_DESC, new Integer(mCurrentRow));
		// adding paging functionality
	}


	
	/**
	 * Returns the total column span
	 * @return		total column span
	 */
	public int getColSpan()
	{
		return colSpan;
	}

	/**
	 * Returns a map of the crosswalk table parameters
	 * @return		Map of the crosswalk table parameters
	 */
	public Map getCwTableParamMap()
	{
		return cwTableParamMap;
	}

	/**
	 * Returns a List of crosswalk record objects of the crosswalk table
	 * @return		List of crosswalk record objects
	 */
	public List getCwTableRecords()
	{
		return cwTableRecords;
	}

	/**
	 * Returns the left column span
	 * @return		left column span
	 */
	public int getLeftColSpan()
	{
		return leftColSpan;
	}

	/**
	 * Returns the crosswalk table object name
	 * @return		crosswalk table object name
	 */
	public String getObjectName()
	{
		return objectName;
	}

	/**
	 * Returns the right column span
	 * @return		right column span
	 */
	public int getRightColSpan()
	{
		return rightColSpan;
	}

	/**
	 * Returns the crosswalk table description
	 * @return		Crosswalk table description
	 */
	public String getTableDesc()
	{
		return tableDesc;
	}

	/**
	 * Returns the date pattern to format dates when viewing date fields
	 * @return		Date pattern to format dates
	 */
	public String getViewDatePattern()
	{
		return viewDatePattern;
	}

	/**
	 * Returns a collection of FieldProperty objects representing
	 * the fields that are viewable for the crosswalk record
	 * being expired
	 * @return		Collection of FieldProperty objects
	 */
	public Vector getViewProps()
	{
		return viewProps;
	}

	/**
	 * Calculates and set the column spans of the view page based upon the
	 * fields that are viewable for the record being expired
	 */
	private void setColSpans()
	{
		// Calculate column spans
		if (viewProps != null && viewProps.size() >= 2)
		{
			// colspan = vector size + one column for Actions coluumn
			colSpan = viewProps.size() + 1;

			rightColSpan = (new Double(Math.floor(colSpan / 2))).intValue();
			leftColSpan = colSpan - rightColSpan;
		}

	}

	/**
	 * Sets the crosswalk table parameter map
	 * @param map		The crosswalk table parameter map
	 */
	public void setCwTableParamMap(Map map)
	{
		cwTableParamMap = map;
	}

	/**
		 * Sets the List of crosswalk record objects of the crosswalk table
	 * @param vector	List of crosswalk record objects
	 */
	public void setCwTableRecords(List results)
	{
		this.cwTableRecords = results;
	}

	/**
	 * Sets the crosswalk table object name
	 * @param objectName		The crosswalk table object name
	 */
	public void setObjectName(String objectName)
	{
		this.objectName = objectName;
	}

	/**
	 * Sets the crosswalk table description
	 * @param tableDesc		The crosswalk table description
	 */
	public void setTableDesc(String tableDesc)
	{
		this.tableDesc = tableDesc;
	}

	/**
	 * Sets the date pattern to format dates when viewing date fields
	 * @param string		Date pattern to format dates
	 */
	public void setViewDatePattern(String string)
	{
		viewDatePattern = string;
	}

	/**
	 * Sets the collection of FieldProperty objects representing
	 * the fields that are viewable for the crosswalk record
	 * being expired
	 * @param viewProps		Collection of FieldProperty objects
	 */
	public void setViewProps(Vector displayProps)
	{
		this.viewProps = displayProps;
		setColSpans();
	}









// adding paging functionality 

   /**
	  * Returns the logger instance of this class
	  * 
	  * @return Logger
	  */
	 protected Logger log ()
	 {
	   return log;
	 } //log()
  
	 /**
	  * Returns the List interface of this class
	  * 
	  * @return List a limited Collection of Objects
	  */
	 protected List getList ()
	 {
	   
	   //mList = cwTableRecords;
	   return mList;
	 } //getList()

	 /**
	  * get Sub set of list
	  */
	 public ArrayList getSubSetList()
	 {
	   List lst = null;
	   if (mCurrentRow + mPageSize > mList.size())
	   {
		 lst = mList.subList(mCurrentRow, mList.size());
	   } //if
	   else
	   {
		 lst = mList.subList(mCurrentRow, mCurrentRow + mPageSize);
	   } //else
	   return new ArrayList(lst);
	 } //getSubSetList()
    
	 /**
	  * Get  total length of mList.
	  */
	 public Integer getMaxSize()
	 {
	   return new Integer (mList.size()); 
	 } //getMaxFetchSize()
  
	 /**
	  * Get position in the recordset.
	  */
	 public Integer getCurrentRow ()
	 {
	   return new Integer(mCurrentRow);
	 } //getCurrentRow()
  
	 /**
	  * Get true resultset 
	  * 
	  */
	 public boolean hasNextSet()
	 {
	   return (mList.size() > mCurrentRow + mPageSize) ? true : false;
	 } //getHasNextSet()
  
	 /**
	  * Get true  resultset the mList 
	  * 
	  */
	 public boolean hasPreviousSet()
	 {
	   return (mCurrentRow > 0 ) ? true : false;
	 } //getHasPreviousSet()
 
	 /**
	  * 
	  * 
	  * 
	  */
	 public int getNextSet ()
	 {
	   if ( mList == null ) return 0;

	   int result = mCurrentRow + mPageSize;
	   if (result < mList.size())
	   {
		 return result;
	   } //if
	   else
	   {
		 return mList.size();
	   } //else
	 }  //getNextSet()
    
	 /**
	  * 
	  * get Previous record set
	  */
	 public int getPreviousSet()
	 {
	   if (mList == null) return 0;
    
	   int result = mCurrentRow - mPageSize;
    
	   if (result > 0)
	   {
		 return result;    
	   }
	   else
	   {
		 return 0;
	   } 
	 } //getPrevious





// adding paging functionality










}
