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.HashSet;
020import java.util.Map;
021import java.util.Set;
022
023import javax.jcr.Node;
024import javax.jcr.RepositoryException;
025import javax.jcr.Value;
026import javax.mail.MessagingException;
027
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.avalon.framework.service.Serviceable;
031
032import org.ametys.core.observation.Event;
033import org.ametys.core.observation.Observer;
034import org.ametys.core.user.UserManager;
035import org.ametys.core.util.I18nUtils;
036import org.ametys.core.util.mail.SendMailHelper;
037import org.ametys.plugins.repository.AmetysObjectResolver;
038import org.ametys.plugins.repository.RepositoryConstants;
039import org.ametys.plugins.repository.jcr.JCRAmetysObject;
040import org.ametys.runtime.config.Config;
041import org.ametys.runtime.plugin.component.AbstractLogEnabled;
042import org.ametys.web.repository.page.Page;
043import org.ametys.web.repository.site.Site;
044
045/**
046 * Abstract implementation of the observers of Page Subscription.
047 */
048public abstract class AbstractPageSubscriptionObserver extends AbstractLogEnabled implements Observer, Serviceable
049{
050    /** Constant for the attachment node name. */
051    public static final String SUBSCRIBERS_PROPERTY_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":subscriberMails";
052
053    /** The user manager */
054    protected UserManager _userManager;
055    /** The Ametys object resolver */
056    protected AmetysObjectResolver _resolver;
057    /** The i18n utils */
058    protected I18nUtils _i18nUtils;
059
060    @Override
061    public void service(ServiceManager smanager) throws ServiceException
062    {
063        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
064        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
065        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
066    }
067
068    public int getPriority(Event event)
069    {
070        return Observer.MIN_PRIORITY;
071    }
072
073    @Override
074    public void observe(Event event, Map<String, Object> transientVars)
075    {
076        Collection<Page> pages = _getPages(event);
077        for (Page page : pages)
078        {
079            if (isNotificationEnabled(page))
080            {
081                Set<String> subscribers = _getSubscribers((JCRAmetysObject) page);
082                if (!subscribers.isEmpty())
083                {
084                    String textBody = _getMailBody(event, page);
085                    String subject = _getMailSubject(event, page);
086
087                    String sender = Config.getInstance().getValue("smtp.mail.from");
088
089                    for (String subscriberMail : subscribers)
090                    {
091                        if (!_userManager.getUser(event.getIssuer()).getEmail().equals(subscriberMail))
092                        {
093                            try
094                            {
095                                SendMailHelper.sendMail(subject, null, textBody, subscriberMail, sender);
096                            }
097                            catch (MessagingException e)
098                            {
099                                getLogger().error("Unable to send a mail to '" + subscriberMail);
100                            }
101                        }
102                    }
103                }
104            }
105        }
106    }
107    
108    /**
109     * Determines if the notification is currently enabled
110     * @param page The page
111     * @return true if the notification is enabled
112     */
113    protected boolean isNotificationEnabled(Page page)
114    {
115        Site site = page.getSite();
116        return site.getValue(getSiteParameterId(), false, false);
117    }
118    
119    /**
120     * Id of the site parameter to enable/disable the notification
121     * @return The id of site
122     */
123    protected abstract String getSiteParameterId();
124
125    /**
126     * Get the page's subscribers
127     * @param page the page
128     * @return the registered emails
129     */
130    protected Set<String> _getSubscribers(JCRAmetysObject page)
131    {
132        Set<String> subscriberMails = new HashSet<>();
133
134        try
135        {
136            Node node = page.getNode();
137            if (node.hasProperty(SUBSCRIBERS_PROPERTY_NAME))
138            {
139                Value[] values = node.getProperty(SUBSCRIBERS_PROPERTY_NAME).getValues();
140                for (Value value : values)
141                {
142                    subscriberMails.add(value.getString());
143                }
144            }
145        }
146        catch (RepositoryException e)
147        {
148            getLogger().error("Unable to retrieve subscribers for page of id'" + page.getId() + "'", e);
149        }
150
151        return subscriberMails;
152    }
153
154    /**
155     * Get the absolute URL of a page
156     * @param page The page to retrieve the url
157     * @return The absolute url of the page
158     */
159    protected String _getAbsolutePageUrl(Page page)
160    {
161        return new StringBuilder().append(page.getSite().getUrl())
162                .append("/").append(page.getSitemapName())
163                .append("/").append(page.getPathInSitemap())
164                .append(".html").toString();
165    }
166
167    /**
168     * Create the mail subject depend on the event
169     * @param event the event
170     * @param page the page
171     * @return the mail subject
172     */
173    protected abstract String _getMailSubject(Event event, Page page);
174
175    /**
176     * Create the mail body depend on the event
177     * @param event the event
178     * @param page the page
179     * @return the mail body
180     */
181    protected abstract String _getMailBody(Event event, Page page);
182
183    /**
184     * Get the pages linked to the event
185     * @param event the event
186     * @return the pages linked to the event
187     */
188    protected abstract Collection<Page> _getPages(Event event);
189}