/// <summary>
 /// Kiểm tra và thêm mới VideoClip
 /// </summary>
 /// <param name="entity">Entity</param>
 /// <returns>Int32: ID của VideoClip Mới Thêm Vào</returns>
 public static Int32 Add(VideoClipEntity entity)
 {
     checkLogic(entity);
     checkDuplicate(entity, false);
     checkFK(entity);
     return VideoClipDAL.Add(entity);
 }
 /// <summary>
 /// Kiểm tra và chỉnh sửa VideoClip
 /// </summary>
 /// <param name="entity">VideoClipEntity</param>
 /// <returns>bool:kết quả thực hiện</returns>
 public static bool Edit(VideoClipEntity entity)
 {
     checkExist(entity.PK_iVideoID);
     checkLogic(entity);
     checkDuplicate(entity, true);
     checkFK(entity);
     return VideoClipDAL.Edit(entity);
 }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        byte[] bytVideo = null;
        VideoClipEntity entity = new VideoClipEntity();
        try
        {
            if (Convert.ToBoolean(Session["uploadOK"].ToString())==true&&Session["strUploadedFile"]!=null)
            {
                // Sửa đoạn này lại để có thể đọc được dung lượng tệp
                bytVideo = StreamFile(Session["strUploadedFile"].ToString());
                bVideoClip = bytVideo;
                        entity.iDungluong = bytVideo.Length;
                        entity.sMota = txtMota.Text;
                        entity.sTieude = txtTieude.Text;
                FileInfo fUploadded = new FileInfo(Session["strUploadedFile"].ToString());
                entity.sTentep = fUploadded.Name;
                        entity.FK_iCategoryID = 9;
                        entity.sAnhMinhHoa = ResolveUrl("~/upload/videos/play-default.png");
                        entity.dNgaytai = DateTime.Today;
            }
            else
            {
                // Nếu là sửa chữa thì check xem nếu đã có logo trong CSDL thì thôi không thông báo
                if (!Convert.ToBoolean(Session["hasVideo"].ToString()) == true)
                    lblLoi.Text = "Bạn chưa chọn tệp video";
                return;
            }
            // Xử lý nút bấm
            if (btnOK.CommandName == "Edit")
            {
                entity.PK_iVideoID = Convert.ToInt32(btnOK.CommandArgument);
                entity.iSolanxem = entity.iSolanxem + 1;
                VideoClipBRL.Edit(entity);
                lblThongbao.Text = "Cập nhập thành công";
                divVideo.InnerHtml = "";
                Response.Write("<script language=\"javascript\">alert('Cập nhập thành công!');location='Default.aspx?page=VideoClipsManager';</script>");
            }
            else
            {
                entity.iSolanxem = 0;
                VideoClipBRL.Add(entity);
                lblThongbao.Text = "Bổ sung thành công";
                divVideo.InnerHtml = "";
                Response.Write("<script language=\"javascript\">alert('Bổ sung thành công!');location='Default.aspx?page=VideoClipsManager';</script>");

            }
            //Nạp lại dữ liệu
            napGridView();
            pnlEdit.Visible = false;
        }
        catch (Exception ex)
        {
            Response.Write("<script language=\"javascript\">alert('" + ex.Message + "');location='Default.aspx?page=VideoClipsManager';</script>");
        }
    }
 /// <summary>
 /// Kiểm tra logic Entity
 /// </summary>
 /// <param name="entity">VideoClipEntity: entity</param>
 private static void checkLogic(VideoClipEntity entity)
 {
     if (String.IsNullOrEmpty(entity.sTentep))
         throw new Exception(EX_STENTEP_EMPTY);
     if (String.IsNullOrEmpty(entity.sTieude))
         throw new Exception(EX_STIEUDE_EMPTY);
     if (entity.iSolanxem < 0)
         throw new Exception(EX_ISOLANXEM_INVALID);
     if (entity.iDungluong < 0)
         throw new Exception(EX_IDUNGLUONG_INVALID);
     if (entity.iWidth < 0)
         throw new Exception(EX_IWIDTH_INVALID);
     if (entity.iHeight < 0)
         throw new Exception(EX_IHEIGHT_INVALID);
     if (entity.FK_iCategoryID < 0)
         throw new Exception(EX_FK_ICATEGORYID_INVALID);
     if (DateTime.Parse("1753-01-01")>entity.dNgaytai)
         throw new Exception(EX_DNGAYTAI_INVALID);
     if (String.IsNullOrEmpty(entity.sAnhMinhHoa))
         throw new Exception(EX_SANHMINHHOA_EMPTY);
 }
 /// <summary>
 /// Kiểm tra tồn tại khóa ngoại
 /// </summary>
 /// <param name="entity">VideoClipEntity:entity</param>
 private static void checkFK(VideoClipEntity entity)
 {
     CategoryEntity oCategory = CategoryDAL.GetOne(entity.FK_iCategoryID);
     if (oCategory==null)
     {
         throw new Exception("Không tìm thấy :FK_iCategoryID");
     }
 }
 /// <summary>
 /// Kiểm tra trùng lặp bản ghi
 /// </summary>
 /// <param name="entity">VideoClipEntity: VideoClipEntity</param>
 private static void checkDuplicate(VideoClipEntity entity,bool checkPK)
 {
     /*
     Example
     List<VideoClipEntity> list = VideoClipDAL.GetAll();
     if (list.Exists(
         delegate(VideoClipEntity oldEntity)
         {
             bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
             if(checkPK)
                 result=result && oldEntity.PK_iVideoID != entity.PK_iVideoID;
             return result;
         }
     ))
     {
         list.Clear();
         throw new Exception(EX_FIELD_EXISTED);
     }
     */
 }