You are on page 1of 1

/**

* Adds public holidays by country and year from a website with all public
holidays.
*
* @param countryHolidaysDto The DTO with country and year.
* @return TRUE after all the entities have been added.
*/
Boolean addPublicHolidays(CountryHolidaysDto countryHolidaysDto) throws
IOException {

String url = "https://www.officeholidays.com/countries/" +


countryHolidaysDto.getCountry().toLowerCase() + "/" + countryHolidaysDto.getYear();
Document document = Jsoup.connect(url).get();

Elements dates = document.getElementsByTag("time");


Elements holidayNames = document.getElementsByClass("country-listing");
Elements types = document.getElementsByClass("comments");

types.remove(0);

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-


dd");

for (int i = 0; i < types.size(); i++) {

if (types.get(i).text().equals("National Holiday")) {
HolidayPublic holidayPublic = new HolidayPublic();
holidayPublic.setName(holidayNames.get(i).text());
holidayPublic.setDate(LocalDate.parse(dates.get(i).attr("datetime"),
dateTimeFormatter));
holidayPublicRepository.save(holidayPublic);
}
}
return true;
}

You might also like