/// <summary>
        /// UpdateCommentsHistoryForSummaryEdit
        /// </summary>
        public void UpdateCommentsHistoryForSummaryEdit()
        {
            FlatSectionJlinerJuntionLiner2CommentGateway flatSectionJlinerJuntionLiner2CommentGateway = new FlatSectionJlinerJuntionLiner2CommentGateway();
            FlatSectionJlinerJuntionLiner2Comment jlinerComments = new FlatSectionJlinerJuntionLiner2Comment(flatSectionJlinerJuntionLiner2CommentGateway.Data);

            FlatSectionJlinerJuntionLiner2HistoryGateway flatSectionJlinerJuntionLiner2HistoryGateway = new FlatSectionJlinerJuntionLiner2HistoryGateway();
            FlatSectionJlinerJuntionLiner2History jlinerHistory = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerJuntionLiner2HistoryGateway.Data);

            foreach (FlatSectionJlinerTDS.FlatSectionJlinerRow row in (FlatSectionJlinerTDS.FlatSectionJlinerDataTable)Table)
            {
                flatSectionJlinerJuntionLiner2CommentGateway.LoadByIdRefId(row.ID, row.RefID, row.COMPANY_ID);
                row.Comments = jlinerComments.GetAllComments(row.ID, row.RefID, row.COMPANY_ID, flatSectionJlinerJuntionLiner2CommentGateway.Table.Rows.Count, "\n");

                flatSectionJlinerJuntionLiner2HistoryGateway.LoadByIdRefId(row.ID, row.RefID, row.COMPANY_ID);
                row.History = jlinerHistory.GetAllHistory(row.ID, row.RefID, row.COMPANY_ID, flatSectionJlinerJuntionLiner2HistoryGateway.Table.Rows.Count, "\n");
            }
        }
        private void UpdateDatabase()
        {
            DB.Open();
            DB.BeginTransaction();
            try
            {
                Guid id = new Guid(hdfId.Value);
                int refId = Int32.Parse(hdfRefId.Value);
                int companyId = Int32.Parse(hdfCompanyId.Value);

                // Save Comments
                FlatSectionJlinerJuntionLiner2Comment flatSectionJlinerJuntionLiner2Comment = new FlatSectionJlinerJuntionLiner2Comment(flatSectionJlinerTDS);
                flatSectionJlinerJuntionLiner2Comment.Save(id, companyId);

                // Save History
                FlatSectionJlinerJuntionLiner2History flatSectionJlinerJuntionLiner2History = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerTDS);
                flatSectionJlinerJuntionLiner2History.Save(id, companyId);

                // Save jliner
                FlatSectionJliner flatSectionJlinerUpdateCommentsHistory = new FlatSectionJliner();
                flatSectionJlinerUpdateCommentsHistory.LoadByIdRefId(id, refId, companyId);
                flatSectionJlinerUpdateCommentsHistory.UpdateComments(id.ToString());
                flatSectionJlinerUpdateCommentsHistory.UpdateHistory(id.ToString());

                Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
                flatSectionJlinerUpdateCommentsHistory.Save(companyId);

                // Update FlatSectionJliner for new Comments
                FlatSectionJliner flatSectionJlinerForCommentsHistory = new FlatSectionJliner(flatSectionJlinerTDS);
                flatSectionJlinerForCommentsHistory.UpdateCommentsHistoryForSummaryEdit();

                Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;

                DB.CommitTransaction();
            }
            catch (Exception ex)
            {
                DB.RollbackTransaction();

                string url = string.Format("./../../error_page.aspx?error={0}", ex.Message.Replace('\n', ' '));
                Response.Redirect(url);
            }
        }
        /// <summary>
        /// Transfer to History
        /// </summary>
        /// <param name="loginId">loginId</param>
        /// <param name="adminPermission">admin Permission</param>
        /// <param name="jlinerHistory">jlinerHistory</param>
        public void transferToHistory(int loginId, bool adminPermission, FlatSectionJlinerJuntionLiner2History jlinerHistory)
        {
            foreach (FlatSectionJlinerTDS.JuntionLiner2CommentRow commentRow in (FlatSectionJlinerTDS.JuntionLiner2CommentDataTable)Table)
            {
                if (adminPermission)
                {
                    if ((commentRow.toHistory) && (!commentRow.Deleted))
                    {
                        // Insert to history
                        jlinerHistory.Insert(commentRow.ID, commentRow.RefID, commentRow.COMPANY_ID, commentRow.DateTime_, commentRow.LoginID, commentRow.Comment, commentRow.Deleted, false, false);

                        // Delete from comments
                        commentRow.Deleted = true;
                    }
                }
                else
                {
                    if (commentRow.LoginID == loginId)
                    {
                        if ((commentRow.toHistory) && (!commentRow.Deleted))
                        {
                            // Insert to history
                            jlinerHistory.Insert(commentRow.ID, commentRow.RefID, commentRow.COMPANY_ID, commentRow.DateTime_, commentRow.LoginID, commentRow.Comment, commentRow.Deleted, false, false);

                            // Delete from comments
                            commentRow.Deleted = true;
                        }
                    }
                }
            }
        }
        private void TransferToHistory()
        {
            int loginId = Convert.ToInt32(hdfLoginId.Value);
            bool adminPermission = Convert.ToBoolean(Session["sgLFS_APP_ADMIN"]);

            FlatSectionJlinerJuntionLiner2Comment flatSectionJlinerJuntionLiner2Comment = new FlatSectionJlinerJuntionLiner2Comment(flatSectionJlinerTDS);
            FlatSectionJlinerJuntionLiner2History flatSectionJlinerJuntionLiner2History = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerTDS);

            flatSectionJlinerJuntionLiner2Comment.transferToHistory(loginId, adminPermission, flatSectionJlinerJuntionLiner2History);
            Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
        }
        private void GrdHistoryAdd()
        {
            if (ValidateHistoryFooter())
            {
                Page.Validate("commentsDataAdd");
                if (Page.IsValid)
                {
                    Guid id = new Guid(hdfId.Value);
                    int refId = Convert.ToInt32(hdfRefId.Value);

                    int companyId = Int32.Parse(hdfCompanyId.Value);
                    int loginId = Convert.ToInt32(Session["loginID"]);
                    DateTime dateTime_ = DateTime.Now;
                    bool inDatabase = false;
                    bool deleted = false;

                    string newComment = ((TextBox)grdHistory.FooterRow.FindControl("tbxHistoryNew")).Text.Trim();
                    bool toComments = ((CheckBox)grdHistory.FooterRow.FindControl("ckbxToCommentsNew")).Checked;

                    LoginGateway loginGateway = new LoginGateway();
                    loginGateway.LoadByLoginId(loginId, companyId);
                    string userFullName = loginGateway.GetLastName(loginId, companyId) + " " + loginGateway.GetFirstName(loginId, companyId);

                    FlatSectionJlinerJuntionLiner2History model = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerTDS);
                    model.Insert(id, refId, companyId, dateTime_, loginId, newComment, deleted, toComments, inDatabase);

                    Session.Remove("historyDummy");
                    Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
                    Session["history"] = flatSectionJlinerTDS.JuntionLiner2History;

                    grdHistory.DataBind();
                    grdHistory.PageIndex = grdHistory.PageCount - 1;
                }
            }
        }
        // ////////////////////////////////////////////////////////////////////////
        // EVENTS
        //
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register client scripts
            this.RegisterClientScripts();

            if (!IsPostBack)
            {
                // Security check
                if (!(Convert.ToBoolean(Session["sgLFS_APP_VIEW"]) && Convert.ToBoolean(Session["sgLFS_APP_EDIT"])))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "You are not authorized to view this page. Contact your system administrator.");
                }

                // Validate query string
                if (((string)Request.QueryString["source_page"] == null) || ((string)Request.QueryString["client"] == null))
                {
                    Response.Redirect("./../../error_page.aspx?error=" + "Invalid query string in flat_section_jliner_edit.aspx");
                }

                // Tag page
                hdfCurrentClient.Value = (string)Request.QueryString["client"];
                hdfLoginId.Value = Convert.ToInt32(Session["loginID"]).ToString();
                hdfId.Value = Convert.ToString(Request.QueryString["rowId"]);
                hdfRefId.Value = Convert.ToString(Request.QueryString["rowRefId"]);
                hdfCompanyId.Value = Convert.ToString(Request.QueryString["rowCompanyId"]);
                hdfAdminPermission.Value = Convert.ToBoolean(Session["sgLFS_APP_ADMIN"]).ToString();
                hdfUpdate.Value = "yes";

                // Prepare initial data
                Session.Remove("historyDummy");

                // ... Names for UserList
                int companyId = Int32.Parse(hdfCompanyId.Value);

                LoginGateway loginGateway = new LoginGateway();
                loginGateway.LoadByLoginId(Convert.ToInt32(hdfLoginId.Value), companyId);
                hdfCreatedBy.Value = loginGateway.GetLastName(Convert.ToInt32(hdfLoginId.Value), companyId) + " " + loginGateway.GetFirstName(Convert.ToInt32(hdfLoginId.Value), companyId);
                hdfCreationDateTime.Value = DateTime.Now.ToString();

                // If comming from
                // ... flat_section_jliner_summary.aspx
                if (Request.QueryString["source_page"] == "flat_section_jliner_summary.aspx" || Request.QueryString["source_page"] == "flat_section_jliner_edit.aspx")
                {
                    StoreNavigatorState();
                    ViewState["update"] = Request.QueryString["update"];
                    Session["rowFocus"] = Convert.ToInt32(Request.QueryString["rowFocus"].ToString());

                    // ... Load history to edit
                    sectionTDS = new SectionTDS();
                    flatSectionJlinerTDS = (FlatSectionJlinerTDS)Session["flatSectionJlinerTDS"];

                    SectionGateway sectionGateway = new SectionGateway(sectionTDS);
                    sectionGateway.LoadById(new Guid(hdfId.Value), Convert.ToInt32(hdfCompanyId.Value));

                    JlinerGateway jlinerGateway = new JlinerGateway(sectionTDS);
                    jlinerGateway.LoadByIdCompanyIdRefId(new Guid(hdfId.Value), Convert.ToInt32(hdfCompanyId.Value), Convert.ToInt32(hdfRefId.Value));

                    FlatSectionJlinerJuntionLiner2HistoryGateway flatSectionJlinerJuntionLiner2HistoryGateway = new FlatSectionJlinerJuntionLiner2HistoryGateway(flatSectionJlinerTDS);
                    flatSectionJlinerJuntionLiner2HistoryGateway.LoadAllByIdRefId(new Guid(hdfId.Value), Convert.ToInt32(hdfRefId.Value), Convert.ToInt32(hdfCompanyId.Value));

                    FlatSectionJlinerJuntionLiner2History flatSectionJlinerJuntionLiner2History = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerJuntionLiner2HistoryGateway.Data);
                    flatSectionJlinerJuntionLiner2History.UpdateForProcess();

                    // ... Load comments for transfers
                    FlatSectionJlinerJuntionLiner2CommentGateway flatSectionJlinerJuntionLiner2CommentGateway = new FlatSectionJlinerJuntionLiner2CommentGateway(flatSectionJlinerTDS);
                    flatSectionJlinerJuntionLiner2CommentGateway.LoadAllByIdRefId(new Guid(hdfId.Value), Convert.ToInt32(hdfRefId.Value), Convert.ToInt32(hdfCompanyId.Value));

                    // ... Store datasets
                    Session["sectionTDS"] = sectionTDS;
                    Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
                    history = flatSectionJlinerTDS.JuntionLiner2History;
                    Session["history"] = history;
                }
            }
            else
            {
                // Restore datasets
                sectionTDS = (SectionTDS)Session["sectionTDS"];
                flatSectionJlinerTDS = (FlatSectionJlinerTDS)Session["flatSectionJlinerTDS"];
                Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
                history = flatSectionJlinerTDS.JuntionLiner2History;
                Session["history"] = history;
            }
        }
        protected void grdHistory_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Page.Validate("commentsDataEdit");
            if (Page.IsValid)
            {
                Guid id = new Guid(((Label)grdHistory.Rows[e.RowIndex].Cells[0].FindControl("lblId")).Text);
                int refId = Int32.Parse(((Label)grdHistory.Rows[e.RowIndex].Cells[1].FindControl("lblRefID")).Text);
                int companyId = Int32.Parse(hdfCompanyId.Value);
                int historyId = Int32.Parse(((Label)grdHistory.Rows[e.RowIndex].Cells[2].FindControl("lblHistoryID")).Text);

                int loginId = Convert.ToInt32(Session["loginID"]);
                bool adminPermission = Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_ADMIN"]);
                string newHistory = ((TextBox)grdHistory.Rows[e.RowIndex].Cells[5].FindControl("tbxHistoryEdit")).Text.Trim();
                bool toComments = ((CheckBox)grdHistory.Rows[e.RowIndex].Cells[4].FindControl("ckbxToCommentsEdit")).Checked;

                // Update data
                FlatSectionJlinerJuntionLiner2History model = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerTDS);
                model.Update(id, refId, companyId, historyId, loginId, newHistory, false, adminPermission, toComments);

                // Store dataset
                Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
                Session["history"] = flatSectionJlinerTDS.JuntionLiner2History;
            }
            else
            {
                e.Cancel = true;
            }
        }
        protected void grdHistory_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Comments Gridview, if the gridview is edition mode
            if (grdHistory.EditIndex >= 0)
            {
                grdHistory.UpdateRow(grdHistory.EditIndex, true);
            }

            // Delete history
            Guid id = new Guid(((Label)grdHistory.Rows[e.RowIndex].Cells[0].FindControl("lblId")).Text);
            int refId = Int32.Parse(((Label)grdHistory.Rows[e.RowIndex].Cells[1].FindControl("lblRefID")).Text);
            int companyId = Int32.Parse(hdfCompanyId.Value);
            int historyId = Int32.Parse(((Label)grdHistory.Rows[e.RowIndex].Cells[2].FindControl("lblHistoryID")).Text);
            int loginId = Convert.ToInt32(hdfLoginId.Value);
            bool adminPermission = Convert.ToBoolean(Session["sgLFS_CWP_JUNCTIONLINING_ADMIN"]);

            FlatSectionJlinerJuntionLiner2History model = new FlatSectionJlinerJuntionLiner2History(flatSectionJlinerTDS);
            model.Delete(id, refId, companyId, historyId, loginId, adminPermission);

            // Store dataset
            Session["flatSectionJlinerTDS"] = flatSectionJlinerTDS;
        }