private void ExecuteAction() { //Kiểm tra tính hợp lệ của dữ liệu trước khi thêm mới if (!IsValidData()) { return; } //Gán ProcedureEntity vào DataEntity SetValueforEntity(); //Khởi tạo BusinessRule để xử lý nghiệp vụ ProcedureInfor Infor = new ProcedureInfor(); Utility.MapValueFromEntityIntoObjectInfor(Infor, ProcedureEntity); ProcedureController _BusRule = new ProcedureController(Infor); switch (Act) { case action.Insert: //Gọi nghiệp vụ Insert dữ liệu ActionResult InsertResult = _BusRule.Insert(); if (InsertResult == ActionResult.Success)//Nếu thành công { //Thêm mới một dòng vào Datasource để cập nhật lại dữ liệu trên DataGridView //phải đảm bảo Datasource và ProcedureEntity có cấu trúc giống nhau mới dùng hàm này DataRow dr = Utility.CopyData(ProcedureEntity.Rows[0], DataSource); dr["Procedure_ID"] = Infor.Procedure_ID; if (dr != null)//99.99% là sẽ !=null { DataSource.Rows.Add(dr); DataSource.AcceptChanges(); } //Thêm vào treeView string Code =Infor.Procedure_Code; string newText = ""; if (Code.Trim() != "") newText = Code + "-" + Infor.DISPLAY_NAME; else newText = Infor.DISPLAY_NAME; TreeNode newNode = new TreeNode(newText); if (tvwProcedure.SelectedNode.Tag.ToString().Contains("ROOT")) { newNode.Tag = Infor.Procedure_ID.ToString() + "#PARENT"; newNode.ForeColor = System.Drawing.Color.DarkBlue; } else { newNode.Tag = Infor.Procedure_ID.ToString() + "#LEAF$" + Infor.Pos.ToString(); newNode.ForeColor = System.Drawing.Color.Black; } newNode.NodeFont = new Font("Microsoft Sans Serif", 9, FontStyle.Bold, GraphicsUnit.Point); tvwProcedure.SelectedNode.Nodes.Add(newNode); tvwProcedure.SelectedNode.Expand(); //Return to the InitialStatus Act = action.FirstOrFinished; //Nhảy đến bản ghi vừa thêm mới trên lưới. Do txtID chưa bị reset nên dùng luôn mdlStatic.SetMsg(lblMsg,"Thêm mới dữ liệu thành công!",false); SetControlStatus(); SelecttionChanged(); } else//Có lỗi xảy ra { switch (InsertResult) { case ActionResult.ExistedRecord: mdlStatic.SetMsg(lblMsg, "Đã tồn tại Vị trí có mã: " + txtCode.Text.Trim() + ". Đề nghị bạn xem lại",true); Utility.FocusAndSelectAll( txtCode); break; default: mdlStatic.SetMsg(lblMsg,"Lỗi trong quá trình thêm mới Vị trí. Liên hệ với VBIT",true); break; } } break; case action.Update: //Gọi Business cập nhật dữ liệu ActionResult UpdateResult = _BusRule.Update(); if (UpdateResult == ActionResult.Success)//Nếu thành công { //Cập nhật số thứ tự cho bản ghi tráo số thứ tự với bản ghi đang được cập nhật(nếu có)? foreach (DataRow drUpdatePos in DataSource.Rows) { if (Utility.Int16Dbnull(drUpdatePos["Pos"]) == Convert.ToInt16(txtPos.Text)) { drUpdatePos["Pos"] = OldPos; break; // TODO: might not be correct. Was : Exit For } } DataSource.AcceptChanges(); //Cập nhật dòng hiện thời trong Datasource để cập nhật lại dữ liệu trên DataGridView DataRow dr = Utility.GetDataRow(DataSource, "Procedure_ID", txtID.Text.Trim()); if (dr != null) { Utility.CopyData(ProcedureEntity.Rows[0], ref dr); DataSource.AcceptChanges(); } string Code = Infor.Procedure_Code; string newText = ""; if (Code.Trim() != "") newText = Code + "-" + Infor.DISPLAY_NAME; else newText = Infor.DISPLAY_NAME; tvwProcedure.SelectedNode.Text=newText; if (tvwProcedure.SelectedNode.Tag.ToString().Contains("PARENT")) tvwProcedure.SelectedNode.Tag = Infor.Procedure_ID.ToString() + "#PARENT$" + Infor.Pos.ToString(); else tvwProcedure.SelectedNode.Tag = Infor.Procedure_ID.ToString() + "#LEAF$" + Infor.Pos.ToString(); //Return to the InitialStatus Act = action.FirstOrFinished; //Nhảy đến bản ghi vừa cập nhật trên lưới. Do txtID chưa bị reset nên dùng luôn //Utility.GotoNewRow(grdList, "colProcedure_ID", txtCode.Text.Trim()); mdlStatic.SetMsg(lblMsg,"Cập nhật dữ liệu thành công.",false); SetControlStatus(); SelecttionChanged(); } else//Có lỗi xảy ra { switch (UpdateResult) { case ActionResult.Error: mdlStatic.SetMsg(lblMsg,"Lỗi khi cập nhật Vị trí. Liên hệ với VBIT",true); break; default: mdlStatic.SetMsg(lblMsg,"Lỗi khi cập nhật Vị trí. Liên hệ với VBIT",true); break; } } break; case action.Delete: if (Utility.AcceptQuestion("Bạn có muốn xóa Vị trí đang chọn hay không?", "Xác nhận xóa", true)) { string Procedure_ID = txtID.Text.Trim(); //Gọi nghiệp vụ xóa dữ liệu ActionResult DeleteResult = _BusRule.Delete(); if (DeleteResult == ActionResult.Success)//Nếu xóa thành công trong CSDL { //Xóa dòng dữ liệu vừa chọn trong Datasource để cập nhật lại dữ liệu trên DataGridView DataRow dr = Utility.GetDataRow(DataSource, "Procedure_ID", Procedure_ID); if (dr != null) { DataSource.Rows.Remove(dr); DataSource.AcceptChanges(); } //remove khỏi TreeView tvwProcedure.Nodes.Remove(tvwProcedure.SelectedNode); //Return to the InitialStatus Act = action.FirstOrFinished; mdlStatic.SetMsg(lblMsg, "Đã xóa Vị trí có ID: " + Procedure_ID + " ra khỏi hệ thống.", false); SetControlStatus(); SelecttionChanged(); } else//Có lỗi xảy ra { switch (DeleteResult) { case ActionResult.DataHasUsedinAnotherTable: mdlStatic.SetMsg(lblMsg, "Vị trí có ID: " + Procedure_ID + " đã được sử dụng trong bảng khác nên bạn không thể xóa!", true); break; default: mdlStatic.SetMsg(lblMsg,"Lỗi khi xóa Vị trí. Liên hệ với VBIT",true); break; } } } break; default: break; } }
/// <summary> /// Tạo DataRow của Entity từ ObjectInfor /// </summary> /// <param name="dr">DataRow</param> /// <param name="Infor">ObjectInfor</param> public static void CreateDatarowFromObjectInfor(ref DataRow dr, ProcedureInfor Infor) { if (Infor != null) { dr["Procedure_ID"] =Infor.Procedure_ID; dr["Procedure_Code"] = Utility.sDbnull(Infor.Procedure_Code); dr["DISPLAY_NAME"] = Utility.sDbnull(Infor.DISPLAY_NAME); dr["STANDARD_NAME"] = Utility.sDbnull(Infor.STANDARD_NAME); dr["Parent_ID"] = Utility.Int16Dbnull(Infor.Parent_ID); dr["MODALITY_ID"] = Utility.Int16Dbnull(Infor.MODALITY_ID); dr["MODALITY_Name"] = Utility.sDbnull(Infor.MODALITY_Name); dr["PRICE"] = Utility.fDbnull(Infor.PRICE); dr["Pos"] = Utility.Int16Dbnull(Infor.Pos); dr["DirectionCapture"] = Infor.DirectionCapture; dr["IsEmerency"] = Utility.Int16Dbnull(Infor.IsEmerency); dr["Desc"] = Utility.sDbnull(Infor.Desc); } }
public string GetCode(Int16 ID) { DataSet ds = new ProcedureController().GetData("Procedure_ID=" + ID); ProcedureInfor Infor = new ProcedureInfor(); if (ds != null) { if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { return ds.Tables[0].Rows[0]["Procedure_Code"].ToString(); } else { return "Undefined"; } } else { return "Undefined"; } }
/// <summary> /// Trả về đối tượng Infor dựa vào Primary key của nó /// </summary> /// <param name="ID"></param> /// <returns></returns> public static ProcedureInfor GetInfor(Int16 ID) { DataSet ds = new ProcedureController().GetData("Procedure_ID=" + ID ); ProcedureInfor Infor = new ProcedureInfor(); if (ds != null) { if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { Infor.Procedure_Code = Utility.sDbnull(ds.Tables[0].Rows[0]["Procedure_Code"]); Infor.Procedure_ID = Utility.Int16Dbnull(ds.Tables[0].Rows[0]["Procedure_ID"]); Infor.Parent_ID = Utility.Int16Dbnull(ds.Tables[0].Rows[0]["Parent_ID"]); Infor.DISPLAY_NAME = Utility.sDbnull(ds.Tables[0].Rows[0]["DISPLAY_NAME"]); Infor.STANDARD_NAME = Utility.sDbnull(ds.Tables[0].Rows[0]["STANDARD_NAME"]); Infor.MODALITY_ID = Utility.Int16Dbnull(ds.Tables[0].Rows[0]["MODALITY_ID"]); Infor.MODALITY_Name = Utility.sDbnull(ds.Tables[0].Rows[0]["MODALITY_Name"]); Infor.PRICE = Utility.fDbnull(ds.Tables[0].Rows[0]["PRICE"]); Infor.Pos = Utility.Int16Dbnull(ds.Tables[0].Rows[0]["Pos"]); Infor.IsEmerency = Utility.Int16Dbnull(ds.Tables[0].Rows[0]["IsEmerency"]); Infor.Desc = Utility.sDbnull(ds.Tables[0].Rows[0]["Desc"]); Infor.DirectionCapture = Convert.ToByte(ds.Tables[0].Rows[0]["DirectionCapture"]); return Infor; } else { return null; } } else { return null; } }
public ProcedureController(ProcedureInfor Infor) { this.Infor = Infor; }
/// <summary> /// Trả về đối tượng Infor dựa vào Primary key của nó /// </summary> /// <param name="ID"></param> /// <returns></returns> public static ProcedureInfor GetInfor(DataRow dr) { ProcedureInfor Infor = new ProcedureInfor(); if (dr != null) { Infor.Procedure_Code = Utility.sDbnull(dr["Procedure_Code"]); Infor.Procedure_ID = Utility.Int16Dbnull(dr["Procedure_ID"]); Infor.Parent_ID = Utility.Int16Dbnull(dr["Parent_ID"]); Infor.DISPLAY_NAME = Utility.sDbnull(dr["DISPLAY_NAME"]); Infor.STANDARD_NAME = Utility.sDbnull(dr["STANDARD_NAME"]); Infor.MODALITY_ID = Utility.Int16Dbnull(dr["MODALITY_ID"]); Infor.MODALITY_Name = Utility.sDbnull(dr["MODALITY_Name"]); Infor.PRICE = Utility.fDbnull(dr["PRICE"]); Infor.Pos = Utility.Int16Dbnull(dr["Pos"]); Infor.IsEmerency = Utility.Int16Dbnull(dr["IsEmerency"]); Infor.DirectionCapture = Convert.ToByte(dr["DirectionCapture"]); Infor.Desc = Utility.sDbnull(dr["Desc"]); return Infor; } else { return null; } }