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.calendars.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 filtered events service 038 */ 039public class FilteredEventsUserPreferencesManager implements Component, Serviceable 040{ 041 /** The Avalon role */ 042 public static final String ROLE = FilteredEventsUserPreferencesManager.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 involved zone item 069 * @param view the view used to display the filtered events 070 * @param calendarIds the ids of the calendars to use as a filter 071 * @param tagIds the ids of the tags to use as a filter 072 * @throws UserPreferencesException if an error occurs while setting the user preferences 073 */ 074 @Callable 075 public void setUserPreferences(String siteName, String zoneItemId, String view, List<String> calendarIds, List<String> tagIds) throws UserPreferencesException 076 { 077 // We save user preferences for the front-end users only 078 // FIXME to uncomment 079// if (_renderingContextHandler.getRenderingContext().equals(RenderingContext.FRONT)) 080// { 081 UserIdentity user = _currentUserProvider.getUser(); 082 if (user != null && StringUtils.isNotEmpty(user.getLogin()) && StringUtils.isNotEmpty(user.getPopulationId())) 083 { 084 Map<String, String> values = new HashMap<>(); 085 Map<String, String> contextVars = _getContextVars(siteName); 086 087 values.put("view", view); 088 values.put("calendarIds", _jsonUtils.convertObjectToJson(calendarIds)); 089 values.put("tagIds", _jsonUtils.convertObjectToJson(tagIds)); 090 091 _userPreferencesManager.setUserPreferences(user, siteName + "/" + zoneItemId, contextVars, values); 092 } 093// } 094 } 095 096 /** 097 * Get the preferences of the current user 098 * @param siteName the name of the site 099 * @param zoneItemId the id of the invovlved zone item 100 * @return the current user's preferences 101 * @throws UserPreferencesException if an error occurs while retrieving the preferences of the current user 102 */ 103 @Callable 104 public Map<String, Object> getUserPreferences(String siteName, String zoneItemId) throws UserPreferencesException 105 { 106 Map<String, Object> userPreferences = new HashMap<> (); 107 108 UserIdentity user = _currentUserProvider.getUser(); 109 if (user != null) 110 { 111 Map<String, String> contextVars = _getContextVars(siteName); 112 113 Map<String, String> unTypedUserPrefs = _userPreferencesManager.getUnTypedUserPrefs(user, siteName + "/" + zoneItemId, contextVars); 114 115 String view = "minicalendar"; 116 Object[] calendars = new Object[]{null}; 117 Object[] tags = new Object[]{}; 118 119 if (!unTypedUserPrefs.isEmpty()) 120 { 121 view = _userPreferencesManager.getUserPreferenceAsString(user, siteName + "/" + zoneItemId, contextVars, "view"); 122 calendars = _jsonUtils.convertJsonToArray(_userPreferencesManager.getUserPreferenceAsString(user, siteName + "/" + zoneItemId, contextVars, "calendarIds")); 123 tags = _jsonUtils.convertJsonToArray(_userPreferencesManager.getUserPreferenceAsString(user, siteName + "/" + zoneItemId, contextVars, "tagIds")); 124 } 125 126 userPreferences.put("view", view); 127 userPreferences.put("calendars", calendars); 128 userPreferences.put("tags", tags); 129 } 130 131 return userPreferences; 132 } 133 134 private Map<String, String> _getContextVars(String siteName) 135 { 136 Map<String, String> contextVars = new HashMap<>(); 137 contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName); 138 return contextVars; 139 } 140}