001/*
002 *  Copyright 2023 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.gdpr;
017
018import java.util.Map;
019import java.util.Objects;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.Enumerator;
029
030/**
031 * {@link Enumerator} for the GDPR component enumerator.
032 */
033public class GDPRComponentEnumerator implements Enumerator<String>, Serviceable, Component
034{
035    /** Avalon role */
036    public static final String ROLE = GDPRComponentEnumerator.class.getName();
037    
038    /** The GDPR component extension point */
039    protected GDPRComponentExtensionPoint _gdprComponentEP;
040    
041    /** The service manager */
042    protected ServiceManager _manager;
043    
044    @Override
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _manager = manager;
048    }
049    
050    private GDPRComponentExtensionPoint _getGDPRComponentExtensionPoint()
051    {
052        if (_gdprComponentEP == null)
053        {
054            try
055            {
056                _gdprComponentEP = (GDPRComponentExtensionPoint) _manager.lookup(GDPRComponentExtensionPoint.ROLE);
057            }
058            catch (ServiceException e)
059            {
060                // Can be called during application startup
061                return null;
062            }
063        }
064        return _gdprComponentEP;
065    }
066    
067    @Override
068    public I18nizableText getEntry(String value) throws Exception
069    {
070        GDPRComponentExtensionPoint gdprComponentEP = _getGDPRComponentExtensionPoint();
071        if (gdprComponentEP != null && gdprComponentEP.hasExtension(value))
072        {
073            return gdprComponentEP.getExtension(value).getLabel();
074        }
075        else
076        {
077            // Can be called during application startup
078            return new I18nizableText(value);
079        }
080    }
081
082    @Override
083    public Map<String, I18nizableText> getTypedEntries() throws Exception
084    {
085        GDPRComponentExtensionPoint gdprComponentEP = _getGDPRComponentExtensionPoint();
086        if (gdprComponentEP == null)
087        {
088            return Map.of();
089        }
090        
091        return gdprComponentEP.getExtensionsIds()
092                    .stream()
093                    .map(id -> gdprComponentEP.getExtension(id))
094                    .filter(Objects::nonNull)
095                    .collect(
096                        Collectors.toMap(
097                            GDPRComponent::getId, 
098                            GDPRComponent::getLabel
099                        )
100                    );
101    }
102}