001/*
002 *  Copyright 2019 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.frontoffice.search.requesttime.impl;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021import java.util.stream.Collectors;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.core.user.CurrentUserProvider;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.core.userpref.UserPreferencesManager;
030import org.ametys.core.util.JSONUtils;
031import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
032import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
033import org.ametys.web.frontoffice.search.requesttime.input.SearchUserInputs;
034
035/**
036 * {@link SearchComponent} to save the selected user filters in the user preferences if the user is authenticated.
037 */
038public class SaveUserPrefsSearchComponent implements SearchComponent, Serviceable
039{
040    private static final String __ENABLE_SAVE_USER_PREFS_PARAMETER_NAME = "saveUserPrefs";
041    
042    /** The JSON utils */
043    protected JSONUtils _jsonUtils;
044    /** The user preferences manager */
045    protected UserPreferencesManager _userPrefsManager;
046    /** The current user provider */
047    protected CurrentUserProvider _currentUserProvider;
048    
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
053        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
054        _userPrefsManager = (UserPreferencesManager) manager.lookup(UserPreferencesManager.ROLE);
055    }
056
057    @Override
058    public int priority()
059    {
060        return MIN_PRIORITY - 500;
061    }
062
063    @Override
064    public boolean supports(SearchComponentArguments args)
065    {
066        return args.serviceInstance().saveUserPrefs() && args.generatorParameters().getParameterAsBoolean(__ENABLE_SAVE_USER_PREFS_PARAMETER_NAME, true);
067    }
068
069    @Override
070    public void execute(SearchComponentArguments args) throws Exception
071    {
072        // If the user is connected
073        UserIdentity currentUser = _currentUserProvider.getUser();
074        if (currentUser != null)
075        {
076            SearchUserInputs searchUserInputs = args.userInputs();
077            
078            String serviceId = args.serviceInstance().getId();
079
080            Map<String, String> values = new HashMap<>();
081            
082            values.put("search.criteria", _jsonUtils.convertObjectToJson(searchUserInputs.criteria()));
083            values.put("search.facets", _jsonUtils.convertObjectToJson(searchUserInputs.facets()));
084            
085            List<Map<String, Object>> jsonSorts = searchUserInputs.sorts()
086                .stream()
087                .map(sortPair ->
088                {
089                    Map<String, Object> sortMap = new HashMap<>();
090                    sortMap.put("key", sortPair.getLeft());
091                    sortMap.put("order", sortPair.getRight().name());
092                    return sortMap;
093                })
094                .collect(Collectors.toList());
095            values.put("search.sorts", _jsonUtils.convertObjectToJson(jsonSorts));
096            
097            _userPrefsManager.setUserPreferences(currentUser, "search-" + serviceId, new HashMap<>(), values);
098        }
099    }
100    
101}