Advertisement
stefony

SoftWeather Forecast

Mar 28th, 2024
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | Software | 0 0
  1. @Override
  2.     public String importCities() throws IOException {
  3.         StringBuilder sb = new StringBuilder();
  4.  
  5.         ImplementCityDto[] implementCityDtos = this.gson.fromJson(
  6.                 new FileReader((CITY_PATH)),ImplementCityDto[].class);
  7.  
  8.         for (ImplementCityDto implementCityDto:implementCityDtos){
  9.  
  10.             Optional<City> optional = this.cityRepository.
  11.                     findByCityName(implementCityDto.getCityName());
  12.             if (!this.validationUtil.isValid(implementCityDto)|| optional.isPresent()){
  13.                 sb.append("Invalid city\n");
  14.                 continue;
  15.  
  16.             }
  17.             Optional<Country> countryOptional = countryRepository.findById(implementCityDto.getCountry());
  18.             if (!countryOptional.isPresent()) {
  19.                 sb.append("Invalid country \n");
  20.                 continue;
  21.             }
  22.             City city = this.modelMapper.map(implementCityDto,City.class);
  23.             city.setCountry(countryOptional.get());
  24.             this.cityRepository.saveAndFlush(city);
  25.  
  26.             sb.append(String.format("Successfully imported city  %s - %d\n", city.getCityName(),city.getPopulation()));
  27.  
  28.         }
  29.         return sb.toString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement