2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.transform.vat.internal.profile;
15 import static org.openhab.transform.vat.internal.VATTransformationConstants.PROFILE_TYPE_UID;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Locale;
22 import java.util.concurrent.ConcurrentHashMap;
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;
46 * The {@link VATTransformationProfileFactory} is responsible for creating profiles.
48 * @author Jacob Laursen - Initial contribution
51 @Component(service = { ProfileFactory.class, ProfileTypeProvider.class })
52 public class VATTransformationProfileFactory implements ProfileFactory, ProfileTypeProvider {
54 private final LocaleProvider localeProvider;
55 private final ProfileTypeI18nLocalizationService profileTypeI18nLocalizationService;
56 private final Map<LocalizedKey, ProfileType> localizedProfileTypeCache = new ConcurrentHashMap<>();
57 private final Bundle bundle;
59 private @NonNullByDefault({}) TransformationService transformationService;
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);
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));
77 public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
78 ProfileContext profileContext) {
79 return new VATTransformationProfile(callback, transformationService, profileContext, localeProvider);
82 private ProfileType createLocalizedProfileType(ProfileType profileType, @Nullable Locale locale) {
83 final LocalizedKey localizedKey = new LocalizedKey(profileType.getUID(),
84 locale != null ? locale.toLanguageTag() : null);
86 final ProfileType cachedlocalizedProfileType = localizedProfileTypeCache.get(localizedKey);
87 if (cachedlocalizedProfileType != null) {
88 return cachedlocalizedProfileType;
91 final ProfileType localizedProfileType = profileTypeI18nLocalizationService.createLocalizedProfileType(bundle,
93 localizedProfileTypeCache.put(localizedKey, localizedProfileType);
95 return localizedProfileType;
99 public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
100 return List.of(PROFILE_TYPE_UID);
103 @Reference(target = "(openhab.transform=VAT)")
104 public void addTransformationService(TransformationService service) {
105 this.transformationService = service;
108 public void removeTransformationService(TransformationService service) {
109 this.transformationService = null;