using System;
using System.Data;
using System.Configuration;
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;

using Westwind.BusinessObjects;


namespace ScriptCallbacks.BusinessObjects
{

   

    /// <summary>
    /// Summary description for busCustomer
    /// </summary>
    public class busCustomer : wwBusiness<CustomerEntity>
    {
        public busCustomer()
        {
            this.ConnectionString = ConfigurationManager.ConnectionStrings["AjaxAspNet"].ConnectionString; 
            this.ConnectType = ServerTypes.SqlServer;
            this.DataRowUpdateMode = DataRowUpdateModes.OptimizedByPk;
            this.Tablename = "Customers";
            this.PkField = "CustomerId";
            this.PkType = PkFieldTypes.stringType;
        }

        /// <summary>
        /// Retrieves a list of all customers into a TCustomerList table
        /// </summary>
        /// <returns></returns>
        public int GetCustomerList()
        {
            return this.Execute("select CustomerId,CompanyName from " + this.Tablename + " Order by CompanyName", "TCustomerList");
        }

        public override bool Validate()
        {
            if (!base.Validate())
                return false;

            CustomersRow Cust = this.Entity;

            this.ValidationErrors.Clear();

            if (Cust.Companyname == null || Cust.Companyname == string.Empty)
                this.ValidationErrors.Add("Company cannot be left blank");

            if (Cust.Contactname == null || Cust.Contactname == string.Empty)
                this.ValidationErrors.Add("Name cannot be left blank");

            if (this.ValidationErrors.Count > 0)
                return false;

            return true;
        }
    }
}