/// <summary>
 /// Kiểm tra và thêm mới Position
 /// </summary>
 /// <param name="entity">Entity</param>
 /// <returns>Int32: ID của Position Mới Thêm Vào</returns>
 public static Int32 Add(PositionEntity entity)
 {
     checkLogic(entity);
     checkDuplicate(entity, true);
     checkFK(entity);
     return PositionDAL.Add(entity);
 }
 /// <summary>
 /// Kiểm tra và chỉnh sửa Position
 /// </summary>
 /// <param name="entity">PositionEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Edit(PositionEntity entity)
 {
     checkExist(entity);
     checkLogic(entity);
     checkDuplicate(entity, false);
     checkFK(entity);
     return PositionDAL.Edit(entity);
 }
 protected string getPositionName(Object x)
 {
     int iPositionID = int.Parse(x.ToString());
     String sPositionName = string.Empty;
     PositionEntity oPos = new PositionEntity();
     oPos = PositionBRL.GetOne(iPositionID);
     if (oPos != null)
         sPositionName = oPos.sName;
     return sPositionName;
 }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        if (btnOK.Text.CompareTo(Resources.language.them) == 0)
            {
                txtPosName.Enabled = true;
                txtPosName.Text = "";
                btnOK.Text = Resources.language.luu;
                btnOK.CommandName = "Add";

            }
            else
            {
                Page.Validate("vgPosition");
                if (Page.IsValid)
                {
                    int result = 0;
                try
                {
                    PositionEntity oPos = new PositionEntity();
                    oPos.sName = txtPosName.Text;
                    oPos.iPositionID = Convert.ToInt32(ddlPositionID.SelectedValue);
                    if (btnOK.CommandName=="Edit")
                    {

                        PositionBRL.Edit(oPos);
                        lblThongbao.Text = Resources.language.capnhapthanhcong;
                        txtPosName.Enabled = false;

                    }
                    else
                        result=PositionBRL.Add(oPos);
                    Response.Redirect(Request.Url.ToString());
                }
                catch (Exception ex)
                {
                    lblThongbao.Text = ex.Message;
                }
                if (result > 0) lblThongbao.Text = Resources.language.dathemmoivitriquangcao;
            }
        }
    }
    protected void lbtnDelete_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {

                foreach (GridViewRow row in grvPosition.Rows)
                {
                    CheckBox chkDelete = row.FindControl("chkDelete") as CheckBox;
                    int positionID = Convert.ToInt32(grvPosition.DataKeys[row.RowIndex].Values["iPositionID"]);
                    if (chkDelete != null && chkDelete.Checked)
                    {
                        PositionEntity oPos = new PositionEntity();
                        oPos.iPositionID = positionID;
                        PositionBRL.Remove(oPos);
                    }
                }
                //Nap lai du lieu
                Response.Redirect(Request.Url.ToString());
            }
            catch (Exception ex)
            {
                lblThongbao.Text = ex.Message;
            }
        }
    }
        /// <summary>
        /// Kiểm tra và xoá Position
        /// </summary>
        /// <param name="entity">PositionEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(PositionEntity entity)
        {
            checkExist(entity);

            return PositionDAL.Remove(entity.iPositionID);
        }
 /// <summary>
 /// Kiểm tra logic Entity
 /// </summary>
 /// <param name="entity">PositionEntity: entity</param>
 private static void checkLogic(PositionEntity entity)
 {
     if (String.IsNullOrEmpty(entity.sName))
         throw new Exception(EX_SNAME_EMPTY);
 }
 /// <summary>
 /// Kiểm tra tồn tại khóa ngoại
 /// </summary>
 /// <param name="entity">PositionEntity:entity</param>
 private static void checkFK(PositionEntity entity)
 {
     PositionEntity oPosition = PositionDAL.GetOne(entity.iPositionID);
     if (oPosition == null)
     {
         throw new Exception(EX_IDPOSITION_NOTFOUND);
     }
 }
 private static void checkExist(PositionEntity entity)
 {
     PositionEntity oPosition=PositionDAL.GetOne(entity.iPositionID);
     if(oPosition==null)
         throw new Exception(EX_NOT_EXIST);
 }
 /// <summary>
 /// Kiểm tra trùng lặp bản ghi
 /// </summary>
 /// <param name="entity">PositionEntity: PositionEntity</param>
 private static void checkDuplicate(PositionEntity entity,bool CheckInsert)
 {
     List<PositionEntity> list = PositionDAL.GetAll();
     if (list.Exists(
         delegate(PositionEntity oldEntity)
         {
             bool result =oldEntity.sName.Equals(entity.sName, StringComparison.OrdinalIgnoreCase);
             if(!CheckInsert)
                 result=result && oldEntity.iPositionID != entity.iPositionID;
             return result;
         }
     ))
     {
         list.Clear();
         throw new Exception("Vị trí này đã tồn tại");
     }
 }