package com.independenthealth.edwdma.crosswalks.actions;

import java.util.List;
import java.util.HashMap;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectstyle.cayenne.access.DataContext;
import org.objectstyle.cayenne.conf.BasicServletConfiguration;
import org.objectstyle.cayenne.query.SelectQuery;


import com.independenthealth.edwdma.util.Constants;
import com.independenthealth.edwdma.util.ErrorHandling;
import com.independenthealth.edwdma.crosswalks.Crosswalkable;
import com.independenthealth.edwdma.crosswalks.beans.ComplexList;
import com.independenthealth.edwdma.crosswalks.beans.views.DispCwRecView;
import com.independenthealth.edwdma.crosswalks.util.CwUtils;
//import crosswalks.beans.PagedObjectList;
//import crosswalks.beans.ComplexList;
/**
 * Action class to retrieve crosswalk records of a 
 * given crosswalk table
 * 
 * 
 * 
 */
public class GetCwRecords extends Action
{

	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws Exception
	{

		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward();
		

		String objectName = request.getParameter(Constants.CW_REQ_PARAM_OBJECT_NAME);
		String tableDesc = request.getParameter(Constants.CW_REQ_PARAM_TABLE_DESC);
		//int pageSize = Integer.parseInt(request.getParameter(Constants.CW_REQ_PARAM_PAGE_SIZE));
		String currentRow = request.getParameter(Constants.CW_REQ_PARAM_CURRENT_ROW);

		if (objectName != null)
		{
			try
			{
				// get session DataContext to perform database transactions
				DataContext context =
					BasicServletConfiguration.getDefaultContext(request.getSession());

				Class cwClass = CwUtils.getClassObject(objectName);
				Crosswalkable cwObject = CwUtils.getCwObject(cwClass);

				// Set the Crosswalk table query and orderings
				SelectQuery cwDataQuery = new SelectQuery(cwClass);
				cwDataQuery.addOrderings(cwObject.getViewOrderings());
				//cwDataQuery.setFetchLimit(100);
				cwDataQuery.setPageSize(25);

				// Perform the query
				List resultList = context.performQuery(cwDataQuery);
				
				

							

				// Create the Display object to forward to the view
				Vector viewProps = cwObject.getViewProperties();
				DispCwRecView cwViewBean =
					new DispCwRecView(
						tableDesc,
						objectName,
						viewProps,
						Crosswalkable.viewDatePattern,resultList,50,1
						
						);

				// If there are results, store them in the Display object
				if (!resultList.isEmpty())
				{
					cwViewBean.setCwTableRecords(resultList);
					
					cwViewBean.getSubSetList();
					
					
					// adding paging fuctionality
					Boolean hasPrev = new Boolean(cwViewBean.hasPreviousSet());
					request.setAttribute("hasPrevious", hasPrev);
					
					HashMap m1 = new HashMap();
					m1.put("curRow", new Integer(cwViewBean.getPreviousSet()));
					request.setAttribute("prevSet", m1);
					
					// Next ResultSet
					Boolean hasNext = new Boolean(cwViewBean.hasNextSet());
					request.setAttribute("hasNext", hasNext);
					
					HashMap m2 = new HashMap();
					m2.put("curRow", new Integer(cwViewBean.getNextSet()));
					request.setAttribute("nextSet", m2);
				  
					String curRow = (String)request.getParameter("nextSet");
					//String curRow = (String)request.getParameter("nextSet");

					// Remember the current row.
					request.setAttribute("curRow", curRow == null ? "0" : curRow);
					
					// adding paging functionality
				}

				// Store the Display object into the request object
				request.setAttribute(Constants.ATTRIB_DISPLAY_RESULT, cwViewBean);

			}

			catch (Exception e)
			{
				errors = CwUtils.processException(errors, e, tableDesc, objectName);
			}
		}

		else // no crosswalk table defined to retrieve
			{
			errors = CwUtils.processError(errors, Constants.CW_ERROR_OBJ_NOT_DEFINED);
		}

		if (!errors.isEmpty())
		{
			saveErrors(request, errors);
			forward = mapping.findForward(Constants.FAILURE);
		}
		else
		{
			forward = mapping.findForward(Constants.SUCCESS);
		}

		// Finish with
		return (forward);

	}
}
