/// <summary> /// Copies the XML data to database. /// </summary> /// <param name="xmlFile">The the XML data source file</param> /// <param name="batReferenceDataContext">The bat reference data context.</param> /// <exception cref="NotImplementedException"></exception> private void copyXMLDataToDatabase(string xmlFile, BatReferenceDBLinqDataContext batReferenceDataContext) { var xmlBats = XElement.Load(xmlFile).Descendants("Bat"); short i = 0; foreach(XElement bat in xmlBats) { MergeBatToDB(bat, batReferenceDataContext,i++); } }
/// <summary> /// Copies the XML data to database. /// </summary> /// <param name="xmlFile">The the XML data source file</param> /// <param name="batReferenceDataContext">The bat reference data context.</param> /// <exception cref="NotImplementedException"></exception> private void copyXMLDataToDatabase(string xmlFile, BatReferenceDBLinqDataContext batReferenceDataContext) { var xmlBats = XElement.Load(xmlFile).Descendants("Bat"); short i = 0; foreach (XElement bat in xmlBats) { MergeBatToDB(bat, batReferenceDataContext, i++); } }
//public ObservableCollection<Bat> BatList; public BatEditor(BatReferenceDBLinqDataContext BatReferenceDataContext) { batReferenceDataContext = BatReferenceDataContext; BatList = new ObservableCollection <Bat>(); InitializeComponent(); this.DataContext = this; LoadDataToBatList(); BatNameListBox.ItemsSource = BatList; }
//public ObservableCollection<Bat> BatList; public BatEditor(BatReferenceDBLinqDataContext BatReferenceDataContext) { batReferenceDataContext = BatReferenceDataContext; BatList = new ObservableCollection<Bat>(); InitializeComponent(); this.DataContext = this; LoadDataToBatList(); BatNameListBox.ItemsSource = BatList; }
/// <summary> /// Initializes a new instance of the <see cref="BatSummary"/> class. /// </summary> public BatSummary() { try { string DBFileName = "BatReferenceDB.mdf"; string workingDatabaseLocation= Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Echolocation\WinBLP\"); if (!Directory.Exists(workingDatabaseLocation)) { Directory.CreateDirectory(workingDatabaseLocation); } //One off routine for transition from XML to DB batReferenceDataContext = new BatReferenceDBLinqDataContext(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + workingDatabaseLocation + DBFileName + @";Integrated Security=False;Connect Timeout=30"); if (batReferenceDataContext == null) return; if (!batReferenceDataContext.DatabaseExists()) { batReferenceDataContext.CreateDatabase(); } if (File.Exists(workingDatabaseLocation+"EditableBatReferenceXMLFile.xml")) { copyXMLDataToDatabase(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml", batReferenceDataContext); if(File.Exists(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml.bak")) { File.Delete(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml.bak"); } File.Move(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml", workingDatabaseLocation + "EditableBatReferenceXMLFile.xml.bak"); } } catch (Exception ex) { Debug.WriteLine(ex); } }
/// <summary> /// Initializes a new instance of the <see cref="BatSummary"/> class. /// </summary> public BatSummary() { try { string DBFileName = "BatReferenceDB.mdf"; string workingDatabaseLocation = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Echolocation\WinBLP\"); if (!Directory.Exists(workingDatabaseLocation)) { Directory.CreateDirectory(workingDatabaseLocation); } //One off routine for transition from XML to DB batReferenceDataContext = new BatReferenceDBLinqDataContext(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + workingDatabaseLocation + DBFileName + @";Integrated Security=False;Connect Timeout=30"); if (batReferenceDataContext == null) { return; } if (!batReferenceDataContext.DatabaseExists()) { batReferenceDataContext.CreateDatabase(); } if (File.Exists(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml")) { copyXMLDataToDatabase(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml", batReferenceDataContext); if (File.Exists(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml.bak")) { File.Delete(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml.bak"); } File.Move(workingDatabaseLocation + "EditableBatReferenceXMLFile.xml", workingDatabaseLocation + "EditableBatReferenceXMLFile.xml.bak"); } } catch (Exception ex) { Debug.WriteLine(ex); } }
/// <summary> /// Merges the tags 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 MergeTags(XElement bat, int thisBatID, BatReferenceDBLinqDataContext batReferenceDataContext) { var newTags = bat.Descendants("BatTag"); var oldTags = from tag in batReferenceDataContext.BatTags where tag.BatID == thisBatID select tag.BatTag1; var oldTagsAsList = oldTags.ToList(); foreach (string tag in newTags) { if (!oldTagsAsList.Contains(tag)) { BatTag bt = new BatTag(); bt.BatTag1 = tag; bt.BatID = thisBatID; batReferenceDataContext.BatTags.InsertOnSubmit(bt); batReferenceDataContext.SubmitChanges(); } } }
/// <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(); } } }
/// <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> /// 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> /// Merges the tags 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 MergeTags(XElement bat,int thisBatID, BatReferenceDBLinqDataContext batReferenceDataContext) { var newTags = bat.Descendants("BatTag"); var oldTags = from tag in batReferenceDataContext.BatTags where tag.BatID == thisBatID select tag.BatTag1; var oldTagsAsList = oldTags.ToList(); foreach (string tag in newTags) { if (!oldTagsAsList.Contains(tag)) { BatTag bt = new BatTag(); bt.BatTag1 = tag; bt.BatID = thisBatID; batReferenceDataContext.BatTags.InsertOnSubmit(bt); batReferenceDataContext.SubmitChanges(); } } }
/// <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(); } } }