/// <summary>
 /// Kiểm tra và thêm mới AdvCategory
 /// </summary>
 /// <param name="entity">Entity</param>
 /// <returns>Int32: ID của AdvCategory Mới Thêm Vào</returns>
 public static Int32 Add(AdvCategoryEntity entity)
 {
     checkLogic(entity);
     checkDuplicate(entity, false);
     checkFK(entity);
     return AdvCategoryDAL.Add(entity);
 }
 /// <summary>
 /// Kiểm tra và chỉnh sửa AdvCategory
 /// </summary>
 /// <param name="entity">AdvCategoryEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Edit(AdvCategoryEntity entity)
 {
     checkExist(entity.iAdvCategory);
     checkLogic(entity);
     checkDuplicate(entity, true);
     checkFK(entity);
     return AdvCategoryDAL.Edit(entity);
 }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        Page.Validate("vgQuangcao");
        if (Page.IsValid)
        {
            int Result = 0;
                try
                {
                    AdvEntity oAdv = new AdvEntity();
                    oAdv.sTitle = txtTitle.Text;
                    oAdv.sMedia = txtMedia.Text;
                    oAdv.sLink = txtLink.Text;
                    oAdv.sDesc = txtAdvDesc.Text;
                    oAdv.iOrder = Byte.Parse(txtOrder.Text);
                    oAdv.iType = Byte.Parse(cboType.SelectedValue);
                    oAdv.iWidth = Convert.ToInt16(txtWidth.Text);
                    oAdv.iHeight = Convert.ToInt16(txtHeight.Text);
                    oAdv.iPositionID = Convert.ToInt32(ddlPosition.SelectedValue);
                    if (btnOK.CommandName == "Edit")
                    {
                        oAdv.iAdvID = Convert.ToInt32(btnOK.CommandArgument);
                        bool resultEdit = AdvBRL.Edit(oAdv);
                        //Cập nhật trong AdvCategory
                        foreach (ListItem item in lstbNhomtin.Items)
                        {
                            try
                            {
                                int categoryID = Convert.ToInt32(item.Value);
                                if (!item.Selected)
                                {
                                    AdvCategoryBRL.RemoveByiCategoryID(categoryID);
                                }
                                else
                                {

                                    AdvCategoryEntity oAdvCat = new AdvCategoryEntity();
                                    oAdvCat.iAdvID = oAdv.iAdvID;
                                    oAdvCat.iCategoryID = categoryID;
                                    AdvCategoryBRL.Add(oAdvCat);
                                }
                            }
                            catch { }

                        }
                        if (resultEdit) lblThongbao.Text = Resources.language.capnhapthanhcong;

                    }
                    else
                    {
                        Result = AdvBRL.Add(oAdv);
                        //Thêm bản ghi vào tblAdvCategory
                        if (Result > 0)
                        {
                            foreach (ListItem item in lstbNhomtin.Items)
                            {
                                if (item.Selected)
                                {
                                    AdvCategoryEntity oAdvCat = new AdvCategoryEntity();
                                    oAdvCat.iAdvID = Result;
                                    oAdvCat.iCategoryID = Convert.ToInt32(item.Value);
                                    AdvCategoryBRL.Add(oAdvCat);
                                }
                            }
                        }
                    }

                    if (Result > 0) lblThongbao.Text = Resources.language.quangcaodaduocthietlap;
                    Response.Redirect(Request.Url.ToString());
                }
                catch (Exception ex)
                { lblThongbao.Text = ex.Message; }

        }
    }
 /// <summary>
 /// Kiểm tra logic Entity
 /// </summary>
 /// <param name="entity">AdvCategoryEntity: entity</param>
 private static void checkLogic(AdvCategoryEntity entity)
 {
     if (entity.iAdvID < 0)
         throw new Exception(EX_IADVID_INVALID);
     if (entity.iCategoryID < 0)
         throw new Exception(EX_ICATEGORYID_INVALID);
 }
 /// <summary>
 /// Kiểm tra tồn tại khóa ngoại
 /// </summary>
 /// <param name="entity">AdvCategoryEntity:entity</param>
 private static void checkFK(AdvCategoryEntity entity)
 {
     AdvEntity oAdv = AdvDAL.GetOne(entity.iAdvID);
     if (oAdv==null)
     {
         throw new Exception("Không tìm thấy quảng cáo này");
     }
     CategoryEntity oCategory = CategoryDAL.GetOne(entity.iCategoryID);
     if (oCategory==null)
     {
         throw new Exception("Không tìm thấy nhóm tin này");
     }
 }
 /// <summary>
 /// Kiểm tra trùng lặp bản ghi
 /// </summary>
 /// <param name="entity">AdvCategoryEntity: AdvCategoryEntity</param>
 private static void checkDuplicate(AdvCategoryEntity entity,bool checkPK)
 {
     List<AdvCategoryEntity> list = AdvCategoryDAL.GetAll();
     if (list.Exists(
         delegate(AdvCategoryEntity oldEntity)
         {
             bool result = oldEntity.iAdvID == entity.iAdvID && oldEntity.iCategoryID == entity.iCategoryID;
             if(checkPK)
                 result=result && oldEntity.iAdvCategory != entity.iAdvCategory;
             return result;
         }
     ))
     {
         list.Clear();
         throw new Exception(EX_EXISTED);
     }
 }