public JsonResult DeleteIncompleteCase(ADDB context, int caseID) { string r = ""; //check if there have been symptoms log but no results var signsForCase = context.SignsForCases.Where(x => x.CaseID == caseID).ToList(); if (signsForCase.Count > 0) { //delete the symps context.SignsForCases.RemoveRange(signsForCase); context.SaveChanges(); } //finally delete the case context.Cases.Remove(context.Cases.Find(caseID)); context.SaveChanges(); return(Json(r)); }
protected override ReturnValue GetSymptomsForCaseAndPopulateTable(int originalCaseId, int newCaseId, string tableName) { bool thereWasAnError = false; try { if (tableName.Equals(SET_CASE_TABLE)) { //selected symptoms n //get the symptoms for the specific case List <SignForCases> syptoms = eddie.SelectedSymptomsN.Where(x => x.CaseId == originalCaseId).Select(x => new SignForCases { CaseId = newCaseId, SignId = GetSignID(x.SymptomName), SignPresence = TranslateSymptomPresenceFromEddieToNewFrameWork(x.Selection) }).ToList(); ADDB.SignForCases.AddRange(syptoms); } else if (tableName.Equals(CASE_INFO_TABLE)) { //selected symptoms List <SignForCases> syptoms = eddie.SelectedSymptoms.Where(x => x.CaseId == originalCaseId).Select(x => new SignForCases { CaseId = newCaseId, SignId = GetSignID(x.SymptomName), SignPresence = TranslateSymptomPresenceFromEddieToNewFrameWork(x.Selection) }).ToList();; ADDB.SignForCases.AddRange(syptoms); } ADDB.SaveChanges(); } catch (Exception e) { logFileWriter.WriteLine("Problem with {0} in method GetDiseaseSymptomsforCaseAndPopulateTable from table {1}", originalCaseId, tableName); logFileWriter.WriteLine("Excpetion details: {0} {1}", e.Message, e.StackTrace); } Console.WriteLine("Succesfully added signs for case {0}: {1} ", tableName, originalCaseId); return(new ReturnValue { ID = 0, ThereWasAnError = thereWasAnError }); }
public void CreateSign(ADDB context, Sign sign) { if (String.IsNullOrWhiteSpace(sign.Name)) { TempData["Errors"] = "Sign Name is missing"; return; } sign.Name = sign.Name.ToUpper(); if (context.Signs.Where(s => s.Name == sign.Name).Count() > 0) { TempData["Errors"] = "Sign " + sign.Name + " already exists!"; return; } //TO DO ADDITIONAL CHECKS switch (sign.Type_of_Value) { case SignTypes.NUMERICAL: break; case SignTypes.OBSERVATIONAL: break; } context.Signs.Add(sign); context.SaveChanges(); TempData["Errors"] = null; }
public ActionResult RemoveDiseasePrior(ADDB context, int id) { PriorsDiseases priorToRemove = context.PriorsDiseases.Find(id); context.PriorsDiseases.Remove(priorToRemove); context.SaveChanges(); return(RedirectToAction("Index")); }
public void LoadSignsMasterList(ADDB context) { string extension = ".xlsx"; string filename = "master_list"; string path = Server.MapPath(@"~/Files/" + filename + extension); try { Excel.Application app = new Excel.Application(); Excel.Workbook WB = app.Workbooks.Open(path); Excel.Worksheet signsWorkSheet = WB.Worksheets["Signs core"]; Excel.Range usedCells = signsWorkSheet.UsedRange; object[,] valueArray = (object[, ])usedCells.get_Value( XlRangeValueDataType.xlRangeValueDefault); int rowsLength = valueArray.GetLength(0); int columnsLength = valueArray.GetLength(1); for (int c = 2; c <= columnsLength; c++) { string name = (string)valueArray[1, c]; if (name == null || name.Equals("Comment")) { continue; } name = name.ToUpper(); // name needs to be uppercase if (name.Equals("Sheep".ToUpper())) { CreateSignCoresForAnimal(context, "SHEEP", c, rowsLength, valueArray); CreateSignCoresForAnimal(context, "GOAT", c, rowsLength, valueArray); } else if (name.Equals("Equid".ToUpper())) { CreateSignCoresForAnimal(context, "HORSE_MULE", c, rowsLength, valueArray); CreateSignCoresForAnimal(context, "DONKEY", c, rowsLength, valueArray); } else { CreateSignCoresForAnimal(context, name, c, rowsLength, valueArray); } } //close the workbook and the app WB.Close(); Marshal.ReleaseComObject(WB); Marshal.ReleaseComObject(app); context.SaveChanges(); } catch (Exception e) { Console.Write(e.Message); } }
public void CreateLikelihood(ADDB context, Likelihood likelihood) { //deal with duplicate entries var duplicates = context.Likelihoods.Where(m => m.AnimalId == likelihood.AnimalId && m.DiseaseId == likelihood.DiseaseId && m.SignId == likelihood.SignId); if (duplicates.Count() > 0) { return; // do not add anything if we already have a likelihood for the same animal, disease and sign } context.Likelihoods.Add(likelihood); context.SaveChanges(); }
public ActionResult RemoveAnimal(ADDB context, string name) { var animalsToRemove = context.Animals.Where(m => m.Name == name); //remove foreign key references before removing the animal //foreach (var a in animalsToRemove) // foreach (var pd in context.PriorsDiseases.Where(m => m.AnimalID == a.Id)) // context.PriorsDiseases.Remove(pd); context.Animals.RemoveRange(animalsToRemove); context.SaveChanges(); return(RedirectToAction("Index", "EditDB")); }
protected override ReturnValue GetResultsForCaseAndPopulateTable(int originalCaseId, int newCaseId, string tableName) { bool thereWasAnError = false; try { if (tableName.Equals(SET_CASE_TABLE)) { //diseaseRank n //get the symptoms for the specific case List <ResultForCases> results = eddie.DiseaseRankN .Where(x => x.CaseId == originalCaseId) .Select(x => new ResultForCases { CaseId = newCaseId, DiseaseId = GetDiseaseID(x.DiseaseName), PredictedLikelihoodOfDisease = ConvertLikelihoodFromStringToFloat(x.Percentage) }).ToList(); ADDB.ResultForCases.AddRange(results); } else if (tableName.Equals(CASE_INFO_TABLE)) { //diseaseRank List <ResultForCases> results = eddie.DiseaseRank .Where(x => x.CaseId == originalCaseId) .Select(x => new ResultForCases { CaseId = newCaseId, DiseaseId = GetDiseaseID(x.DiseaseName), PredictedLikelihoodOfDisease = ConvertLikelihoodFromStringToFloat(x.Percentage) }).ToList(); ADDB.ResultForCases.AddRange(results); } ADDB.SaveChanges(); } catch (Exception e) { thereWasAnError = true; logFileWriter.WriteLine("Problem in method GetResultsForCaseAndPopulateTable() method for case {0} in table {1}", originalCaseId, tableName); logFileWriter.WriteLine("Excpetion details: {0} {1}", e.Message, e.StackTrace); } Console.WriteLine("Sucessfully added results for case {0} from table {1}", originalCaseId, tableName); return(new ReturnValue { ID = 0, ThereWasAnError = thereWasAnError }); }
protected override int IdentifyOrCreateNewPatient(int animalID, int ownerID) { //this method tries to identify the patient to see if two cases are the same //for the moment we'll treat each case as dealing with one patient Patients newPatient = new Patients(); newPatient.AnimalId = animalID; newPatient.OwnerId = ownerID; ADDB.Patients.Add(newPatient); ADDB.SaveChanges(); int newPatientID = ADDB.Patients.Last().Id; return(newPatientID); }
protected override void CleanUpNewPatientAndNewOwnerAsAResultOfAnError(int patientID, int ownerID) { if (patientID != -1) { var patientToRemove = ADDB.Patients.Find(patientID); ADDB.Patients.Remove(patientToRemove); } if (ownerID != -1) { var ownerToRemove = ADDB.Owners.Find(ownerID); ADDB.Owners.Remove(ownerToRemove); } ADDB.SaveChanges(); }
public ActionResult RemoveSign(ADDB context, int id) { Sign signToRemove = context.Signs.Find(id); foreach (Likelihood l in context.Likelihoods.Where(m => m.SignId == id).ToList()) { context.Likelihoods.Remove(l); } context.Signs.Remove(signToRemove); context.SaveChanges(); return(RedirectToAction("Index")); }
public void CreateDiseasePrior(ADDB context, int diseaseID, int animalID, string probability) { var duplicate = context.PriorsDiseases.Where(m => m.DiseaseID == diseaseID && m.AnimalID == animalID).ToList(); if (duplicate.Count > 0) { duplicate[0].Probability = probability; // if we do this the prior gets updated rather than added again } else { PriorsDiseases priorsDiseases = new PriorsDiseases(); priorsDiseases.AnimalID = animalID; priorsDiseases.DiseaseID = diseaseID; priorsDiseases.Probability = probability; context.PriorsDiseases.Add(priorsDiseases); } context.SaveChanges(); }
public void CreateDisease(ADDB context, Disease disease) { if (String.IsNullOrWhiteSpace(disease.Name)) { TempData["Errors"] = "Missing Fields"; return; } disease.Name = disease.Name.ToUpper(); if (context.Diseases.Where(d => d.Name.Equals(disease.Name)).Count() > 0) { TempData["Errors"] = "Disease " + disease.Name + " already exists!"; return; } context.Diseases.Add(disease); context.SaveChanges(); }
public ActionResult RemoveDisease(ADDB context, int id) { Disease diseaseToRemove = context.Diseases.Find(id); foreach (var pd in context.PriorsDiseases.Where(m => m.DiseaseID == id)) { context.PriorsDiseases.Remove(pd); } foreach (var d in context.Likelihoods.Where(m => m.DiseaseId == id)) { context.Likelihoods.Remove(d); } context.Diseases.Remove(diseaseToRemove); context.SaveChanges(); return(RedirectToAction("Index")); }
protected override int IdentifyOrCreateOwnerOfCase(string name, string region, string location) { /*this method analyses each case to identify the owner of a case if it doesn't find one it adds a new owner to the owner table*/ //for the moment since we are copying the data from eddie we will assign the same owner as eddie to all cases Owners owner = new Owners(); owner.Name = name; owner.Profession = "Eddie User"; /* if (ADDB.Owners.Select(x => x.Name).Contains(name)) * return ADDB.Owners.Where(x => x.Equals(name)).First().Id;*/ ADDB.Owners.Add(owner); ADDB.SaveChanges(); int id = ADDB.Owners.Last().Id; return(id); }
protected override int IdentifyOrCreateOwnerOfCase(string name, string region, string location) { Owners owner = new Owners(); owner.Name = name; owner.Profession = "Eddie User"; /* if (ADDB.Owners.Select(x => x.Name).Contains(name)) * return ADDB.Owners.Where(x => x.Equals(name)).First().Id;*/ ADDB.Owners.Add(owner); ADDB.SaveChanges(); if (ADDB.Owners.Count() == 0) { Console.WriteLine("No owners found"); } int id = ADDB.Owners.Last().Id; return(id); }
public JsonResult LogCase(ADDB context, int animalID, string[] signs, Dictionary <string, string> results, string diseasechosenbyuser, string region, string location, DateTime datecaseobserved, string comments) { string r = "Case Added Sucessfully"; string name = "temporary name"; // Create owner int ownerID = IdentifyOrCreateOwnerOfCase(context, name, region, location); try { //create patient Patient newPatient = new Patient(); newPatient.AnimalID = animalID; newPatient.OwnerID = ownerID; context.Patients.Add(newPatient); context.SaveChanges(); int patientID = context.Patients.ToList().Last().ID; //create case Case newCase = new Case(); newCase.Location = location + "," + region; newCase.PatientID = patientID; //get info about the disease chosen by the user(we'll call this dbu) //the data is formatted rank_name_likelihoodvalue % string[] dbu = diseasechosenbyuser.Split('_'); int rank = Convert.ToInt32(dbu[0]); string dbuName = dbu[1]; string dbuLikelihood = dbu[2]; dbuLikelihood = dbuLikelihood.Remove(dbuLikelihood.Length - 1, 1); // remove the percentage symbol float dbuLikelihoodVal; float.TryParse(dbuLikelihood, out dbuLikelihoodVal); newCase.DiseasePredictedByAppID = GetDiseaseID(context, results.Keys.First()); newCase.DiseaseChosenByUserID = GetDiseaseID(context, dbuName); newCase.LikelihoodOfDiseaseChosenByUser = dbuLikelihoodVal; newCase.RankOfDiseaseChosenByUser = rank; newCase.Comments = comments; newCase.DateOfCaseObserved = datecaseobserved; newCase.DateOfCaseLogged = DateTime.Now; #region TEMPORARY DUMMY TREATMENT CODE!!!!!!!! TO DO!! FIX!!! int treatmentID = context.Treatments.ToList().Last().Id; // this dummy treatment was created during conversion of eddie cases... newCase.TreatmentChosenByUserID = treatmentID; #endregion newCase.ApplicationVersion = "1.0"; newCase.OriginTableName = "Cases"; newCase.OriginDBName = "D3FFramework"; context.Cases.Add(newCase); context.SaveChanges(); } catch (Exception) { r = "There was an error logging the case"; } try { int caseID = context.Cases.ToList().Last().ID; //log signs foreach (string sign in signs) { string[] splitSymbol = { "+_" }; string[] s = sign.Split(splitSymbol, StringSplitOptions.None); int signID = Convert.ToInt32(s[0]); string signPresence = s[1]; SignForCase signForCase = new SignForCase(); signForCase.CaseID = caseID; signForCase.SignID = signID; switch (signPresence) { case "P": signForCase.SignPresence = "P"; break; case "NP": signForCase.SignPresence = "NP"; break; case "NO": signForCase.SignPresence = "NO"; break; } context.SignsForCases.Add(signForCase); } //log results foreach (string result in results.Keys) { ResultForCase resultForCase = new ResultForCase(); resultForCase.CaseID = caseID; resultForCase.DiseaseID = GetDiseaseID(context, result); //convert predicted likelihood of case //and remove last char because it's the perc symbol float pl; float.TryParse(results[result].Remove(results[result].Length - 1, 1), out pl); resultForCase.PredictedLikelihoodOfDisease = pl; context.ResultsForCases.Add(resultForCase); } context.SaveChanges(); } catch (Exception e) { r = "There was an error logging the case"; //to do... write some code to clean up incomplete logged cases.... } return(Json(r)); }
private int ProcessCases(string tableName, List <EddieCase> caseData, int startIndex, int endIndex) { foreach (EddieCase c in caseData) { int animalID = -1; int patientID = -1; int ownerID = -1; try { string logFileStartLine = "Adding Case From " + tableName + " ID: " + c.CaseId; Console.WriteLine(logFileStartLine); if (CheckIfCaseWasAlreadyInserted(c.CaseId, tableName)) // skip if this case was already inserted { continue; } // logFileWriter.WriteLine(logFileStartLine); #region GENERAL CASE INFORMATION Cases newCase = new Cases(); newCase.ApplicationVersion = appVersion; newCase.DateOfCaseLogged = currentDate; DateTime caseDate; DateTime.TryParse(c.Date, out caseDate); newCase.DateOfCaseObserved = caseDate; newCase.OriginDbname = "Eddie"; newCase.OriginTableName = tableName; newCase.OriginId = c.CaseId; newCase.Location = c.Location + "," + c.Region; newCase.Comments = c.Comment; #endregion #region ANIMAL/OWNER INFO animalID = GetAnimalIDBasedOnCaseInfo(c.Species, c.Sex, c.Age); ownerID = IdentifyOrCreateOwnerOfCase(c.Owner, c.Region, c.Location); newCase.PatientId = IdentifyOrCreateNewPatient(animalID, ownerID); #endregion #region INFO ON DISEASE CHOSEN BY USER string[] diseaseInfo = GetInfoOnDiseaseChosenByUser(c.UserChdisease); newCase.DiseaseChosenByUserId = GetDiseaseID(diseaseInfo[0]); float likelihoodOfDiseaseChosenByUser; float.TryParse(diseaseInfo[1].Trim(), out likelihoodOfDiseaseChosenByUser); newCase.LikelihoodOfDiseaseChosenByUser = likelihoodOfDiseaseChosenByUser; newCase.RankOfDiseaseChosenByUser = GetRankOfDiseaseChosenByUser(c.CaseId, tableName); #endregion ReturnValue DiseasePredictedByApp = GetInfoOnDiseasePredictedByAppRankedFirst(c.CaseId, tableName); if (DiseasePredictedByApp.ThereWasAnError) { CleanUpNewPatientAndNewOwnerAsAResultOfAnError(patientID, ownerID); continue; } newCase.DiseasePredictedByAppId = DiseasePredictedByApp.ID; #region INFO ON TREATMENT CHOSEN BY USER //!!!TO DO CHANGE THIS WHEN TREATMENT DESIGH IS COMPLETE!!!!! newCase.TreatmentChosenByUserId = ADDB.Treatments.Last().Id; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!////////////////// #endregion ADDB.Add(newCase); ADDB.SaveChanges(); } catch (Exception e) { logFileWriter.WriteLine("Problem With Case " + c.CaseId + "from " + tableName); logFileWriter.WriteLine("Excpetion details: {0} {1}", e.Message, e.StackTrace); if (ADDB.Patients.Count() > 0) { patientID = ADDB.Patients.Last().Id; patientID = ADDB.Owners.Last().Id; CleanUpNewPatientAndNewOwnerAsAResultOfAnError(patientID, ownerID); } continue; } //WARNING THE SYMPTOMS AND RESULTS TABLE FUNCTION NEED THE NEW CASE ID (SO WE MUST UPDATE THE CASE TABLE FIRST AND THEN CALL THE FUNCTIONS #region POPULATE THE SIGNS FOR CASE TABLE int currentCaseID = ADDB.Cases.Last().Id; // get the id of the latest case GetSymptomsForCaseAndPopulateTable(c.CaseId, currentCaseID, tableName); #endregion #region INFO ON RESULTS OF THE CASE ReturnValue ResultsOfTheCaseInsertionOutcome = GetResultsForCaseAndPopulateTable(c.CaseId, currentCaseID, tableName); if (ResultsOfTheCaseInsertionOutcome.ThereWasAnError) { continue; } #endregion Console.WriteLine("Added Case Succesfully"); // logFileWriter.WriteLine("Added Case {0} from {1} Successfully", c.CaseId, tableName); } return(1); }
public void CreateNewAnimal(ADDB context, string name) { //data from the excel file needs to be trimmed if (String.IsNullOrWhiteSpace(name)) { TempData["Errors"] = "Missing Fields"; return; } name = name.ToUpper(); //insert male if (context.Animals.Where(m => m.Name == name).Count() > 0) { TempData["Errors"] = "This Name Already Exists"; return; } //baby Animal a = new Animal(); a.Name = name; context.Animals.Add(a); a.Sex = "M"; a.Age = "BABY"; context.SaveChanges(); //young a = new Animal(); a.Name = name; context.Animals.Add(a); a.Sex = "M"; a.Age = "YOUNG"; context.SaveChanges(); //old a = new Animal(); a.Name = name; context.Animals.Add(a); a.Sex = "M"; a.Age = "OLD"; context.SaveChanges(); //insert female //baby a = new Animal(); a.Name = name; context.Animals.Add(a); a.Sex = "F"; a.Age = "BABY"; context.SaveChanges(); //young a = new Animal(); a.Name = name; context.Animals.Add(a); a.Sex = "F"; a.Age = "YOUNG"; context.SaveChanges(); //old a = new Animal(); a.Name = name; context.Animals.Add(a); a.Sex = "F"; a.Age = "OLD"; context.SaveChanges(); TempData["Errors"] = null; }