using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Bezstanowa logika biznesowa hermetyzujca 
/// moe zosta zrealizowana za pomoc obiektu Customer.
/// Wszystkie metody s statyczne.
/// </summary>
public static class CustomerBusinessLogic
{
   public static ICollection GetAllCustomers()
   {
      ArrayList allCustomers = new ArrayList();

      string connectionString =
      "Data Source=Brahams;Initial Catalog=Northwind;Integrated Security=True";

      string selectCommand = "Select CustomerID from Customers";

      SqlDataSource dataSource =
         new SqlDataSource(connectionString, selectCommand);

      try
      {
         // Polecenie select bez argumentw.
         IEnumerable CustomerIDs =
            dataSource.Select(DataSourceSelectArguments.Empty);

         IEnumerator enumerator = CustomerIDs.GetEnumerator();
         while (enumerator.MoveNext())
         {
            DataRowView drv = enumerator.Current as DataRowView;
            if (drv != null)
            {
               string customerID = drv["CustomerID"].ToString();
               NorthWindCustomer cust = new NorthWindCustomer(customerID);
               allCustomers.Add(cust);
            } // Koniec, jeeli otrzymamy wartoci niezerowe.
         } // Koniec ptli while (wyliczenia).
      } // Koniec bloku try.
      finally
      {
         dataSource.Dispose();
      }
      return allCustomers;
   }

   public static void UpdateCustomerInformation(NorthWindCustomer customer)
   {
      bool returnValue = customer.Save();
      if (returnValue == false)
      {
         throw new ApplicationException("Brak moliwoci uaktualnienia klienta.");
      }
   }

   public static NorthWindCustomer GetCustomer(string custID)
   {
      return new NorthWindCustomer(custID);
   }
}
