public bool UpdateVoter() { bool success = false; VoterFileDataContext _voterDB = new VoterFileDataContext(string.Format(VoterFileDataContext.DBConnectionString, _settings.DbFileName)); if (_voterDB.DatabaseExists()) { try { IQueryable <VoterFileEntry> voterQuery = from voter in _voterDB.AllVoters where voter.VoterID == _selectedVoter.VoterID select voter; VoterFileEntry voterToUpdate = voterQuery.FirstOrDefault(); if (voterToUpdate.VoterID == _selectedVoter.VoterID) { voterToUpdate.Party = _selectedVoter.VoterFile.Party; voterToUpdate.ResultOfContact = _selectedVoter.ResultOfContact; voterToUpdate.Email = _selectedVoter.Email; voterToUpdate.CellPhone = _selectedVoter.CellPhone; voterToUpdate.IsSupporter = _selectedVoter.IsSupporter; voterToUpdate.IsVolunteer = _selectedVoter.IsVolunteer; voterToUpdate.IsUpdated = _selectedVoter.VoterFile.IsUpdated; voterToUpdate.Comments = _selectedVoter.Comments; voterToUpdate.ModifiedTime = DateTime.Now; _selectedVoter.VoterFile.ModifiedTime = voterToUpdate.ModifiedTime; _voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict); success = true; NotifyPropertyChanged("HasUpdates"); } else { Log(String.Format(" ERROR: Voter returned from query (id={0}) did not match selected voter (id={1}).", voterToUpdate.VoterID, _selectedVoter.VoterID)); } } catch (Exception ex) { Log(" Error updating voter record: " + _selectedVoter.FullName + " : " + ex.ToString()); } } _voterDB.Dispose(); return(success); }
private void SaveVoterChanges() { // If either the email or cell textbox controls had focus when save button was tapped those changes were // not updated ot the view model (because focus was not lost), so we will do that now. if (txtEmail.Text != ((VoterFileEntry)DataContext).Email) { BindingExpression expression = txtEmail.GetBindingExpression(TextBox.TextProperty); expression.UpdateSource(); } if (txtCell.Text != ((VoterFileEntry)DataContext).CellPhone) { BindingExpression expression = txtCell.GetBindingExpression(TextBox.TextProperty); expression.UpdateSource(); } if (txtComments.Text != ((VoterFileEntry)DataContext).Comments) { BindingExpression expression = txtComments.GetBindingExpression(TextBox.TextProperty); expression.UpdateSource(); } if (_voterDB.DatabaseExists()) { ((VoterFileEntry)DataContext).ModifiedTime = System.DateTime.Now; ((VoterFileEntry)DataContext).IsUpdated = true; _voterDB.SubmitChanges(); App.Log("Submitted changes to database"); } else { App.Log("ERROR: we don't have a database!"); } _voterDB.Dispose(); // PostUpdate() will attempt to post this update (and any outstanding updates) if there is network access. // If the update fails, then the next change will trigger another call to PostUpdate(), which will attempt // to post that update and this one. App.thisApp.PostUpdate(); }
public void LoadDatabase(Object voterFileName) { // TODO: Update passed object to stream, rely on dataformat setting to get type of data in voter list /* recid,address,address2,city,state,zip,phone,lastname,firstname,email,precinct,party,pri,gen,location,resultofcontact,cellphone,issupporter,comments,modifiedtime * WA00092212345,1234 Anystreet,,Sammamish,WA,98074,,BOND,JAMES,,SAM 45-1234,1,2,4,"47.6161616,-122.0551111",,,,, */ string fileName = ""; if (voterFileName is string) { fileName = voterFileName as string; } else { return; } IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream voterFile = isf.OpenFile(fileName, FileMode.Open); Stream voterDataStream = voterFile; if (fileName.EndsWith(".csv")) { App.thisApp._settings.UpdateSetting("dataformat", "csv"); } else if (fileName.EndsWith(".zip")) { string _voterFileName = "voters.csv"; if (voterFile.CanSeek) { byte[] fileFront = new byte[64]; int bytesRead = voterFile.Read(fileFront, 0, 64); voterFile.Seek(0, SeekOrigin.Begin); byte[] fileNameBytes = new byte[20]; int i = 0; for (i = 0; i < 16; i++) { fileNameBytes[i] = fileFront[30 + i]; if (fileNameBytes[i] > 128) { fileNameBytes[i] = 0; break; } } _voterFileName = Encoding.UTF8.GetString(fileNameBytes, 0, i); } StreamResourceInfo zipInfo = new StreamResourceInfo(voterFile, "application/zip"); StreamResourceInfo streamInfo = Application.GetResourceStream(zipInfo, new Uri(_voterFileName, UriKind.Relative)); if (streamInfo != null) { voterDataStream = streamInfo.Stream; } } int nVoters = 0; VoterFileDataContext _voterDB = new VoterFileDataContext(string.Format(VoterFileDataContext.DBConnectionString, _settings.DbFileName)); if (_settings.DbStatus == DbState.Loaded && DbHasUpdates()) { PostUpdate(); _delayLoadDbFile = fileName; return; } else { try { if (!_voterDB.DatabaseExists()) { App.Log("Creating new database..."); _voterDB.CreateDatabase(); _settings.DbStatus = DbState.Empty; App.Log(" Database created"); } else { App.Log("WARNING: Deleting existing database..."); _voterDB.DeleteDatabase(); App.Log("Creating new database..."); _voterDB.CreateDatabase(); _settings.DbStatus = DbState.Empty; App.Log(" Database created"); } if (App.thisApp._settings.GetSetting <string>("dataformat") == "csv") { // Load voters from rows in csv text StreamReader csvReader = new StreamReader(voterDataStream); String headerLine = csvReader.ReadLine().ToLower(); String voterLine = csvReader.ReadLine(); _settings.DbStatus = DbState.Loading; App.Log(" Parsing voters from CSV"); while (voterLine != null) { // recid,firstname,lastname,address,address2,city,state,zip,precinct,lat,long,gender,birthdate,pri,gen,phone,email,party,cellphone,comments,issupporter XElement voterElement = GetVoterXmlFromCsv(headerLine, voterLine); // We need at least the basics of address, city, and voterid to do anything useful with this record try { VoterFileEntry voter = new VoterFileEntry { Address = (string)voterElement.Element("address"), City = (string)voterElement.Element("city"), // VoterID = (string)voterElement.Element("recid"), ModifiedTime = DateTime.Now }; if (voterElement.Elements("recid").Count() > 0) { voter.VoterID = (string)voterElement.Element("recid"); App.thisApp._settings.UpdateSetting("_trhack", false); } // NOTE: This is a hack to fix my data error just before TechRoanoke conference else if (voterElement.Elements("redid").Count() > 0) { voter.VoterID = (string)voterElement.Element("redid"); App.thisApp._settings.UpdateSetting("_trhack", true); } if (voterElement.Elements("firstname").Count() > 0) { voter.FirstName = (string)voterElement.Element("firstname"); } if (voterElement.Elements("lastname").Count() > 0) { voter.LastName = (string)voterElement.Element("lastname"); } if (voterElement.Elements("address2").Count() > 0) { voter.Address2 = (string)voterElement.Element("address2"); } if (voterElement.Elements("state").Count() > 0) { voter.State = (string)voterElement.Element("state"); } else { voter.State = "WA"; } if (voterElement.Elements("zip").Count() > 0) { voter.Zip = (string)voterElement.Element("zip"); } if (voterElement.Elements("party").Count() > 0) { voter.PartyString = (string)voterElement.Element("party"); } if (voterElement.Elements("precinctname").Count() > 0) // Prefer "precinctname" over "precinct" { voter.Precinct = (string)voterElement.Element("precinctname"); } else if (voterElement.Elements("precinct").Count() > 0) { voter.Precinct = (string)voterElement.Element("precinct"); } if (voterElement.Elements("pri").Count() > 0) { voter.PrimaryVoteHistoryString = (string)voterElement.Element("pri"); } if (voterElement.Elements("gen").Count() > 0) { voter.GeneralVoteHistoryString = (string)voterElement.Element("gen"); } if (voterElement.Elements("email").Count() > 0) { voter.Email = (string)voterElement.Element("email"); } if (voterElement.Elements("phone").Count() > 0) { voter.Phone = (string)voterElement.Element("phone"); } if (voterElement.Elements("location").Count() > 0) { voter.Coordinates = (string)voterElement.Element("location"); } else { if (voterElement.Elements("lat").Count() > 0) { voter.LatitudeString = (string)voterElement.Element("lat"); } if (voterElement.Elements("long").Count() > 0) { voter.LongitudeString = (string)voterElement.Element("long"); } } if (voterElement.Elements("resultofcontact").Count() > 0) { voter.ResultOfContactString = (string)voterElement.Element("resultofcontact"); } if (voterElement.Elements("cellphone").Count() > 0) { voter.CellPhone = (string)voterElement.Element("cellphone"); } if (voterElement.Elements("issupporter").Count() > 0) { voter.IsSupporterString = (string)voterElement.Element("issupporter"); } if (voterElement.Elements("isvolunteer").Count() > 0) { voter.IsVolunteerString = (string)voterElement.Element("isvolunteer"); } if (voterElement.Elements("comments").Count() > 0) { voter.Comments = (string)voterElement.Element("comments"); } if (voterElement.Elements("address2").Count() > 0) { voter.Address2 = (string)voterElement.Element("address2"); } if (voterElement.Elements("gender").Count() > 0) { voter.Gender = (string)voterElement.Element("gender"); voter.FirstName = voter.FirstName + " " + voter.Gender; } if (voterElement.Elements("birthdate").Count() > 0) { voter.Birthdate = (string)voterElement.Element("birthdate"); if (voter.Age > 0) { voter.FirstName = voter.FirstName + " " + voter.Age.ToString(); } } // We don't have lat/long for this voter, but we can still show them in precinct and street lists if (0.0 == voter.Latitude || 0.0 == voter.Longitude) { // continue; } nVoters++; _voterDB.AllVoters.InsertOnSubmit(voter); } catch (Exception ex) { App.Log("There was a problem parsing voter record - " + voterLine + " : " + ex.ToString()); } voterLine = csvReader.ReadLine(); } App.Log(" Parsing voters completed"); } else { App.Log("Unrecognized file type - " + fileName); } voterDataStream.Close(); voterDataStream.Dispose(); voterFile.Close(); voterFile.Dispose(); App.Log(" Voters submitted to database: " + nVoters.ToString()); _voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict); isf.DeleteFile(fileName); _settings.DbStatus = DbState.Loaded; _settings.UpdateSetting("lastsync", DateTime.Now); App.Log(" Loading Precincts table"); IEnumerable <string> precincts = (from v in _voterDB.AllVoters select v.Precinct).Distinct(); foreach (string precinct in precincts) { PrecinctTableEntry thisPrecinct = new PrecinctTableEntry(); thisPrecinct.Name = precinct; List <VoterFileEntry> pList = (from VoterFileEntry vPrecinct in _voterDB.AllVoters where vPrecinct.Precinct == precinct && vPrecinct.Latitude > 47.0 && vPrecinct.Longitude < 122.0 select vPrecinct).ToList(); App.Log(String.Format(" There are {0} voters in {1} precinct.", pList.Count, precinct)); if (pList.Count > 0) { thisPrecinct.West = pList.Min <VoterFileEntry, double>(vMin => vMin.Longitude); // smallest longitude found in list (or largest absolute value) thisPrecinct.East = pList.Max <VoterFileEntry, double>(vMax => vMax.Longitude); // largest longitude found in list (or smallest absolute value) thisPrecinct.North = pList.Max <VoterFileEntry, double>(vMax => vMax.Latitude); // largest latitude found in list thisPrecinct.South = pList.Min <VoterFileEntry, double>(vMin => vMin.Latitude); // smallest latitude found in list App.Log(String.Format("Precinct {4} - North={0}, South={1}, East={2}, West={3}.", thisPrecinct.North, thisPrecinct.South, thisPrecinct.East, thisPrecinct.West, thisPrecinct.Name)); double diffLat = thisPrecinct.North - thisPrecinct.South; double diffLong = thisPrecinct.East - thisPrecinct.West; thisPrecinct.CenterLatitude = thisPrecinct.South + (diffLat / 2); thisPrecinct.CenterLongitude = thisPrecinct.West + (diffLong / 2); _voterDB.Precincts.InsertOnSubmit(thisPrecinct); } } App.Log(" Submitting precincts to database: "); _voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict); App.Log(" Loading Streets table"); IEnumerable <string> _streets = (from v in _voterDB.AllVoters select v.Street).Distinct(); foreach (string street in _streets) { StreetTableEntry thisStreet = new StreetTableEntry(); thisStreet.Name = street; List <VoterFileEntry> pList = (from VoterFileEntry vStreet in _voterDB.AllVoters where vStreet.Street == street && vStreet.Latitude > 47.0 && vStreet.Longitude < 122.0 select vStreet).ToList(); App.Log(String.Format(" There are {0} voters on {1} street.", pList.Count, street)); if (pList.Count > 0) { thisStreet.West = pList.Min <VoterFileEntry, double>(vMin => vMin.Longitude); // smallest longitude found in list (or largest absolute value) thisStreet.East = pList.Max <VoterFileEntry, double>(vMax => vMax.Longitude); // largest longitude found in list (or smallest absolute value) thisStreet.North = pList.Max <VoterFileEntry, double>(vMax => vMax.Latitude); // largest latitude found in list thisStreet.South = pList.Min <VoterFileEntry, double>(vMin => vMin.Latitude); // smallest latitude found in list App.Log(String.Format("Street {4} - North={0}, South={1}, East={2}, West={3}.", thisStreet.North, thisStreet.South, thisStreet.East, thisStreet.West, thisStreet.Name)); double diffLat = thisStreet.North - thisStreet.South; double diffLong = thisStreet.East - thisStreet.West; thisStreet.CenterLatitude = thisStreet.South + (diffLat / 2); thisStreet.CenterLongitude = thisStreet.West + (diffLong / 2); _voterDB.Streets.InsertOnSubmit(thisStreet); } } App.Log(" Submitting streets to database: "); _voterDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict); App.Log(" Changes committed to database"); _settings.UpdateSetting("lastsync", DateTime.Now); } catch (Exception ex) { App.Log("Exception loading voters to database: " + ex.ToString()); } } _dbLoadThread = null; }