From b960fbe7e799bee786c26a66b08857b9ebe5aa6b Mon Sep 17 00:00:00 2001 From: Matthew Winston Date: Mon, 2 Oct 2023 17:26:44 -0400 Subject: [PATCH] Refactor Hydro functions to their own manager --- .../Maturity/HydroMaturityBusiness.cs | 310 ++++++++++++++++++ .../Maturity/MaturityBusiness.cs | 293 ----------------- .../Controllers/MaturityController.cs | 4 +- .../Controllers/ReportsController.cs | 12 +- 4 files changed, 318 insertions(+), 301 deletions(-) create mode 100644 CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/HydroMaturityBusiness.cs diff --git a/CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/HydroMaturityBusiness.cs b/CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/HydroMaturityBusiness.cs new file mode 100644 index 0000000000..041c7e33bb --- /dev/null +++ b/CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/HydroMaturityBusiness.cs @@ -0,0 +1,310 @@ +using CSETWebCore.DataLayer.Model; +using CSETWebCore.Interfaces.AdminTab; +using CSETWebCore.Interfaces.Helpers; +using CSETWebCore.Model.Hydro; +using Microsoft.IdentityModel.Tokens; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CSETWebCore.Business.Maturity +{ + public class HydroMaturityBusiness : MaturityBusiness + { + private CSETContext _context; + public HydroMaturityBusiness(CSETContext context, IAssessmentUtil assessmentUtil, IAdminTabBusiness adminTabBusiness) : base(context, assessmentUtil, adminTabBusiness) + { + this._context = context; + } + + public List GetHydroDonutData(int assessmentId) + { + var result = from question in _context.MATURITY_QUESTIONS + join action in _context.ISE_ACTIONS on question.Mat_Question_Id equals action.Mat_Question_Id + join answer in _context.ANSWER on question.Mat_Question_Id equals answer.Question_Or_Requirement_Id + join answerOption in _context.MATURITY_ANSWER_OPTIONS on answer.Mat_Option_Id equals answerOption.Mat_Option_Id + where question.Maturity_Model_Id == 13 && answer.Answer_Text == "S" && answerOption.Mat_Option_Id == action.Mat_Option_Id && answer.Assessment_Id == assessmentId + select new { question, action, answerOption }; + + List response = new List(); + + foreach (var item in result.Distinct().ToList()) + { + HydroDonutData data = new HydroDonutData() + { + Actions = item.action, + //Answer = item.answer, + AnswerOption = item.answerOption, + Question = item.question + }; + + response.Add(data); + } + + return response; + } + + public List GetHydroActions(int assessmentId) + { + + var result = from subGrouping in _context.MATURITY_GROUPINGS + join domain in _context.MATURITY_GROUPINGS on subGrouping.Parent_Id equals domain.Grouping_Id + join question in _context.MATURITY_QUESTIONS on subGrouping.Grouping_Id equals question.Grouping_Id + join action in _context.ISE_ACTIONS on question.Mat_Question_Id equals action.Mat_Question_Id + join answer in _context.ANSWER on action.Mat_Option_Id equals answer.Mat_Option_Id + where question.Maturity_Model_Id == 13 && answer.Answer_Text == "S" + && answer.Mat_Option_Id == action.Mat_Option_Id + && answer.Assessment_Id == assessmentId + select new { subGrouping, domain, question, action, answer }; + + List actionsByDomains = new List(); + List actionQuestions = new List(); + + if (result.IsNullOrEmpty()) + { + return actionsByDomains; + } + + var currDomain = result.ToList().FirstOrDefault().domain; + + foreach (var item in result.Distinct().ToList()) + { + if (item.domain != currDomain) + { + HydroActionsByDomain domainData = new HydroActionsByDomain() + { + DomainName = currDomain.Title, + DomainSequence = currDomain.Sequence, + ActionsQuestions = actionQuestions + }; + + actionsByDomains.Add(domainData); + currDomain = item.domain; + actionQuestions = new List(); + } + + + if (_context.HYDRO_DATA_ACTIONS.Find(item.answer.Answer_Id) == null) + { + _context.HYDRO_DATA_ACTIONS.Add( + new HYDRO_DATA_ACTIONS() + { + Answer = item.answer, + Answer_Id = item.answer.Answer_Id, + Progress_Id = 1, + Comment = "" + } + ); + _context.SaveChanges(); + } + + HYDRO_DATA_ACTIONS actionData = _context.HYDRO_DATA_ACTIONS.Where(x => x.Answer_Id == item.answer.Answer_Id).FirstOrDefault(); + + actionQuestions.Add( + new HydroActionQuestion() + { + Action = item.action, + Question = item.question, + ActionData = actionData + } + ); + } + + + + actionsByDomains.Add( + new HydroActionsByDomain() + { + DomainName = currDomain.Title, + DomainSequence = currDomain.Sequence, + ActionsQuestions = actionQuestions + } + ); + + return actionsByDomains; + } + + public List GetHydroActionsReport(int assessmentId) + { + + var result = from subGrouping in _context.MATURITY_GROUPINGS + join domain in _context.MATURITY_GROUPINGS on subGrouping.Parent_Id equals domain.Grouping_Id + join question in _context.MATURITY_QUESTIONS on subGrouping.Grouping_Id equals question.Grouping_Id + join action in _context.ISE_ACTIONS on question.Mat_Question_Id equals action.Mat_Question_Id + join answer in _context.ANSWER on action.Mat_Option_Id equals answer.Mat_Option_Id + where question.Maturity_Model_Id == 13 && answer.Answer_Text == "S" + && answer.Mat_Option_Id == action.Mat_Option_Id + && answer.Assessment_Id == assessmentId + select new { subGrouping, domain, question, action, answer }; + + List actionQuestions = new List(); + + if (result.IsNullOrEmpty()) + { + return actionQuestions; + } + + var currDomain = result.ToList().FirstOrDefault()?.domain; + + foreach (var item in result.Distinct().ToList()) + { + if (_context.HYDRO_DATA_ACTIONS.Find(item.answer.Answer_Id) == null) + { + _context.HYDRO_DATA_ACTIONS.Add( + new HYDRO_DATA_ACTIONS() + { + Answer_Id = item.answer.Answer_Id, + Progress_Id = 1, + Comment = "" + } + ); + _context.SaveChanges(); + } + + HYDRO_DATA_ACTIONS actionData = _context.HYDRO_DATA_ACTIONS.Where(x => x.Answer_Id == item.answer.Answer_Id).FirstOrDefault(); + + actionQuestions.Add( + new HydroActionQuestion() + { + Action = item.action, + Question = item.question, + ActionData = actionData + } + ); + }; + + return actionQuestions; + } + + public List GetResultsData(int assessmentId) + { + var response = from answer in _context.ANSWER + join data in _context.HYDRO_DATA on answer.Mat_Option_Id equals data.Mat_Option_Id + join question in _context.MATURITY_QUESTIONS + on answer.Question_Or_Requirement_Id equals question.Mat_Question_Id + join grouping in _context.MATURITY_GROUPINGS + on question.Grouping_Id equals grouping.Grouping_Id + join parentGrouping in _context.MATURITY_GROUPINGS + on grouping.Parent_Id equals parentGrouping.Grouping_Id + where answer.Assessment_Id == assessmentId && answer.Answer_Text == "S" + select new { answer, data, question, grouping, parentGrouping }; + + List resultsList = new List(); + List groupItems = new List(); + int currParentSequence = 0; + int currParentId = 0; + int currQuestionId = 0; + bool notFirst = false; + bool impactLimitNotReached = true; + bool feasibilityLimitNotReached = true; + + HydroImpacts impactTotals = new HydroImpacts(); + HydroFeasibilities feasibilityTotals = new HydroFeasibilities(); + + foreach (var item in response) + { + if (currParentId != item.parentGrouping.Grouping_Id && notFirst) + { + HydroResults results = new HydroResults() + { + HydroData = groupItems, + impactTotals = impactTotals, + feasibilityTotals = feasibilityTotals, + parentGroupId = currParentId, + parentSequence = currParentSequence + }; + resultsList.Add(results); + + groupItems = new List(); // clear the groupItems list + impactTotals = new HydroImpacts(); + feasibilityTotals = new HydroFeasibilities(); + } + + if (currQuestionId != item.question.Mat_Question_Id) + { + impactLimitNotReached = true; + feasibilityLimitNotReached = true; + } + + var impactStrings = item.data.Impact.Split(','); + + if (impactLimitNotReached && (impactStrings.Length != 1 || !string.IsNullOrEmpty(impactStrings[0]))) + { + foreach (string impact in impactStrings) // start here, incorperate the impactLimits per question (checkboxes don't count mulitple times for one question)) + { + if (impact.Equals("1")) + { + impactTotals.Economic++; + impactLimitNotReached = false; + } + else if (impact.Equals("2")) + { + impactTotals.Environmental++; + impactLimitNotReached = false; + } + else if (impact.Equals("3")) + { + impactTotals.Operational++; + impactLimitNotReached = false; + } + else if (impact.Equals("4")) + { + impactTotals.Safety++; + impactLimitNotReached = false; + } + } + } + + var feasibilityStrings = item.data.Feasibility.Split(','); + + if (feasibilityLimitNotReached && (feasibilityStrings.Length != 1 || !string.IsNullOrEmpty(feasibilityStrings[0]))) + { + foreach (string feas in feasibilityStrings) // start here, incorperate the impactLimits per question (checkboxes don't count mulitple times for one question)) + { + if (feas.Equals("1")) + { + feasibilityTotals.Easy++; + feasibilityLimitNotReached = false; + } + else if (feas.Equals("2")) + { + feasibilityTotals.Medium++; + feasibilityLimitNotReached = false; + } + else if (feas.Equals("3")) + { + feasibilityTotals.Hard++; + feasibilityLimitNotReached = false; + } + } + } + + currParentId = item.parentGrouping.Grouping_Id; // update the current ID and name + currParentSequence = item.parentGrouping.Sequence; + currQuestionId = item.question.Mat_Question_Id; + + groupItems.Add(item.data); + notFirst = true; + } + + HydroResults lastResults = new HydroResults() + { + HydroData = groupItems, + impactTotals = impactTotals, + feasibilityTotals = feasibilityTotals, + parentGroupId = currParentId, + parentSequence = currParentSequence + }; + resultsList.Add(lastResults); + + return resultsList; + } + + public List GetHydroProgress() + { + return _context.HYDRO_PROGRESS.ToList(); + } + } +} diff --git a/CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/MaturityBusiness.cs b/CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/MaturityBusiness.cs index ae3824958b..8e04e8388a 100644 --- a/CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/MaturityBusiness.cs +++ b/CSETWebApi/CSETWeb_Api/CSETWebCore.Business/Maturity/MaturityBusiness.cs @@ -22,7 +22,6 @@ using System.Linq; using System.Xml.Linq; using CSETWebCore.Model.Mvra; -using CSETWebCore.Model.Hydro; using J2N; using Microsoft.AspNetCore.Http.Features; using System.ComponentModel; @@ -2478,297 +2477,5 @@ private List MapQuestions(Model.Nested.Grouping g) return questionAnswer; } - - - public List GetHydroDonutData(int assessmentId) - { - var result = from question in _context.MATURITY_QUESTIONS - join action in _context.ISE_ACTIONS on question.Mat_Question_Id equals action.Mat_Question_Id - join answer in _context.ANSWER on question.Mat_Question_Id equals answer.Question_Or_Requirement_Id - join answerOption in _context.MATURITY_ANSWER_OPTIONS on answer.Mat_Option_Id equals answerOption.Mat_Option_Id - where question.Maturity_Model_Id == 13 && answer.Answer_Text == "S" && answerOption.Mat_Option_Id == action.Mat_Option_Id && answer.Assessment_Id == assessmentId - select new { question, action, answerOption }; - - List response = new List(); - - foreach (var item in result.Distinct().ToList()) - { - HydroDonutData data = new HydroDonutData() - { - Actions = item.action, - //Answer = item.answer, - AnswerOption = item.answerOption, - Question = item.question - }; - - response.Add(data); - } - - return response; - } - - - public List GetHydroActions(int assessmentId) - { - - var result = from subGrouping in _context.MATURITY_GROUPINGS - join domain in _context.MATURITY_GROUPINGS on subGrouping.Parent_Id equals domain.Grouping_Id - join question in _context.MATURITY_QUESTIONS on subGrouping.Grouping_Id equals question.Grouping_Id - join action in _context.ISE_ACTIONS on question.Mat_Question_Id equals action.Mat_Question_Id - join answer in _context.ANSWER on action.Mat_Option_Id equals answer.Mat_Option_Id - where question.Maturity_Model_Id == 13 && answer.Answer_Text == "S" - && answer.Mat_Option_Id == action.Mat_Option_Id - && answer.Assessment_Id == assessmentId - select new { subGrouping, domain, question, action, answer }; - - List actionsByDomains = new List(); - List actionQuestions = new List(); - - if (result.IsNullOrEmpty()) - { - return actionsByDomains; - } - - var currDomain = result.ToList().FirstOrDefault().domain; - - foreach (var item in result.Distinct().ToList()) - { - if (item.domain != currDomain) - { - HydroActionsByDomain domainData = new HydroActionsByDomain() - { - DomainName = currDomain.Title, - DomainSequence = currDomain.Sequence, - ActionsQuestions = actionQuestions - }; - - actionsByDomains.Add(domainData); - currDomain = item.domain; - actionQuestions = new List(); - } - - - if (_context.HYDRO_DATA_ACTIONS.Find(item.answer.Answer_Id) == null) - { - _context.HYDRO_DATA_ACTIONS.Add( - new HYDRO_DATA_ACTIONS() - { - Answer = item.answer, - Answer_Id = item.answer.Answer_Id, - Progress_Id = 1, - Comment = "" - } - ); - _context.SaveChanges(); - } - - HYDRO_DATA_ACTIONS actionData = _context.HYDRO_DATA_ACTIONS.Where(x => x.Answer_Id == item.answer.Answer_Id).FirstOrDefault(); - - actionQuestions.Add( - new HydroActionQuestion() - { - Action = item.action, - Question = item.question, - ActionData = actionData - } - ); - } - - - - actionsByDomains.Add( - new HydroActionsByDomain() - { - DomainName = currDomain.Title, - DomainSequence = currDomain.Sequence, - ActionsQuestions = actionQuestions - } - ); - - return actionsByDomains; - } - - - public List GetHydroActionsReport(int assessmentId) - { - - var result = from subGrouping in _context.MATURITY_GROUPINGS - join domain in _context.MATURITY_GROUPINGS on subGrouping.Parent_Id equals domain.Grouping_Id - join question in _context.MATURITY_QUESTIONS on subGrouping.Grouping_Id equals question.Grouping_Id - join action in _context.ISE_ACTIONS on question.Mat_Question_Id equals action.Mat_Question_Id - join answer in _context.ANSWER on action.Mat_Option_Id equals answer.Mat_Option_Id - where question.Maturity_Model_Id == 13 && answer.Answer_Text == "S" - && answer.Mat_Option_Id == action.Mat_Option_Id - && answer.Assessment_Id == assessmentId - select new { subGrouping, domain, question, action, answer }; - - List actionQuestions = new List(); - - if (result.IsNullOrEmpty()) - { - return actionQuestions; - } - - var currDomain = result.ToList().FirstOrDefault()?.domain; - - foreach (var item in result.Distinct().ToList()) - { - if (_context.HYDRO_DATA_ACTIONS.Find(item.answer.Answer_Id) == null) - { - _context.HYDRO_DATA_ACTIONS.Add( - new HYDRO_DATA_ACTIONS() - { - Answer_Id = item.answer.Answer_Id, - Progress_Id = 1, - Comment = "" - } - ); - _context.SaveChanges(); - } - - HYDRO_DATA_ACTIONS actionData = _context.HYDRO_DATA_ACTIONS.Where(x => x.Answer_Id == item.answer.Answer_Id).FirstOrDefault(); - - actionQuestions.Add( - new HydroActionQuestion() - { - Action = item.action, - Question = item.question, - ActionData = actionData - } - ); - }; - - return actionQuestions; - } - - - public List GetResultsData(int assessmentId) - { - var response = from answer in _context.ANSWER - join data in _context.HYDRO_DATA on answer.Mat_Option_Id equals data.Mat_Option_Id - join question in _context.MATURITY_QUESTIONS - on answer.Question_Or_Requirement_Id equals question.Mat_Question_Id - join grouping in _context.MATURITY_GROUPINGS - on question.Grouping_Id equals grouping.Grouping_Id - join parentGrouping in _context.MATURITY_GROUPINGS - on grouping.Parent_Id equals parentGrouping.Grouping_Id - where answer.Assessment_Id == assessmentId && answer.Answer_Text == "S" - select new { answer, data, question, grouping, parentGrouping }; - - List resultsList = new List(); - List groupItems = new List(); - int currParentSequence = 0; - int currParentId = 0; - int currQuestionId = 0; - bool notFirst = false; - bool impactLimitNotReached = true; - bool feasibilityLimitNotReached = true; - - HydroImpacts impactTotals = new HydroImpacts(); - HydroFeasibilities feasibilityTotals = new HydroFeasibilities(); - - foreach (var item in response) - { - if (currParentId != item.parentGrouping.Grouping_Id && notFirst) - { - HydroResults results = new HydroResults() - { - HydroData = groupItems, - impactTotals = impactTotals, - feasibilityTotals = feasibilityTotals, - parentGroupId = currParentId, - parentSequence = currParentSequence - }; - resultsList.Add(results); - - groupItems = new List(); // clear the groupItems list - impactTotals = new HydroImpacts(); - feasibilityTotals = new HydroFeasibilities(); - } - - if (currQuestionId != item.question.Mat_Question_Id) - { - impactLimitNotReached = true; - feasibilityLimitNotReached = true; - } - - var impactStrings = item.data.Impact.Split(','); - - if (impactLimitNotReached && (impactStrings.Length != 1 || !string.IsNullOrEmpty(impactStrings[0]))) - { - foreach (string impact in impactStrings) // start here, incorperate the impactLimits per question (checkboxes don't count mulitple times for one question)) - { - if (impact.Equals("1")) - { - impactTotals.Economic++; - impactLimitNotReached = false; - } - else if (impact.Equals("2")) - { - impactTotals.Environmental++; - impactLimitNotReached = false; - } - else if (impact.Equals("3")) - { - impactTotals.Operational++; - impactLimitNotReached = false; - } - else if (impact.Equals("4")) - { - impactTotals.Safety++; - impactLimitNotReached = false; - } - } - } - - var feasibilityStrings = item.data.Feasibility.Split(','); - - if (feasibilityLimitNotReached && (feasibilityStrings.Length != 1 || !string.IsNullOrEmpty(feasibilityStrings[0]))) - { - foreach (string feas in feasibilityStrings) // start here, incorperate the impactLimits per question (checkboxes don't count mulitple times for one question)) - { - if (feas.Equals("1")) - { - feasibilityTotals.Easy++; - feasibilityLimitNotReached = false; - } - else if (feas.Equals("2")) - { - feasibilityTotals.Medium++; - feasibilityLimitNotReached = false; - } - else if (feas.Equals("3")) - { - feasibilityTotals.Hard++; - feasibilityLimitNotReached = false; - } - } - } - - currParentId = item.parentGrouping.Grouping_Id; // update the current ID and name - currParentSequence = item.parentGrouping.Sequence; - currQuestionId = item.question.Mat_Question_Id; - - groupItems.Add(item.data); - notFirst = true; - } - - HydroResults lastResults = new HydroResults() - { - HydroData = groupItems, - impactTotals = impactTotals, - feasibilityTotals = feasibilityTotals, - parentGroupId = currParentId, - parentSequence = currParentSequence - }; - resultsList.Add(lastResults); - - return resultsList; - } - - public List GetHydroProgress() - { - return _context.HYDRO_PROGRESS.ToList(); - } } } diff --git a/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/MaturityController.cs b/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/MaturityController.cs index 068b436d77..72a2aefd37 100644 --- a/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/MaturityController.cs +++ b/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/MaturityController.cs @@ -308,7 +308,7 @@ public IActionResult GetResultsData() { int assessmentId = _tokenManager.AssessmentForUser(); - return Ok(new MaturityBusiness(_context, _assessmentUtil, _adminTabBusiness).GetResultsData(assessmentId)); + return Ok(new HydroMaturityBusiness(_context, _assessmentUtil, _adminTabBusiness).GetResultsData(assessmentId)); } @@ -318,7 +318,7 @@ public IActionResult getProgressText() { int assessmentId = _tokenManager.AssessmentForUser(); - return Ok(new MaturityBusiness(_context, _assessmentUtil, _adminTabBusiness).GetHydroProgress()); + return Ok(new HydroMaturityBusiness(_context, _assessmentUtil, _adminTabBusiness).GetHydroProgress()); } diff --git a/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/ReportsController.cs b/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/ReportsController.cs index b3d3e62f6d..8d7266b2a0 100644 --- a/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/ReportsController.cs +++ b/CSETWebApi/CSETWeb_Api/CSETWeb_ApiCore/Controllers/ReportsController.cs @@ -364,9 +364,9 @@ public IActionResult GetHydroDonutData() int assessmentId = _token.AssessmentForUser(); _context.FillEmptyMaturityQuestionsForAnalysis(assessmentId); - var mm = new MaturityBusiness(_context, _assessmentUtil, _adminTabBusiness); + var hmm = new HydroMaturityBusiness(_context, _assessmentUtil, _adminTabBusiness); - return Ok(mm.GetHydroDonutData(assessmentId)); + return Ok(hmm.GetHydroDonutData(assessmentId)); } @@ -381,9 +381,9 @@ public IActionResult GetHydroActionItems() int assessmentId = _token.AssessmentForUser(); _context.FillEmptyMaturityQuestionsForAnalysis(assessmentId); - var mm = new MaturityBusiness(_context, _assessmentUtil, _adminTabBusiness); + var hmm = new HydroMaturityBusiness(_context, _assessmentUtil, _adminTabBusiness); - return Ok(mm.GetHydroActions(assessmentId)); + return Ok(hmm.GetHydroActions(assessmentId)); } @@ -398,9 +398,9 @@ public IActionResult GetHydroActionItemsReport() int assessmentId = _token.AssessmentForUser(); _context.FillEmptyMaturityQuestionsForAnalysis(assessmentId); - var mm = new MaturityBusiness(_context, _assessmentUtil, _adminTabBusiness); + var hmm = new HydroMaturityBusiness(_context, _assessmentUtil, _adminTabBusiness); - return Ok(mm.GetHydroActionsReport(assessmentId)); + return Ok(hmm.GetHydroActionsReport(assessmentId)); }