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> /// Merges the common names contained in the XElement Bat into the database /// whose DataContext is provided linked to the bat entry ID /// </summary> /// <param name="bat">The bat.</param> /// <param name="thisBatID">The this bat identifier.</param> /// <param name="batReferenceDataContext">The bat reference data context.</param> private void MergeCommonNames(XElement bat, int thisBatID, BatReferenceDBLinqDataContext batReferenceDataContext) { var newCommonNames = bat.Descendants("BatCommonName"); var oldCommonNames = from name in batReferenceDataContext.BatCommonNames where name.BatID == thisBatID select name.BatCommonName1; var oldCommonNamesAsList = oldCommonNames.ToList(); foreach (string newname in newCommonNames) { if (!oldCommonNamesAsList.Contains(newname)) { BatCommonName bcn = new BatCommonName(); bcn.BatCommonName1 = newname; bcn.BatID = thisBatID; batReferenceDataContext.BatCommonNames.InsertOnSubmit(bcn); batReferenceDataContext.SubmitChanges(); } } }
private void CommonNameAddButton_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrWhiteSpace(CommonNameEditBox.Text)) { var bat = BatList[BatNameListBox.SelectedIndex] as Bat; var name = from n in bat.BatCommonNames where n.BatCommonName1.ToUpper() == CommonNameEditBox.Text.ToUpper() select n; if (name != null && name.Count() > 0) { return;// name in the edit box already exists for this bat } else { BatCommonName bcn = new BatCommonName(); bcn.BatCommonName1 = CommonNameEditBox.Text; bcn.BatID = bat.Id; bat.BatCommonNames.Add(bcn); RefreshBatNameListBox(true); } } }
/// <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 DeleteBatCommonName(BatCommonName instance);
partial void UpdateBatCommonName(BatCommonName instance);
partial void InsertBatCommonName(BatCommonName instance);
private void detach_BatCommonNames(BatCommonName entity) { this.SendPropertyChanging(); entity.Bat = 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> /// 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); } }
private void attach_BatCommonNames(BatCommonName entity) { this.SendPropertyChanging(); entity.Bat = this; }
private void CommonNameAddButton_Click(object sender, RoutedEventArgs e) { if (!String.IsNullOrWhiteSpace(CommonNameEditBox.Text)) { var bat = BatList[BatNameListBox.SelectedIndex] as Bat; var name = from n in bat.BatCommonNames where n.BatCommonName1.ToUpper() == CommonNameEditBox.Text.ToUpper() select n; if(name!=null && name.Count() > 0) { return;// name in the edit box already exists for this bat } else { BatCommonName bcn = new BatCommonName(); bcn.BatCommonName1 = CommonNameEditBox.Text; bcn.BatID = bat.Id; bat.BatCommonNames.Add(bcn); RefreshBatNameListBox(true); } } }