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