001/* 002 * Copyright 2017 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.observer; 017 018import java.io.IOException; 019import java.util.Collection; 020import java.util.Map; 021import java.util.Set; 022 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.observation.Event; 029import org.ametys.core.observation.Observer; 030import org.ametys.core.user.UserManager; 031import org.ametys.core.util.I18nUtils; 032import org.ametys.core.util.mail.SendMailHelper; 033import org.ametys.plugins.pagesubscription.PageSubscriptionDAO; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.runtime.plugin.component.AbstractLogEnabled; 036import org.ametys.web.repository.page.Page; 037import org.ametys.web.repository.site.Site; 038 039import jakarta.mail.MessagingException; 040 041/** 042 * Abstract implementation of the observers of Page Subscription. 043 */ 044public abstract class AbstractPageSubscriptionObserver extends AbstractLogEnabled implements Observer, Serviceable 045{ 046 /** The user manager */ 047 protected UserManager _userManager; 048 /** The Ametys object resolver */ 049 protected AmetysObjectResolver _resolver; 050 /** The i18n utils */ 051 protected I18nUtils _i18nUtils; 052 /** The subscription dao */ 053 protected PageSubscriptionDAO _subscriptionDAO; 054 055 @Override 056 public void service(ServiceManager smanager) throws ServiceException 057 { 058 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 059 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 060 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 061 _subscriptionDAO = (PageSubscriptionDAO) smanager.lookup(PageSubscriptionDAO.ROLE); 062 } 063 064 public int getPriority(Event event) 065 { 066 return Observer.MIN_PRIORITY; 067 } 068 069 @Override 070 public void observe(Event event, Map<String, Object> transientVars) 071 { 072 Collection<Page> pages = _getPages(event); 073 for (Page page : pages) 074 { 075 if (isNotificationEnabled(page)) 076 { 077 Set<String> subscribers = _subscriptionDAO.getSubscribers(page); 078 if (!subscribers.isEmpty()) 079 { 080 String textBody = _getMailBody(event, page); 081 String subject = _getMailSubject(event, page); 082 083 String sender = page.getSite().getValue("site-mail-from"); 084 085 for (String subscriberMail : subscribers) 086 { 087 if (StringUtils.isNotBlank(subscriberMail) && !_userManager.getUser(event.getIssuer()).getEmail().equals(subscriberMail)) 088 { 089 try 090 { 091 SendMailHelper.newMail() 092 .withSubject(subject) 093 .withTextBody(textBody) 094 .withSender(sender) 095 .withRecipient(subscriberMail) 096 .sendMail(); 097 } 098 catch (MessagingException | IOException e) 099 { 100 getLogger().error("Unable to send a mail to '" + subscriberMail); 101 } 102 } 103 } 104 } 105 } 106 } 107 } 108 109 /** 110 * Determines if the notification is currently enabled 111 * @param page The page 112 * @return true if the notification is enabled 113 */ 114 protected boolean isNotificationEnabled(Page page) 115 { 116 Site site = page.getSite(); 117 return site.getValue(getSiteParameterId(), true, false); 118 } 119 120 /** 121 * Id of the site parameter to enable/disable the notification 122 * @return The id of site 123 */ 124 protected abstract String getSiteParameterId(); 125 126 /** 127 * Get the absolute URL of a page 128 * @param page The page to retrieve the url 129 * @return The absolute url of the page 130 */ 131 protected String _getAbsolutePageUrl(Page page) 132 { 133 return new StringBuilder().append(page.getSite().getUrl()) 134 .append("/").append(page.getSitemapName()) 135 .append("/").append(page.getPathInSitemap()) 136 .append(".html").toString(); 137 } 138 139 /** 140 * Create the mail subject depend on the event 141 * @param event the event 142 * @param page the page 143 * @return the mail subject 144 */ 145 protected abstract String _getMailSubject(Event event, Page page); 146 147 /** 148 * Create the mail body depend on the event 149 * @param event the event 150 * @param page the page 151 * @return the mail body 152 */ 153 protected abstract String _getMailBody(Event event, Page page); 154 155 /** 156 * Get the pages linked to the event 157 * @param event the event 158 * @return the pages linked to the event 159 */ 160 protected abstract Collection<Page> _getPages(Event event); 161}