private string InsertMyActionAndApproval(EbReviewStage nextStage, string[] _col_val, string masterId, string description, bool insInEdit)
        {
            string 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");

            if (this.isInsert)// eb_approval - insert entry here
            {
                insUpQ += this.GetApprovalInsertQry(true);
            }
            else if (insInEdit)
            {
                insUpQ += this.GetApprovalInsertQry(false);
            }
            else // eb_approval - update eb_my_actions_id
            {
                insUpQ += this.GetApprovalUpdateQry(null, false, false);
            }
            return(insUpQ);
        }
        private string GetMyActionInsertQry(string[] _col_val, EbReviewStage nextStage, string masterId, string description)
        {
            return($@"
INSERT INTO eb_my_actions(
    {_col_val[0]}, 
    from_datetime, 
    is_completed, 
    eb_stages_id, 
    form_ref_id, 
    form_data_id, 
    eb_del, 
    description, 
    is_form_data_editable, 
    my_action_type,
    action_type,
    hide)
VALUES (
    {_col_val[1]}, 
    {this.DataDB.EB_CURRENT_TIMESTAMP}, 
    'F', 
    (SELECT id FROM eb_stages WHERE stage_unique_id = '{nextStage.EbSid}' AND form_ref_id = '{this.webForm.RefId}' AND eb_del = 'F'), 
    '{this.webForm.RefId}', 
    {masterId}, 
    'F', 
    '{description}', 
    '{(nextStage.IsFormEditable ? "T" : "F")}', 
    '{MyActionTypes.Approval}',
    {(int)MyActionTypes.Approval},
    '{(nextStage.HideNotification ? "T" : "F")}'); ");
        }
        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);
        }
Пример #4
0
        public void setCurrentStageDataEditable()
        {
            EbReviewStage curS = (EbReviewStage)this.CtrlObj.FormStages.Find(e => (e as EbReviewStage).Name == this.currentStage.name);

            if (curS != null)
            {
                curS.IsFormEditable = true;
            }
        }
Пример #5
0
        public _FG_Review(EbReview CtrlObj, SingleTable Table)
        {
            this.Name    = CtrlObj.Name;
            this.CtrlObj = CtrlObj;
            this.Table   = Table;

            SingleRow _Row = null;

            foreach (SingleRow Row in this.Table)
            {
                if (Row.RowId <= 0 && Row.Columns.Count > 0)
                {
                    _Row = Row;
                    break;
                }
            }

            this.stages = new Dictionary <string, _FG_Review_Stage>();

            foreach (ReviewStageAbstract stage in this.CtrlObj.FormStages)
            {
                EbReviewStage            _eb_stage  = stage as EbReviewStage;
                List <_FG_Review_Action> fg_actions = new List <_FG_Review_Action>();
                foreach (ReviewActionAbstract action in _eb_stage.StageActions)
                {
                    fg_actions.Add(new _FG_Review_Action((action as EbReviewAction).Name));
                }
                if (_Row != null && Convert.ToString(_Row["stage_unique_id"]) == _eb_stage.EbSid)
                {
                    EbReviewAction    eb_curAct = (EbReviewAction)_eb_stage.StageActions.Find(e => (e as EbReviewAction).EbSid == Convert.ToString(_Row["action_unique_id"]));
                    _FG_Review_Action fg_curAct = null;
                    if (eb_curAct != null)
                    {
                        fg_curAct = fg_actions.Find(e => e.name == eb_curAct.Name);
                    }
                    this.stages.Add(_eb_stage.Name, new _FG_Review_Stage(_eb_stage.Name, fg_actions, fg_curAct));
                    this.currentStage = this.stages[_eb_stage.Name];
                }
                else
                {
                    this.stages.Add(_eb_stage.Name, new _FG_Review_Stage(_eb_stage.Name, fg_actions, null));
                }
            }
        }
        private string[] GetApproverEntityValues(ref int i, EbReviewStage nextStage, out bool hasPerm)
        {
            string _col = string.Empty, _val = string.Empty;

            this.webForm.MyActNotification = new MyActionNotification()
            {
                ApproverEntity       = nextStage.ApproverEntity,
                SendPushNotification = !nextStage.HideNotification
            };
            if (nextStage.ApproverEntity == ApproverEntityTypes.Role)
            {
                _col = "role_ids";
                _val = $"@role_ids_{i}";
                string roles = nextStage.ApproverRoles == null ? string.Empty : nextStage.ApproverRoles.Join(",");
                this.param.Add(this.DataDB.GetNewParameter($"role_ids_{i++}", EbDbTypes.String, roles));
                this.webForm.MyActNotification.RoleIds = nextStage.ApproverRoles;
                hasPerm = this.webForm.UserObj.RoleIds.Any(e => nextStage.ApproverRoles.Contains(e));
            }
            else if (nextStage.ApproverEntity == ApproverEntityTypes.UserGroup)
            {
                _col = "usergroup_id";
                _val = $"@usergroup_id_{i}";
                this.param.Add(this.DataDB.GetNewParameter($"usergroup_id_{i++}", EbDbTypes.Int32, nextStage.ApproverUserGroup));
                this.webForm.MyActNotification.UserGroupId = nextStage.ApproverUserGroup;
                hasPerm = this.webForm.UserObj.UserGroupIds.Any(e => e == nextStage.ApproverUserGroup);
            }
            else if (nextStage.ApproverEntity == ApproverEntityTypes.Users || nextStage.ApproverEntity == ApproverEntityTypes.DynamicRole)
            {
                string             t1 = string.Empty, t2 = string.Empty, t3 = string.Empty;
                List <DbParameter> _params = new List <DbParameter>();
                int    _idx = 0, ErrCod = (int)HttpStatusCode.BadRequest;
                string ErrMsg = "GetFirstMyActionInsertQuery: Review control parameter {0} is not idetified";
                foreach (KeyValuePair <string, string> p in nextStage.QryParams)
                {
                    if (EbFormHelper.IsExtraSqlParam(p.Key, this.webForm.TableName))
                    {
                        continue;
                    }
                    SingleTable _Table = null;
                    if (this.webForm.FormData.MultipleTables.ContainsKey(p.Value))
                    {
                        _Table = this.webForm.FormData.MultipleTables[p.Value];
                    }
                    else if (this.webForm.FormDataBackup != null && this.webForm.FormDataBackup.MultipleTables.ContainsKey(p.Value))
                    {
                        _Table = this.webForm.FormDataBackup.MultipleTables[p.Value];
                    }
                    else
                    {
                        throw new FormException($"Bad Request", ErrCod, string.Format(ErrMsg, p.Key), $"{p.Value} not found in MultipleTables");
                    }
                    TableSchema _table = this.webForm.FormSchema.Tables.Find(e => e.TableName == p.Value);
                    if (_table.TableType != WebFormTableTypes.Normal)
                    {
                        throw new FormException($"Bad Request", ErrCod, string.Format(ErrMsg, p.Key), $"{p.Value} found in MultipleTables but it is not a normal table");
                    }
                    if (_Table.Count != 1)
                    {
                        throw new FormException($"Bad Request", ErrCod, string.Format(ErrMsg, p.Key), $"{p.Value} found in MultipleTables but table is empty");
                    }
                    SingleColumn Column = _Table[0].Columns.Find(e => e.Control?.Name == p.Key);
                    if (Column == null || Column.Control == null)
                    {
                        throw new FormException($"Bad Request", ErrCod, string.Format(ErrMsg, p.Key), $"{p.Value} found in MultipleTables but data not available");
                    }

                    ParameterizeCtrl_Params args = new ParameterizeCtrl_Params(this.DataDB, _params, Column, _idx, this.webForm.UserObj);
                    Column.Control.ParameterizeControl(args, this.webForm.CrudContext);
                    _idx = args.i;
                    _params[_idx - 1].ParameterName = p.Key;
                }
                List <int> ids = new List <int>();
                EbFormHelper.AddExtraSqlParams(_params, this.DataDB, this.webForm.TableName, this.webForm.TableRowId, this.webForm.LocationId, this.webForm.UserObj.UserId);
                string      qry = nextStage.ApproverEntity == ApproverEntityTypes.Users ? nextStage.ApproverUsers.Code : nextStage.ApproverRoleQuery.Code;
                EbDataTable dt  = this.DataDB.DoQuery(qry, _params.ToArray());
                foreach (EbDataRow dr in dt.Rows)
                {
                    int.TryParse(dr[0].ToString(), out int temp);
                    if (!ids.Contains(temp))
                    {
                        ids.Add(temp);
                    }
                }
                _val = $"'{ids.Join(",")}'";
                if (nextStage.ApproverEntity == ApproverEntityTypes.Users)
                {
                    _col = "user_ids";
                    this.webForm.MyActNotification.UserIds = ids;
                    hasPerm = ids.Any(e => e == this.webForm.UserObj.UserId);
                }
                else
                {
                    _col = "role_ids";
                    this.webForm.MyActNotification.RoleIds = ids;
                    hasPerm = this.webForm.UserObj.RoleIds.Any(e => ids.Contains(e));
                }
            }
            else
            {
                throw new FormException("Unable to process review control", (int)HttpStatusCode.InternalServerError, "Invalid value for ApproverEntity : " + nextStage.ApproverEntity, "From GetMyActionInsertUpdateQuery");
            }
            if (this.webForm.UserObj.Roles.Contains(SystemRoles.SolutionOwner.ToString()) || this.webForm.UserObj.Roles.Contains(SystemRoles.SolutionAdmin.ToString()))
            {
                hasPerm = true;
            }

            return(new string[] { _col, _val });
        }
        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 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);
        }
        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);
        }