Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ephemeris] Add support for overriding holiday definitions #3573

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import de.jollyday.HolidayManager;
import de.jollyday.ManagerParameter;
import de.jollyday.ManagerParameters;
import de.jollyday.parameter.CalendarPartManagerParameter;
import de.jollyday.util.ResourceUtil;

/**
Expand All @@ -82,7 +83,9 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi
public static final String CONFIG_REGION = "region";
public static final String CONFIG_CITY = "city";

private static final String JOLLYDAY_COUNTRY_DESCRIPTIONS = "jollyday/descriptions/country_descriptions.properties";
private static final String RESOURCES_ROOT = "jollyday/";
private static final String JOLLYDAY_COUNTRY_DESCRIPTIONS = RESOURCES_ROOT
+ "descriptions/country_descriptions.properties";
private static final String PROPERTY_COUNTRY_DESCRIPTION_PREFIX = "country.description.";
private static final String PROPERTY_COUNTRY_DESCRIPTION_DELIMITER = "\\.";

Expand All @@ -99,6 +102,7 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi
private final ResourceUtil resourceUtil = new ResourceUtil();

private final LocaleProvider localeProvider;
private final Bundle bundle = FrameworkUtil.getBundle(getClass());

private @NonNullByDefault({}) String country;
private @Nullable String region;
Expand All @@ -107,7 +111,6 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi
public EphemerisManagerImpl(final @Reference LocaleProvider localeProvider) {
this.localeProvider = localeProvider;

final Bundle bundle = FrameworkUtil.getBundle(getClass());
jlaur marked this conversation as resolved.
Show resolved Hide resolved
try (InputStream stream = bundle.getResource(JOLLYDAY_COUNTRY_DESCRIPTIONS).openStream()) {
final Properties properties = new Properties();
properties.load(stream);
Expand Down Expand Up @@ -164,23 +167,26 @@ protected void modified(Map<String, Object> config) {
}
});

if (config.containsKey(CONFIG_COUNTRY)) {
country = config.get(CONFIG_COUNTRY).toString().toLowerCase();
Object configValue = config.get(CONFIG_COUNTRY);
if (configValue != null) {
country = configValue.toString().toLowerCase();
} else {
country = localeProvider.getLocale().getCountry().toLowerCase();
logger.debug("Using system default country '{}' ", country);
}

if (config.containsKey(CONFIG_REGION)) {
String region = config.get(CONFIG_REGION).toString().toLowerCase();
configValue = config.get(CONFIG_REGION);
if (configValue != null) {
String region = configValue.toString().toLowerCase();
countryParameters.add(region);
this.region = region;
} else {
this.region = null;
}

if (config.containsKey(CONFIG_CITY)) {
countryParameters.add(config.get(CONFIG_CITY).toString());
configValue = config.get(CONFIG_CITY);
if (configValue != null) {
countryParameters.add(configValue.toString());
}
}

Expand Down Expand Up @@ -231,9 +237,16 @@ private URL getUrl(String filename) throws FileNotFoundException {
private HolidayManager getHolidayManager(Object managerKey) {
HolidayManager holidayManager = holidayManagers.get(managerKey);
if (holidayManager == null) {
final ManagerParameter parameters = managerKey.getClass() == String.class
? ManagerParameters.create((String) managerKey)
: ManagerParameters.create((URL) managerKey);
final ManagerParameter parameters;
if (managerKey instanceof String stringKey) {
URL urlOverride = bundle
.getResource(RESOURCES_ROOT + CalendarPartManagerParameter.getConfigurationFileName(stringKey));
parameters = urlOverride != null //
? ManagerParameters.create(urlOverride)
: ManagerParameters.create(stringKey);
} else {
parameters = ManagerParameters.create((URL) managerKey);
}
holidayManager = HolidayManager.getInstance(parameters);
holidayManagers.put(managerKey, holidayManager);
}
Expand Down Expand Up @@ -297,9 +310,10 @@ public boolean isWeekend(ZonedDateTime date) {

@Override
public boolean isInDayset(String daysetName, ZonedDateTime date) {
if (daysets.containsKey(daysetName)) {
Set<DayOfWeek> dayset = daysets.get(daysetName);
if (dayset != null) {
DayOfWeek dow = date.getDayOfWeek();
return daysets.get(daysetName).contains(dow);
return dayset.contains(dow);
} else {
logger.warn("This dayset is not configured : {}", daysetName);
return false;
Expand Down Expand Up @@ -381,8 +395,9 @@ void parseProperty(Object key, Object value) throws IllegalArgumentException {
case 2:
part = getValidPart(parts[0]);
option = new ParameterOption(getValidPart(parts[1]), name);
if (regions.containsKey(part)) {
regions.get(part).add(option);
List<ParameterOption> regionsPart = regions.get(part);
if (regionsPart != null) {
regionsPart.add(option);
} else {
final List<ParameterOption> options = new ArrayList<>();
options.add(option);
Expand All @@ -392,8 +407,9 @@ void parseProperty(Object key, Object value) throws IllegalArgumentException {
case 3:
part = getValidPart(parts[1]);
option = new ParameterOption(getValidPart(parts[2]), name);
if (cities.containsKey(part)) {
cities.get(part).add(option);
List<ParameterOption> citiesPart = cities.get(part);
if (citiesPart != null) {
citiesPart.add(option);
} else {
final List<ParameterOption> options = new ArrayList<>();
options.add(option);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<tns:Configuration hierarchy="dk" description="Denmark" xmlns:tns="http://www.example.org/Holiday"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/Holiday /Holiday.xsd">
<tns:Holidays>
<tns:Fixed month="JANUARY" day="1" descriptionPropertiesKey="NEW_YEAR"/>
<tns:Fixed month="DECEMBER" day="25" descriptionPropertiesKey="CHRISTMAS"/>
<tns:Fixed month="DECEMBER" day="26" descriptionPropertiesKey="STEPHENS"/>
<tns:ChristianHoliday type="EASTER"/>
<tns:ChristianHoliday type="MAUNDY_THURSDAY"/>
<tns:ChristianHoliday type="GOOD_FRIDAY"/>
<tns:ChristianHoliday type="EASTER_MONDAY"/>
<tns:ChristianHoliday type="GENERAL_PRAYER_DAY" validTo="2023"/>
<tns:ChristianHoliday type="ASCENSION_DAY"/>
<tns:ChristianHoliday type="WHIT_MONDAY"/>
<tns:ChristianHoliday type="PENTECOST"/>
</tns:Holidays>
</tns:Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
public class EphemerisManagerImplOSGiTest extends JavaOSGiTest {
private static final String INTERNAL_DAYSET = "internal";
private static final URI CONFIG_URI = URI.create("system:ephemeris");
private static final String COUNTRY_DENMARK_KEY = "dk";
private static final String COUNTRY_AUSTRALIA_KEY = "au";
private static final String COUNTRY_AUSTRALIA_NAME = "Australia";
private static final String REGION_BAVARIA_KEY = "by";
Expand Down Expand Up @@ -263,6 +264,20 @@ public void testIsBankHoliday() {
assertFalse(vacation);
}

@Test
public void testIsBankHolidayWhenGeneralPrayerDay2023() {
ZonedDateTime generalPrayerDay = ZonedDateTime.of(2023, 05, 05, 0, 0, 0, 0, ZoneId.of("Europe/Paris"));
ephemerisManager.modified(Map.of(CONFIG_COUNTRY, COUNTRY_DENMARK_KEY));
assertTrue(ephemerisManager.isBankHoliday(generalPrayerDay));
}

@Test
public void testIsBankHolidayWhenGeneralPrayerDay2024() {
ZonedDateTime generalPrayerDay = ZonedDateTime.of(2024, 04, 26, 0, 0, 0, 0, ZoneId.of("Europe/Paris"));
ephemerisManager.modified(Map.of(CONFIG_COUNTRY, COUNTRY_DENMARK_KEY));
assertFalse(ephemerisManager.isBankHoliday(generalPrayerDay));
}

@Test
public void testGetBankHoliday() {
ZonedDateTime theDay = ZonedDateTime.of(2019, 01, 01, 0, 0, 0, 0, ZoneId.of("Europe/Paris"));
Expand Down