001/*
002 *  Copyright 2015 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.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.core.observation.Event;
025import org.ametys.core.user.User;
026import org.ametys.core.user.UserIdentity;
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.explorer.threads.jcr.JCRPost;
029import org.ametys.plugins.explorer.threads.jcr.JCRThread;
030import org.ametys.plugins.workspaces.project.objects.Project;
031import org.ametys.plugins.workspaces.threads.ThreadWorkspaceModule;
032
033/**
034 * This component is used to send mail notification
035 *
036 */
037public class ThreadsMailNotifierObserver extends AbstractSendNotificationObserver
038{
039    @Override
040    public boolean supports(Event event)
041    {
042        return event.getId().equals(ObservationConstants.EVENT_THREAD_POST_CREATED);
043    }
044    
045    @Override
046    protected String getUrl(Project project, String objectId)
047    {
048        return getModuleUrl(project, ThreadWorkspaceModule.THREAD_MODULE_ID, objectId);
049    }
050    
051    @Override
052    protected String getRightIdForNotify()
053    {
054        return "Plugins_Projects_Thread_Mail_Notification";
055    }
056    
057    @Override
058    protected void notifyEvent(Project project, String eventId, Map<String, Object> eventParams, User issuer)
059    {
060        // Subject
061        String mailSubjecti18nKey = "PROJECT_MAIL_NOTIFICATION_SUBJECT_" + StringUtils.replaceChars(eventId.toUpperCase(), '.', '_');
062        // Body
063        String mailBodyi18nKey = "PROJECT_MAIL_NOTIFICATION_BODY_" + StringUtils.replaceChars(eventId.toUpperCase(), '.', '_');
064        
065        JCRPost post = (JCRPost) eventParams.get(ObservationConstants.ARGS_POST);
066        JCRThread thread = post.getParent();
067        
068        if (thread != null)
069        {
070            List<String> mailSubjectParams = new ArrayList<>();
071            mailSubjectParams.add(project.getTitle());
072            mailSubjectParams.add(thread.getTitle());
073            
074            List<String> mailBodyParams = getMailCommonParams(project, issuer, thread.getId());
075
076            // {4} thread title
077            mailBodyParams.add(thread.getTitle());
078            
079            List<UserIdentity> recipients = getUsersToNotify(eventId, thread);
080            
081            sendMail(eventId, recipients, mailBodyi18nKey, mailSubjecti18nKey, mailBodyParams, mailSubjectParams);
082        }
083    }
084}