001/*
002 *  Copyright 2024 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.userpref;
017
018import java.util.Collections;
019import java.util.Map;
020
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.cocoon.environment.Request;
029import org.apache.commons.lang3.StringUtils;
030
031import org.ametys.core.user.User;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.core.user.dataprovider.UserDataProvider;
034import org.ametys.core.userpref.UserPreferencesException;
035import org.ametys.core.userpref.UserPreferencesExtensionPoint;
036import org.ametys.core.userpref.UserPreferencesManager;
037import org.ametys.runtime.plugin.component.AbstractLogEnabled;
038import org.ametys.web.WebHelper;
039
040/**
041 * A user data provider which gets the value from the user preferences associated with the {@link User}
042 */
043public class FOUserPreferencesDataProvider extends AbstractLogEnabled implements UserDataProvider, Serviceable, Contextualizable
044{
045    /** The user preferences extension point */
046    protected UserPreferencesExtensionPoint _userPrefEPFO;
047    /** The user preferences manager */
048    protected UserPreferencesManager _userPrefManagerFO;
049    private Context _context;
050    
051    @Override
052    public void service(ServiceManager serviceManager) throws ServiceException
053    {
054        _userPrefEPFO = (UserPreferencesExtensionPoint) serviceManager.lookup(UserPreferencesExtensionPoint.ROLE + ".FO");
055        _userPrefManagerFO = (UserPreferencesManager) serviceManager.lookup(UserPreferencesManager.ROLE + ".FO");
056    }
057    
058    @Override
059    public void contextualize(Context context) throws ContextException
060    {
061        _context = context;
062    }
063    
064    @Override
065    public int getPriority()
066    {
067        return 30000;
068    }
069    
070    @Override
071    public boolean supports(String element)
072    {
073        return _userPrefEPFO.getUserPreference(Map.of(), element) != null;
074    }
075    
076    @Override
077    public Object getValue(User user, String dataId)
078    {
079        try
080        {
081            Map<String, String> userPref = getUserPreferences(user.getIdentity(), dataId);
082            
083            if (userPref != null && userPref.get(dataId) != null && StringUtils.isNotBlank(userPref.get(dataId)))
084            {
085                return userPref.get(dataId);
086            }
087        }
088        catch (UserPreferencesException e)
089        {
090            getLogger().warn("An error occured while retrieving user preference of user {}", user.getIdentity(), e);
091        }
092        
093        return null;
094    }
095    
096    /**
097     * Get the user preferences of a user
098     * @param userIdentity The userIdentity of the User
099     * @param dataId The if of the data wanted
100     * @return The user preferences depending on the context
101     * @throws UserPreferencesException If an error occurs
102     */
103    public Map<String, String> getUserPreferences(UserIdentity userIdentity, String dataId) throws UserPreferencesException
104    {
105        Request request = ContextHelper.getRequest(_context);
106        
107        // Get the site from the request
108        String siteName = WebHelper.getSiteName(request);
109
110        if (siteName == null)
111        {
112            return null;
113        }
114        String storageContext = "/sites/" + siteName;
115        
116        return _userPrefManagerFO.getTypedUserPrefs(userIdentity, storageContext, Collections.EMPTY_MAP);
117    }
118}