001/* 002 * Copyright 2013 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.web.repository.page.actions; 017 018import java.util.HashMap; 019import java.util.HashSet; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023import java.util.stream.Collectors; 024 025import javax.mail.MessagingException; 026 027import org.apache.avalon.framework.parameters.Parameters; 028import org.apache.avalon.framework.service.ServiceException; 029import org.apache.avalon.framework.service.ServiceManager; 030import org.apache.cocoon.acting.ServiceableAction; 031import org.apache.cocoon.environment.ObjectModelHelper; 032import org.apache.cocoon.environment.Redirector; 033import org.apache.cocoon.environment.SourceResolver; 034import org.apache.commons.lang.StringUtils; 035 036import org.ametys.core.group.Group; 037import org.ametys.core.group.GroupIdentity; 038import org.ametys.core.group.GroupManager; 039import org.ametys.core.user.CurrentUserProvider; 040import org.ametys.core.user.User; 041import org.ametys.core.user.UserIdentity; 042import org.ametys.core.user.UserManager; 043import org.ametys.core.util.I18nUtils; 044import org.ametys.core.util.mail.SendMailHelper; 045import org.ametys.plugins.repository.AmetysObjectResolver; 046import org.ametys.runtime.config.Config; 047import org.ametys.runtime.i18n.I18nizableText; 048import org.ametys.web.repository.page.Page; 049import org.ametys.web.repository.site.Site; 050import org.ametys.web.site.SiteConfigurationExtensionPoint; 051 052/** 053 * This actions send a email to notify the publication of a page. 054 * 055 */ 056public class SendOnlineNotificationAction extends ServiceableAction 057{ 058 private UserManager _userManager; 059 private GroupManager _groupManager; 060 private SiteConfigurationExtensionPoint _siteConfiguration; 061 private AmetysObjectResolver _resolver; 062 private I18nUtils _i18nUtils; 063 private CurrentUserProvider _userProvider; 064 065 066 @Override 067 public void service(ServiceManager smanager) throws ServiceException 068 { 069 super.service(smanager); 070 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 071 _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 072 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 073 _groupManager = (GroupManager) smanager.lookup(GroupManager.ROLE); 074 _siteConfiguration = (SiteConfigurationExtensionPoint) smanager.lookup(SiteConfigurationExtensionPoint.ROLE); 075 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 076 } 077 078 079 @Override 080 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 081 { 082 @SuppressWarnings("unchecked") 083 Map<String, Object> jsParameters = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 084 085 String pageId = (String) jsParameters.get("id"); 086 Page page = _resolver.resolveById(pageId); 087 088 UserIdentity userIdentity = _userProvider.getUser(); 089 User notifier = _userManager.getUser(userIdentity.getPopulationId(), userIdentity.getLogin()); 090 091 String from = _getSender(notifier, page); 092 String subject = _getSubject (notifier, page); 093 094 @SuppressWarnings("unchecked") 095 List<Map<String, String>> jsUsers = (List<Map<String, String>>) jsParameters.get("users"); 096 List<UserIdentity> userIdentities = jsUsers.stream() 097 .map(jsUser -> new UserIdentity(jsUser.get("login"), jsUser.get("populationId"))) 098 .collect(Collectors.toList()); 099 100 @SuppressWarnings("unchecked") 101 List<Map<String, String>> jsGroups = (List<Map<String, String>>) jsParameters.get("groups"); 102 List<GroupIdentity> groupIdentities = jsGroups.stream() 103 .map(jsGroup -> new GroupIdentity(jsGroup.get("id"), jsGroup.get("groupDirectory"))) 104 .collect(Collectors.toList()); 105 106 Set<User> users = _getDistinctUsers(userIdentities, groupIdentities); 107 for (User user : users) 108 { 109 if (user.getEmail() != null) 110 { 111 try 112 { 113 String body = _getBody(notifier, user, page); 114 SendMailHelper.sendMail(subject, null, body, user.getEmail(), from); 115 } 116 catch (MessagingException e) 117 { 118 getLogger().error("Unable to send mail to user " + user.getEmail(), e); 119 } 120 } 121 } 122 123 return EMPTY_MAP; 124 } 125 126 /** 127 * Get the email sender 128 * @param sender The user responsible for the action 129 * @param page The published page 130 * @return The sender 131 */ 132 protected String _getSender (User sender, Page page) 133 { 134 if (sender != null && sender.getEmail() != null) 135 { 136 return sender.getFullName() + " <" + sender.getEmail() + ">"; 137 } 138 139 String from = _siteConfiguration.getValueAsString(page.getSiteName(), "site-mail-from"); 140 if (StringUtils.isBlank(from)) 141 { 142 from = Config.getInstance().getValueAsString("smtp.mail.from"); 143 } 144 return from; 145 } 146 147 /** 148 * Get the email subject 149 * @param sender The user responsible for the notification 150 * @param page The published page 151 * @return The email subject 152 */ 153 protected String _getSubject (User sender, Page page) 154 { 155 Site site = page.getSite(); 156 157 Map<String, I18nizableText> subjectI18nParameters = new HashMap<>(); 158 subjectI18nParameters.put("user", new I18nizableText(sender.getFullName())); 159 subjectI18nParameters.put("siteTitle", new I18nizableText(site.getTitle())); 160 subjectI18nParameters.put("pageTitle", new I18nizableText(page.getTitle())); 161 162 I18nizableText msg = new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_SUBJECT", subjectI18nParameters); 163 164 return _i18nUtils.translate(msg, page.getSitemapName()); 165 } 166 167 /** 168 * Get the email body 169 * @param sender The user responsible for the notification 170 * @param user The recipient of the notification 171 * @param page The published page 172 * @return The email body 173 */ 174 protected String _getBody (User sender, User user, Page page) 175 { 176 Site site = page.getSite(); 177 178 Map<String, I18nizableText> bodyI18nParameters = new HashMap<>(); 179 bodyI18nParameters.put("sender", new I18nizableText(sender.getFullName())); 180 bodyI18nParameters.put("user", new I18nizableText(user.getFullName())); 181 bodyI18nParameters.put("siteTitle", new I18nizableText(site.getTitle())); 182 bodyI18nParameters.put("siteUrl", new I18nizableText(site.getUrl())); 183 bodyI18nParameters.put("pageTitle", new I18nizableText(page.getTitle())); 184 bodyI18nParameters.put("pageUrl", new I18nizableText(site.getUrl() + "/" + page.getSitemap().getName() + "/" + page.getPathInSitemap() + ".html")); 185 186 I18nizableText msg = new I18nizableText("plugin.web", "PLUGINS_WEB_PAGE_NOTIFY_ONLINE_PUBLICATION_MAIL_BODY", bodyI18nParameters); 187 188 return _i18nUtils.translate(msg, page.getSitemapName()); 189 } 190 191 private Set<User> _getDistinctUsers (List<UserIdentity> userIdentities, List<GroupIdentity> groupIdentities) 192 { 193 Set<User> users = new HashSet<>(); 194 195 for (UserIdentity userIdentity : userIdentities) 196 { 197 User user = _userManager.getUser(userIdentity); 198 if (user != null) 199 { 200 users.add(user); 201 } 202 } 203 204 for (GroupIdentity groupIdentity : groupIdentities) 205 { 206 Group group = _groupManager.getGroup(groupIdentity); 207 if (group != null) 208 { 209 Set<UserIdentity> userGroupIdentities = group.getUsers(); 210 for (UserIdentity userGroupIdentity : userGroupIdentities) 211 { 212 User user = _userManager.getUser(userGroupIdentity); 213 if (user != null) 214 { 215 users.add(user); 216 } 217 } 218 } 219 } 220 221 return users; 222 } 223}