001/* 002 * Copyright 2021 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.frontnotification.userprefs; 017 018import java.util.Arrays; 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.acting.ServiceableAction; 027import org.apache.commons.lang.StringUtils; 028 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.web.userpref.FOUserPreferencesConstants; 034 035/** 036 * Abstract classes for flash info user preferences 037 * 038 */ 039public abstract class AbstractItemsAction extends ServiceableAction 040{ 041 /** The id of user preference for flash info contents */ 042 public static final String FLASHINFO_USER_PREF_ITEM_IDS = "flashInfoItemIds"; 043 044 /** The current user provider */ 045 protected CurrentUserProvider _currentUserProvider; 046 /** The user preferences manager */ 047 protected UserPreferencesManager _userPrefManager; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 054 _userPrefManager = (UserPreferencesManager) smanager.lookup(UserPreferencesManager.ROLE + ".FO"); 055 } 056 057 /** 058 * Get the ids of read flash infos 059 * @param user the user 060 * @param siteName the site name 061 * @return the ids of read contents 062 * @throws UserPreferencesException if failed to retrieve user preferences 063 */ 064 protected List<String> getReadItems(UserIdentity user, String siteName) throws UserPreferencesException 065 { 066 Map<String, String> contextVars = new HashMap<>(); 067 contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName); 068 069 String contentIdsAsStr = _userPrefManager.getUserPreferenceAsString(user, "/sites/" + siteName, contextVars, FLASHINFO_USER_PREF_ITEM_IDS); 070 if (StringUtils.isNotBlank(contentIdsAsStr)) 071 { 072 return Arrays.asList(StringUtils.split(contentIdsAsStr, ",")); 073 } 074 075 return Collections.emptyList(); 076 } 077 078 /** 079 * Save the ids of read items in user preferences 080 * @param owner the user 081 * @param itemIds the id of read items 082 * @param siteName the site name 083 * @throws UserPreferencesException if failed to save user preferences 084 */ 085 protected void saveReadItems(UserIdentity owner, List<String> itemIds, String siteName) throws UserPreferencesException 086 { 087 Map<String, String> contextVars = new HashMap<>(); 088 contextVars.put(FOUserPreferencesConstants.CONTEXT_VAR_SITENAME, siteName); 089 090 _userPrefManager.addUserPreference(owner, "/sites/" + siteName, contextVars, FLASHINFO_USER_PREF_ITEM_IDS, StringUtils.join(itemIds, ",")); 091 } 092 093}