private void ClickedGeochachePin(Geocache geocache) { if (activePerson != null && activePerson.Geocaches == null || activePerson != null && !activePerson.Geocaches.Contains(geocache)) { var listOfIds = db.FoundGeocache.Where(fg => fg.PersonID == activePerson.ID).Select(fg => fg.GeocacheID).ToList(); if (listOfIds.Contains(geocache.ID)) { var foundGeocacheToDelete = db.FoundGeocache.First(fg => (fg.PersonID == activePerson.ID) && (fg.GeocacheID == geocache.ID)); db.Remove(foundGeocacheToDelete); db.SaveChanges(); } else { FoundGeocache foundgeocache = new FoundGeocache() { PersonID = activePerson.ID, GeocacheID = geocache.ID }; db.Add(foundgeocache); db.SaveChanges(); } } UpdateMap(); }
private void AddFoundGeochacheToDB(Geocache geocache) { var foundGeocache = new FoundGeocache(); foundGeocache.GeocacheID = geocache.ID; foundGeocache.PersonID = activePerson.ID; db.FoundGeocaches.Add(foundGeocache); db.SaveChanges(); }
private void OnSaveToFileClick(object sender, RoutedEventArgs args) { var dialog = new Microsoft.Win32.SaveFileDialog(); dialog.DefaultExt = ".txt"; dialog.Filter = "Text documents (.txt)|*.txt"; dialog.FileName = "Geocaches"; bool?result = dialog.ShowDialog(); if (result != true) { return; } string path = dialog.FileName; // Write to the selected file here. List <string> fileLines = new List <string>(); Person[] persons = db.Person.OrderByDescending(p => p).ToArray(); foreach (Person person in persons) { fileLines.Add(person.ToString()); Geocache[] geocaches = db.Geocache. Where(g => g.PersonID == person.ID). OrderByDescending(a => a).ToArray(); foreach (var geocache in geocaches) { fileLines.Add(geocache.ToString()); } FoundGeocache[] personFoundGeocaches = db.FoundGeocache. Where(fg => fg.PersonID == person.ID). OrderByDescending(a => a).ToArray(); fileLines.Add(FoundGeocache.CreateOutputString(personFoundGeocaches)); fileLines.Add(""); } //fileLines.RemoveAt(fileLines.Count() - 1); File.WriteAllLines(path, fileLines); }
private void ColorGeoPin() { IEnumerable <Geocache> GeocacheList = new List <Geocache>(); GeocacheList = db.Geocache.Include(geo => geo.Person).Include(geo => geo.FoundGeocaches); foreach (var g in GeocacheList) { Location location = new Location(g.Latitude, g.Longitude); string geoToolTip = TooltipMessageGeo(g); if (CurrentPerID == 0) { color = Colors.Gray; } else if (g.Person.ID == CurrentPerID) { color = Colors.Black; } else { color = Colors.Red; } var pinGeo = AddGeoPin(location, geoToolTip, color, 1, g); foreach (var fg in g.FoundGeocaches) { if (fg.PersonID == CurrentPerID) { pinGeo = AddGeoPin(location, geoToolTip, Colors.Green, 1, g); } } pinGeo.MouseDown += (s, a) => { a.Handled = true; Geocache currentGeo = new Geocache(); List <int> currentFGeoIDs = new List <int>(); currentGeo = db.Geocache.Include(geo => geo.Person) .First(geo => geo.Latitude == location.Latitude && geo.Longitude == location.Longitude); // To find out each geocache ID that the currently selected person has found and add all of them to a list. currentFGeoIDs = db.FoundGeocache.Where(fg => fg.PersonID == CurrentPerID).Select(fg => fg.GeocacheID).ToList(); if (CurrentPerID != currentGeo.Person.ID && CurrentPerID != 0) { if (currentFGeoIDs.Contains(currentGeo.ID)) { // if currently clicked person have found the geocache, remove from db and change color to red pinGeo = AddGeoPin(location, geoToolTip, Colors.Red, 1, currentGeo); var fgToDelete = db.FoundGeocache.First(fg => (fg.PersonID == CurrentPerID) && (fg.GeocacheID == currentGeo.ID)); db.Remove(db.FoundGeocache.Single(fg => fg.GeocacheID == fgToDelete.GeocacheID && fg.PersonID == CurrentPerID)); } else { // if currently clicked person haven't found geocache add to db and change color to green. pinGeo = AddGeoPin(location, geoToolTip, Colors.Green, 1, currentGeo); var foundFG = new FoundGeocache { GeocacheID = currentGeo.ID, PersonID = CurrentPerID }; db.Add(foundFG); } db.SaveChanges(); UpdateMap(); } }; } }
private void OnLoadFromFileClick(object sender, RoutedEventArgs args) { var dialog = new Microsoft.Win32.OpenFileDialog(); dialog.DefaultExt = ".txt"; dialog.Filter = "Text documents (.txt)|*.txt"; bool?result = dialog.ShowDialog(); if (result != true) { return; } string path = dialog.FileName; // Read the selected file here. Dictionary <Person, List <int> > personFoundGeocaches = new Dictionary <Person, List <int> >(); Dictionary <int, Geocache> specificGeocache = new Dictionary <int, Geocache>(); string[] lines = File.ReadAllLines(path, Encoding.GetEncoding("ISO-8859-1")).ToArray(); int counterPersonObject = 0; int emptyLineCounter = 0; Person person = new Person(); Geocache geocache = new Geocache(); db.Person.RemoveRange(db.Person); db.Geocache.RemoveRange(db.Geocache); db.SaveChanges(); foreach (string line in lines) { if (!line.Contains("Found")) { string[] values = line.Split('|').Select(v => v.Trim()).ToArray(); if (values[0] != "" && counterPersonObject == 0) { person = new Person(); person.FirstName = values[0]; person.LastName = values[1]; person.Address = new Address { Country = values[2], City = values[3], StreetName = values[4], StreetNumber = byte.Parse(values[5]) }; person.GeoCoordinate = new Coordinate { Latitude = double.Parse(values[6]), Longitude = double.Parse(values[7]) }; db.Person.Add(person); db.SaveChanges(); emptyLineCounter = 0; counterPersonObject++; } else if (values[0] == "") { counterPersonObject = 0; emptyLineCounter++; if (emptyLineCounter == 2) { return; } } else { geocache = new Geocache(); int geocacheNumber = int.Parse(values[0]); geocache.GeoCoordinate = new Coordinate { Latitude = double.Parse(values[1]), Longitude = double.Parse(values[2]) }; geocache.Contents = values[3]; geocache.Message = values[4]; geocache.Person = person; db.Geocache.Add(geocache); db.SaveChanges(); specificGeocache.Add(geocacheNumber, geocache); } } else { string[] geocachesFound = line.Split(':', ',').Skip(1).Select(v => v.Trim()).ToArray(); List <int> geocachesId = new List <int>(); foreach (var item in geocachesFound) { int geocacheId = int.Parse(item); geocachesId.Add(geocacheId); } personFoundGeocaches.Add(person, geocachesId); } } foreach (var personObject in personFoundGeocaches.Keys) { foreach (int geocacheId in personFoundGeocaches[personObject]) { var geocacheObject = specificGeocache[geocacheId]; FoundGeocache foundGeocache = new FoundGeocache { PersonID = personObject.ID, GeocacheID = geocacheObject.ID }; db.Add(foundGeocache); db.SaveChanges(); } ; } pushpins.Clear(); layer.Children.Clear(); activePerson = null; ReadPersonAndGeocacheFromDatabase(); }
// Load from file. private async void OnLoadFromFileClick(object sender, RoutedEventArgs args) { var dialog = new Microsoft.Win32.OpenFileDialog(); dialog.DefaultExt = ".txt"; dialog.Filter = "Text documents (.txt)|*.txt"; bool?result = dialog.ShowDialog(); if (result != true) { return; } string path = dialog.FileName; // Clear database and reset map. using (var database = new AppDbContext()) { var task = Task.Run(() => { database.Person.RemoveRange(database.Person); database.Geocache.RemoveRange(database.Geocache); database.SaveChanges(); }); await(task); ActivePersonID = 0; layer.Children.Clear(); } // Read and split selected file here. List <Person> personsTotalList = new List <Person> { }; string textFromFile = File.ReadAllText(path, Encoding.Default); string[] textParts = textFromFile.Split(new string[] { Environment.NewLine + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); // Loop to get person objects and there placed geocaches to lists. foreach (string part in textParts) { List <Geocache> geocaches = new List <Geocache> { }; string[] lines = part.Split('\n'); foreach (string line in lines) { if (char.IsDigit(line[0])) { string[] values = line.Split('|'); Geocache g = new Geocache { Content = values[3].Trim(), Message = values[4].Trim(), Coordinates = new GeoCoordinate { Latitude = Convert.ToDouble(values[1]), Longitude = Convert.ToDouble(values[2]) } }; geocaches.Add(g); } else if (line.StartsWith("Found:") == false && char.IsDigit(line[0]) == false) { string[] values = line.Split('|'); Person p = new Person { FirstName = values[0].Trim(), LastName = values[1].Trim(), Country = values[2].Trim(), City = values[3].Trim(), StreetName = values[4].Trim(), StreetNumber = Convert.ToByte(values[5]), Geocaches = geocaches, Coordinates = new GeoCoordinate { Latitude = Convert.ToDouble(values[6]), Longitude = Convert.ToDouble(values[7]) } }; personsTotalList.Add(p); } } } // Loop to put all geocaches in geocachesTotalList. List <Geocache> geocachesTotalList = new List <Geocache> { }; foreach (Person p in personsTotalList) { foreach (Geocache g in p.Geocaches) { geocachesTotalList.Add(g); } } // Loop to get FoundGeocaches and connections to join-tabel to a dictionary. Dictionary <Person, List <int> > foundGeocachesDictionary = new Dictionary <Person, List <int> >(); foreach (string part in textParts) { List <int> indexes = new List <int>(); string[] lines = part.Split('\n'); foreach (string line in lines) { string ln1 = line.Substring(6); if (line.StartsWith("Found:") && ln1 != " " && ln1 != "") { string[] numbers = ln1.Split(','); foreach (string n in numbers) { int n1 = Convert.ToInt32(n); indexes.Add(n1); } } else if (line.StartsWith("Found:") == false && char.IsDigit(line[0]) == false) { string[] values = line.Split('|'); foreach (Person p in personsTotalList) { if (p.FirstName == values[0].Trim() && p.LastName == values[1].Trim()) { foundGeocachesDictionary[p] = indexes; } } ; } } } // Loop through dictionary and make a lista of FoundGeocaches. List <FoundGeocache> foundGeocaches = new List <FoundGeocache>(); foreach (KeyValuePair <Person, List <int> > pair in foundGeocachesDictionary) { foreach (int i in pair.Value) { FoundGeocache fg = new FoundGeocache { Person = pair.Key, Geocache = geocachesTotalList[i - 1] }; foundGeocaches.Add(fg); } } // Loop through all persons and add to foundGeocaches list. foreach (Person p in personsTotalList) { p.FoundGeocaches = new List <FoundGeocache>(); foreach (FoundGeocache fg in foundGeocaches) { if (fg.Person.FirstName == p.FirstName && fg.Person.LastName == p.LastName) { p.FoundGeocaches.Add(fg); } } } // Loop through all geocaches and add to foundGeocaches list. foreach (Geocache g in geocachesTotalList) { g.FoundGeocaches = new List <FoundGeocache>(); foreach (FoundGeocache fg in foundGeocaches) { if (fg.Geocache.Message == g.Message && fg.Geocache.Content == g.Content) { g.FoundGeocaches.Add(fg); } } } // Save all persons and geocaches to database, asynchronously. using (var database = new AppDbContext()) { foreach (Person p in personsTotalList) { database.Add(p); } foreach (Geocache g in geocachesTotalList) { database.Add(g); } var task = Task.Run(() => { database.SaveChanges(); }); await(task); } UpdateMap(); }
// Loops through Geocache and gives the right color to the pins, asynchronously. private async void BeginColorGeocachePins() { var database = new AppDbContext(); IEnumerable <Geocache> GeocacheAsync = new List <Geocache>(); var task = Task.Run(() => { GeocacheAsync = database.Geocache.Include(g => g.Person).Include(g => g.FoundGeocaches); }); await(task); foreach (var g in GeocacheAsync) { Location locationGeocache = new Location(g.Coordinates.Latitude, g.Coordinates.Longitude); string geocacheToolTip = "long:" + g.Coordinates.Longitude + "\nlat: " + g.Coordinates.Latitude + "\nContent: " + g.Content + "\nMessage: " + g.Message + "\nPlaced by: " + g.Person.FirstName + " " + g.Person.LastName; if (ActivePersonID == 0) { color = Colors.Gray; } else if (g.Person.ID == ActivePersonID) { color = Colors.Black; } else { color = Colors.Red; } var pinGeocache = AddPin(locationGeocache, geocacheToolTip, color); foreach (var f in g.FoundGeocaches) { if (f.PersonID == ActivePersonID) { pinGeocache = AddPin(locationGeocache, geocacheToolTip, Colors.Green); } } // Put delay here to check asynchronisity on geocache-pins. await Task.Delay(0); // Change color on Geocache-pins on click. pinGeocache.MouseDown += async(s, a) => { a.Handled = true; Geocache activeGeocache = new Geocache(); List <int> ActivePersonFoundGeocachIDs = new List <int>(); var task1 = Task.Run(() => { activeGeocache = database.Geocache.Include(e => e.Person).First(gc => gc.Coordinates.Latitude == locationGeocache.Latitude && g.Coordinates.Longitude == locationGeocache.Longitude); ActivePersonFoundGeocachIDs = database.FoundGeocache.Where(f => f.PersonID == ActivePersonID).Select(f => f.GeocacheID).ToList(); }); await(task1); // If ActivePerson is selected and is not the one who placed the geocache... if (ActivePersonID != activeGeocache.Person.ID && ActivePersonID != 0) { //... and ActivePerson already found this geocachen, then change the pin-color to red and remove from FoundGeocache. if (ActivePersonFoundGeocachIDs.Contains(activeGeocache.ID)) { pinGeocache = AddPin(locationGeocache, geocacheToolTip, Colors.Red); var task2 = Task.Run(() => { var foundGeocacheToDelete = database.FoundGeocache.First(fg => (fg.PersonID == ActivePersonID) && (fg.GeocacheID == activeGeocache.ID)); database.Remove(database.FoundGeocache.Single(fg => fg.GeocacheID == foundGeocacheToDelete.GeocacheID && fg.PersonID == ActivePersonID)); }); await(task2); } // Else change pin-color to green and add to FoundGeocache. else { pinGeocache = AddPin(locationGeocache, geocacheToolTip, Colors.Green); var foundGeocache = new FoundGeocache { GeocacheID = activeGeocache.ID, PersonID = ActivePersonID }; database.Add(foundGeocache); } var task3 = Task.Run(() => { database.SaveChanges(); }); await(task3); UpdateMap(); } }; } }
public void ReadFromFile(string path, AppDbContext db) { var person = new List <Person>(); var found = new Dictionary <Person, int[]>(); var geocache = new Dictionary <int, Geocache>(); found.Clear(); string[] lines = File.ReadAllLines(path); foreach (string line in lines) { string[] split = line.Split('|').Select(v => v.Trim()).ToArray(); if (split[0] != "") { if (split.Length == 8) { string firstName = split[0]; string lastName = split[1]; string country = split[2]; string city = split[3]; string streetName = split[4]; Int16 streetNumber = Int16.Parse(split[5]); double latitude = double.Parse(split[6]); double longitude = double.Parse(split[7]); var newPerson = new Person { FirstName = firstName, LastName = lastName, Country = country, City = city, StreetName = streetName, StreetNumber = streetNumber, Latitude = latitude, Longitude = longitude }; if (person.Count == 0) { person.Add(newPerson); } else { person.Clear(); person.Add(newPerson); } } else if (split.Length == 5) { int id = int.Parse(split[0]); double latitude = double.Parse(split[1]); double longitude = double.Parse(split[2]); string contents = split[3]; string message = split[4]; var newGeo = new Geocache { Latitude = latitude, Longitude = longitude, Contents = contents, Message = message, Person = person[0] }; geocache.Add(id, newGeo); } else { string foundString = split[0].Substring(6); int[] intSplit = foundString.Split(',').Select(s => int.Parse(s.Trim())).ToArray(); if (foundString != "") { found.Add(person[0], intSplit); } else { db.Add(person[0]); } } } } foreach (var item in found) { foreach (var foundGeo in item.Value) { var newfg = new FoundGeocache() { Person = item.Key, Geocache = geocache.Where(s => s.Key == foundGeo).Select(fg => fg.Value).First() }; db.Add(newfg); try { db.SaveChanges(); } catch { } } } }
private void RemoveFoundGeocacheFromDb(FoundGeocache foundGeocache) { db.FoundGeocaches.Remove(foundGeocache); db.SaveChanges(); }
private void OnLoadFromFileClick(object sender, RoutedEventArgs args) { activePerson = new Person(); ClearDatabase(); var dialog = new Microsoft.Win32.OpenFileDialog(); dialog.DefaultExt = ".txt"; dialog.Filter = "Text documents (.txt)|*.txt"; bool?result = dialog.ShowDialog(); if (result != true) { return; } string path = dialog.FileName; // Read the selected file here. string[] lines = File.ReadAllLines(path).ToArray(); var foundList = new Dictionary <Person, List <int> >(); try { foreach (string line in lines) { string[] values = line.Split('|').Select(v => v.Trim()).ToArray(); if (!line.StartsWith("Found:") && values.Length == 8) { var person = new Person(); person.FirstName = values[0]; person.LastName = values[1]; person.Country = values[2]; person.City = values[3]; person.StreetName = values[4]; person.StreetNumber = byte.Parse(values[5]); person.Latitude = double.Parse(values[6]); person.Longitude = double.Parse(values[7]); db.Add(person); db.SaveChanges(); } else if (!line.StartsWith("Found:") && values.Length == 5) { var person = db.Person.OrderByDescending(p => p.ID).First(); var geocache = new Geocache(); geocache.Person = person; geocache.ID = int.Parse(values[0]); geocache.Latitude = double.Parse(values[1]); geocache.Longitude = double.Parse(values[2]); geocache.Contents = values[3]; geocache.Message = values[4]; db.Add(geocache); db.SaveChanges(); } else if (line.StartsWith("Found:")) { var person = db.Person.OrderByDescending(p => p.ID).First(); string[] found = line.Split(',', ':').Skip(1).Select(v => v.Trim()).ToArray(); var intList = new List <int>(); foreach (var foundNum in found) { int intAdd; var num = int.TryParse(foundNum, out intAdd); if (num) { intList.Add(intAdd); } } foundList.Add(person, intList); } } foreach (var pair in foundList) { var personValue = pair.Value; foreach (var value in personValue) { var geocache = db.Geocache.First(g => g.ID == value); var foundGeocache = new FoundGeocache(); foundGeocache.Person = pair.Key; foundGeocache.Geocache = geocache; db.Add(foundGeocache); db.SaveChanges(); } } } catch { MessageBox.Show("The textfile is not properly formatted"); } UpdateMap(); }