/*
 * ReportDataAccess.java
 *
 * Created on 8 de octubre de 2004, 06:43 PM
 */

package com.fenalco.entities;

import java.sql.Date;
import java.sql.Time;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import com.fenalco.utilities.database.QueryException;
import com.fenalco.utilities.database.SQLUtilities;

/**
 * 
 * @author admin
 */
public class ReportDataAccess
{

	/** Creates a new instance of ReportDataAccess */
	public ReportDataAccess()
	{
	}

	public static OrderList storeOrders(OrderList orderList, int storeId)
	{
		OrderList list = null;
		Vector orders = new Vector();
		Enumeration e = orderList.getOrders().elements();
		while (e.hasMoreElements())
		{
			Order order = (Order) e.nextElement();
			if (order.getStoreId() == storeId)
			{
				orders.add(order);
			}
		}

		list.setOrders(orders);
		return list;
	}

	public static OrderList communityOrders(OrderList orderList, int communityId)
	{
		OrderList list = null;
		Vector orders = new Vector();

		String sql = "SELECT esta_id FROM comunidad_establecimiento WHERE comu_id="
				+ communityId + ";";
		Hashtable aux = new Hashtable();
		try
		{
			Vector result = SQLUtilities.query(sql);

			Enumeration e = orderList.getOrders().elements();
			while (e.hasMoreElements())
			{
				Order order = (Order) e.nextElement();
				int storeId = order.getStoreId();
				for (int i = 0; i < result.size(); i++)
				{
					if (((Integer) aux.get("esta_id")).intValue() == storeId)
					{
						orders.add(order);
						break;
					}
				}
			}

			list.setOrders(orders);
		} catch (QueryException qe)
		{
			System.out.println(qe);
			return list;
		}

		return list;
	}

	public static OrderList zoneOrders(OrderList orderList, int zoneId)
	{
		OrderList zoneOrders = null;
		Vector orders = new Vector();
		Enumeration e = orderList.getOrders().elements();
		Vector nIds = ReportDataAccess.zoneNeighbourhoodsIds(zoneId);
		int nSize = nIds.size();
		boolean belongToZone;

		while (e.hasMoreElements())
		{
			belongToZone = false;
			Order order = (Order) e.nextElement();
			Store store = new Store(order.getStoreId());
			int i = 0;

			while (i < nSize)
			{

				if (store.getNeighbourhood() == ((Integer) nIds.elementAt(i))
						.intValue())
				{
					belongToZone = true;
					break;
				}

				i++;
			}

			if (belongToZone)
			{
				orders.addElement(order);
			}

		}

		zoneOrders.setOrders(orders);
		return zoneOrders;
	}

	/* :x */
	public static Vector zoneNeighbourhoodsIds(int zoneId)
	{
		Vector ids = new Vector();
		String sql = "SELECT barr_id FROM barrio WHERE zona_id=" + zoneId + ";";

		try
		{

			Vector result = SQLUtilities.query(sql);
			int size = result.size();

			if (size > 0)
			{

				for (int i = 0; i < size; i++)
				{
					Hashtable aux = (Hashtable) result.elementAt(i);
					ids.addElement(aux.get("barr_id"));
				}

			}

		} catch (QueryException qe)
		{

			System.out.println("Query Exception " + qe);

		}

		return ids;
	}

	public static OrderList categoryOrders(OrderList orderList, int categoryId)
	{
		OrderList list = null;
		/*
		 * Vector orders = new Vector();
		 * 
		 * String sql="SELECT prod_prov_id FROM producto_proveedor WHERE
		 * cate_prod_codigo="+categoryId+";"; Hashtable aux = new Hashtable();
		 * try{ Vector result = SQLUtilities.query(sql);
		 * 
		 * Enumeration e = orderList.getOrders().elements();
		 * while(e.hasMoreElements()){ Order order = (Order)e.nextElement(); int
		 * storeId = order.getStoreId(); for(int i=0;i <result.size();i++){
		 * if(((Integer)aux.get("esta_id")).intValue()==storeId){
		 * orders.add(order); break; } } }
		 * 
		 * list.setOrders(orders); }catch(QueryException qe){
		 * System.out.println(qe); return list; }
		 */

		return list;
	}

	public static OrderList filterOrdersByTransaction(OrderList orderList,
			int transactionType)
	{
		OrderList list = null;
		Vector orders = new Vector();
		Enumeration e = orderList.getOrders().elements();

		while (e.hasMoreElements())
		{
			Order order = (Order) e.nextElement();
			if (transactionType == 1)
			{
				if (order.getOrderStatus() == 1 || order.getOrderStatus() == 3)
				{
					orders.addElement(order);
				}
			} else
			{
				if (order.getOrderStatus() == 4)
				{
					orders.addElement(order);
				}
			}
		}

		list.setOrders(orders);
		return list;
	}

	/**
	 * Hora promedio en que se realizan mas pedidos. **Opcional
	 */
	public static Time orderSubmitAverage(Date startDate, Date endDate,
			int transactionType)
	{
		Time avg = new Time(0);
		return avg;
	}

	/**
	 * Listado de los accesos de un usuario al sistema. **Opcional
	 */
	public static Vector userAccess(String login, Date startDate, Date endDate)
	{
		Vector userAccess = new Vector();
		return userAccess;
	}

	public static Vector getShopKeeperStoresIds(int shopKeeperId)
	{
		Vector ids = new Vector();
		return ids;
	}

	public static Vector getSupplierCommunitiesIds(int supplierId)
	{
		Vector ids = new Vector();
		return ids;
	}

	public static Vector getCitiesZonesIds(int cityId)
	{
		Vector ids = new Vector();
		return ids;
	}
}