public ActionResult RemoveDiseasePrior(ADDB context, int id)
        {
            PriorsDiseases priorToRemove = context.PriorsDiseases.Find(id);

            context.PriorsDiseases.Remove(priorToRemove);
            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 ActionResult LoadFromExcel(ADDB context)
        {
            string extension = ".xlsx";
            string filename  = "data";
            string path      = Server.MapPath(@"~/Files/" + filename + extension);

            try
            {
                Excel.Application app = new Excel.Application();

                Excel.Workbook WB = app.Workbooks.Open(path);

                // statement get the workbookname
                string ExcelWorkbookname = WB.Name;

                // statement get the worksheet count
                int worksheetcount = WB.Worksheets.Count;


                StringBuilder logBuilder = new StringBuilder();
                logBuilder.AppendLine(ExcelWorkbookname + " loaded. <br/>");
                logBuilder.AppendLine(ExcelWorkbookname + " has " + worksheetcount + "worksheets");

                Excel.Worksheet abbrWorkSheet = WB.Worksheets["Abbr"];


                //deal with abbreviations
                Excel.Range usedCells = abbrWorkSheet.UsedRange;
                object[,] valueArray = (object[, ])usedCells.get_Value(
                    XlRangeValueDataType.xlRangeValueDefault);
                int rowsLength    = valueArray.GetLength(0);
                int columnsLength = valueArray.GetLength(1);


                Dictionary <String, String> abbrSigns = new Dictionary <string, string>();
                //THIS IS NOT 0 INDEXED!!!
                for (int r = 2; r <= rowsLength; r++)
                {
                    string abbrivieatedName = (string)valueArray[r, 2];
                    string fullName         = (string)valueArray[r, 1];
                    if (abbrivieatedName == null || fullName == null)
                    {
                        continue;
                    }
                    if (abbrSigns.ContainsKey(abbrivieatedName))
                    {
                        continue;// skip to the next sign if we've seen this sign before
                    }
                    abbrSigns.Add(abbrivieatedName, fullName);

                    Sign sign = new Sign();
                    sign.Name          = fullName;
                    sign.Type_of_Value = SignTypes.OBSERVATIONAL;

                    CreateSign(context, sign);
                }


                foreach (Excel.Worksheet w in WB.Worksheets)
                {
                    if (w.Name.Equals("Abbr"))
                    {
                        continue;
                    }
                    usedCells  = w.UsedRange;
                    valueArray = (object[, ])usedCells.get_Value(
                        XlRangeValueDataType.xlRangeValueDefault);
                    rowsLength    = valueArray.GetLength(0);
                    columnsLength = valueArray.GetLength(1);

                    //we need to deal with the signs first so we can know what the abbrivieations mean once we get the likelihoods from the data


                    if (w.Name.Contains("Disease"))
                    {
                        string prefix     = "Disease-Sign";
                        string animalName = w.Name.Replace(prefix, "");
                        logBuilder.AppendLine(animalName + " <br />");

                        //Save  animal names

                        //CreateNewAnimal(context, animalName);

                        for (int r = 2; r <= rowsLength; r++)
                        {
                            if (valueArray[r, 1] == null)
                            {
                                continue;
                            }
                            Disease d = new Disease();

                            d.Name = (string)valueArray[r, 1];
                            //Save disease Name

                            // CreateDisease(context, d);
                            int diseaseID = FindDiseaseIDWithName(context, d.Name);

                            //calculate disease priors for current disease
                            foreach (int id in FindAllAnimalsIDsWithName(context, animalName))
                            {
                                PriorsDiseases pd = new PriorsDiseases();
                                pd.DiseaseID   = diseaseID;
                                pd.AnimalID    = id;
                                pd.Probability = ((rowsLength / 100.0f)).ToString();   /*we don't do -1 because we add +1 in the end anyway*/
                                CreateDiseasePrior(context, diseaseID, id, pd.Probability);

                                // for (int c = 2; c <= columnsLength; c++)
                                // {
                                //     if (valueArray[r, c] == null || valueArray[1, c] == null)
                                //         continue;
                                //     //Grab Likelihoods from the spreadsheet
                                //     Likelihood likelihood = new Likelihood();
                                //     likelihood.AnimalId = id;
                                //     likelihood.DiseaseId = diseaseID;
                                //     likelihood.SignId = FindSignIDWithName(context, abbrSigns[(string)valueArray[1, c]]); // find the id using the abbr dictionary
                                //     likelihood.Value = valueArray[r, c].ToString();
                                //     CreateLikelihood(context, likelihood);
                                //}
                            }
                        }
                    }
                }

                TempData["LOG"] = logBuilder.ToString();

                WB.Close();

                Marshal.ReleaseComObject(WB);
                Marshal.ReleaseComObject(app);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return(RedirectToAction("Index"));
        }