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.pagesubscription; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Objects; 021import java.util.Optional; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.commons.lang.StringUtils; 028 029import org.ametys.core.user.CurrentUserProvider; 030import org.ametys.core.user.User; 031import org.ametys.core.user.UserManager; 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.runtime.i18n.I18nizableTextParameter; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035 036/** 037 * Helper for broadcast channel 038 */ 039public class BroadcastChannelHelper extends AbstractLogEnabled implements Component, Serviceable 040{ 041 /** The current user provider */ 042 protected static CurrentUserProvider _currentUserProvider; 043 044 /** The user manager */ 045 protected static UserManager _userManager; 046 047 /** 048 * The broadcast channel of the subscription 049 */ 050 public enum BroadcastChannel 051 { 052 /** The mail broadcast channel */ 053 MAIL, 054 /** The site broadcast channel */ 055 SITE; 056 } 057 058 public void service(ServiceManager manager) throws ServiceException 059 { 060 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 061 _userManager = (UserManager) manager.lookup(UserManager.ROLE); 062 } 063 064 /** 065 * Get the label of the broadcast channel 066 * @param broadcastChannel the broadcast channel 067 * @return the broadcast channel label 068 */ 069 public static I18nizableText getLabel(BroadcastChannel broadcastChannel) 070 { 071 return new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_USER_SUBSCRIPTIONS_BROADCAST_CHANNEL_LABEL_" + broadcastChannel.name()); 072 } 073 074 /** 075 * Get the smart label of the broadcast channel 076 * @param broadcastChannel the broadcast channel 077 * @return the broadcast channel smart label 078 */ 079 public static I18nizableText getSmartLabel(BroadcastChannel broadcastChannel) 080 { 081 Map<String, I18nizableTextParameter> params = new HashMap<>(); 082 switch (broadcastChannel) 083 { 084 case MAIL: 085 String email = _getCurrentUserMail(); 086 params.put("mail", new I18nizableText(email)); 087 break; 088 default: // Do nothing .. 089 break; 090 } 091 return new I18nizableText("plugin.page-subscription", "PLUGINS_PAGE_SUBSCRIBE_USER_SUBSCRIPTIONS_BROADCAST_CHANNEL_SMART_LABEL_" + broadcastChannel.name(), params); 092 } 093 094 private static String _getCurrentUserMail() 095 { 096 return Optional.ofNullable(_currentUserProvider.getUser()) 097 .map(_userManager::getUser) 098 .filter(Objects::nonNull) 099 .map(User::getEmail) 100 .orElse(StringUtils.EMPTY); 101 } 102}