Exemplo n.º 1
0
        /// <summary>
        /// Imports name data from <paramref name="source"/>.
        /// </summary>
        /// <param name="source">
        /// The data source to import name data from.
        /// </param>
        public void Import(INameImportSource source)
        {
            this.InitContext();

            // read from the input source until that's no longer possible
            while (source.Read())
            {
                // try to load the field values from the current row
                String nameStr       = this.GetField(source, NameProperty.Name);
                String maleOccsStr   = this.GetField(source, NameProperty.MaleOccurrences);
                String femaleOccsStr = this.GetField(source, NameProperty.FemaleOccurrences);
                String yearStr       = this.GetField(source, NameProperty.Year);
                String surnameStr    = this.GetField(source, NameProperty.Surname);

                // set some default values
                Int32 year       = this.DefaultYear;
                Int64 maleOccs   = 0;
                Int64 femaleOccs = 0;

                // try to parse field data, if available
                if (!String.IsNullOrWhiteSpace(maleOccsStr))
                {
                    maleOccs = Int64.Parse(maleOccsStr, NumberStyles.AllowThousands);
                }
                if (!String.IsNullOrWhiteSpace(femaleOccsStr))
                {
                    femaleOccs = Int64.Parse(femaleOccsStr, NumberStyles.AllowThousands);
                }
                if (!String.IsNullOrWhiteSpace(yearStr))
                {
                    year = Convert.ToInt32(yearStr);
                }

                if (!String.IsNullOrWhiteSpace(surnameStr))
                {
                    surnameStr = surnameStr.Trim();

                    if (this.onProcessing != null)
                    {
                        this.onProcessing(this.entries, surnameStr);
                    }

                    // try to find an existing entry for this name in the database
                    Name name = this.FindOrCreateName(surnameStr);

                    NameUsage familyUsage = name.UsageFor(this.Culture, true);

                    if (familyUsage == null)
                    {
                        familyUsage            = new NameUsage(this.Culture, Sex.NotApplicable);
                        familyUsage.FamilyName = true;
                        familyUsage.NameID     = name.ID;
                        familyUsage.Name       = name;

                        name.Usage.Add(familyUsage);

                        this.context.Usage.Add(familyUsage);
                        this.Commit();
                    }
                }

                if (!String.IsNullOrWhiteSpace(nameStr))
                {
                    nameStr = nameStr.Trim();

                    if (this.onProcessing != null)
                    {
                        this.onProcessing(this.entries, nameStr);
                    }

                    // try to find an existing entry for this name in the database
                    Name name = this.FindOrCreateName(nameStr);

                    // add usage and frequency data for the current name
                    if (maleOccs > 0)
                    {
                        this.AddFrequency(name, Sex.Male, year, maleOccs);
                    }
                    if (femaleOccs > 0)
                    {
                        this.AddFrequency(name, Sex.Female, year, femaleOccs);
                    }

                    // update the static probabilities for the current name
                    foreach (NameUsage usage in name.Usage)
                    {
                        usage.RecalculateProbability();
                    }

                    entries++;
                }
            }

            // save all remaining changes
            context.SaveChanges();
            context.Dispose();
            context = null;
        }