public void AddPersonToDb(Person item) { // don't add here... code adds to list }
public override void Process() { // <Person LName="Accorti" FName="Pónt" AKAName="Paul" AgeGroup="Youth"></Person> // <Person LName="Brown" FName="Lesley" AgeGroup="Adult" IneligibleToReceiveVotes="true" ReasonToNotReceive="Resides elsewhere"></Person> // <Person FName="Aria" AgeGroup="Adult" LName="Danton" EnvNum="7" Voted="DroppedOff"></Person> foreach (XmlElement personXml in _xmlDoc.DocumentElement.SelectNodes("//Person")) { var ageGroup = personXml.GetAttribute("AgeGroup"); if (ageGroup.DefaultTo("Adult") != "Adult") { _nonAdults++; continue; } var lastName = personXml.GetAttribute("LName"); var firstName = personXml.GetAttribute("FName"); var akaName = personXml.GetAttribute("AKAName"); // check for matches var matchedExisting = _people.Any(p => p.LastName.DefaultTo("") == lastName && p.FirstName.DefaultTo("") == firstName && p.OtherNames.DefaultTo("") == akaName); if (matchedExisting) { _alreadyLoaded++; continue; } _peopleAdded++; var newPerson = new Person { PersonGuid = Guid.NewGuid(), LastName = lastName, FirstName = firstName }; AddPerson(newPerson); _people.Add(newPerson); if (akaName.HasContent()) { newPerson.OtherNames = akaName; } var bahaiId = personXml.GetAttribute("BahaiId"); if (bahaiId.HasContent()) { newPerson.BahaiId = bahaiId; } var ineligible = personXml.GetAttribute("IneligibleToReceiveVotes").AsBoolean(); newPerson.CanReceiveVotes = ineligible; if (ineligible) { newPerson.IneligibleReasonGuid = MapIneligible("Ineligible", personXml.GetAttribute("ReasonToNotReceive")); } //TODO: var voteMethod = personXml.GetAttribute("Voted"); switch (voteMethod) { case "VotedInPerson": newPerson.VotingMethod = VotingMethodEnum.InPerson; break; case "DroppedOff": newPerson.VotingMethod = VotingMethodEnum.DroppedOff; break; case "Mailed": newPerson.VotingMethod = VotingMethodEnum.MailedIn; break; } var envNum = personXml.GetAttribute("EnvNum").AsInt(); if (envNum != 0) { newPerson.EnvNum = envNum; } } _file.ProcessingStatus = "Imported"; _db.SaveChanges(); ImportSummaryMessage = "Imported {0} {1}.".FilledWith(_peopleAdded, _peopleAdded.Plural("people", "person")); if (_alreadyLoaded > 0) { ImportSummaryMessage += " Skipped {0} {1} matching existing.".FilledWith(_alreadyLoaded, _alreadyLoaded.Plural("people", "person")); } if (_nonAdults > 0) { ImportSummaryMessage += " Skipped {0} non-adult{1}.".FilledWith(_nonAdults, _nonAdults.Plural()); } _logHelper.Add("Imported v1 community file #" + _file.C_RowId + ": " + ImportSummaryMessage); }
public JsonResult SavePerson(Person person) { return new PeopleModel().SavePerson(person); }
/// <Summary>Vote is not spoiled, so determine who this is...</Summary> private void ImportVotePerson(Ballot ballot, XmlElement voteXml, int voteNum) { var personXml = voteXml.SelectSingleNode("Person") as XmlElement; if (personXml == null) { // can't happen if XML is okay return; } Vote vote; var lastName = personXml.GetAttribute("LName"); var firstName = personXml.GetAttribute("FName"); var akaName = personXml.GetAttribute("AKAName"); // check for matches Person person; var matchedPeople = _people.Where(p => p.LastName.DefaultTo("") == lastName && p.FirstName.DefaultTo("") == firstName && p.OtherNames.DefaultTo("") == akaName).ToList(); var numMatched = matchedPeople.Count; switch (numMatched) { case 1: person = matchedPeople[0]; break; case 0: // vote is valid, but person not found! // is okay if we are loading without a community file if (lastName.HasNoContent() && firstName.HasNoContent()) { vote = new Vote { BallotGuid = ballot.BallotGuid, PositionOnBallot = voteNum, InvalidReasonGuid = IneligibleReasonEnum.Unreadable_Vote_is_blank, StatusCode = VoteHelper.VoteStatusCode.Ok }; _storeVote(vote); return; } else { person = new Person { PersonGuid = Guid.NewGuid(), ElectionGuid = _election.ElectionGuid, LastName = lastName, FirstName = firstName }; AddPerson(person); _people.Add(person); if (akaName.HasContent()) { person.OtherNames = akaName; } var bahaiId = personXml.GetAttribute("BahaiId"); if (bahaiId.HasContent()) { person.BahaiId = bahaiId; } var ineligible = personXml.GetAttribute("IneligibleToReceiveVotes").AsBoolean(); person.CanReceiveVotes = ineligible; var ineligibleReason = personXml.GetAttribute("ReasonToNotReceive").AsBoolean(); //TODO... } break; default: throw new ApplicationException("Name in Vote matches {0} {1}".FilledWith(numMatched, numMatched.Plural("person", "people"))); } // got the person vote = new Vote { BallotGuid = ballot.BallotGuid, PositionOnBallot = voteNum, PersonCombinedInfo = person.CombinedInfo, PersonGuid = person.PersonGuid, StatusCode = VoteHelper.VoteStatusCode.Ok }; _storeVote(vote); }
public JsonResult Import(int rowId) { var file = Db.ImportFiles.SingleOrDefault( fi => fi.ElectionGuid == UserSession.CurrentElectionGuid && fi.C_RowId == rowId); if (file == null) { throw new ApplicationException("File not found"); } var columnsToRead = file.ColumnsToRead; if (columnsToRead == null) { throw new ApplicationException("Mapping not defined"); } var textReader = new StringReader(file.Contents.AsString(file.CodePage)); var csv = new CsvReader(textReader, true) { SkipEmptyLines = true }; //mapping: csv->db,csv->db var currentMappings = columnsToRead.DefaultTo("").SplitWithString(",").Select(s => s.SplitWithString("->")).ToList(); var dbFields = DbFieldsList.ToList(); var validMappings = currentMappings.Where(mapping => dbFields.Contains(mapping[1])).ToList(); if (validMappings.Count == 0) { throw new ApplicationException("Mapping not defined"); } var currentPeople = Db.People.Where(p => p.ElectionGuid == UserSession.CurrentElectionGuid).ToList(); var personModel = new PeopleModel(); var rowsProcessed = 0; var rowsSkipped = 0; var peopleAdded = 0; var peopleSkipped = 0; csv.ReadNextRecord(); while (!csv.EndOfStream) { rowsProcessed++; var valuesSet = false; var namesFoundInRow = false; var query = currentPeople.AsQueryable(); var person = new Person(); foreach (var currentMapping in validMappings) { var dbFieldName = currentMapping[1]; var value = csv[currentMapping[0]]; person.SetPropertyValue(dbFieldName, value); valuesSet = true; switch (dbFieldName) { case "LastName": query = query.Where(p => p.LastName == value); namesFoundInRow = namesFoundInRow || value.HasContent(); break; case "FirstName": query = query.Where(p => p.FirstName == value); namesFoundInRow = namesFoundInRow || value.HasContent(); break; case "OtherLastNames": query = query.Where(p => p.OtherLastNames == value); break; case "OtherNames": query = query.Where(p => p.OtherNames == value); break; case "OtherInfo": query = query.Where(p => p.OtherInfo == value); break; case "Area": query = query.Where(p => p.Area == value); break; case "BahaiId": query = query.Where(p => p.BahaiId == value); break; default: throw new ApplicationException("Unexpected: " + dbFieldName); } } if (!valuesSet || !namesFoundInRow) { rowsSkipped++; } else if (query.Any()) { peopleSkipped++; } else { //get ready for DB person.ElectionGuid = UserSession.CurrentElectionGuid; person.PersonGuid = Guid.NewGuid(); personModel.SetCombinedInfoAtStart(person); personModel.ResetInvolvementFlags(person); Db.People.Add(person); if (peopleAdded == 1 || peopleAdded % 100 == 0) { // save after first, then as we go Db.SaveChanges(); } peopleAdded++; } csv.ReadNextRecord(); } file.ProcessingStatus = "Imported"; Db.SaveChanges(); var result = "Processed {0} row{1}. Added {2} {3}.".FilledWith(rowsProcessed, rowsProcessed.Plural(), peopleAdded, peopleAdded.Plural("people", "person")); if (peopleSkipped > 0) { result += " {0} {1} matched.".FilledWith(peopleSkipped, peopleSkipped.Plural("people", "person")); } if (rowsSkipped > 0) { result += " {0} row{1} skipped.".FilledWith(rowsSkipped, rowsSkipped.Plural()); } new LogHelper().Add("Imported file #" + rowId + ": " + result); return new { result }.AsJsonResult(); }
private static string ShowTellers(Dictionary<Guid, string> tellers, Person p) { var names = new List<string> { p.TellerAtKeyboard.HasValue ? (tellers.ContainsKey(p.TellerAtKeyboard.Value) ? tellers[p.TellerAtKeyboard.Value] : "?") : "", p.TellerAssisting.HasValue ? (tellers.ContainsKey(p.TellerAssisting.Value) ? tellers[p.TellerAssisting.Value] : "?") : "" }; return names.JoinedAsString(", ", true); }
private static string ShowRegistrationTime(int timeOffset, Person p) { return p.RegistrationTime.HasValue ? p.RegistrationTime.Value.AddMilliseconds(0 - timeOffset).ToString("h:mm tt").ToLowerInvariant() : ""; }
private static int? ShowEnvNum(Person p) { return p.VotingMethod.DefaultTo(VotingMethodEnum.InPerson) == VotingMethodEnum.InPerson ? null : p.EnvNum; }
public JsonResult SavePerson(Person personFromInput) { var savedPerson = PeopleInCurrentElection().SingleOrDefault(p => p.C_RowId == personFromInput.C_RowId && p.ElectionGuid == CurrentElectionGuid); if (savedPerson == null) { if (personFromInput.C_RowId != -1) { return new { Status = "Unknown ID" }.AsJsonResult(); } savedPerson = new Person { PersonGuid = Guid.NewGuid(), ElectionGuid = CurrentElectionGuid }; ResetInvolvementFlags(savedPerson); Db.People.Add(savedPerson); } if (personFromInput.IneligibleReasonGuid == Guid.Empty) { personFromInput.IneligibleReasonGuid = null; } var editableFields = new { // personFromInput.AgeGroup, personFromInput.BahaiId, personFromInput.FirstName, personFromInput.IneligibleReasonGuid, personFromInput.LastName, personFromInput.OtherInfo, personFromInput.OtherLastNames, personFromInput.OtherNames, personFromInput.Area, }.GetAllPropertyInfos().Select(pi => pi.Name).ToArray(); var changed = personFromInput.CopyPropertyValuesTo(savedPerson, editableFields); // these two may not be present, depending on the election type const string all = ElectionModel.CanVoteOrReceive.All; var canReceiveVotes = personFromInput.CanReceiveVotes.GetValueOrDefault(CurrentElection.CanReceive == all); if (savedPerson.CanReceiveVotes != canReceiveVotes) { savedPerson.CanReceiveVotes = canReceiveVotes; changed = true; } var canVote = personFromInput.CanVote.GetValueOrDefault(CurrentElection.CanVote == all); if (savedPerson.CanVote != canVote) { savedPerson.CanVote = canVote; changed = true; } if (changed) { SetCombinedInfos(savedPerson); Db.SaveChanges(); } return new { Status = "Saved", Person = PersonForEdit(savedPerson), OnFile = Db.People.Count(p => p.ElectionGuid == CurrentElectionGuid) }.AsJsonResult(); }
private static object PersonForEdit(Person person) { return new { person.C_RowId, //person.AgeGroup, person.BahaiId, person.CanReceiveVotes, person.CanVote, person.FirstName, person.IneligibleReasonGuid, person.LastName, person.OtherInfo, person.OtherLastNames, person.OtherNames, person.Area, person.C_FullName }; }
//public void ResetVotingRecords(Person person) //{ // person.RegistrationTime = null; // person.VotingLocationGuid = null; // person.VotingMethod = null; // person.EnvNum = null; //} /// <summary> /// Set person's flag based on what is default for this election /// </summary> /// <param name="person"> </param> /// <param name="election"> </param> public void ResetInvolvementFlags(Person person) { //var canVote = true; // person.AgeGroup.HasNoContent() || person.AgeGroup == AgeGroup.Adult; //person.IneligibleReasonGuid = canVote ? null : IneligibleReasonEnum.Ineligible_Not_Adult; var whoCanVote = CurrentElection.CanVote; var whoCanReceiveVotes = CurrentElection.CanReceive; person.CanVote = whoCanVote == ElectionModel.CanVoteOrReceive.All; person.CanReceiveVotes = whoCanReceiveVotes == ElectionModel.CanVoteOrReceive.All; }
public void SetCombinedInfos(Person person) { person.CombinedInfo = person.MakeCombinedInfo(); person.UpdateCombinedSoundCodes(); }