001/*
002 *  Copyright 2021 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.List;
019import java.util.Optional;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.runtime.plugin.component.AbstractThreadSafeComponentExtensionPoint;
026
027/**
028 * This class is in charge of handling GDPR service extension point.
029 */
030public class GDPRServicesExtensionPoint extends AbstractThreadSafeComponentExtensionPoint<GDPRService>
031{
032    /** Avalon Role */
033    public static final String ROLE = GDPRServicesExtensionPoint.class.getName();
034    
035    /** The GDPR component helper */
036    protected GDPRComponentHelper _gdprComponentHelper;
037    
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        super.service(manager);
042        _gdprComponentHelper = (GDPRComponentHelper) manager.lookup(GDPRComponentHelper.ROLE);
043    }
044    
045    /**
046     * Get the service XSLT path
047     * @param serviceId the service id
048     * @return the service XSLT path 
049     */
050    public String getServiceXSLTPath(String serviceId)
051    {
052        GDPRComponent gdprComponent = _gdprComponentHelper.getSelectedGDPRComponent();
053        
054        Optional<String> serviceXSLTPath = Optional.empty();
055        
056        // Get all services with id 'serviceId'
057        List<GDPRService> gdprServices = getExtensionsIds().stream()
058            .map(this::getExtension)
059            .filter(service -> service.getId().equals(serviceId))
060            .collect(Collectors.toList());
061        
062        // Find a service linked to current GDPR component id
063        serviceXSLTPath = gdprServices.stream()
064            .filter(service -> service.getGDPRId().isPresent())
065            .filter(service -> service.getGDPRId().get().equals(gdprComponent.getId()))
066            .map(GDPRService::getXSLTPath)
067            .findFirst();
068            
069        if (serviceXSLTPath.isEmpty())
070        {
071            // Find a default service xslt not linked to a GDPR component
072            serviceXSLTPath = gdprServices.stream()
073                .filter(service -> service.getGDPRId().isEmpty())
074                .map(GDPRService::getXSLTPath)
075                .findFirst();
076        }
077        
078        if (serviceXSLTPath.isEmpty())
079        {
080            throw new IllegalArgumentException("Can't find XSLT path for service id: " + serviceId);
081        }
082        
083        return serviceXSLTPath.get();
084    }
085}