001/*
002 *  Copyright 2023 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.calendar.search;
017
018import java.util.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.core.user.CurrentUserProvider;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.core.userpref.UserPreferencesManager;
029import org.ametys.core.util.JSONUtils;
030import org.ametys.web.frontoffice.search.requesttime.SearchComponent;
031import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments;
032
033/**
034 * {@link SearchComponent} to save the selected user calendar's filters in the user preferences if the user is authenticated.
035 */
036public class SaveUserPrefsSearchComponent implements SearchComponent, Serviceable
037{
038    /** The JSON utils */
039    protected JSONUtils _jsonUtils;
040    /** The user preferences manager */
041    protected UserPreferencesManager _userPrefsManager;
042    /** The current user provider */
043    protected CurrentUserProvider _currentUserProvider;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
049        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
050        _userPrefsManager = (UserPreferencesManager) manager.lookup(UserPreferencesManager.ROLE);
051    }
052
053    @Override
054    public int priority()
055    {
056        // Should save in user prefs before sax
057        return MAX_PRIORITY + 500;
058    }
059
060    @Override
061    public boolean supports(SearchComponentArguments args)
062    {
063        return CalendarSearchService.isFormSubmit(args) && CalendarSearchService.saveUserPrefsEnabled(args) && CalendarSearchService.isActive(args) && CalendarSearchService.hasTagCategories(args.serviceInstance()) && CalendarSearchService.saveFilterInUserPrefs(args.serviceInstance());
064    }
065
066    @Override
067    public void execute(SearchComponentArguments args) throws Exception
068    {
069        // If the user is connected
070        UserIdentity currentUser = _currentUserProvider.getUser();
071        if (currentUser != null)
072        {
073            List<String> selectedTags = CalendarSearchService.getSelectedTags(args);
074            
075            String serviceId = args.serviceInstance().getId();
076
077            Map<String, String> values = new HashMap<>();
078            
079            values.put("search.calendar.tags", _jsonUtils.convertObjectToJson(selectedTags));
080            
081            _userPrefsManager.setUserPreferences(currentUser, "calendar-search-" + serviceId, new HashMap<>(), values);
082        }
083    }
084    
085}