001/* 002 * Copyright 2019 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.project.notification; 017 018import java.util.Arrays; 019import java.util.List; 020import java.util.Objects; 021import java.util.Optional; 022import java.util.stream.Collectors; 023 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.group.GroupManager; 030import org.ametys.core.observation.AsyncObserver; 031import org.ametys.core.observation.Event; 032import org.ametys.core.user.User; 033import org.ametys.core.user.UserManager; 034import org.ametys.core.util.I18nUtils; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.plugins.workspaces.members.ProjectMemberManager; 037import org.ametys.plugins.workspaces.project.ProjectManager; 038import org.ametys.plugins.workspaces.project.objects.Project; 039import org.ametys.runtime.plugin.component.AbstractLogEnabled; 040import org.ametys.runtime.plugin.component.PluginAware; 041import org.ametys.web.population.PopulationContextHelper; 042import org.ametys.web.repository.site.Site; 043import org.ametys.web.repository.site.SiteManager; 044 045/** 046 * Abstract observer for sending mail to members 047 */ 048public abstract class AbstractMemberMailNotifierObserver extends AbstractLogEnabled implements AsyncObserver, PluginAware, Serviceable 049{ 050 /** The name of current plugin */ 051 protected String _pluginName; 052 /** The Ametys Object resolver */ 053 protected AmetysObjectResolver _resolver; 054 /** The i18n utils */ 055 protected I18nUtils _i18nUtils; 056 /** The project member manager */ 057 protected ProjectMemberManager _projectMemberManager; 058 /** Project manager */ 059 protected ProjectManager _projectManager; 060 /** Site manager */ 061 protected SiteManager _siteManager; 062 /** Population context helper */ 063 protected PopulationContextHelper _populationContextHelper; 064 /** User manager */ 065 protected UserManager _userManager; 066 /** Group manager */ 067 protected GroupManager _groupManager; 068 069 070 @Override 071 public void setPluginInfo(String pluginName, String featureName, String id) 072 { 073 _pluginName = pluginName; 074 } 075 076 public void service(ServiceManager manager) throws ServiceException 077 { 078 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 079 _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE); 080 _projectMemberManager = (ProjectMemberManager) manager.lookup(ProjectMemberManager.ROLE); 081 _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE); 082 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 083 _populationContextHelper = (PopulationContextHelper) manager.lookup(org.ametys.core.user.population.PopulationContextHelper.ROLE); 084 _userManager = (UserManager) manager.lookup(UserManager.ROLE); 085 _groupManager = (GroupManager) manager.lookup(GroupManager.ROLE); 086 } 087 088 @Override 089 public int getPriority(Event event) 090 { 091 return MIN_PRIORITY; 092 } 093 094 095 /** 096 * Gets the URL of the project 097 * @param project The project 098 * @return the URL of the project 099 */ 100 protected String getProjectUrl(Project project) 101 { 102 return project.getSites().stream() 103 .map(Site::getUrl) 104 .filter(Objects::nonNull) 105 .findFirst() 106 .orElse(""); 107 } 108 109 110 /** 111 * Get the projects catalog URL 112 * @return The projects catalog URL 113 */ 114 protected String getProjectsCatalogUrl() 115 { 116 return Optional.ofNullable(_projectManager.getCatalogSiteName()) 117 .filter(StringUtils::isNotEmpty) 118 .map(_siteManager::getSite) 119 .map(Site::getUrl) 120 .orElse(""); 121 } 122 123 124 /** 125 * Get the list of emails from users, filtering out users with no emails 126 * @param users The list of users 127 * @return The list of emails 128 */ 129 protected List<String> getUsersEmail(List<User> users) 130 { 131 return users.stream() 132 .map(User::getEmail) 133 .filter(StringUtils::isNotEmpty) 134 .collect(Collectors.toList()); 135 } 136 137 /** 138 * Helper method to get the list of parameters for a I18nizableText 139 * @param params The parameters array 140 * @return The list of parameters without null values 141 */ 142 protected List<String> getI18nParams(String... params) 143 { 144 return Arrays.stream(params) 145 .map(StringUtils::defaultString) 146 .collect(Collectors.toList()); 147 } 148}