Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public String importCities() throws IOException {
- StringBuilder sb = new StringBuilder();
- ImplementCityDto[] implementCityDtos = this.gson.fromJson(
- new FileReader((CITY_PATH)),ImplementCityDto[].class);
- for (ImplementCityDto implementCityDto:implementCityDtos){
- Optional<City> optional = this.cityRepository.
- findByCityName(implementCityDto.getCityName());
- if (!this.validationUtil.isValid(implementCityDto)|| optional.isPresent()){
- sb.append("Invalid city\n");
- continue;
- }
- Optional<Country> countryOptional = countryRepository.findById(implementCityDto.getCountry());
- if (!countryOptional.isPresent()) {
- sb.append("Invalid country \n");
- continue;
- }
- City city = this.modelMapper.map(implementCityDto,City.class);
- city.setCountry(countryOptional.get());
- this.cityRepository.saveAndFlush(city);
- sb.append(String.format("Successfully imported city %s - %d\n", city.getCityName(),city.getPopulation()));
- }
- return sb.toString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement