private string GetDescription(EbReviewStage nextStage, string autoId)
        {
            string description = null;

            if (!string.IsNullOrEmpty(nextStage.NotificationContent?.Code?.Trim()))
            {
                if (this.globals == null)
                {
                    this.globals = GlobalsGenerator.GetCSharpFormGlobals_NEW(this.webForm, this.webForm.FormData, this.webForm.FormDataBackup, this.DataDB, null, false);
                }
                object msg = this.webForm.ExecuteCSharpScriptNew(nextStage.NotificationContent.Code, this.globals);
                description = Convert.ToString(msg);
                if (!string.IsNullOrEmpty(description))
                {
                    if (this.webForm.AutoId != null && this.isInsert && description.Contains(FG_Constants.AutoId_PlaceHolder))
                    {
                        description = description.Replace(FG_Constants.AutoId_PlaceHolder, autoId);
                    }
                }
            }
            if (string.IsNullOrEmpty(description))
            {
                description = $"{this.webForm.DisplayName} {(autoId.IsEmpty() ? string.Empty : (CharConstants.SPACE + autoId))}in {nextStage.Name}";
            }
            return(description);
        }
        public string GetMyActionInsertUpdateQuery(ref int i)
        {
            string        insUpQ = string.Empty, masterId = $"@{this.webForm.TableName}_id";
            bool          insMyActRequired = false, insInEdit = false, entryCriteriaRslt = true, entryCriteriaExecuted = false;
            EbReviewStage nextStage = null;

            if (!string.IsNullOrWhiteSpace(this.ebReview.EntryCriteria?.Code))
            {
                this.globals = GlobalsGenerator.GetCSharpFormGlobals_NEW(this.webForm, this.webForm.FormData, this.webForm.FormDataBackup, this.DataDB, null, false);
                object retval = this.webForm.ExecuteCSharpScriptNew(this.ebReview.EntryCriteria.Code, this.globals);
                if (retval is bool)
                {
                    bool.TryParse(Convert.ToString(retval), out entryCriteriaRslt);
                }
                else if (retval is FG_Review_Stage fg_stage)
                {
                    nextStage = this.ebReview.FormStages.Find(e => e.Name == fg_stage.name);
                }
                entryCriteriaExecuted = true;
            }
            if (this.isInsert)
            {
                if (entryCriteriaRslt)
                {
                    masterId = $"(SELECT eb_currval('{this.webForm.TableName}_id_seq'))";

                    if (entryCriteriaExecuted && this.IsAutoApproveRequired(ref i, ref insUpQ, masterId))
                    {
                        return(insUpQ);
                    }
                    if (nextStage == null)
                    {
                        nextStage = this.ebReview.FormStages[0];
                    }
                }
                else
                {
                    return(string.Empty);
                }
            }
            else
            {
                if (!entryCriteriaRslt)
                {
                    insUpQ += this.DeleteIfExists(ref i);
                }
                else
                {
                    if (this.Table.Count == 1)
                    {
                        nextStage = this.ExecuteOneStage(ref insUpQ, ref i, ref insMyActRequired, false);
                    }
                    else if (this.Table.Count == 0)
                    {
                        if (this.TableBkUp.Count == 0)
                        {
                            insInEdit = true;
                            if (nextStage == null)
                            {
                                nextStage = this.ebReview.FormStages[0];
                            }
                        }
                        if (!insInEdit)
                        {
                            Console.WriteLine("No items reviewed in this form data save");
                            return(string.Empty);
                        }
                    }
                    else
                    {
                        throw new FormException("Bad Request for review control", (int)HttpStatusCode.BadRequest, "eb_approval_lines contains more than one rows, only one review allowed at a time", "From GetMyActionInsertUpdateQuery");
                    }
                }
            }

            if (this.isInsert || insMyActRequired || insInEdit)// first save or insert myaction required in edit
            {
                string[] _col_val    = this.GetApproverEntityValues(ref i, nextStage, out _);
                string   autoId      = this.GetAutoId(masterId);
                string   description = this.GetDescription(nextStage, autoId);
                insUpQ += this.InsertMyActionAndApproval(nextStage, _col_val, masterId, description, insInEdit);
            }

            return(insUpQ);
        }
        private EbReviewStage ExecuteOneStage(ref string insUpQ, ref int i, ref bool insMyActRequired, bool ApprovalFlow)
        {
            EbReviewStage nextStage = null;

            insUpQ += this.GetApprovalLinesInsertQry(ref i);
            insUpQ += this.UpdateMyAction(ref i);

            if (!(this.ebReview.FormStages.Find(e => e.EbSid == Convert.ToString(this.Table[0][FormConstants.stage_unique_id])) is EbReviewStage currentStage))
            {
                throw new FormException("Bad Request", (int)HttpStatusCode.BadRequest, $"eb_approval_lines contains an invalid stage_unique_id: {this.Table[0]["stage_unique_id"]} ", "From GetMyActionInsertUpdateQuery");
            }

            if (!(currentStage.StageActions.Find(e => e.EbSid == Convert.ToString(this.Table[0][FormConstants.action_unique_id])) is EbReviewAction currentAction))
            {
                throw new FormException("Bad Request", (int)HttpStatusCode.BadRequest, $"eb_approval_lines contains an invalid action_unique_id: {this.Table[0]["action_unique_id"]} ", "From GetMyActionInsertUpdateQuery");
            }

            if (currentAction.CommentsRequired && string.IsNullOrWhiteSpace(Convert.ToString(this.Table[0]["comments"])))
            {
                throw new FormException("Comments required to complete the review", (int)HttpStatusCode.BadRequest, $"Comments required for stage: {currentStage.EbSid}, action: {currentAction.EbSid} ", "From GetMyActionInsertUpdateQuery");
            }

            if (this.globals == null)
            {
                this.globals = GlobalsGenerator.GetCSharpFormGlobals_NEW(this.webForm, this.webForm.FormData, this.webForm.FormDataBackup, this.DataDB, null, false);
            }

            object stageObj  = this.webForm.ExecuteCSharpScriptNew(currentStage.NextStage.Code, this.globals);
            string nxtStName = string.Empty;

            if (stageObj is FG_Review_Stage)
            {
                nxtStName = (stageObj as FG_Review_Stage).name;
            }

            GlobalsGenerator.PostProcessGlobals(this.webForm, this.globals, this.service);
            string _reviewStatus = this.globals.form.review._ReviewStatus;

            if (_reviewStatus == ReviewStatus.Completed || _reviewStatus == ReviewStatus.Abandoned)
            {
                if (ApprovalFlow)
                {
                    this.webForm.AfterSaveRoutines = this.ebReview.OnApprovalRoutines;
                }
                else
                {
                    this.webForm.AfterSaveRoutines.AddRange(this.ebReview.OnApprovalRoutines);
                }
                insMyActRequired = false;
                insUpQ          += this.GetApprovalUpdateQry(_reviewStatus, false, false);
            }
            else
            {
                EbReviewStage nxtSt = currentStage;
                if (!nxtStName.IsNullOrEmpty())
                {
                    nxtSt = this.ebReview.FormStages.Find(e => e.Name == nxtStName);
                }

                if (nxtSt != null)
                {
                    //backtrack to the same user - code here if needed
                    nextStage        = nxtSt;
                    insMyActRequired = true;
                }
                else
                {
                    throw new FormException("Unable to decide next stage", (int)HttpStatusCode.InternalServerError, "NextStage C# script returned a value that is not recognized as a stage", "Return value : " + nxtStName);
                }
            }
            return(nextStage);
        }
        public string GetMyActionInsertUpdateQueryxx(ref int i)
        {
            string        insUpQ = string.Empty, masterId = $"@{this.webForm.TableName}_id";;
            bool          insMyActRequired = false, entryCriteriaRslt = true;
            EbReviewStage nextStage = null;

            if (Convert.ToString(this.Table[0][FormConstants.stage_unique_id]) == FormConstants.__system_stage &&
                Convert.ToString(this.Table[0][FormConstants.action_unique_id]) == FormConstants.__review_reset)
            {
                insUpQ += this.GetApprovalLinesInsertQry(ref i);
                bool hasRoleMatch = false;
                if (this.ebReview.ResetterRoles != null)
                {
                    hasRoleMatch = this.webForm.UserObj.RoleIds.Select(x => x).Intersect(this.ebReview.ResetterRoles).Any() ||
                                   this.webForm.UserObj.RoleIds.Contains((int)SystemRoles.SolutionOwner) ||
                                   this.webForm.UserObj.RoleIds.Contains((int)SystemRoles.SolutionAdmin);
                }
                if (!hasRoleMatch)
                {
                    throw new FormException("Access denied to reset review control", (int)HttpStatusCode.Unauthorized, $"User.RolesId does not contains any of permited roleIds[ResetterRoles]", "From GetMyActionInsertUpdateQuery");
                }

                SingleRow RowBkUp = this.TableBkUp.Find(e => e.RowId <= 0);
                if (RowBkUp != null)
                {
                    insUpQ += this.GetMyActionDeleteQry(ref i, Convert.ToInt32(RowBkUp[FormConstants.eb_my_actions_id]));
                }

                if (!string.IsNullOrWhiteSpace(this.ebReview.EntryCriteria?.Code))
                {
                    this.globals = GlobalsGenerator.GetCSharpFormGlobals_NEW(this.webForm, this.webForm.FormData, this.webForm.FormDataBackup, this.DataDB, null, false);
                    object retval = this.webForm.ExecuteCSharpScriptNew(this.ebReview.EntryCriteria.Code, this.globals);//status or stage

                    if (retval is bool)
                    {
                        bool.TryParse(Convert.ToString(retval), out entryCriteriaRslt);
                    }
                    else if (retval is FG_Review_Stage fg_stage)
                    {
                        nextStage = this.ebReview.FormStages.Find(e => e.Name == fg_stage.name);
                    }
                }
                if (entryCriteriaRslt)
                {
                    if (nextStage == null)
                    {
                        nextStage = this.ebReview.FormStages[0];
                    }
                }
                else if (this.TableBkUp.Find(e => e.RowId > 0) != null)
                {
                    insUpQ += this.GetApprovalUpdateQry(null, true, false);
                    insUpQ += this.GetApprovalLinesDeleteQry();
                }
            }
            else
            {
                nextStage = this.ExecuteOneStage(ref insUpQ, ref i, ref insMyActRequired, true);
            }

            if (nextStage != null)
            {
                string[] _col_val    = this.GetApproverEntityValues(ref i, nextStage, out _);
                string   autoId      = this.GetAutoId(masterId);
                string   description = this.GetDescription(nextStage, autoId);

                insUpQ += this.GetMyActionInsertQry(_col_val, nextStage, masterId, description);
                if (this.DataDB.Vendor == DatabaseVendors.MYSQL)
                {
                    insUpQ += "SELECT eb_persist_currval('eb_my_actions_id_seq'); ";
                }

                this.webForm.MyActNotification.Title = "Review required";
                Console.WriteLine("Will try to INSERT eb_my_actions");

                insUpQ += this.GetApprovalUpdateQry(null, false, true);
            }
            return(insUpQ);
        }