/// <summary> /// Closes and disposes of the loading form. /// </summary> private void DisposeLoadingForm() { if (LoadingForm != null) { LoadingForm.CloseForm(); LoadingForm.Dispose(); LoadingForm = null; } }
/// <summary> /// Read and parse data about drugs from *.txt file /// </summary> /// <param name="fileName"></param> private async void TxtParseMethod(string fileName) { var progress = new LoadingForm(); progress.Show(); Enabled = false; var fileContent = File.ReadAllText(fileName, Encoding.UTF8); List <string> stringMedicine = fileContent.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); using (var context = new HospitalDbContext()) { try { List <MedicineType> drugs = new List <MedicineType>(); List <Manufacturer> manuf = new List <Manufacturer>(); foreach (var item in stringMedicine) { List <string> medItems = item.Split(',').ToList(); MedicineType drugType = new MedicineType() { Name = medItems[3] }; Manufacturer manufacturer = new Manufacturer() { FactoryName = medItems[1].Trim(), Country = medItems[2].Trim() }; drugs.Add(drugType); manuf.Add(manufacturer); } context.Manufacturers.AddRange(manuf); context.MedicineType.AddRange(drugs); await context.SaveChangesAsync(); List <Medicine> medicine = new List <Medicine>(); int i = 0; foreach (var item in stringMedicine) { List <string> medItems = item.Split(',').ToList(); var mydrugs = new Medicine() { Name = medItems[0].Trim(), Manufacturer = manuf[i], MedicineType = drugs[i] }; medicine.Add(mydrugs); i++; } context.Medicines.AddRange(medicine); await context.SaveChangesAsync(); } catch (DbEntityValidationException a) { ShowErrors(a); throw; } finally { progress.CloseForm(); Enabled = true; } } var name = fileName.Split('\\').Last(); //AddDrugToDb(medDrug, name); }
/// <summary> /// This method is called upon selecting a new Medal Data Profile. /// It loads the new medal data, and calls the parser to parse it. /// </summary> private void ProfileSelector_SelectedIndexChanged(object sender, EventArgs e) { // Make sure we have an index! Also make sure we didnt select the same profile again if (ProfileSelector.SelectedIndex == -1 || ProfileSelector.SelectedItem.ToString() == LastSelectedProfile) { return; } // Get current profile string Profile = ProfileSelector.SelectedItem.ToString(); // Make sure the user wants to commit without saving changes if (ChangesMade && MessageBox.Show("Some changes have not been saved. Are you sure you want to continue?", "Confirm", MessageBoxButtons.YesNo) != DialogResult.Yes) { ProfileSelector.SelectedIndex = Profiles.IndexOf(LastSelectedProfile.ToLower()); return; } // Disable the form to prevent errors. Show loading screen this.Enabled = false; LoadingForm.ShowScreen(this); // Suppress repainting the TreeView until all the objects have been created. AwardConditionsTree.Nodes.Clear(); AwardTree.BeginUpdate(); // Remove old medal data if applicable for (int i = 0; i < 4; i++) { AwardTree.Nodes[i].Nodes.Clear(); AwardTree.Nodes[i].ForeColor = Color.Black; } // Get Medal Data try { MedalDataParser.LoadMedalDataFile(Path.Combine(PythonPath, "medal_data_" + Profile + ".py")); } catch (Exception ex) { AwardTree.EndUpdate(); MessageBox.Show(ex.Message, "Medal Data Parse Error"); ProfileSelector.SelectedIndex = -1; this.Enabled = true; LoadingForm.CloseForm(); return; } // Iterator for badges int itr = -1; // Add all awards to the corresponding Node foreach (Award A in AwardCache.GetBadges()) { if (Award.GetBadgeLevel(A.Id) == BadgeLevel.Bronze) { AwardTree.Nodes[0].Nodes.Add(A.Id, A.Name.Replace("Basic ", "")); ++itr; } AwardTree.Nodes[0].Nodes[itr].Nodes.Add(A.Id, A.Name.Split(' ')[0]); } foreach (Award A in AwardCache.GetMedals()) { AwardTree.Nodes[1].Nodes.Add(A.Id, A.Name); } foreach (Award A in AwardCache.GetRibbons()) { AwardTree.Nodes[2].Nodes.Add(A.Id, A.Name); } foreach (Rank R in AwardCache.GetRanks()) { AwardTree.Nodes[3].Nodes.Add(R.Id.ToString(), R.Name); } // Begin repainting the TreeView. AwardTree.CollapseAll(); AwardTree.EndUpdate(); // Reset current award data AwardNameBox.Text = null; AwardTypeBox.Text = null; AwardPictureBox.Image = null; // Process Active profile button if (Profile == ActiveProfile) { ActivateProfileBtn.Text = "Current Server Profile"; ActivateProfileBtn.BackgroundImage = Resources.check; } else { ActivateProfileBtn.Text = "Set as Server Profile"; ActivateProfileBtn.BackgroundImage = Resources.power; } // Apply inital highlighting of condition nodes ValidateConditions(); // Enable form controls AwardTree.Enabled = true; AwardConditionsTree.Enabled = true; DelProfileBtn.Enabled = true; ActivateProfileBtn.Enabled = true; SaveBtn.Enabled = true; this.Enabled = true; LoadingForm.CloseForm(); // Set this profile as the last selected profile LastSelectedProfile = Profile; ChangesMade = false; }
/// <summary> /// Method for parsing csv and adding parsed list to database /// </summary> /// <param name="fileName">full name of working file</param> private async void ParseAndAddCsvToDb(string fileName) { var progress = new LoadingForm(); progress.Show(); Enabled = false; var fileContent = File.ReadAllText(fileName, Encoding.UTF8); var linesMedicine = fileContent.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList(); var nameFields = linesMedicine.ElementAt(0).Split(',').ToList(); linesMedicine.RemoveAt(0); var refMed = typeof(Medicine).GetProperties(); var refMan = typeof(Manufacturer).GetProperties(); var refTypeMed = typeof(MedicineType).GetProperties(); int nameMedicine = nameFields.FindIndex(x => x.Trim().Equals(refMed[0].Name)); int typeMedicine = nameFields.FindLastIndex(x => x.Trim().Equals(refTypeMed[0].Name)); int manufacturerName = nameFields.FindIndex(x => x.Trim().Equals(refMan[0].Name)); int manufacturerCountry = nameFields.FindIndex(x => x.Trim().Equals(refMan[1].Name)); using (var context = new HospitalDbContext()) { try { List <MedicineType> drugs = new List <MedicineType>(); List <Manufacturer> manuf = new List <Manufacturer>(); foreach (var item in linesMedicine) { List <string> medItems = item.Split(',').ToList(); MedicineType drugType = new MedicineType() { Name = medItems[typeMedicine] }; Manufacturer manufacturer = new Manufacturer() { FactoryName = medItems[manufacturerName].Trim(), Country = medItems[manufacturerCountry].Trim() }; drugs.Add(drugType); manuf.Add(manufacturer); } context.Manufacturers.AddRange(manuf); context.MedicineType.AddRange(drugs); await context.SaveChangesAsync(); List <Medicine> medicine = new List <Medicine>(); int i = 0; foreach (var item in linesMedicine) { List <string> medItems = item.Split(',').ToList(); var mydrugs = new Medicine() { Name = medItems[nameMedicine].Trim(), Manufacturer = manuf[i], MedicineType = drugs[i] }; medicine.Add(mydrugs); i++; } context.Medicines.AddRange(medicine); await context.SaveChangesAsync(); } catch (DbEntityValidationException a) { ShowErrors(a); throw; } finally { progress.CloseForm(); Enabled = true; } } var name = fileName.Split('\\').Last(); MessageBox.Show(@"File " + name + @" imported!", @"Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }