]> git.basschouten.com Git - openhab-addons.git/blob
67f254d6807ae0b9008953b49e1ca064e374f7a9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.transform.vat.internal.profile;
14
15 import static org.openhab.transform.vat.internal.VATTransformationConstants.PROFILE_TYPE_UID;
16
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.concurrent.ConcurrentHashMap;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.i18n.LocaleProvider;
27 import org.openhab.core.i18n.LocalizedKey;
28 import org.openhab.core.library.CoreItemFactory;
29 import org.openhab.core.thing.profiles.Profile;
30 import org.openhab.core.thing.profiles.ProfileCallback;
31 import org.openhab.core.thing.profiles.ProfileContext;
32 import org.openhab.core.thing.profiles.ProfileFactory;
33 import org.openhab.core.thing.profiles.ProfileType;
34 import org.openhab.core.thing.profiles.ProfileTypeBuilder;
35 import org.openhab.core.thing.profiles.ProfileTypeProvider;
36 import org.openhab.core.thing.profiles.ProfileTypeUID;
37 import org.openhab.core.thing.profiles.i18n.ProfileTypeI18nLocalizationService;
38 import org.openhab.core.transform.TransformationService;
39 import org.openhab.core.util.BundleResolver;
40 import org.osgi.framework.Bundle;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44
45 /**
46  * The {@link VATTransformationProfileFactory} is responsible for creating profiles.
47  *
48  * @author Jacob Laursen - Initial contribution
49  */
50 @NonNullByDefault
51 @Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
52 public class VATTransformationProfileFactory implements ProfileFactory, ProfileTypeProvider {
53
54     private final LocaleProvider localeProvider;
55     private final ProfileTypeI18nLocalizationService profileTypeI18nLocalizationService;
56     private final Map<LocalizedKey, ProfileType> localizedProfileTypeCache = new ConcurrentHashMap<>();
57     private final Bundle bundle;
58
59     private @NonNullByDefault({}) TransformationService transformationService;
60
61     @Activate
62     public VATTransformationProfileFactory(final @Reference LocaleProvider localeProvider,
63             final @Reference ProfileTypeI18nLocalizationService profileTypeI18nLocalizationService,
64             final @Reference BundleResolver bundleResolver) {
65         this.localeProvider = localeProvider;
66         this.profileTypeI18nLocalizationService = profileTypeI18nLocalizationService;
67         this.bundle = bundleResolver.resolveBundle(VATTransformationProfileFactory.class);
68     }
69
70     @Override
71     public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
72         return Set.of(createLocalizedProfileType(ProfileTypeBuilder.newState(PROFILE_TYPE_UID, PROFILE_TYPE_UID.getId())
73                 .withSupportedItemTypes(CoreItemFactory.NUMBER).build(), locale));
74     }
75
76     @Override
77     public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
78             ProfileContext profileContext) {
79         return new VATTransformationProfile(callback, transformationService, profileContext, localeProvider);
80     }
81
82     private ProfileType createLocalizedProfileType(ProfileType profileType, @Nullable Locale locale) {
83         final LocalizedKey localizedKey = new LocalizedKey(profileType.getUID(),
84                 locale != null ? locale.toLanguageTag() : null);
85
86         final ProfileType cachedlocalizedProfileType = localizedProfileTypeCache.get(localizedKey);
87         if (cachedlocalizedProfileType != null) {
88             return cachedlocalizedProfileType;
89         }
90
91         final ProfileType localizedProfileType = profileTypeI18nLocalizationService.createLocalizedProfileType(bundle,
92                 profileType, locale);
93         localizedProfileTypeCache.put(localizedKey, localizedProfileType);
94
95         return localizedProfileType;
96     }
97
98     @Override
99     public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
100         return List.of(PROFILE_TYPE_UID);
101     }
102
103     @Reference(target = "(openhab.transform=VAT)")
104     public void addTransformationService(TransformationService service) {
105         this.transformationService = service;
106     }
107
108     public void removeTransformationService(TransformationService service) {
109         this.transformationService = null;
110     }
111 }