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