private void SeedSEIFA_2016(SeedingContext context) { string resourceName = @"Repository.SeedData.SEIFA_2016.csv"; IScoreImportStrategy strategy = new ScoreDetailImportStrategy(); Stream stream = GetStream(resourceName); }
public void SeedToContext(Stream stream, SeedingContext context) { using (stream) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { CsvReader csvReader = new CsvReader(reader); try { csvReader.Read(); csvReader.ReadHeader(); //Get year from the header var year = csvReader.GetString(0).GetYear(); //Skip calling SkipHeaders //csvReader.SkipHeaders(1); State state = new State(); while (csvReader.Read()) { var stateName = csvReader.GetString(0); var locationName = csvReader.GetString(1); if (!string.IsNullOrWhiteSpace(locationName)) { if (!string.IsNullOrWhiteSpace(stateName)) { state = new State() { StateName = stateName }; context.States.AddOrUpdate(state); } Location location = new Location() { PlaceName = locationName, State = state }; location = context.Locations.AddOrUpdate(location); var disadvantage = csvReader.GetInt(2); var advantage = csvReader.GetInt(3); Score score = new Score() { DisadvantageScore = disadvantage, AdvantageDisadvantageScore = advantage, Year = year, Location = location }; context.Scores.AddOrUpdate(score); } } } // Todo: Create exception class catch (Exception e) { } } } }
private void SeedSEIFA_2011(SeedingContext context) { string resourceName = @"Repository.SeedData.SEIFA_2011.csv"; IScoreImportStrategy strategy = new ScoreImportStrategy(); Stream stream = GetStream(resourceName); strategy.SeedToContext(stream, context, 2011); }
protected override void Seed(LGAContext context) { SeedingContext seedingContext = new SeedingContext(); SeedingAdapter adapter = new SeedingAdapter(); SeedSEIFA_2011(seedingContext); SeedSEIFA_2016(seedingContext); CalculateMedianByState(seedingContext); adapter.MergeContext(context, seedingContext); base.Seed(context); }
private void CalculateMedianByState(SeedingContext context) { foreach (var state in context.States) { var data = context.Scores .Where(p => p.DisadvantageScore.HasValue && p.Location != null && p.Location.State != null && p.Location.State.StateName.Equals (state.StateName, System.StringComparison.InvariantCultureIgnoreCase)) .Select(p => (int)p.DisadvantageScore) .ToList(); if (data.Count > 0) { state.Median = data.GetMedian(); } } }
public void SeedToContext(Stream stream, SeedingContext context, int year) { using (stream) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { CsvReader csvReader = new CsvReader(reader); try { csvReader.SkipHeaders(6); while (csvReader.Read()) { var locationCode = csvReader.GetInt(0); var locationName = csvReader.GetString(1); if (!string.IsNullOrWhiteSpace(locationName)) { Location location = new Location() { PlaceName = locationName, Code = locationCode, State = null }; location = context.Locations.AddOrUpdate(location); var disadvantage = csvReader.GetInt(2); var advantage = csvReader.GetInt(4); Score score = new Score() { DisadvantageScore = disadvantage, AdvantageDisadvantageScore = advantage, Year = year, Location = location }; score = context.Scores.AddOrUpdate(score); var disadvantageDecile = csvReader.GetInt(3); var advantageDecile = csvReader.GetInt(5); var indexOfEconomicResourcesScore = csvReader.GetInt(6); var indexOfEconomicResourcesDecile = csvReader.GetInt(7); var indexOfEducationAndOccupationScore = csvReader.GetInt(8); var indexOfEducationAndOccupationDecile = csvReader.GetInt(9); var usualResedantPopulation = csvReader.GetDecimal(10); ScoreDetail scoreDetail = new ScoreDetail() { Score = score, DisadvantageDecile = disadvantageDecile, AdvantageDisadvantageDecile = advantageDecile, IndexOfEconomicResourcesScore = indexOfEconomicResourcesScore, IndexOfEconomicResourcesDecile = indexOfEconomicResourcesDecile, IndexOfEducationAndOccupationScore = indexOfEducationAndOccupationScore, IndexOfEducationAndOccupationDecile = indexOfEducationAndOccupationDecile, UsualResedantPopulation = usualResedantPopulation }; scoreDetail.Score = score; context.ScoreDetails.Add(scoreDetail); } } } // Todo: Create exception class to be caught and logged the right logging catch (Exception e) { throw new DataImportException() { Year = year, Context = csvReader.Context, ThrownException = e }; } } } }