コード例 #1
0
        private static void ProcessUploadThread(object guidObject)
        {
            string    guid        = (string)guidObject;
            Documents documents   = Documents.RecentFromDescription(guid);
            Document  uploadedDoc = documents[0];
            string    data        = string.Empty;
            int       count       = 0;

            using (StreamReader reader = uploadedDoc.GetReader(1252))
            {
                data = reader.ReadToEnd();
            }

            string[] lines = data.Split('\n');

            Random random = new Random();

            Dictionary <string, bool> postalCodeDupes = new Dictionary <string, bool>();

            foreach (string lineRaw in lines)
            {
                count++;
                string   line      = lineRaw.Trim();
                string[] lineParts = line.Split('\t');

                if (lineParts.Length != 4)
                {
                    continue;
                }

                int percent = (count * 99) / lines.Length;
                if (percent == 0)
                {
                    percent = 1;
                }
                GuidCache.Set(guid + "-Progress", percent);


                string countryCode = lineParts[0].Trim().ToUpperInvariant();
                string postalCode  = lineParts[1].Trim();
                string cityName    = lineParts[2].Trim();
                string nodeName    = lineParts[3].Trim();

                // Dupecheck

                if (postalCodeDupes.ContainsKey(postalCode))
                {
                    continue;
                }

                Geography geography = Geography.FromName(nodeName);  // may dupe!

                // First, make sure country exists

                Country country = Country.FromCode(countryCode);

                // Then, check if the city name exists

                City city = null;

                try
                {
                    city = City.FromName(cityName, country.Identity);
                }
                catch (ArgumentException)
                {
                    city = City.Create(cityName, country.Identity, geography.Identity);
                }

                // Last, add the postal code, if there is any

                if (!string.IsNullOrEmpty(postalCode))
                {
                    SwarmDb.GetDatabaseForWriting().CreatePostalCode(postalCode, city.Identity, country.Identity);
                    postalCodeDupes[postalCode] = true;
                }
            }

            GuidCache.Set(guid + "-Progress", 100);
        }