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