public RequirementLevel GetRequirementLevel(List <REQUIREMENT_LEVELS> listRequirementLevels) { REQUIREMENT_LEVELS level = listRequirementLevels[0]; int sallevels = GetRequirementMinLevelOrder(listRequirementLevels); string requirement_SAL_Type = GetStandard(level.Standard_Level); String requirement_sal_level = GetFullName(requirement_SAL_Type, sallevels); RequirementLevel requirementLevel = new RequirementLevel() { LevelOrder = sallevels, StandardLevel = requirement_sal_level }; return(requirementLevel); }
/// <summary> /// Clones requirements and their connecting rows into a new Set. /// </summary> /// <param name="copySet"></param> private void CloneRequirements(SETS copySet) { Dictionary <int, int> requirementIdMap = new Dictionary <int, int>(); Dictionary <int, int> questionSetIdMap = new Dictionary <int, int>(); using (var db = new CSET_Context()) { var queryReq = from r in db.NEW_REQUIREMENT from rs in db.REQUIREMENT_SETS.Where(x => x.Requirement_Id == r.Requirement_Id && x.Set_Name == this.origSetName) select new { r, rs }; var originalRequirements = queryReq.ToList(); // Clone NEW_REQUIREMENT and REQUIREMENT_SETS foreach (var origRequirement in originalRequirements) { var newReq = (NEW_REQUIREMENT)db.Entry(origRequirement.r).CurrentValues.ToObject(); newReq.Requirement_Id = 0; db.NEW_REQUIREMENT.Add(newReq); db.SaveChanges(); requirementIdMap.Add(origRequirement.r.Requirement_Id, newReq.Requirement_Id); var copyReqSet = (REQUIREMENT_SETS)db.Entry(origRequirement.rs).CurrentValues.ToObject(); copyReqSet.Requirement_Id = newReq.Requirement_Id; copyReqSet.Set_Name = copySet.Set_Name; db.REQUIREMENT_SETS.Add(copyReqSet); // Clone SAL levels for requirement var dbRL = db.REQUIREMENT_LEVELS .Where(x => x.Requirement_Id == origRequirement.r.Requirement_Id).ToList(); foreach (REQUIREMENT_LEVELS origLevel in dbRL) { var copyLevel = new REQUIREMENT_LEVELS { Requirement_Id = newReq.Requirement_Id, Standard_Level = origLevel.Standard_Level, Level_Type = origLevel.Level_Type, Id = origLevel.Id }; db.REQUIREMENT_LEVELS.Add(copyLevel); } } // Clone REQUIREMENT_QUESTIONS_SETS var dbRQS = db.REQUIREMENT_QUESTIONS_SETS.Where(x => x.Set_Name == origSetName).ToList(); foreach (REQUIREMENT_QUESTIONS_SETS origRQS in dbRQS) { var copyRQS = (REQUIREMENT_QUESTIONS_SETS)db.Entry(origRQS).CurrentValues.ToObject(); copyRQS.Set_Name = copySet.Set_Name; copyRQS.Requirement_Id = requirementIdMap[copyRQS.Requirement_Id]; db.REQUIREMENT_QUESTIONS_SETS.Add(copyRQS); } // Clone NEW_QUESTIONS_SETS var dbQS = db.NEW_QUESTION_SETS.Where(x => x.Set_Name == origSetName).ToList(); foreach (NEW_QUESTION_SETS origQS in dbQS) { var copyQS = (NEW_QUESTION_SETS)db.Entry(origQS).CurrentValues.ToObject(); copyQS.Set_Name = copySet.Set_Name; // default the identity PK copyQS.New_Question_Set_Id = 0; db.NEW_QUESTION_SETS.Add(copyQS); db.SaveChanges(); questionSetIdMap.Add(origQS.New_Question_Set_Id, copyQS.New_Question_Set_Id); } // Clone NEW_QUESTION_LEVELS for the new NEW_QUESTIONS_SETS just created var dbQL = from nql in db.NEW_QUESTION_LEVELS join nqs in db.NEW_QUESTION_SETS on nql.New_Question_Set_Id equals nqs.New_Question_Set_Id where nqs.Set_Name == this.origSetName select nql; var listQL = dbQL.ToList(); foreach (NEW_QUESTION_LEVELS origQL in listQL) { var copyQL = (NEW_QUESTION_LEVELS)db.Entry(origQL).CurrentValues.ToObject(); copyQL.New_Question_Set_Id = questionSetIdMap[origQL.New_Question_Set_Id]; db.NEW_QUESTION_LEVELS.Add(copyQL); } // There is no need to clone UNIVERSAL_SUB_CATEGORY_HEADINGS // because the classification of a Question with a Question Header and a Subcategory // only exists once. The Set it is tied to is the Set where the original // classification was made. // Clone REQUIREMENT_SOURCE_FILES var queryRSF = from rsf in db.REQUIREMENT_SOURCE_FILES join rs in db.REQUIREMENT_SETS on rsf.Requirement_Id equals rs.Requirement_Id where rs.Set_Name == this.origSetName select rsf; var listRSF = queryRSF.ToList(); foreach (var rsf in listRSF) { var newRSF = (REQUIREMENT_SOURCE_FILES)db.Entry(rsf).CurrentValues.ToObject(); newRSF.Requirement_Id = requirementIdMap[newRSF.Requirement_Id]; db.REQUIREMENT_SOURCE_FILES.Add(newRSF); } // Clone REQUIREMENT_REFERENCES var queryRR = from rr in db.REQUIREMENT_REFERENCES join rs in db.REQUIREMENT_SETS on rr.Requirement_Id equals rs.Requirement_Id where rs.Set_Name == this.origSetName select rr; var listRR = queryRR.ToList(); foreach (var rr in listRR) { var newRR = (REQUIREMENT_REFERENCES)db.Entry(rr).CurrentValues.ToObject(); newRR.Requirement_Id = requirementIdMap[newRR.Requirement_Id]; db.REQUIREMENT_REFERENCES.Add(newRR); } db.SaveChanges(); } }
public static async Task <ConverterResult <NEW_REQUIREMENT> > ToRequirement(this IExternalRequirement externalRequirement, string setName, ILogger logger) { var result = new ConverterResult <NEW_REQUIREMENT>(logger); var newRequirement = result.Result; //basic mappings newRequirement.Supplemental_Info = externalRequirement.Supplemental; newRequirement.Requirement_Text = externalRequirement.Text; newRequirement.Requirement_Title = externalRequirement.Identifier; newRequirement.Original_Set_Name = setName; newRequirement.Weight = externalRequirement.Weight; newRequirement.REQUIREMENT_LEVELS = new List <REQUIREMENT_LEVELS>(); newRequirement.REQUIREMENT_REFERENCES = new List <REQUIREMENT_REFERENCES>(); newRequirement.REQUIREMENT_SETS = new List <REQUIREMENT_SETS>() { new REQUIREMENT_SETS() { Set_Name = setName } }; //newRequirement.NEW_QUESTION = new List<NEW_QUESTION>(); QUESTION_GROUP_HEADING questionGroupHeading = null; UNIVERSAL_SUB_CATEGORY_HEADINGS subcategory = null; using (var db = new CSET_Context()) { try { questionGroupHeading = db.QUESTION_GROUP_HEADING.FirstOrDefault(s => s.Question_Group_Heading1.Trim().ToLower() == externalRequirement.Heading.Trim().ToLower()); try { var subcatId = db.UNIVERSAL_SUB_CATEGORIES.FirstOrDefault(s => s.Universal_Sub_Category.Trim().ToLower() == externalRequirement.Subheading.Trim().ToLower())?.Universal_Sub_Category_Id ?? 0; if (subcatId == 0) { var subcat = new UNIVERSAL_SUB_CATEGORIES() { Universal_Sub_Category = externalRequirement.Subheading }; db.UNIVERSAL_SUB_CATEGORIES.Add(subcat); await db.SaveChangesAsync(); subcatId = subcat.Universal_Sub_Category_Id; } try { subcategory = db.UNIVERSAL_SUB_CATEGORY_HEADINGS.FirstOrDefault(s => (s.Universal_Sub_Category_Id == subcatId) && (s.Question_Group_Heading_Id == questionGroupHeading.Question_Group_Heading_Id)); if (subcategory == null) { subcategory = new UNIVERSAL_SUB_CATEGORY_HEADINGS() { Universal_Sub_Category_Id = subcatId, Question_Group_Heading_Id = questionGroupHeading.Question_Group_Heading_Id }; db.UNIVERSAL_SUB_CATEGORY_HEADINGS.Add(subcategory); await db.SaveChangesAsync(); } } catch { } } catch { } } catch { } } if (questionGroupHeading == null) { result.LogError(String.Format("Heading invalid for requirement {0} {1}. Please double check that the heading is spelled correctly.", externalRequirement.Identifier, externalRequirement.Text)); } else { newRequirement.Question_Group_Heading_Id = questionGroupHeading.Question_Group_Heading_Id; } if (subcategory == null) { result.LogError(String.Format("Subheading invalid for requirement {0} {1}. Please double check that the heading is spelled correctly.", externalRequirement.Identifier, externalRequirement.Text)); } externalRequirement.Category = string.IsNullOrWhiteSpace(externalRequirement.Category) ? externalRequirement.Heading : externalRequirement.Category; using (var db = new CSET_Context()) { var category = db.STANDARD_CATEGORY.FirstOrDefault(s => s.Standard_Category1 == externalRequirement.Category); if (category == null) { newRequirement.Standard_CategoryNavigation = new STANDARD_CATEGORY() { Standard_Category1 = externalRequirement.Category }; } else { newRequirement.Standard_Category = category.Standard_Category1; } } foreach (var sal in Enum.GetValues(typeof(SalValues)).Cast <SalValues>().ToList()) { try { if ((int)sal >= (externalRequirement.SecurityAssuranceLevel ?? 0)) { var rl = new REQUIREMENT_LEVELS() { Standard_Level = sal.ToString(), Level_Type = "NST" }; newRequirement.REQUIREMENT_LEVELS.Add(rl); } } catch { result.LogError(String.Format("An error occurred while adding SALs for requirement {0} {1}.", externalRequirement.Identifier, externalRequirement.Text)); } } var importer = new DocumentImporter(); if (externalRequirement.References != null) { foreach (var reference in externalRequirement.References) { var reqReference = new REQUIREMENT_REFERENCES(); try { reqReference.Destination_String = reference.Destination; reqReference.Page_Number = reference.PageNumber; reqReference.Section_Ref = String.IsNullOrEmpty(reference.SectionReference) ? "" : reference.SectionReference; reqReference.Gen_File_Id = importer.LookupGenFileId(reference.FileName); } catch { result.LogError(String.Format("Reference {0} could not be added for requirement {1} {2}.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } if (reqReference.Gen_File_Id == 0) { result.LogError(String.Format("Reference {0} has not been loaded into CSET. Please add the file and try again.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } else { newRequirement.REQUIREMENT_REFERENCES.Add(reqReference); } } } var reqSource = new REQUIREMENT_SOURCE_FILES(); try { if (externalRequirement.Source != null) { reqSource.Gen_File_Id = importer.LookupGenFileId(externalRequirement.Source.FileName); reqSource.Page_Number = externalRequirement.Source.PageNumber; reqSource.Destination_String = externalRequirement.Source.Destination; reqSource.Section_Ref = String.IsNullOrEmpty(externalRequirement.Source.SectionReference) ? "" : externalRequirement.Source.SectionReference; if (reqSource.Gen_File_Id == 0) { result.LogError(String.Format("Source {0} has not been loaded into CSET. Please add the file and try again.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } else { newRequirement.REQUIREMENT_SOURCE_FILES.Add(reqSource); } } } catch { result.LogError(String.Format("Source {0} could not be added for requirement {1} {2}.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } if (externalRequirement.Questions == null || externalRequirement.Questions.Count() == 0) { externalRequirement.Questions = new QuestionList() { externalRequirement.Text }; } foreach (var question in externalRequirement.Questions) { NEW_QUESTION newQuestion = null; var set = new NEW_QUESTION_SETS() { Set_Name = setName, NEW_QUESTION_LEVELS = new List <NEW_QUESTION_LEVELS>() }; using (var db = new CSET_Context()) { newQuestion = db.NEW_QUESTION.FirstOrDefault(s => s.Simple_Question.ToLower().Trim() == question.ToLower().Trim()); if (newQuestion != null) { db.Entry(newQuestion).State = EntityState.Detached; } } if (newQuestion == null) { newQuestion = new NEW_QUESTION(); try { newQuestion.Original_Set_Name = setName; newQuestion.Simple_Question = question; newQuestion.Weight = externalRequirement.Weight; newQuestion.Question_Group_Id = questionGroupHeading.Question_Group_Heading_Id; newQuestion.Universal_Sal_Level = ((SalValues)(externalRequirement.SecurityAssuranceLevel ?? (int)SalValues.L)).ToString(); newQuestion.Std_Ref = setName.Replace("_", ""); newQuestion.Std_Ref = newQuestion.Std_Ref.Substring(0, Math.Min(newQuestion.Std_Ref.Length, 50)); newQuestion.Heading_Pair_Id = subcategory.Heading_Pair_Id; } catch { result.LogError(String.Format("Question {0} could not be added for requirement {1} {2}.", question, externalRequirement.Identifier, externalRequirement.Text)); } } foreach (var sal in Enum.GetValues(typeof(SalValues)).Cast <SalValues>().ToList()) { try { if ((int)sal >= (externalRequirement.SecurityAssuranceLevel ?? 0)) { var rl = new NEW_QUESTION_LEVELS() { Universal_Sal_Level = sal.ToString(), }; set.NEW_QUESTION_LEVELS.Add(rl); } } catch { result.LogError(String.Format("An error occurred while adding SALs for requirement {1} {2}.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } } newQuestion.NEW_QUESTION_SETS = new List <NEW_QUESTION_SETS>(); newQuestion.REQUIREMENT_QUESTIONS_SETS = new List <REQUIREMENT_QUESTIONS_SETS>(); newQuestion.NEW_QUESTION_SETS.Add(set); newQuestion.REQUIREMENT_QUESTIONS_SETS.Add(new REQUIREMENT_QUESTIONS_SETS { Set_Name = setName, Requirement_ = newRequirement }); using (CSET_Context db = new CSET_Context()) { db.NEW_QUESTION.Add(newQuestion); } } return(result); }
/// <summary> /// /// </summary> /// <param name="externalRequirement"></param> /// <param name="setName"></param> /// <param name="logger"></param> /// <returns></returns> public static NEW_REQUIREMENT SaveRequirement(this IExternalRequirement externalRequirement, string setName, int sequence, ILogger logger) { var result = new ConverterResult <NEW_REQUIREMENT>(logger); var newRequirement = result.Result; //basic mappings newRequirement.Supplemental_Info = externalRequirement.Supplemental; newRequirement.Requirement_Text = externalRequirement.Text; newRequirement.Requirement_Title = externalRequirement.Identifier; newRequirement.Original_Set_Name = setName; newRequirement.Weight = externalRequirement.Weight; newRequirement.REQUIREMENT_LEVELS = new List <REQUIREMENT_LEVELS>(); newRequirement.REQUIREMENT_REFERENCES = new List <REQUIREMENT_REFERENCES>(); newRequirement.REQUIREMENT_SETS = new List <REQUIREMENT_SETS>() { new REQUIREMENT_SETS() { Set_Name = setName, Requirement_Sequence = sequence } }; QUESTION_GROUP_HEADING questionGroupHeading = null; UNIVERSAL_SUB_CATEGORY_HEADINGS uschPairing = null; var db = new CSET_Context(); try { questionGroupHeading = db.QUESTION_GROUP_HEADING.FirstOrDefault(s => s.Question_Group_Heading1.Trim().ToLower() == externalRequirement.Heading.Trim().ToLower()); try { var subcatId = db.UNIVERSAL_SUB_CATEGORIES.FirstOrDefault(s => s.Universal_Sub_Category.Trim().ToLower() == externalRequirement.Subheading.Trim().ToLower())?.Universal_Sub_Category_Id ?? 0; if (subcatId == 0) { var subcat = new UNIVERSAL_SUB_CATEGORIES() { Universal_Sub_Category = externalRequirement.Subheading }; db.UNIVERSAL_SUB_CATEGORIES.Add(subcat); db.SaveChanges(); subcatId = subcat.Universal_Sub_Category_Id; } try { uschPairing = db.UNIVERSAL_SUB_CATEGORY_HEADINGS.FirstOrDefault(s => (s.Universal_Sub_Category_Id == subcatId) && (s.Question_Group_Heading_Id == questionGroupHeading.Question_Group_Heading_Id)); if (uschPairing == null) { uschPairing = new UNIVERSAL_SUB_CATEGORY_HEADINGS() { Set_Name = "Standards", Universal_Sub_Category_Id = subcatId, Question_Group_Heading_Id = questionGroupHeading.Question_Group_Heading_Id }; db.UNIVERSAL_SUB_CATEGORY_HEADINGS.Add(uschPairing); db.SaveChanges(); } } catch (Exception exc) { var myExc = exc; } } catch { } } catch { } if (questionGroupHeading == null) { result.LogError(String.Format("Heading invalid for requirement {0} {1}. Please double check that the heading is spelled correctly.", externalRequirement.Identifier, externalRequirement.Text)); } else { newRequirement.Question_Group_Heading_Id = questionGroupHeading.Question_Group_Heading_Id; } if (uschPairing == null) { result.LogError(String.Format("Subheading invalid for requirement {0} {1}. Please double check that the heading is spelled correctly.", externalRequirement.Identifier, externalRequirement.Text)); } externalRequirement.Category = string.IsNullOrWhiteSpace(externalRequirement.Category) ? externalRequirement.Heading : externalRequirement.Category; var category = db.STANDARD_CATEGORY.FirstOrDefault(s => s.Standard_Category1 == externalRequirement.Category); if (category == null) { newRequirement.Standard_CategoryNavigation = new STANDARD_CATEGORY() { Standard_Category1 = externalRequirement.Category }; } else { newRequirement.Standard_Category = category.Standard_Category1; } newRequirement.Standard_Sub_Category = externalRequirement.Subheading; // SAL foreach (string sal in externalRequirement.SecurityAssuranceLevels) { var rl = new REQUIREMENT_LEVELS() { Standard_Level = sal, Level_Type = "NST" }; if (newRequirement.REQUIREMENT_LEVELS.Count(x => x.Standard_Level == rl.Standard_Level) == 0) { newRequirement.REQUIREMENT_LEVELS.Add(rl); } } var importer = new DocumentImporter(); if (externalRequirement.References != null) { foreach (var reference in externalRequirement.References) { var reqReference = new REQUIREMENT_REFERENCES(); try { reqReference.Destination_String = reference.Destination; reqReference.Page_Number = reference.PageNumber; reqReference.Section_Ref = String.IsNullOrEmpty(reference.SectionReference) ? "" : reference.SectionReference; reqReference.Gen_File_Id = importer.LookupGenFileId(reference.FileName); } catch { result.LogError(String.Format("Reference {0} could not be added for requirement {1} {2}.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } if (reqReference.Gen_File_Id == 0) { result.LogError(String.Format("Reference {0} has not been loaded into CSET. Please add the file and try again.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } else { newRequirement.REQUIREMENT_REFERENCES.Add(reqReference); } } } var reqSource = new REQUIREMENT_SOURCE_FILES(); try { if (externalRequirement.Source != null) { reqSource.Gen_File_Id = importer.LookupGenFileId(externalRequirement.Source.FileName); reqSource.Page_Number = externalRequirement.Source.PageNumber; reqSource.Destination_String = externalRequirement.Source.Destination; reqSource.Section_Ref = String.IsNullOrEmpty(externalRequirement.Source.SectionReference) ? "" : externalRequirement.Source.SectionReference; if (reqSource.Gen_File_Id == 0) { result.LogError(String.Format("Source {0} has not been loaded into CSET. Please add the file and try again.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } else { newRequirement.REQUIREMENT_SOURCE_FILES.Add(reqSource); } } } catch { result.LogError(String.Format("Source {0} could not be added for requirement {1} {2}.", externalRequirement.Source?.FileName, externalRequirement.Identifier, externalRequirement.Text)); } try { db.NEW_REQUIREMENT.Add(newRequirement); db.SaveChanges(); } catch (Exception exc) { // throw exc; } // Save any Questions associated with the Requirement SaveQuestions(setName, externalRequirement, newRequirement, questionGroupHeading, uschPairing, db); return(newRequirement); }