001/* 002 * Copyright 2025 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.preferences; 017 018import java.util.Map; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022 023import org.ametys.core.ui.Callable; 024import org.ametys.core.userpref.UserPreferencesException; 025import org.ametys.core.userpref.UserPreferencesManager; 026import org.ametys.plugins.workspaces.AbstractWorkspaceDAO; 027 028/** 029 * DAO for workspaces view user preferences, such as choosing between cards or list view 030 */ 031public class WorkspaceViewUserPreferencesDAO extends AbstractWorkspaceDAO 032{ 033 /** Avalon Role */ 034 public static final String ROLE = WorkspaceViewUserPreferencesDAO.class.getName(); 035 036 /** the user preferences context for workspaces view */ 037 public static final String VIEW_USER_PREF_CONTEXT = "/workspaces/view"; 038 039 /** The user preferences */ 040 protected UserPreferencesManager _userPrefsManager; 041 042 @Override 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 super.service(manager); 046 _userPrefsManager = (UserPreferencesManager) manager.lookup(UserPreferencesManager.ROLE); 047 } 048 049 /** 050 * Update the user preferences of current user for a view on given module 051 * @param moduleId the module id 052 * @param preferences the preferences value 053 */ 054 @Callable (rights = Callable.NO_CHECK_REQUIRED) // Preferences are user-specific, so no rights check is needed 055 public void setUserPreference(String moduleId, String preferences) 056 { 057 try 058 { 059 _userPrefsManager.addUserPreference(_currentUserProvider.getUser(), VIEW_USER_PREF_CONTEXT, Map.of(), moduleId, preferences); 060 } 061 catch (UserPreferencesException e) 062 { 063 getLogger().error("An error occured while setting the user preferences.", e); 064 } 065 } 066 067 /** 068 * Get the view preference of the current user on given module 069 * @param moduleId the module id 070 * @return the view preference (true for grid, false for list) 071 */ 072 @Callable (rights = Callable.NO_CHECK_REQUIRED) // Preferences are user-specific, so no rights check is needed 073 public String getUserPreference(String moduleId) 074 { 075 String userPreference = null; 076 try 077 { 078 userPreference = _userPrefsManager.getUserPreferenceAsString(_currentUserProvider.getUser(), VIEW_USER_PREF_CONTEXT, Map.of(), moduleId); 079 } 080 catch (UserPreferencesException e) 081 { 082 getLogger().error("An error occured while getting the user preferences.", e); 083 } 084 return userPreference; 085 } 086}