From a0656a9237321311bdb4899421e58b8cefd8d14a Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sun, 23 Apr 2023 21:05:39 +0200 Subject: [PATCH 1/4] Add support for overriding holiday definitions End Danish General Prayer Day Signed-off-by: Jacob Laursen --- .../internal/EphemerisManagerImpl.java | 19 ++++++++++++++----- .../jollyday/holidays/Holidays_dk.xml | 17 +++++++++++++++++ .../EphemerisManagerImplOSGiTest.java | 15 +++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 bundles/org.openhab.core.ephemeris/src/main/resources/jollyday/holidays/Holidays_dk.xml diff --git a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java index ab27188f319..b7d795b1fdd 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java +++ b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java @@ -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; /** @@ -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 = "\\."; @@ -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; @@ -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()); try (InputStream stream = bundle.getResource(JOLLYDAY_COUNTRY_DESCRIPTIONS).openStream()) { final Properties properties = new Properties(); properties.load(stream); @@ -231,9 +234,15 @@ 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.getClass() == String.class) { + URL urlOverride = bundle.getResource( + RESOURCES_ROOT + CalendarPartManagerParameter.getConfigurationFileName((String) managerKey)); + parameters = urlOverride != null ? ManagerParameters.create(urlOverride) + : ManagerParameters.create((String) managerKey); + } else { + parameters = ManagerParameters.create((URL) managerKey); + } holidayManager = HolidayManager.getInstance(parameters); holidayManagers.put(managerKey, holidayManager); } diff --git a/bundles/org.openhab.core.ephemeris/src/main/resources/jollyday/holidays/Holidays_dk.xml b/bundles/org.openhab.core.ephemeris/src/main/resources/jollyday/holidays/Holidays_dk.xml new file mode 100644 index 00000000000..e7510def3c3 --- /dev/null +++ b/bundles/org.openhab.core.ephemeris/src/main/resources/jollyday/holidays/Holidays_dk.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + diff --git a/itests/org.openhab.core.ephemeris.tests/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImplOSGiTest.java b/itests/org.openhab.core.ephemeris.tests/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImplOSGiTest.java index 5f086250f77..5a95b81b8d0 100644 --- a/itests/org.openhab.core.ephemeris.tests/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImplOSGiTest.java +++ b/itests/org.openhab.core.ephemeris.tests/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImplOSGiTest.java @@ -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"; @@ -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")); From 0f6871739d00a292baba73004b354bd487611255 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 24 Apr 2023 17:47:27 +0200 Subject: [PATCH 2/4] Refactor type check Signed-off-by: Jacob Laursen --- .../core/ephemeris/internal/EphemerisManagerImpl.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java index b7d795b1fdd..5485a306059 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java +++ b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java @@ -235,11 +235,12 @@ private HolidayManager getHolidayManager(Object managerKey) { HolidayManager holidayManager = holidayManagers.get(managerKey); if (holidayManager == null) { final ManagerParameter parameters; - if (managerKey.getClass() == String.class) { - URL urlOverride = bundle.getResource( - RESOURCES_ROOT + CalendarPartManagerParameter.getConfigurationFileName((String) managerKey)); - parameters = urlOverride != null ? ManagerParameters.create(urlOverride) - : ManagerParameters.create((String) managerKey); + 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); } From 1e0d7bc4bf989a566a475106ba8a75055c92535d Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 24 Apr 2023 20:07:14 +0200 Subject: [PATCH 3/4] Fix double map lookups (causing null annotation warnings) Signed-off-by: Jacob Laursen --- .../internal/EphemerisManagerImpl.java | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java index 5485a306059..abda7db3bca 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java +++ b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java @@ -167,23 +167,26 @@ protected void modified(Map 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()); } } @@ -307,9 +310,10 @@ public boolean isWeekend(ZonedDateTime date) { @Override public boolean isInDayset(String daysetName, ZonedDateTime date) { - if (daysets.containsKey(daysetName)) { + Set 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; @@ -391,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 regionsPart = regions.get(part); + if (regionsPart != null) { + regionsPart.add(option); } else { final List options = new ArrayList<>(); options.add(option); @@ -402,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 citiesPart = cities.get(part); + if (citiesPart != null) { + citiesPart.add(option); } else { final List options = new ArrayList<>(); options.add(option); From d2b82850e19c462c15fa0f1c408ab2b258728838 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 1 May 2023 19:52:16 +0200 Subject: [PATCH 4/4] Refactor to get injected BundleContext Signed-off-by: Jacob Laursen --- .../core/ephemeris/internal/EphemerisManagerImpl.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java index abda7db3bca..65b9b8bf9d0 100644 --- a/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java +++ b/bundles/org.openhab.core.ephemeris/src/main/java/org/openhab/core/ephemeris/internal/EphemerisManagerImpl.java @@ -46,8 +46,8 @@ import org.openhab.core.ephemeris.EphemerisManager; import org.openhab.core.i18n.LocaleProvider; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; -import org.osgi.framework.FrameworkUtil; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Modified; @@ -102,14 +102,15 @@ public class EphemerisManagerImpl implements EphemerisManager, ConfigOptionProvi private final ResourceUtil resourceUtil = new ResourceUtil(); private final LocaleProvider localeProvider; - private final Bundle bundle = FrameworkUtil.getBundle(getClass()); + private final Bundle bundle; private @NonNullByDefault({}) String country; private @Nullable String region; @Activate - public EphemerisManagerImpl(final @Reference LocaleProvider localeProvider) { + public EphemerisManagerImpl(final @Reference LocaleProvider localeProvider, final BundleContext bundleContext) { this.localeProvider = localeProvider; + bundle = bundleContext.getBundle(); try (InputStream stream = bundle.getResource(JOLLYDAY_COUNTRY_DESCRIPTIONS).openStream()) { final Properties properties = new Properties();