示例#1
0
        private async Task <bool> InitAsync()
        {
            if (_countries == null)
            {
                //Chargement des pays :
                String countriesFileText = await _contentGetter.GetCountriesData();

                lock (_locker)
                {
                    _countries = JsonConvert.DeserializeObject <List <CountryModel> >(countriesFileText);
                }
                String boundariesFileText = await _contentGetter.GetBoundariesData();

                lock (_locker)
                {
                    FeatureCollection fc = JsonConvert.DeserializeObject <FeatureCollection>(boundariesFileText);
                    //La première itération avec une clé vide ne renvoi pas de pays mais l'id de la prochaine itération.
                    CountriesIteration ct = new CountriesIteration();
                    ct.iterationKey    = "1";
                    ct.finalItemsCount = _countries.Count;
                    _countriesMap.Add("", ct);
                    //La seconde itération renvoi les 20 premiers pays
                    ct = new CountriesIteration();
                    ct.iterationKey    = "2";
                    ct.finalItemsCount = _countries.Count;
                    ct.countries       = new List <CountryModel>();
                    for (int i = 0; i < 19; i++)
                    {
                        ct.countries.Add(_countries[i]);
                    }
                    _countriesMap.Add("1", ct);

                    //La seconde itération renvoi les 100 pays suivants
                    ct = new CountriesIteration();
                    ct.iterationKey    = "3";
                    ct.finalItemsCount = _countries.Count;
                    ct.countries       = new List <CountryModel>();
                    for (int i = 19; i < 120; i++)
                    {
                        ct.countries.Add(_countries[i]);
                    }
                    _countriesMap.Add("2", ct);

                    //La dernière itération renvoi  le reste.
                    ct = new CountriesIteration();
                    ct.finalItemsCount = _countries.Count;
                    ct.iterationKey    = "";
                    ct.countries       = new List <CountryModel>();
                    for (int i = 120; i < ct.finalItemsCount; i++)
                    {
                        ct.countries.Add(_countries[i]);
                    }
                    _countriesMap.Add("3", ct);
                }
            }
            return(true);
        }
 /// <summary>
 /// 1- If Buffer is null
 /// 1.1- Asynchronous Load of jsonData file
 /// 1.2- ForEach Feature retrieved, add a new HRCountryBorder with ISO3/alpha3Code and Features/OgcFeature
 /// 2- Else retrun true
 /// Does not catch any Exception and throw one if data can not be reached.
 /// </summary>
 /// <returns>true or throw an exception if data can not be loaded</returns>
 private async Task <bool> InitAsync()
 {
     //1-
     if (_countriesBorderBuffer == null)
     {
         //1.1-
         if (_contentGetter != null)
         {
             FeatureCollection featureCollectionToCache = JsonConvert.DeserializeObject <FeatureCollection>(await _contentGetter.GetBoundariesData());
             if (featureCollectionToCache != null)
             {
                 lock (_locker)
                 {
                     _countriesBorderBuffer = new List <HRCountryBorder>();
                     int featuresCount = featureCollectionToCache.Features.Count;
                     //1.2-
                     for (int i = 0; i < featuresCount; i++)
                     {
                         HRCountryBorder border   = new HRCountryBorder();
                         Feature         featurei = featureCollectionToCache.Features[i];
                         if (featurei.Properties != null &&
                             featurei.Properties.Keys.Contains(Constant.BOUNDARIES_STUB.ISO3_PROPERTIE_NAME))
                         {
                             border.alpha3Code = featurei.Properties[Constant.BOUNDARIES_STUB.ISO3_PROPERTIE_NAME].ToString();
                             FeatureCollection fCollection = new FeatureCollection();
                             fCollection.Features.Add(featurei);
                             border.oGCFeature = JsonConvert.SerializeObject(fCollection);
                             _countriesBorderBuffer.Add(border);
                         }
                     }
                 }
             }
             else
             {
                 throw new Exception(Constant.BOUNDARIES_STUB.NO_DATA_ERROR_MESSAGE);
             }
         }
         else
         {
             throw new Exception(Constant.BOUNDARIES_STUB.NO_DATA_ERROR_MESSAGE);
         }
     }
     return(true);
 }