001/*
002 *  Copyright 2016 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.plugins.workspaces.events.activitystream;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
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;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.core.user.CurrentUserProvider;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.core.userpref.UserPreferencesException;
032import org.ametys.core.userpref.UserPreferencesManager;
033import org.ametys.core.util.JSONUtils;
034import org.ametys.web.userpref.FOUserPreferencesConstants;
035
036/**
037 * Class handling the front-office user preferences for the filtered events service
038 */
039public class ActivityStreamUserPreferencesManager implements Component, Serviceable
040{
041    /** The Avalon role */
042    public static final String ROLE = ActivityStreamUserPreferencesManager.class.getName();
043    
044    /** The current user provider */
045    private CurrentUserProvider _currentUserProvider;
046    
047    /** The rendering context handler */
048//    private RenderingContextHandler _renderingContextHandler;
049
050    /** The manager of user preferences for the front-office */
051    private UserPreferencesManager _userPreferencesManager;
052
053    /** Utility method for JSON strings */
054    private JSONUtils _jsonUtils;
055
056    @Override
057    public void service(ServiceManager serviceManager) throws ServiceException
058    {
059        _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE);
060        _jsonUtils = (JSONUtils) serviceManager.lookup(JSONUtils.ROLE);
061//        _renderingContextHandler = (RenderingContextHandler) serviceManager.lookup(RenderingContextHandler.ROLE);
062        _userPreferencesManager = (UserPreferencesManager) serviceManager.lookup(UserPreferencesManager.ROLE /* + ".FO" */); //FIXME uncomment
063    }
064    
065    /**
066     * Set the preferences of the current user for the filtered events service
067     * @param siteName the name of the site
068     * @param zoneItemId the id of the involved zone item
069     * @param projectNames the names of the projects to use as a filter 
070     * @param eventTypes the enabled/disabled activity types sorted by category
071     * @throws UserPreferencesException if an error occurs while setting the user preferences
072     */
073    @Callable
074    public void setUserPreferences(String siteName, String zoneItemId, List<String> projectNames, Map<String, Object> eventTypes) throws UserPreferencesException
075    {
076        UserIdentity user = _currentUserProvider.getUser();
077        if (user != null && StringUtils.isNotEmpty(user.getLogin()) && StringUtils.isNotEmpty(user.getPopulationId()))
078        {
079            Map<String, String> values = new HashMap<>();
080            
081            Map<String, String> contextVars = _getContextVars(siteName);
082            
083            values.put("projects", _jsonUtils.convertObjectToJson(projectNames));
084            values.put("eventTypes", _jsonUtils.convertObjectToJson(eventTypes));
085            
086            _userPreferencesManager.setUserPreferences(user, siteName + "/" + zoneItemId, contextVars, values);
087        }
088    }
089    
090    /**
091     * Get the preferences of the current user
092     * @param siteName the name of the site
093     * @param zoneItemId the id of the involved zone item
094     * @return the current user's preferences 
095     * @throws UserPreferencesException if an error occurs while retrieving the preferences of the current user
096     */
097    @Callable
098    public Map<String, Object> getUserPreferences(String siteName, String zoneItemId) throws UserPreferencesException
099    {
100        Map<String, Object> userPreferences = new HashMap<> ();
101
102        UserIdentity user = _currentUserProvider.getUser();
103        if (user != null)
104        {
105            Map<String, String> contextVars = _getContextVars(siteName);
106            
107            Map<String, String> unTypedUserPrefs = _userPreferencesManager.getUnTypedUserPrefs(user, siteName + "/" + zoneItemId, contextVars);
108            
109            Object[] projects = new Object[0];
110            Map<String, Object> eventTypes = new HashMap<>();
111            
112            if (!unTypedUserPrefs.isEmpty())
113            {
114                projects = _jsonUtils.convertJsonToArray(_userPreferencesManager.getUserPreferenceAsString(user, siteName + "/" + zoneItemId, contextVars, "projects"));
115                eventTypes = _jsonUtils.convertJsonToMap(_userPreferencesManager.getUserPreferenceAsString(user, siteName + "/" + zoneItemId, contextVars, "eventTypes"));
116            }
117            
118            userPreferences.put("projects", projects);
119            userPreferences.put("eventTypes", eventTypes);
120        }
121        
122        return userPreferences;
123    }
124    
125    private Map<String, String> _getContextVars(String siteName)
126    {
127        Map<String, String> contextVars = new HashMap<>();
128        contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName);
129        return contextVars;
130    }
131}