/// <summary> /// Converts the bat to xelement. Takes a Bat reference from the database and /// formats the database information related to that bat into a single /// XElelement /// </summary> /// <param name="bat">The bat.</param> /// <returns></returns> public XElement ConvertBatToXelement(Bat bat) { XElement result = new XElement("Bat"); result.Add(new XAttribute("Name", bat.Name)); result.Add(new XElement("BatGenus", bat.Batgenus)); result.Add(new XElement("BatSpecies", bat.BatSpecies)); var commonNames = from cn in batReferenceDataContext.BatCommonNames where cn.BatID == bat.Id select cn.BatCommonName1; foreach (var name in commonNames) { result.Add(new XElement("BatCommonName", name)); } var tags = from tag in batReferenceDataContext.BatTags where tag.BatID == bat.Id select tag.BatTag1; foreach (var tag in tags) { result.Add(new XElement("BatTag", tag)); } return(result); }
/// <summary> /// Validates the bat. Checks that CommonName, Genus, Species and Tag /// are all present. If not throws up a message box, and returns FALSE /// </summary> /// <param name="unselected">The unselected.</param> /// <returns></returns> private String ValidateBat(Bat unselected) { Bat previousBat = SelectedBat(unselected); int NumberOfCommonNames = previousBat.BatCommonNames.Count; if (NumberOfCommonNames <= 0) { return("At least one Common Name required"); } RenameBat(previousBat); if (String.IsNullOrWhiteSpace(previousBat.Batgenus)) { return("Bat Genus required"); } if (String.IsNullOrWhiteSpace(previousBat.BatSpecies)) { return("Bat Species required"); } if (previousBat.BatTags.Count <= 0) { return("Bat Tag required"); } return(null); }
private void AddRecordButton_Click(object sender, RoutedEventArgs e) { String errString = ValidateBat((Bat)BatNameListBox.SelectedItem); int index = BatNameListBox.SelectedIndex; if (String.IsNullOrWhiteSpace(errString)) { Bat bat = new Bat(); bat.Name = "bat"; bat.Batgenus = "BatGenus"; bat.BatSpecies = "BatSpecies"; BatCommonName bcn = new BatCommonName(); bcn.SortIndex = (short)-1; bcn.BatCommonName1 = "BatCommonName"; short max = short.MinValue; foreach (var cn in bat.BatCommonNames) { if (cn.SortIndex > max) { max = cn.SortIndex; } } bcn.SortIndex = ++max; bat.BatCommonNames.Add(bcn); BatTag bt = new BatTag(); bt.BatTag1 = "BatTag"; max = short.MinValue; foreach (var tg in bat.BatTags) { if (tg.SortIndex.Value > max) { max = tg.SortIndex.Value; } } bt.SortIndex = ++max; bat.BatTags.Add(bt); int?maxi = int.MaxValue; foreach (var b in BatList) { if (b.SortIndex > maxi) { maxi = b.SortIndex; } } bat.SortIndex = maxi.Value + 1; BatList.Add(bat); BatList = new ObservableCollection <Bat>(BatList.OrderBy(newbat => newbat.SortIndex)); BatNameListBox.ItemsSource = BatList; BatNameListBox.SelectedItem = bat; } else { DisplayInvalidErrorMessage(errString); } }
/// <summary> /// Selecteds the bat. Returns the XElement from BatList for the bat /// corresponding to the XmlElement supplied as a parameter. /// </summary> /// <param name="selectedBat">The selected bat.</param> /// <returns></returns> private Bat SelectedBat(Bat selectedBatElement) { /*string batName = selectedBatElement.GetAttribute("Name"); * * var selectedBat = (from bat in BatList.Descendants("Bat") * where bat.Attribute("Name").Value == batName * select bat).FirstOrDefault();*/ return(selectedBatElement); }
private void RenameBat(Bat bat) { try { string CommonName = (from b in bat.BatCommonNames orderby b.SortIndex select b).First().BatCommonName1; string newName = CommonName.Trim().Replace(" ", ""); if (String.IsNullOrWhiteSpace(bat.Name)) { bat.Name = newName; } batReferenceDataContext.SubmitChanges(); bool OldChanging = changing; RefreshBatNameListBox(true); changing = OldChanging; } catch (Exception) { } }
/// <summary> /// Merges the bat to database. /// </summary> /// <param name="bat">The bat.</param> /// <param name="batReferenceDataContext">The bat reference data context.</param> /// <exception cref="NotImplementedException"></exception> private void MergeBatToDB(XElement bat, BatReferenceDBLinqDataContext batReferenceDataContext,short i) { try { Bat newBat = null; if (batReferenceDataContext == null) return; if (bat == null) return; bool isNew = false; if (batReferenceDataContext.Bats.Count() <= 0) { isNew = true; newBat = new Bat(); newBat.Id = -1; } else { var newBats = (from dBbat in batReferenceDataContext.Bats where dBbat.Name == bat.Attribute("Name").Value select dBbat); if (newBats != null && newBats.Count() > 0) { newBat = newBats.FirstOrDefault(); } } if (newBat == null || newBat.Id < 0) { newBat = new Bat(); newBat.Id = -1; isNew = true; } newBat.Name = bat.Attribute("Name").Value; newBat.Batgenus = bat.Descendants("BatGenus").FirstOrDefault().Value; newBat.BatSpecies = bat.Descendants("BatSpecies").FirstOrDefault().Value; newBat.SortIndex = i; if (isNew) { batReferenceDataContext.Bats.InsertOnSubmit(newBat); } batReferenceDataContext.SubmitChanges(); var newCommonNames = bat.Descendants("BatCommonName"); if (newCommonNames != null && newCommonNames.Count() > 0) { short index = 0; foreach (var name in newCommonNames) { BatCommonName bcn = new BatCommonName(); bcn.BatCommonName1 = name.Value; bcn.BatID = newBat.Id; bcn.SortIndex = index++; newBat.BatCommonNames.Add(bcn); } } var newTags = bat.Descendants("BatTag"); if (newTags != null && newTags.Count() > 0) { short index = 0; foreach (var tag in newTags) { BatTag bt = new BatTag(); bt.BatTag1 = tag.Value; bt.BatID = newBat.Id; bt.SortIndex = index++; newBat.BatTags.Add(bt); } } /* if (isNew) { batReferenceDataContext.Bats.InsertOnSubmit(newBat); }*/ batReferenceDataContext.SubmitChanges(); //int thisBatID = newBat.Id; //MergeCommonNames(bat, thisBatID, batReferenceDataContext); //MergeTags(bat, thisBatID, batReferenceDataContext); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex); } }
/// <summary> /// Selecteds the bat. Returns the XElement from the master BatList /// corresponding to the bat selected in the BatNameListBox. /// </summary> /// <returns></returns> private Bat SelectedBat() { Bat selectedBatElement = (Bat)BatNameListBox.SelectedItem; return(SelectedBat(selectedBatElement)); }
/// <summary> /// Merges the bat to database. /// </summary> /// <param name="bat">The bat.</param> /// <param name="batReferenceDataContext">The bat reference data context.</param> /// <exception cref="NotImplementedException"></exception> private void MergeBatToDB(XElement bat, BatReferenceDBLinqDataContext batReferenceDataContext, short i) { try { Bat newBat = null; if (batReferenceDataContext == null) { return; } if (bat == null) { return; } bool isNew = false; if (batReferenceDataContext.Bats.Count() <= 0) { isNew = true; newBat = new Bat(); newBat.Id = -1; } else { var newBats = (from dBbat in batReferenceDataContext.Bats where dBbat.Name == bat.Attribute("Name").Value select dBbat); if (newBats != null && newBats.Count() > 0) { newBat = newBats.FirstOrDefault(); } } if (newBat == null || newBat.Id < 0) { newBat = new Bat(); newBat.Id = -1; isNew = true; } newBat.Name = bat.Attribute("Name").Value; newBat.Batgenus = bat.Descendants("BatGenus").FirstOrDefault().Value; newBat.BatSpecies = bat.Descendants("BatSpecies").FirstOrDefault().Value; newBat.SortIndex = i; if (isNew) { batReferenceDataContext.Bats.InsertOnSubmit(newBat); } batReferenceDataContext.SubmitChanges(); var newCommonNames = bat.Descendants("BatCommonName"); if (newCommonNames != null && newCommonNames.Count() > 0) { short index = 0; foreach (var name in newCommonNames) { BatCommonName bcn = new BatCommonName(); bcn.BatCommonName1 = name.Value; bcn.BatID = newBat.Id; bcn.SortIndex = index++; newBat.BatCommonNames.Add(bcn); } } var newTags = bat.Descendants("BatTag"); if (newTags != null && newTags.Count() > 0) { short index = 0; foreach (var tag in newTags) { BatTag bt = new BatTag(); bt.BatTag1 = tag.Value; bt.BatID = newBat.Id; bt.SortIndex = index++; newBat.BatTags.Add(bt); } } /* if (isNew) * { * batReferenceDataContext.Bats.InsertOnSubmit(newBat); * }*/ batReferenceDataContext.SubmitChanges(); //int thisBatID = newBat.Id; //MergeCommonNames(bat, thisBatID, batReferenceDataContext); //MergeTags(bat, thisBatID, batReferenceDataContext); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex); } }
partial void DeleteBat(Bat instance);
partial void UpdateBat(Bat instance);
/// <summary> /// Validates the bat. Checks that CommonName, Genus, Species and Tag /// are all present. If not throws up a message box, and returns FALSE /// </summary> /// <param name="unselected">The unselected.</param> /// <returns></returns> private String ValidateBat(Bat unselected) { Bat previousBat = SelectedBat(unselected); int NumberOfCommonNames = previousBat.BatCommonNames.Count; if (NumberOfCommonNames<=0){ return("At least one Common Name required"); } RenameBat(previousBat); if (String.IsNullOrWhiteSpace(previousBat.Batgenus)){ return("Bat Genus required"); } if (String.IsNullOrWhiteSpace(previousBat.BatSpecies)){ return("Bat Species required"); } if (previousBat.BatTags.Count<=0){ return("Bat Tag required"); } return (null); }
/// <summary> /// Converts the bat to xelement. Takes a Bat reference from the database and /// formats the database information related to that bat into a single /// XElelement /// </summary> /// <param name="bat">The bat.</param> /// <returns></returns> public XElement ConvertBatToXelement(Bat bat) { XElement result = new XElement("Bat"); result.Add(new XAttribute("Name", bat.Name)); result.Add(new XElement("BatGenus", bat.Batgenus)); result.Add(new XElement("BatSpecies", bat.BatSpecies)); var commonNames = from cn in batReferenceDataContext.BatCommonNames where cn.BatID == bat.Id select cn.BatCommonName1; foreach(var name in commonNames) { result.Add(new XElement("BatCommonName", name)); } var tags = from tag in batReferenceDataContext.BatTags where tag.BatID == bat.Id select tag.BatTag1; foreach(var tag in tags) { result.Add(new XElement("BatTag", tag)); } return (result); }
partial void InsertBat(Bat instance);
private void AddRecordButton_Click(object sender, RoutedEventArgs e) { String errString = ValidateBat((Bat)BatNameListBox.SelectedItem); int index = BatNameListBox.SelectedIndex; if (String.IsNullOrWhiteSpace(errString)) { Bat bat = new Bat(); bat.Name = "bat"; bat.Batgenus = "BatGenus"; bat.BatSpecies = "BatSpecies"; BatCommonName bcn = new BatCommonName(); bcn.SortIndex = (short)-1; bcn.BatCommonName1 = "BatCommonName"; short max = short.MinValue; foreach(var cn in bat.BatCommonNames) { if (cn.SortIndex > max) max = cn.SortIndex; } bcn.SortIndex = ++max; bat.BatCommonNames.Add(bcn); BatTag bt = new BatTag(); bt.BatTag1 = "BatTag"; max = short.MinValue; foreach(var tg in bat.BatTags) { if (tg.SortIndex.Value > max) max = tg.SortIndex.Value; } bt.SortIndex = ++max; bat.BatTags.Add(bt); int? maxi = int.MaxValue; foreach(var b in BatList) { if (b.SortIndex > maxi) maxi = b.SortIndex; } bat.SortIndex = maxi.Value + 1; BatList.Add(bat); BatList = new ObservableCollection<Bat>(BatList.OrderBy(newbat => newbat.SortIndex)); BatNameListBox.ItemsSource = BatList; BatNameListBox.SelectedItem = bat; } else { DisplayInvalidErrorMessage(errString); } }
/// <summary> /// Selecteds the bat. Returns the XElement from BatList for the bat /// corresponding to the XmlElement supplied as a parameter. /// </summary> /// <param name="selectedBat">The selected bat.</param> /// <returns></returns> private Bat SelectedBat(Bat selectedBatElement) { /*string batName = selectedBatElement.GetAttribute("Name"); var selectedBat = (from bat in BatList.Descendants("Bat") where bat.Attribute("Name").Value == batName select bat).FirstOrDefault();*/ return (selectedBatElement); }