public WorkflowRequestProccess GetInicialData(IAMDatabase database) { DbParameterCollection par = new DbParameterCollection(); par.Add("@enterprise_id", typeof(Int64)).Value = this.enterprise_id; par.Add("@request_id", typeof(Int64)).Value = this.workflow_request_id; DataTable dtWorkflowRequest = database.ExecuteDataTable("select r.*, e.full_name, e.id entity_id, e.login, w.name workflow_name, c.enterprise_id from st_workflow_request r with(nolock) inner join st_workflow w with(nolock) on w.id = r.workflow_id inner join entity e with(nolock) on e.id = r.entity_id inner join context c with(nolock) on c.id = e.context_id where r.id = @request_id", CommandType.Text, par, null); if ((dtWorkflowRequest == null) || (dtWorkflowRequest.Rows.Count == 0)) { return(new WorkflowRequestProccess(false, "Access request not found")); } this.status = (WorkflowRequestStatus)((Int32)dtWorkflowRequest.Rows[0]["status"]); this.enterprise_id = (Int64)dtWorkflowRequest.Rows[0]["enterprise_id"]; this.user_name = dtWorkflowRequest.Rows[0]["full_name"].ToString(); this.user_login = dtWorkflowRequest.Rows[0]["login"].ToString(); this.user_id = (Int64)dtWorkflowRequest.Rows[0]["entity_id"]; try { workflow = new WorkflowConfig(); workflow.GetDatabaseData(database, (Int64)dtWorkflowRequest.Rows[0]["workflow_id"]); if (workflow == null) { throw new Exception(""); } } catch (Exception ex) { return(new WorkflowRequestProccess(false, "Fail on get workflow config", ex.Message)); } if ((workflow.Activities == null) || (workflow.Activities.Count == 0)) { return(new WorkflowRequestProccess(false, "Activity list is empty on workflow " + workflow.Name)); } //Verifica o último status para chegar em que activity esta requisição está DataTable dtLogs = database.ExecuteDataTable("select * from st_workflow_request_status where workflow_request_id = @request_id order by date", CommandType.Text, par, null); if ((dtLogs == null) || (dtLogs.Rows.Count == 0)) { return(new WorkflowRequestProccess(false, "Access request status list not found")); } //Resgata a maior activity try { List <Int64> actList = new List <Int64>(); foreach (DataRow dr in dtLogs.Rows) { if (!actList.Contains((Int64)dr["activity_id"])) { actList.Add((Int64)dr["activity_id"]); } } //Ordena de forma descrecente workflow.Activities.Sort(delegate(WorkflowActivity a1, WorkflowActivity a2) { return(a2.ExeutionOrder.CompareTo(a1.ExeutionOrder)); }); //Remove da lista todas as atividades ja aprovadas foreach (WorkflowActivity act in workflow.Activities) { DateTime last = new DateTime(1970, 1, 1); WorkflowRequestStatus st = WorkflowRequestStatus.Waiting; foreach (DataRow drSt in dtLogs.Rows) { if (drSt["activity_id"].ToString() == act.ActivityId.ToString()) { if (last.CompareTo((DateTime)drSt["date"]) < 0) { last = (DateTime)drSt["date"]; st = (WorkflowRequestStatus)((Int32)drSt["status"]); } } } if (st == WorkflowRequestStatus.Approved) { actList.Remove(act.ActivityId); } } //Primeiro busca a menor atividade foreach (WorkflowActivity act in workflow.Activities) { if (activity == null)//Como esta ordenado de forma decrescente, pegará a última atividade do array { activity = act; } if ((actList.Contains(act.ActivityId)) && (act.ExeutionOrder < activity.ExeutionOrder)) { activity = act; } } if (activity == null) { throw new Exception("Activity is empty"); } foreach (DataRow dr in dtLogs.Rows) { if ((Int64)dr["activity_id"] == activity.ActivityId) { if (this.activity_created.Year == 1970) { this.activity_created = (DateTime)dr["date"]; this.last_executed_by = (Int64)dr["executed_by_entity_id"]; } if (this.activity_created.CompareTo((DateTime)dr["date"]) < 0) { this.activity_created = (DateTime)dr["date"]; this.last_executed_by = (Int64)dr["executed_by_entity_id"]; } } } } catch (Exception ex) { return(new WorkflowRequestProccess(false, "Error on proccess activities")); } //Verifica se essa é a última activity //Se sim irá realizar a ação final this.nextActivity = null; foreach (WorkflowActivity act in workflow.Activities) { if (act.ExeutionOrder > activity.ExeutionOrder) { this.nextActivity = act; } } return(new WorkflowRequestProccess(true, "")); }
public void GetDatabaseData(IAMDatabase database, Int64 workflowId, Object transaction) { this.workflow_id = workflowId; DataTable dtWorkflow = database.ExecuteDataTable("select * from st_workflow where id = " + this.workflow_id, CommandType.Text, null, transaction); if ((dtWorkflow == null) || (dtWorkflow.Rows.Count == 0)) { throw new Exception("Workflow not found"); } this.context_id = (Int64)dtWorkflow.Rows[0]["context_id"]; this.name = (String)dtWorkflow.Rows[0]["name"]; this.description = (String)dtWorkflow.Rows[0]["description"]; this.owner = (Int64)dtWorkflow.Rows[0]["owner_id"]; this.enabled = (Boolean)dtWorkflow.Rows[0]["enabled"]; this.deleted = (Boolean)dtWorkflow.Rows[0]["deleted"]; this.deprecated = (Boolean)dtWorkflow.Rows[0]["deprecated"]; this.original_id = (Int64)dtWorkflow.Rows[0]["original_id"]; this.version = (Int64)dtWorkflow.Rows[0]["version"]; this.create_date = (DateTime)dtWorkflow.Rows[0]["create_date"]; switch (dtWorkflow.Rows[0]["type"].ToString().ToLower()) { case "rolegrant": this.access_type = WorkflowAccessType.RoleGrant; break; case "delegation": this.access_type = WorkflowAccessType.Delegation; break; case "unlock": this.access_type = WorkflowAccessType.Unlock; break; default: throw new Exception(String.Format("Access type {0} not implemented yet", dtWorkflow.Rows[0]["type"])); break; } switch (this.access_type) { case WorkflowAccessType.RoleGrant: WorkflowAccessRoleGrant roleGrant = new WorkflowAccessRoleGrant(); DataTable dtRG = database.ExecuteDataTable("select * from st_workflow_access_role where workflow_id = " + this.workflow_id, CommandType.Text, null, transaction); if (dtRG != null) { foreach (DataRow dr in dtRG.Rows) { roleGrant.Add((Int64)dr["role_id"]); } } if ((roleGrant.Roles == null) || (roleGrant.Roles.Count == 0)) { throw new Exception("Role list is empty"); } this.access = roleGrant; break; case WorkflowAccessType.Delegation: WorkflowAccessDelegation entityDelegation = new WorkflowAccessDelegation(); DataTable dtED = database.ExecuteDataTable("select * from st_workflow_access_role where workflow_id = " + this.workflow_id, CommandType.Text, null, transaction); if (dtED != null) { foreach (DataRow dr in dtED.Rows) { entityDelegation.Entity = (Int64)dr["entity_id"]; } } if (entityDelegation.Entity == 0) { throw new Exception("Entity id is empty"); } this.access = entityDelegation; break; case WorkflowAccessType.Unlock: this.access = new WorkflowAccessUnlock(); break; } DataTable dtActivity = database.ExecuteDataTable("select * from st_workflow_activity where workflow_id = " + this.workflow_id + " order by execution_order", CommandType.Text, null, transaction); if (dtActivity != null) { foreach (DataRow dr in dtActivity.Rows) { WorkflowActivity activity = new WorkflowActivity( dr["name"].ToString(), (dr["auto_approval"] == DBNull.Value ? 0 : (Int64)dr["auto_approval"]), (dr["auto_deny"] == DBNull.Value ? 0 : (Int64)dr["auto_deny"]), (Int32)dr["escalation_days"], (Int32)dr["expiration_days"] ); activity.ActivityId = (Int64)dr["id"]; activity.ExeutionOrder = (Int32)dr["execution_order"]; DataTable dtManual = database.ExecuteDataTable("select * from st_workflow_activity_manual_approval where workflow_activity_id = " + activity.ActivityId, CommandType.Text, null, transaction); if (dtManual != null) { foreach (DataRow dr2 in dtManual.Rows) { activity.SetApprover( (dr2["entity_approver"] == DBNull.Value ? 0 : (Int64)dr2["entity_approver"]), (dr2["role_approver"] == DBNull.Value ? 0 : (Int64)dr2["role_approver"]) ); } } if ((activity.AutoDeny == 0) && (activity.AutoDeny == 0) && (activity.ManualApproval == null || (activity.ManualApproval.EntityApprover == 0 && activity.ManualApproval.RoleApprover == 0))) { throw new Exception("All activity approvers is empty in activity " + activity.Name); } this.activities.Add(activity); } } if (this.activities.Count == 0) { throw new Exception("Activity list is empty"); } }
public void ParseFromJsonObject(Dictionary <String, Object> data) { this.context_id = Int64.Parse(data["context_id"].ToString()); this.workflow_id = Int64.Parse(data["workflow_id"].ToString()); this.name = (String)data["name"]; this.description = (String)data["description"]; this.owner = Int64.Parse(data["owner_id"].ToString()); this.enabled = Boolean.Parse(data["enabled"].ToString()); this.deleted = Boolean.Parse(data["deleted"].ToString()); this.deprecated = Boolean.Parse(data["deprecated"].ToString()); this.original_id = Int64.Parse(data["original_id"].ToString()); this.version = Int64.Parse(data["version"].ToString()); this.create_date = new DateTime(1970, 1, 1).AddSeconds(Int64.Parse(data["create_date"].ToString())); switch (data["type"].ToString().ToLower()) { case "rolegrant": this.access_type = WorkflowAccessType.RoleGrant; break; case "delegation": this.access_type = WorkflowAccessType.Delegation; break; case "unlock": this.access_type = WorkflowAccessType.Unlock; break; default: throw new Exception(String.Format("Access type {0} not implemented yet", data["type"])); break; } //if (!(lst[i] is Dictionary<String, Object>)) //if (!(parameters["mapping"] is ArrayList)) if (!(data["access"] is Dictionary <String, Object>)) { throw new Exception("Access is not valid"); } Dictionary <String, Object> access = (Dictionary <String, Object>)data["access"]; switch (this.access_type) { case WorkflowAccessType.RoleGrant: WorkflowAccessRoleGrant roleGrant = new WorkflowAccessRoleGrant(); if (!(access["role_id"] is ArrayList)) { throw new Exception("Access is not valid"); } List <Object> lst = new List <Object>(); lst.AddRange(((ArrayList)access["role_id"]).ToArray()); foreach (Object r in lst) { roleGrant.Add(Int64.Parse(r.ToString())); } if ((roleGrant.Roles == null) || (roleGrant.Roles.Count == 0)) { throw new Exception("Role list is empty"); } this.access = roleGrant; break; case WorkflowAccessType.Delegation: WorkflowAccessDelegation entityDelegation = new WorkflowAccessDelegation(); try { entityDelegation.Entity = Int64.Parse(access["entity_id"].ToString()); } catch { throw new Exception("Access is not valid"); } if (entityDelegation.Entity == 0) { throw new Exception("Entity id is empty"); } this.access = entityDelegation; break; case WorkflowAccessType.Unlock: this.access = new WorkflowAccessUnlock(); break; } if (!(data["activities"] is ArrayList)) { throw new Exception("Activity list is not valid"); } List <Object> act = new List <Object>(); act.AddRange(((ArrayList)data["activities"]).ToArray()); for (Int32 i = 0; i < act.Count; i++) { if (!(act[i] is Dictionary <String, Object>)) { throw new Exception("Activity " + i + " is not valid"); } Dictionary <String, Object> a = (Dictionary <String, Object>)act[i]; WorkflowActivity activity = new WorkflowActivity( a["name"].ToString(), Int64.Parse(a["auto_approval"].ToString()), Int64.Parse(a["auto_deny"].ToString()), Int32.Parse(a["escalation_days"].ToString()), Int32.Parse(a["expiration_days"].ToString()) ); activity.ActivityId = Int64.Parse(a["activity_id"].ToString()); if (a.ContainsKey("manual_approval") && (a["manual_approval"] is Dictionary <string, object>)) { activity.SetApprover( Int64.Parse(((Dictionary <string, object>)a["manual_approval"])["entity_approver"].ToString()), Int64.Parse(((Dictionary <string, object>)a["manual_approval"])["role_approver"].ToString()) ); } if ((activity.AutoDeny == 0) && (activity.AutoDeny == 0) && (activity.ManualApproval == null || (activity.ManualApproval.EntityApprover == 0 && activity.ManualApproval.RoleApprover == 0))) { throw new Exception("All activity approvers is empty in activity " + activity.Name); } this.activities.Add(activity); } if (this.activities.Count == 0) { throw new Exception("Activity list is empty"); } }
public void AddActivity(WorkflowActivity activity) { this.activities.Add(activity); }