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 */
036    protected GDPRComponent _gdprComponent;
037    
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        super.service(manager);
042        _gdprComponent = (GDPRComponent) manager.lookup(GDPRComponent.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        Optional<String> serviceXSLTPath = Optional.empty();
053        
054        // Get all services with id 'serviceId'
055        List<GDPRService> gdprServices = getExtensionsIds().stream()
056            .map(this::getExtension)
057            .filter(service -> service.getId().equals(serviceId))
058            .collect(Collectors.toList());
059        
060        // Find a service linked to current GDPR component id
061        serviceXSLTPath = gdprServices.stream()
062            .filter(service -> service.getGDPRId().isPresent())
063            .filter(service -> service.getGDPRId().get().equals(_gdprComponent.getId()))
064            .map(GDPRService::getXSLTPath)
065            .findFirst();
066            
067        if (serviceXSLTPath.isEmpty())
068        {
069            // Find a default service xslt not linked to a GDPR component
070            serviceXSLTPath = gdprServices.stream()
071                .filter(service -> service.getGDPRId().isEmpty())
072                .map(GDPRService::getXSLTPath)
073                .findFirst();
074        }
075        
076        if (serviceXSLTPath.isEmpty())
077        {
078            throw new IllegalArgumentException("Can't find XSLT path for service id: " + serviceId);
079        }
080        
081        return serviceXSLTPath.get();
082    }
083}