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 org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.context.Context;
020import org.apache.avalon.framework.context.ContextException;
021import org.apache.avalon.framework.context.Contextualizable;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.components.ContextHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.commons.lang3.StringUtils;
028
029import org.ametys.runtime.config.Config;
030import org.ametys.web.WebHelper;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033
034/**
035 * Helper for GDPR component
036 */
037public class GDPRComponentHelper implements Component, Serviceable, Contextualizable
038{
039    /** The Avalon role */
040    public static final String ROLE = GDPRComponentHelper.class.getName();
041
042    /** The value for the site configuration to have the general configuration */
043    public static final String SITE_CONFIGURATION_RGPD_GENERAL_CONFIGURATION_VALUE = "_ametys_general_configuration";
044    
045    /** The id of the ametys gdpr component */
046    public static final String AMETYS_GDPR_COMPONENT_ID = "gdpr.web";
047    
048    /** The site configuration gdpr component id */
049    public static final String SITE_CONFIGURATION_GDPR_COMPONENT_ID = "gdpr-component-choice";
050    
051    /** The general configuration gdpr component id */
052    public static final String GENERAL_CONFIGURATION_GDPR_COMPONENT_ID = "plugins.web.gdpr.choice";
053    
054    /** The GDPR component EP */
055    protected GDPRComponentExtensionPoint _gdprComponentEP;
056    
057    /** The site manager */
058    protected SiteManager _siteManager;
059    
060    /** The context */
061    protected Context _context;
062    
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        _gdprComponentEP = (GDPRComponentExtensionPoint) manager.lookup(GDPRComponentExtensionPoint.ROLE);
066        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
067    }
068    
069    public void contextualize(Context context) throws ContextException
070    {
071        _context = context;
072    }
073    
074    /**
075     * Get the selected GDPR component
076     * @return the selected GDPR component
077     */
078    public GDPRComponent getSelectedGDPRComponent()
079    {
080        Request request = ContextHelper.getRequest(_context);
081        String siteName = WebHelper.getSiteName(request);
082        Site site = _siteManager.getSite(siteName);
083      
084        String gdprComponentId = site.getValue(SITE_CONFIGURATION_GDPR_COMPONENT_ID);
085        if (StringUtils.isBlank(gdprComponentId) || SITE_CONFIGURATION_RGPD_GENERAL_CONFIGURATION_VALUE.equals(gdprComponentId))
086        {
087            gdprComponentId = Config.getInstance().getValue(GENERAL_CONFIGURATION_GDPR_COMPONENT_ID);
088        }
089        
090        return _gdprComponentEP.hasExtension(gdprComponentId) ? _gdprComponentEP.getExtension(gdprComponentId) : _gdprComponentEP.getExtension(AMETYS_GDPR_COMPONENT_ID);
091    }
092}