public AssessmentDetail CreateNewAssessment(int currentUserId) { DateTime nowUTC = Utilities.UtcToLocal(DateTime.UtcNow); AssessmentDetail newAssessment = new AssessmentDetail { AssessmentName = "New Assessment", AssessmentDate = nowUTC, CreatorId = currentUserId, CreatedDate = nowUTC, LastModifiedDate = nowUTC }; // Commit the new assessment int assessment_id = SaveAssessmentDetail(0, newAssessment); newAssessment.Id = assessment_id; // Add the current user to the new assessment as an admin that has already been 'invited' ContactsManager contactManager = new ContactsManager(); contactManager.AddContactToAssessment(assessment_id, currentUserId, Constants.AssessmentAdminId, true); new SalManager().SetDefaultSALs(assessment_id); new StandardsManager().PersistSelectedStandards(assessment_id, null); CreateIrpHeaders(assessment_id); return(newAssessment); }
public async Task <IActionResult> Save(List <AssessmentFirstLangCreateTestDetailVm> vm) { var questionsToAdd = new List <Question>(); string fullLink = $"{baseUri}/Assessment/{2}"; var langAssessment = WebApiService.GetApiResult <Assessment>(fullLink); try { foreach (var question in vm) { if (question.IsToBeAdded) { var questionToAdd = WebApiService.GetApiResult <Question>($"{baseUri}/Question/{question.QuestionId}"); questionsToAdd.Add(questionToAdd); } } var assessment = new AssessmentDetail() { AssessmentTitle = $"{langAssessment.AssessmentTitle}, {DateTime.Now.Date}", Questions = questionsToAdd, AssessmentId = 2, Assessment = langAssessment }; await WebApiService.PostCallApi <AssessmentDetail, AssessmentDetail>($"{baseUri}/assessmentdetail", assessment); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { Debug.WriteLine(ex.Message); } return(RedirectToAction(nameof(Index))); }
/// <summary> /// /// </summary> /// <returns></returns> private void GetMaturityModelDetails(ref AssessmentDetail assessment, CSET_Context db) { int assessmentId = assessment.Id; var maturityManager = new MaturityManager(); assessment.MaturityModel = maturityManager.GetMaturityModel(assessmentId); }
/// <summary> /// /// </summary> /// <param name="assessment"></param> /// <param name="db"></param> private void GetSelectedStandards(ref AssessmentDetail assessment, CSET_Context db) { var assessmentId = assessment.Id; var standardsList = db.AVAILABLE_STANDARDS.Where(x => x.Assessment_Id == assessmentId && x.Selected).ToList(); assessment.Standards = new List <string>(); foreach (var s in standardsList) { assessment.Standards.Add(s.Set_Name); } }
public int Post([FromBody] AssessmentDetail assessmentDetail) { // validate the assessment for the user int assessmentId = Auth.AssessmentForUser(); if (assessmentId != assessmentDetail.Id) { throw new CSETApplicationException("Not currently authorized to update the Assessment", null); } AssessmentManager assessmentManager = new AssessmentManager(); return(assessmentManager.SaveAssessmentDetail(assessmentId, assessmentDetail)); }
/// <summary> /// Set features based on existence of data. This is used for assessments that were /// created prior to incorporating features into the assessment data model. /// </summary> /// <param name="assessment"></param> private void DetermineFeaturesFromData(ref AssessmentDetail assessment, CSET_Context db) { var a = assessment; if (db.AVAILABLE_STANDARDS.Any(x => x.Assessment_Id == a.Id)) { assessment.UseStandard = true; } if (db.ASSESSMENT_DIAGRAM_COMPONENTS.Any(x => x.Assessment_Id == a.Id)) { BusinessManagers.DiagramManager dm = new BusinessManagers.DiagramManager(db); assessment.UseDiagram = dm.HasDiagram(a.Id); } // determine if there are maturity answers and attach maturity models var maturityAnswers = db.ANSWER.Where(x => x.Assessment_Id == a.Id && x.Question_Type.ToLower() == "maturity").ToList(); if (maturityAnswers.Count > 0) { assessment.UseMaturity = true; if (!db.AVAILABLE_MATURITY_MODELS.Any(x => x.Assessment_Id == a.Id)) { // determine the maturity models represented by the questions that have been answered var qqq = db.MATURITY_QUESTIONS.Where(q => maturityAnswers.Select(x => x.Question_Or_Requirement_Id).Contains(q.Mat_Question_Id)).ToList(); var maturityModelIds = qqq.Select(x => x.Maturity_Model_Id).Distinct().ToList(); foreach (var modelId in maturityModelIds) { var mm = new AVAILABLE_MATURITY_MODELS() { Assessment_Id = a.Id, model_id = modelId, Selected = true }; db.AVAILABLE_MATURITY_MODELS.Add(mm); db.SaveChanges(); // get the newly-attached model for the response var mmm = new MaturityManager(); assessment.MaturityModel = mmm.GetMaturityModel(a.Id); } } } SaveAssessmentDetail(a.Id, assessment); }
/// <summary> /// Set features based on existence of data. /// </summary> /// <param name="assessment"></param> private void DetermineFeaturesFromData(ref AssessmentDetail assessment, CSET_Context db) { var a = assessment; if (db.AVAILABLE_STANDARDS.Any(x => x.Assessment_Id == a.Id)) { assessment.UseStandard = true; } if (db.ASSESSMENT_DIAGRAM_COMPONENTS.Any(x => x.Assessment_Id == a.Id)) { BusinessManagers.DiagramManager dm = new BusinessManagers.DiagramManager(db); assessment.UseDiagram = dm.HasDiagram(a.Id); } }
/// <summary> /// Persists data to the ASSESSMENTS and INFORMATION tables. /// Date fields should be converted to UTC before sending the Assessment /// to this method. /// </summary> /// <param name="assessment"></param> /// <returns></returns> public int SaveAssessmentDetail(int assessmentId, AssessmentDetail assessment) { var db = new DataLayerCore.Model.CSET_Context(); TokenManager tm = new TokenManager(); string app_code = tm.Payload(Constants.Token_Scope); // Add or update the ASSESSMENT record var dbAssessment = new ASSESSMENTS() { Assessment_Id = assessment.Id, AssessmentCreatedDate = assessment.CreatedDate, AssessmentCreatorId = assessment.CreatorId, Assessment_Date = assessment.AssessmentDate ?? DateTime.Now, LastAccessedDate = assessment.LastModifiedDate, Charter = string.IsNullOrEmpty(assessment.Charter) ? string.Empty : assessment.Charter.PadLeft(5, '0'), CreditUnionName = assessment.CreditUnion, Assets = assessment.Assets, MatDetail_targetBandOnly = app_code == "ACET" }; db.ASSESSMENTS.AddOrUpdate(dbAssessment, x => x.Assessment_Id); db.SaveChanges(); assessmentId = dbAssessment.Assessment_Id; var user = db.USERS.FirstOrDefault(x => x.UserId == dbAssessment.AssessmentCreatorId); // then use its key for the INFORMATION record var dbInfo = new INFORMATION { Id = assessmentId, Assessment_Name = assessment.AssessmentName, Facility_Name = assessment.FacilityName, City_Or_Site_Name = assessment.CityOrSiteName, State_Province_Or_Region = assessment.StateProvRegion, Executive_Summary = assessment.ExecutiveSummary, Assessment_Description = assessment.AssessmentDescription, Additional_Notes_And_Comments = assessment.AdditionalNotesAndComments, IsAcetOnly = assessment.IsAcetOnly }; db.INFORMATION.AddOrUpdate(dbInfo, x => x.Id); db.SaveChanges(); AssessmentUtil.TouchAssessment(assessmentId); return(dbInfo.Id); }
/// <summary> /// Persists data to the ASSESSMENTS and INFORMATION tables. /// Date fields should be converted to UTC before sending the Assessment /// to this method. /// </summary> /// <param name="assessment"></param> /// <returns></returns> public int SaveAssessmentDetail(int assessmentId, AssessmentDetail assessment) { var db = new DataLayerCore.Model.CSET_Context(); // Add or update the ASSESSMENT record var dbAssessment = new ASSESSMENTS() { Assessment_Id = assessment.Id, AssessmentCreatedDate = assessment.CreatedDate, AssessmentCreatorId = assessment.CreatorId, Assessment_Date = assessment.AssessmentDate ?? DateTime.Now, LastAccessedDate = assessment.LastModifiedDate, Charter = assessment.Charter, CreditUnionName = assessment.CreditUnion, Assets = assessment.Assets }; db.ASSESSMENTS.AddOrUpdate(dbAssessment, x => x.Assessment_Id); db.SaveChanges(); assessmentId = dbAssessment.Assessment_Id; var user = db.USERS.FirstOrDefault(x => x.UserId == dbAssessment.AssessmentCreatorId); // then use its key for the INFORMATION record var dbInfo = new INFORMATION { Id = assessmentId, Assessment_Name = assessment.AssessmentName, Facility_Name = assessment.FacilityName, City_Or_Site_Name = assessment.CityOrSiteName, State_Province_Or_Region = assessment.StateProvRegion, Executive_Summary = assessment.ExecutiveSummary, Assessment_Description = assessment.AssessmentDescription, Additional_Notes_And_Comments = assessment.AdditionalNotesAndComments }; db.INFORMATION.AddOrUpdate(dbInfo, x => x.Id); db.SaveChanges(); AssessmentUtil.TouchAssessment(assessmentId); return(dbInfo.Id); }
/// <summary> /// Returns the details for the specified Assessment. /// </summary> /// <param name="assessmentId"></param> /// <returns></returns> public AssessmentDetail GetAssessmentDetail(int assessmentId) { AssessmentDetail assessment = new AssessmentDetail(); TokenManager tm = new TokenManager(); string app_code = tm.Payload(Constants.Token_Scope); using (var db = new CSET_Context()) { var query = (from ii in db.INFORMATION join aa in db.ASSESSMENTS on ii.Id equals aa.Assessment_Id where ii.Id == assessmentId select new { ii, aa }); var result = query.ToList().FirstOrDefault(); if (result != null) { assessment.Id = result.aa.Assessment_Id; assessment.AssessmentName = result.ii.Assessment_Name; assessment.AssessmentDate = result.aa.Assessment_Date; assessment.FacilityName = result.ii.Facility_Name; assessment.CityOrSiteName = result.ii.City_Or_Site_Name; assessment.StateProvRegion = result.ii.State_Province_Or_Region; assessment.ExecutiveSummary = result.ii.Executive_Summary; assessment.AssessmentDescription = result.ii.Assessment_Description; assessment.AdditionalNotesAndComments = result.ii.Additional_Notes_And_Comments; assessment.CreatorId = result.aa.AssessmentCreatorId ?? 0; assessment.CreatedDate = Utilities.UtcToLocal(result.aa.AssessmentCreatedDate); assessment.LastModifiedDate = Utilities.UtcToLocal((DateTime)result.aa.LastAccessedDate); bool defaultAcet = (app_code == "ACET"); assessment.IsAcetOnly = result.ii.IsAcetOnly != null ? result.ii.IsAcetOnly : defaultAcet; assessment.Charter = string.IsNullOrEmpty(result.aa.Charter) ? "" : result.aa.Charter; assessment.CreditUnion = result.aa.CreditUnionName; assessment.Assets = result.aa.Assets; // Fields located on the Overview page assessment.ExecutiveSummary = result.ii.Executive_Summary; assessment.AssessmentDescription = result.ii.Assessment_Description; assessment.AdditionalNotesAndComments = result.ii.Additional_Notes_And_Comments; } return(assessment); } }
/// <summary> /// /// </summary> /// <returns></returns> private void GetMaturityModelDetails(ref AssessmentDetail assessment, CSET_Context db) { int assessmentId = assessment.Id; var query = from avm in db.AVAILABLE_MATURITY_MODELS join mm in db.MATURITY_MODELS on avm.model_id equals mm.Maturity_Model_Id where avm.Assessment_Id == assessmentId select new { mm.Maturity_Model_Id, mm.Model_Name }; var q1 = query.FirstOrDefault(); if (q1 != null) { assessment.MaturityModelId = q1.Maturity_Model_Id; assessment.MaturityModelName = q1.Model_Name; } var ml = db.ASSESSMENT_SELECTED_LEVELS.Where(l => l.Assessment_Id == assessmentId && l.Level_Name == "Maturity_Level").FirstOrDefault(); if (ml != null) { assessment.MaturityTargetLevel = int.Parse(ml.Standard_Specific_Sal_Level); } }
public AssessmentDetail CreateNewAssessmentForImport(int currentUserId) { DateTime nowUTC = DateTime.Now; AssessmentDetail newAssessment = new AssessmentDetail { AssessmentName = "New Assessment", AssessmentDate = nowUTC, CreatorId = currentUserId, CreatedDate = nowUTC, LastModifiedDate = nowUTC }; // Commit the new assessment int assessment_id = SaveAssessmentDetail(0, newAssessment); newAssessment.Id = assessment_id; // Add the current user to the new assessment as an admin that has already been 'invited' ContactsManager contactManager = new ContactsManager(); contactManager.AddContactToAssessment(assessment_id, currentUserId, Constants.AssessmentAdminId, true); return(newAssessment); }
public int RunImportManualPortion(UploadAssessmentModel model, int currentUserId, string primaryEmail , CSET_Context db) { //create the new assessment //copy each of the items to the table //as the copy occurs change to the current assessment_id //update the answer id's Dictionary <int, DOCUMENT_FILE> oldIdToNewDocument = new Dictionary <int, DOCUMENT_FILE>(); AssessmentManager man = new AssessmentManager(); AssessmentDetail detail = man.CreateNewAssessmentForImport(currentUserId); int _assessmentId = detail.Id; Dictionary <int, int> oldAnswerId = new Dictionary <int, int>(); Dictionary <int, ANSWER> oldIdNewAnswer = new Dictionary <int, ANSWER>(); Dictionary <String, int> oldUserNewUser = db.USERS.ToDictionary(x => x.PrimaryEmail, y => y.UserId); foreach (var a in model.jASSESSMENTS) { var item = db.ASSESSMENTS.Where(x => x.Assessment_Id == _assessmentId).FirstOrDefault(); if (item != null) { item.Assets = a.Assets; item.Charter = a.Charter; item.CreditUnionName = a.CreditUnionName; item.IRPTotalOverride = a.IRPTotalOverride; item.IRPTotalOverrideReason = a.IRPTotalOverrideReason; item.MatDetail_targetBandOnly = a.MatDetail_targetBandOnly != null ? a.MatDetail_targetBandOnly : false; db.SaveChanges(); } } // go through the assessment contacts and // if the contact does exist create it then add the id // if the contact does exist update the id foreach (var a in model.jASSESSMENT_CONTACTS.Where(x => x.PrimaryEmail != primaryEmail)) { var item = TinyMapper.Map <ASSESSMENT_CONTACTS>(a); item.Assessment_Id = _assessmentId; item.PrimaryEmail = a.PrimaryEmail; int userid; if (oldUserNewUser.TryGetValue(a.PrimaryEmail, out userid)) { item.UserId = userid; } else { item.UserId = null; } db.ASSESSMENT_CONTACTS.Add(item); } db.SaveChanges(); foreach (var a in model.jUSER_DETAIL_INFORMATION) { if (db.USER_DETAIL_INFORMATION.Where(x => x.Id == a.Id).FirstOrDefault() == null) { var userInfo = TinyMapper.Map <USER_DETAIL_INFORMATION>(a); userInfo.FirstName = String.IsNullOrWhiteSpace(a.FirstName) ? "First Name" : a.FirstName; userInfo.LastName = String.IsNullOrWhiteSpace(a.LastName) ? "Last Name" : a.LastName; db.USER_DETAIL_INFORMATION.Add(userInfo); foreach (var b in a.jADDRESSes) { var item = TinyMapper.Map <ADDRESS>(b); item.AddressType = "Imported"; db.ADDRESS.Add(item); } db.SaveChanges(); } } return(_assessmentId); #region RKW Oct '19 - These table conversions are being commented out, so that the GenericImporter can do it. //foreach (var a in model.jANSWER) //{ // a.Assessment_Id = _assessmentId; // a.Old_Answer_Id = a.Answer_Id; // a.Answer_Id = 0; //} //db.SaveChanges(); //var objBulk = new BulkUploadToSql<jANSWER>() //{ // InternalStore = model.jANSWER, // TableName = "ANSWER", // CommitBatchSize = 1000, // ConnectionString = ((Microsoft.EntityFrameworkCore.DbContext)db).Database.GetDbConnection().ConnectionString //}; //objBulk.Commit(); //oldAnswerId = db.ANSWER.Where(x => x.Assessment_Id == _assessmentId).ToDictionary(x => x.Old_Answer_Id ?? 0, y => y.Answer_Id); //oldIdNewAnswer = db.ANSWER.Where(x => x.Assessment_Id == _assessmentId).ToDictionary(x => x.Old_Answer_Id ?? 0, y => y); //if (model.jSTANDARD_SELECTION.Count > 0) //{ // foreach (var a in model.jSTANDARD_SELECTION) // { // var item = TinyMapper.Map<STANDARD_SELECTION>(a); // item.Assessment_Id = _assessmentId; // db.STANDARD_SELECTION.Add(item); // } //} //else //{ // db.STANDARD_SELECTION.Add(new STANDARD_SELECTION() // { // Application_Mode = AssessmentModeData.QUESTIONS_BASED_APPLICATION_MODE, // Selected_Sal_Level = Constants.SAL_LOW, // Assessment_Id = _assessmentId, // Last_Sal_Determination_Type = Constants.SIMPLE_SAL, // Is_Advanced = false // }); //} //foreach (var a in model.jASSESSMENT_SELECTED_LEVELS) //{ // var item = TinyMapper.Map<ASSESSMENT_SELECTED_LEVELS>(a); // item.Assessment_Id = _assessmentId; // db.ASSESSMENT_SELECTED_LEVELS.Add(item); //} //foreach (var a in model.jAVAILABLE_STANDARDS) //{ // var item = TinyMapper.Map<AVAILABLE_STANDARDS>(a); // item.Assessment_Id = _assessmentId; // db.AVAILABLE_STANDARDS.Add(item); //} //foreach (var a in model.jCNSS_CIA_JUSTIFICATIONS) //{ // var item = TinyMapper.Map<CNSS_CIA_JUSTIFICATIONS>(a); // item.Assessment_Id = _assessmentId; // db.CNSS_CIA_JUSTIFICATIONS.Add(item); //} //foreach (var a in model.jCUSTOM_BASE_STANDARDS) { var item = TinyMapper.Map<CUSTOM_BASE_STANDARDS>(a); db.CUSTOM_BASE_STANDARDS.Add(item); } //foreach (var a in model.jCUSTOM_QUESTIONAIRES) { var item = TinyMapper.Map<CUSTOM_QUESTIONAIRES>(a); db.CUSTOM_QUESTIONAIRES.Add(item); } //foreach (var a in model.jCUSTOM_QUESTIONAIRE_QUESTIONS) //{ // var item = TinyMapper.Map<CUSTOM_QUESTIONAIRE_QUESTIONS>(a); db.CUSTOM_QUESTIONAIRE_QUESTIONS.Add(item); //} //foreach (var a in model.jCUSTOM_STANDARD_BASE_STANDARD) //{ // var item = TinyMapper.Map<CUSTOM_STANDARD_BASE_STANDARD>(a); db.CUSTOM_STANDARD_BASE_STANDARD.Add(item); //} //foreach (var a in model.jDEMOGRAPHICS) //{ // var item = TinyMapper.Map<DEMOGRAPHICS>(a); // item.Assessment_Id = _assessmentId; // if ((a.IndustryId == 0) || (a.SectorId == 0)) // { // //what do we want to do for a default? // } // else // db.DEMOGRAPHICS.Add(item); //} ////this needs the answers inserted first ////then the documents and finally ////we can associate documents and answers ////look at adding a reference to the answer to jDocument_File ////then as we iterate through the answers and documents keep the references //foreach (var a in model.jDOCUMENT_FILE) //{ // var item = TinyMapper.Map<DOCUMENT_FILE>(a); // oldIdToNewDocument.Add(a.Document_Id, item); // item.Assessment_Id = _assessmentId; // item.Document_Id = 0; // db.DOCUMENT_FILE.Add(item); //} //db.SaveChanges(); //foreach (var a in model.jDOCUMENT_ANSWERS) //{ // var item = oldIdToNewDocument[a.Document_Id]; // db.DOCUMENT_ANSWERS.Add(new DOCUMENT_ANSWERS() // { // Answer_Id = oldIdNewAnswer[a.Answer_Id].Answer_Id, // Document_Id = item.Document_Id // }); //} //Dictionary<int, FINDING> idToFinding = new Dictionary<int, FINDING>(); //foreach (var a in model.jFINDING) //{ // var item = TinyMapper.Map<FINDING>(a); // item.Importance_Id = item.Importance_Id == 0 ? 1 : item.Importance_Id; // item.Answer_Id = oldAnswerId[a.Answer_Id]; // idToFinding.Add(a.Finding_Id, item); // db.FINDING.Add(item); //} //var AcontactID = db.ASSESSMENT_CONTACTS.Where(x => x.UserId == currentUserId).FirstOrDefault(); //if (AcontactID != null)//if we dont have a current user we are in trouble //{ // int acid = AcontactID.Assessment_Contact_Id; // foreach (var a in model.jFINDING_CONTACT) // { // db.FINDING_CONTACT.Add(new FINDING_CONTACT() // { // Assessment_Contact_Id = a.Assessment_Contact_Id == 0 ? acid : a.Assessment_Contact_Id, // Finding_Id = idToFinding[a.Finding_Id].Finding_Id, // Id = a.Old_Contact_Id // }); // } //} //foreach (var a in model.jFRAMEWORK_TIER_TYPE_ANSWER) //{ // var item = TinyMapper.Map<FRAMEWORK_TIER_TYPE_ANSWER>(a); // item.Assessment_Id = _assessmentId; // db.FRAMEWORK_TIER_TYPE_ANSWER.Add(item); //} //foreach (var a in model.jGENERAL_SAL) //{ // var item = TinyMapper.Map<GENERAL_SAL>(a); // item.Assessment_Id = _assessmentId; // db.GENERAL_SAL.Add(item); //} //foreach (var a in model.jINFORMATION) //{ // var info = db.INFORMATION.Where(x => x.Id == _assessmentId).FirstOrDefault(); // if (info != null) // { // TinyMapper.Map(a, info); // info.Id = _assessmentId; // db.SaveChanges(); // } // else // { // var item = TinyMapper.Map<INFORMATION>(a); // item.Id = _assessmentId; // db.INFORMATION.Add(item); // } // db.ASSESSMENTS.Where(x => x.Assessment_Id == _assessmentId).First().Assessment_Date = a.Assessment_Date; // db.SaveChanges(); //} //foreach (var a in model.jNIST_SAL_INFO_TYPES) //{ // var item = TinyMapper.Map<NIST_SAL_INFO_TYPES>(a); // item.Assessment_Id = _assessmentId; // db.NIST_SAL_INFO_TYPES.Add(item); //} //foreach (var a in model.jNIST_SAL_QUESTION_ANSWERS) //{ // var item = TinyMapper.Map<NIST_SAL_QUESTION_ANSWERS>(a); // item.Question_Answer = item.Question_Answer ?? "No"; // item.Assessment_Id = _assessmentId; // db.NIST_SAL_QUESTION_ANSWERS.Add(item); // db.SaveChanges(); //} //foreach (var a in model.jPARAMETER_VALUES) //{ // var item = TinyMapper.Map<PARAMETER_VALUES>(a); // item.Answer_Id = oldAnswerId[a.Answer_Id]; // db.PARAMETER_VALUES.Add(item); //} //foreach (var a in model.jPARAMETER_ASSESSMENTs) //{ // var item = TinyMapper.Map<PARAMETER_ASSESSMENT>(a); // item.Assessment_ID = _assessmentId; // db.PARAMETER_ASSESSMENT.Add(item); //} //foreach (var a in model.jSUB_CATEGORY_ANSWERS) //{ // var item = TinyMapper.Map<SUB_CATEGORY_ANSWERS>(a); // item.Assessement_Id = _assessmentId; // if ((a.Question_Group_Heading_Id > 0) && (a.Universal_Sub_Category_Id > 0)) // { // var header = db.UNIVERSAL_SUB_CATEGORY_HEADINGS.Where(x => x.Question_Group_Heading_Id == a.Question_Group_Heading_Id && x.Universal_Sub_Category_Id == a.Universal_Sub_Category_Id).FirstOrDefault(); // if (header != null) // { // item.Heading_Pair_Id = header.Heading_Pair_Id; // } // else // { // try // { // var tempHeading = db.UNIVERSAL_SUB_CATEGORIES.Where(x => x.Universal_Sub_Category_Id == a.Universal_Sub_Category_Id).FirstOrDefault(); // if (tempHeading != null) // { // var adding = new UNIVERSAL_SUB_CATEGORY_HEADINGS() // { // Display_Radio_Buttons = false, // Question_Group_Heading_Id = a.Question_Group_Heading_Id, // Universal_Sub_Category_Id = a.Universal_Sub_Category_Id, // Sub_Heading_Question_Description = null // }; // //see if we can create the record // db.UNIVERSAL_SUB_CATEGORY_HEADINGS.Add(adding); // db.SaveChanges(); // item.Heading_Pair_Id = adding.Heading_Pair_Id; // } // } // catch // { // //silent throw away NOT MY FAVORITE // //but ok because there is nothing else we can do here. // } // } // } // if (!String.IsNullOrWhiteSpace(a.Question_Group_Heading) && !String.IsNullOrWhiteSpace(a.Universal_Sub_Category)) // { // var header = db.vQUESTION_HEADINGS.Where(x => x.Question_Group_Heading == a.Question_Group_Heading && x.Universal_Sub_Category == a.Universal_Sub_Category).FirstOrDefault(); // if (header == null) // {//try by id's // var header2 = db.UNIVERSAL_SUB_CATEGORY_HEADINGS.Where(x => x.Question_Group_Heading_Id == a.Question_Group_Heading_Id && x.Universal_Sub_Category_Id == a.Universal_Sub_Category_Id).FirstOrDefault(); // if (header2 != null) // item.Heading_Pair_Id = header2.Heading_Pair_Id; // } // else // { // item.Heading_Pair_Id = header.Heading_Pair_Id; // } // } // if (item.Heading_Pair_Id > 0) // db.SUB_CATEGORY_ANSWERS.Add(item); //} //// //// NCUA data //// //List<int> supportedDocIds = db.REQUIRED_DOCUMENTATION.Select(d => d.Documentation_Id).ToList(); //foreach (var a in model.jASSESSMENTS_REQUIRED_DOCUMENTATION) //{ // if (supportedDocIds.Contains(a.Documentation_Id)) // { // var item = TinyMapper.Map<ASSESSMENTS_REQUIRED_DOCUMENTATION>(a); // item.Assessment_Id = _assessmentId; // db.ASSESSMENTS_REQUIRED_DOCUMENTATION.Add(item); //} //} //foreach (var a in model.jFINANCIAL_ASSESSMENT_VALUES) //{ // var item = TinyMapper.Map<FINANCIAL_ASSESSMENT_VALUES>(a); // item.Assessment_Id = _assessmentId; // db.FINANCIAL_ASSESSMENT_VALUES.Add(item); //} //foreach (var a in model.jFINANCIAL_HOURS) //{ // var item = TinyMapper.Map<FINANCIAL_HOURS>(a); // item.Assessment_Id = _assessmentId; // db.FINANCIAL_HOURS.Add(item); //} //foreach (var a in model.jASSESSMENT_IRP_HEADER) //{ // var item = TinyMapper.Map<ASSESSMENT_IRP_HEADER>(a); // item.ASSESSMENT_ID = _assessmentId; // db.ASSESSMENT_IRP_HEADER.Add(item); //} //foreach (var a in model.jASSESSMENT_IRP) //{ // var item = TinyMapper.Map<ASSESSMENT_IRP>(a); // item.Assessment_Id = _assessmentId; // item.Answer_Id = 0; // db.ASSESSMENT_IRP.Add(item); //} //try //{ // db.SaveChanges(); //} //catch (Exception e) //{ // throw e; //} #endregion }
/// <summary> /// Persists data to the ASSESSMENTS and INFORMATION tables. /// Date fields should be converted to UTC before sending the Assessment /// to this method. /// </summary> /// <param name="assessment"></param> /// <returns></returns> public int SaveAssessmentDetail(int assessmentId, AssessmentDetail assessment) { using (var db = new DataLayerCore.Model.CSET_Context()) { TokenManager tm = new TokenManager(); string app_code = tm.Payload(Constants.Token_Scope); // Add or update the ASSESSMENTS record var dbAssessment = db.ASSESSMENTS.Where(x => x.Assessment_Id == assessmentId).FirstOrDefault(); if (dbAssessment == null) { dbAssessment = new ASSESSMENTS(); db.ASSESSMENTS.Add(dbAssessment); db.SaveChanges(); assessmentId = dbAssessment.Assessment_Id; } dbAssessment.Assessment_Id = assessmentId; dbAssessment.AssessmentCreatedDate = assessment.CreatedDate; dbAssessment.AssessmentCreatorId = assessment.CreatorId; dbAssessment.Assessment_Date = assessment.AssessmentDate ?? DateTime.Now; dbAssessment.LastAccessedDate = assessment.LastModifiedDate; dbAssessment.Charter = string.IsNullOrEmpty(assessment.Charter) ? string.Empty : assessment.Charter.PadLeft(5, '0'); dbAssessment.CreditUnionName = assessment.CreditUnion; dbAssessment.Assets = assessment.Assets; dbAssessment.MatDetail_targetBandOnly = (app_code == "ACET"); dbAssessment.AnalyzeDiagram = false; db.ASSESSMENTS.AddOrUpdate(dbAssessment, x => x.Assessment_Id); db.SaveChanges(); var user = db.USERS.FirstOrDefault(x => x.UserId == dbAssessment.AssessmentCreatorId); var dbInformation = db.INFORMATION.Where(x => x.Id == assessmentId).FirstOrDefault(); if (dbInformation == null) { dbInformation = new INFORMATION() { Id = assessmentId }; } // add or update the INFORMATION record dbInformation.Assessment_Name = assessment.AssessmentName; dbInformation.Facility_Name = assessment.FacilityName; dbInformation.City_Or_Site_Name = assessment.CityOrSiteName; dbInformation.State_Province_Or_Region = assessment.StateProvRegion; dbInformation.Executive_Summary = assessment.ExecutiveSummary; dbInformation.Assessment_Description = assessment.AssessmentDescription; dbInformation.Additional_Notes_And_Comments = assessment.AdditionalNotesAndComments; dbInformation.IsAcetOnly = assessment.IsAcetOnly; db.INFORMATION.AddOrUpdate(dbInformation, x => x.Id); db.SaveChanges(); AssessmentUtil.TouchAssessment(assessmentId); return(assessmentId); } }
/// <summary> /// Populates a few principal assessment tables. /// </summary> /// <param name="model"></param> /// <param name="currentUserId"></param> /// <param name="primaryEmail"></param> /// <param name="db"></param> /// <returns></returns> public int RunImportManualPortion(UploadAssessmentModel model, int currentUserId, string primaryEmail, CSET_Context db) { //create the new assessment //copy each of the items to the table //as the copy occurs change to the current assessment_id //update the answer id's Dictionary <int, DOCUMENT_FILE> oldIdToNewDocument = new Dictionary <int, DOCUMENT_FILE>(); AssessmentManager man = new AssessmentManager(); AssessmentDetail detail = man.CreateNewAssessmentForImport(currentUserId); int _assessmentId = detail.Id; Dictionary <int, int> oldAnswerId = new Dictionary <int, int>(); Dictionary <int, ANSWER> oldIdNewAnswer = new Dictionary <int, ANSWER>(); Dictionary <string, int> oldUserNewUser = db.USERS.ToDictionary(x => x.PrimaryEmail, y => y.UserId); foreach (var a in model.jASSESSMENTS) { var item = db.ASSESSMENTS.Where(x => x.Assessment_Id == _assessmentId).FirstOrDefault(); if (item != null) { item.Diagram_Markup = a.Diagram_Markup; item.Diagram_Image = a.Diagram_Image; item.Assets = a.Assets; item.Charter = a.Charter; item.CreditUnionName = a.CreditUnionName; item.IRPTotalOverride = a.IRPTotalOverride; item.IRPTotalOverrideReason = a.IRPTotalOverrideReason; item.MatDetail_targetBandOnly = a.MatDetail_targetBandOnly != null ? a.MatDetail_targetBandOnly : false; db.SaveChanges(); } } foreach (var a in model.jINFORMATION) { var item = db.ASSESSMENTS.Where(x => x.Assessment_Id == _assessmentId).FirstOrDefault(); if (item != null) { item.Assessment_Date = a.Assessment_Date; db.SaveChanges(); } } // go through the assessment contacts and: // - if the contact does exist create it then add the id // - if the contact does exist update the id var dictAC = new Dictionary <int, int>(); foreach (var a in model.jASSESSMENT_CONTACTS) { // Don't create another primary contact, but map its ID if (a.PrimaryEmail == primaryEmail) { var newPrimaryContact = db.ASSESSMENT_CONTACTS.Where(x => x.PrimaryEmail == primaryEmail && x.Assessment_Id == _assessmentId).FirstOrDefault(); dictAC.Add(a.Assessment_Contact_Id, newPrimaryContact.Assessment_Contact_Id); continue; } var item = TinyMapper.Map <ASSESSMENT_CONTACTS>(a); item.Assessment_Id = _assessmentId; item.PrimaryEmail = a.PrimaryEmail; if (oldUserNewUser.TryGetValue(a.PrimaryEmail, out int userid)) { item.UserId = userid; } else { item.UserId = null; } db.ASSESSMENT_CONTACTS.Add(item); db.SaveChanges(); int newId; if (a.Assessment_Contact_Id != 0) { if (dictAC.TryGetValue(a.Assessment_Contact_Id, out newId)) { dictAC.Add(newId, newId); a.Assessment_Contact_Id = newId; } else { dictAC.Add(a.Assessment_Contact_Id, item.Assessment_Contact_Id); } } } // map the primary keys so that they can be passed to the generic import logic this.mapIdentity.Add("ASSESSMENT_CONTACTS", dictAC); // foreach (var a in model.jUSER_DETAIL_INFORMATION) { if (db.USER_DETAIL_INFORMATION.Where(x => x.Id == a.Id).FirstOrDefault() == null) { var userInfo = TinyMapper.Map <USER_DETAIL_INFORMATION>(a); userInfo.FirstName = String.IsNullOrWhiteSpace(a.FirstName) ? "First Name" : a.FirstName; userInfo.LastName = String.IsNullOrWhiteSpace(a.LastName) ? "Last Name" : a.LastName; db.USER_DETAIL_INFORMATION.Add(userInfo); foreach (var b in a.jADDRESSes) { var item = TinyMapper.Map <ADDRESS>(b); item.AddressType = "Imported"; db.ADDRESS.Add(item); } db.SaveChanges(); } } return(_assessmentId); }
/// <summary> /// Returns the details for the specified Assessment. /// </summary> /// <param name="assessmentId"></param> /// <returns></returns> public AssessmentDetail GetAssessmentDetail(int assessmentId) { AssessmentDetail assessment = new AssessmentDetail(); TokenManager tm = new TokenManager(); string app_code = tm.Payload(Constants.Token_Scope); using (var db = new CSET_Context()) { var query = (from ii in db.INFORMATION join aa in db.ASSESSMENTS on ii.Id equals aa.Assessment_Id where ii.Id == assessmentId select new { ii, aa }); var result = query.ToList().FirstOrDefault(); if (result != null) { assessment.Id = result.aa.Assessment_Id; assessment.AssessmentName = result.ii.Assessment_Name; assessment.AssessmentDate = result.aa.Assessment_Date; assessment.FacilityName = result.ii.Facility_Name; assessment.CityOrSiteName = result.ii.City_Or_Site_Name; assessment.StateProvRegion = result.ii.State_Province_Or_Region; assessment.ExecutiveSummary = result.ii.Executive_Summary; assessment.AssessmentDescription = result.ii.Assessment_Description; assessment.AdditionalNotesAndComments = result.ii.Additional_Notes_And_Comments; assessment.CreatorId = result.aa.AssessmentCreatorId ?? 0; assessment.CreatedDate = Utilities.UtcToLocal(result.aa.AssessmentCreatedDate); assessment.LastModifiedDate = Utilities.UtcToLocal((DateTime)result.aa.LastAccessedDate); assessment.DiagramMarkup = result.aa.Diagram_Markup; assessment.DiagramImage = result.aa.Diagram_Image; assessment.UseStandard = result.aa.UseStandard; if (assessment.UseStandard) { GetSelectedStandards(ref assessment, db); } assessment.UseDiagram = result.aa.UseDiagram; assessment.UseMaturity = result.aa.UseMaturity; if (assessment.UseMaturity) { GetMaturityModelDetails(ref assessment, db); } // for older assessments, if no features are set, look for actual data and set them if (!assessment.UseMaturity && !assessment.UseStandard && !assessment.UseDiagram) { DetermineFeaturesFromData(ref assessment, db); } bool defaultAcet = (app_code == "ACET"); assessment.IsAcetOnly = result.ii.IsAcetOnly != null ? result.ii.IsAcetOnly : defaultAcet; assessment.Charter = string.IsNullOrEmpty(result.aa.Charter) ? "" : result.aa.Charter; assessment.CreditUnion = result.aa.CreditUnionName; assessment.Assets = result.aa.Assets; // Fields located on the Overview page assessment.ExecutiveSummary = result.ii.Executive_Summary; assessment.AssessmentDescription = result.ii.Assessment_Description; assessment.AdditionalNotesAndComments = result.ii.Additional_Notes_And_Comments; } return(assessment); } }
/// <summary> /// Persists data to the ASSESSMENTS and INFORMATION tables. /// Date fields should be converted to UTC before sending the Assessment /// to this method. /// </summary> /// <param name="assessment"></param> /// <returns></returns> public int SaveAssessmentDetail(int assessmentId, AssessmentDetail assessment) { using (var db = new DataLayerCore.Model.CSET_Context()) { TokenManager tm = new TokenManager(); string app_code = tm.Payload(Constants.Token_Scope); // Add or update the ASSESSMENTS record var dbAssessment = db.ASSESSMENTS.Where(x => x.Assessment_Id == assessmentId).FirstOrDefault(); if (dbAssessment == null) { dbAssessment = new ASSESSMENTS(); db.ASSESSMENTS.Add(dbAssessment); db.SaveChanges(); assessmentId = dbAssessment.Assessment_Id; } dbAssessment.Assessment_Id = assessmentId; dbAssessment.AssessmentCreatedDate = assessment.CreatedDate; dbAssessment.AssessmentCreatorId = assessment.CreatorId; dbAssessment.Assessment_Date = assessment.AssessmentDate ?? DateTime.Now; dbAssessment.LastAccessedDate = assessment.LastModifiedDate; dbAssessment.UseDiagram = assessment.UseDiagram; dbAssessment.UseMaturity = assessment.UseMaturity; dbAssessment.UseStandard = assessment.UseStandard; dbAssessment.Charter = string.IsNullOrEmpty(assessment.Charter) ? string.Empty : assessment.Charter.PadLeft(5, '0'); dbAssessment.CreditUnionName = assessment.CreditUnion; dbAssessment.Assets = assessment.Assets; dbAssessment.MatDetail_targetBandOnly = (app_code == "ACET"); dbAssessment.Diagram_Markup = assessment.DiagramMarkup; dbAssessment.Diagram_Image = assessment.DiagramImage; dbAssessment.AnalyzeDiagram = false; db.ASSESSMENTS.AddOrUpdate(dbAssessment, x => x.Assessment_Id); db.SaveChanges(); var user = db.USERS.FirstOrDefault(x => x.UserId == dbAssessment.AssessmentCreatorId); var dbInformation = db.INFORMATION.Where(x => x.Id == assessmentId).FirstOrDefault(); if (dbInformation == null) { dbInformation = new INFORMATION() { Id = assessmentId }; } // add or update the INFORMATION record dbInformation.Assessment_Name = assessment.AssessmentName; dbInformation.Facility_Name = assessment.FacilityName; dbInformation.City_Or_Site_Name = assessment.CityOrSiteName; dbInformation.State_Province_Or_Region = assessment.StateProvRegion; dbInformation.Executive_Summary = assessment.ExecutiveSummary; dbInformation.Assessment_Description = assessment.AssessmentDescription; dbInformation.Additional_Notes_And_Comments = assessment.AdditionalNotesAndComments; dbInformation.IsAcetOnly = assessment.IsAcetOnly; db.INFORMATION.AddOrUpdate(dbInformation, x => x.Id); db.SaveChanges(); if (assessment.UseMaturity) { SalManager salManager = new SalManager(); salManager.SetDefaultSAL_IfNotSet(assessmentId); //this is at the bottom deliberatly because //we want everything else to succeed first MaturityManager mm = new MaturityManager(); mm.PersistSelectedMaturityModel(assessmentId, "CMMC"); if (mm.GetMaturityLevel(assessmentId) == 0) { mm.PersistMaturityLevel(assessmentId, 1); } } AssessmentUtil.TouchAssessment(assessmentId); return(assessmentId); } }