001/* 002 * Copyright 2016 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.tasks.userprefs; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.component.Component; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.commons.lang3.StringUtils; 027 028import org.ametys.core.ui.Callable; 029import org.ametys.core.user.CurrentUserProvider; 030import org.ametys.core.user.UserIdentity; 031import org.ametys.core.userpref.UserPreferencesException; 032import org.ametys.core.userpref.UserPreferencesManager; 033import org.ametys.core.util.JSONUtils; 034import org.ametys.web.userpref.FOUserPreferencesConstants; 035 036/** 037 * Class handling the front-office user preferences for the tasks list service 038 */ 039public class TasksUserPreferencesManager implements Component, Serviceable 040{ 041 /** The Avalon role */ 042 public static final String ROLE = TasksUserPreferencesManager.class.getName(); 043 044 /** The current user provider */ 045 private CurrentUserProvider _currentUserProvider; 046 047 /** The rendering context handler */ 048// private RenderingContextHandler _renderingContextHandler; 049 050 /** The manager of user preferences for the front-office */ 051 private UserPreferencesManager _userPreferencesManager; 052 053 /** Utility method for JSON strings */ 054 private JSONUtils _jsonUtils; 055 056 @Override 057 public void service(ServiceManager serviceManager) throws ServiceException 058 { 059 _currentUserProvider = (CurrentUserProvider) serviceManager.lookup(CurrentUserProvider.ROLE); 060 _jsonUtils = (JSONUtils) serviceManager.lookup(JSONUtils.ROLE); 061// _renderingContextHandler = (RenderingContextHandler) serviceManager.lookup(RenderingContextHandler.ROLE); 062 _userPreferencesManager = (UserPreferencesManager) serviceManager.lookup(UserPreferencesManager.ROLE /* + ".FO" */); //FIXME uncomment 063 } 064 065 /** 066 * Set the preferences of the current user for the filtered events service 067 * @param siteName the name of the site 068 * @param zoneItemId the id of the zone item 069 * @param projects the projects from which gather the tasks 070 * @throws UserPreferencesException if an error occurs while setting the user preferences 071 */ 072 @Callable 073 public void setUserPreferences(String siteName, String zoneItemId, List<String> projects) throws UserPreferencesException 074 { 075 // We save user preferences for the front-end users only 076 // FIXME to uncomment 077// if (_renderingContextHandler.getRenderingContext().equals(RenderingContext.FRONT)) 078// { 079 UserIdentity user = _currentUserProvider.getUser(); 080 if (user != null && StringUtils.isNotEmpty(user.getLogin()) && StringUtils.isNotEmpty(user.getPopulationId())) 081 { 082 Map<String, String> values = new HashMap<>(); 083 084 Map<String, String> contextVars = _getContextVars(siteName); 085 086 values.put("projects", _jsonUtils.convertObjectToJson(projects)); 087 088 _userPreferencesManager.setUserPreferences(user, siteName + "/" + zoneItemId, contextVars, values); 089 } 090// } 091 } 092 093 /** 094 * Get the preferences of the current user 095 * @param siteName the name of the site 096 * @param zoneItemId the id of the zone item 097 * @return the current user's preferences 098 * @throws UserPreferencesException if an error occurs while retrieving the preferences of the current user 099 */ 100 @Callable 101 public Map<String, Object> getUserPreferences(String siteName, String zoneItemId) throws UserPreferencesException 102 { 103 Map<String, Object> userPreferences = new HashMap<> (); 104 105 UserIdentity user = _currentUserProvider.getUser(); 106 if (user != null) 107 { 108 Map<String, String> contextVars = _getContextVars(siteName); 109 110 Map<String, String> unTypedUserPrefs = _userPreferencesManager.getUnTypedUserPrefs(user, siteName + "/" + zoneItemId, contextVars); 111 112 Object[] projects = new Object[]{null}; 113 114 if (!unTypedUserPrefs.isEmpty()) 115 { 116 projects = _jsonUtils.convertJsonToArray(_userPreferencesManager.getUserPreferenceAsString(user, siteName + "/" + zoneItemId, contextVars, "projects")); 117 } 118 119 userPreferences.put("projects", projects); 120 } 121 122 return userPreferences; 123 } 124 125 private Map<String, String> _getContextVars(String siteName) 126 { 127 Map<String, String> contextVars = new HashMap<>(); 128 contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName); 129 return contextVars; 130 } 131}