001/*
002 *  Copyright 2018 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.ugc.workflow;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.commons.lang.StringUtils;
027
028import org.ametys.cms.contenttype.ContentTypesHelper;
029import org.ametys.cms.repository.WorkflowAwareContent;
030import org.ametys.core.user.User;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.core.user.population.UserPopulationDAO;
033import org.ametys.plugins.ugc.UGCConstants;
034import org.ametys.runtime.config.Config;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.web.repository.content.WebContent;
037import org.ametys.web.repository.site.Site;
038import org.ametys.web.workflow.SendMailFunction;
039
040import com.opensymphony.workflow.WorkflowException;
041
042/**
043 * OS workflow function to send mail after an action is triggered.
044 */
045public class SendMailToCreatorFunction extends SendMailFunction
046{
047    private ContentTypesHelper _cTypeHelper;
048
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        super.service(smanager);
053        _cTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE);
054    }
055    
056    @Override
057    protected Set<String> getRecipients(Map transientVars, WorkflowAwareContent content, Set<String> rights) throws WorkflowException
058    {
059        String recipient = null; 
060                
061        UserIdentity creator = content.getCreator();
062        if (!UserPopulationDAO.SYSTEM_USER_IDENTITY.equals(creator))
063        {
064            User user = _userManager.getUser(creator);
065            recipient = user.getEmail();
066        }
067        else if (_cTypeHelper.isInstanceOf(content, UGCConstants.UGC_MIXIN_TYPE))
068        {
069            recipient = content.getValue(UGCConstants.ATTRIBUTE_PATH_UGC_MAIL, false, null);
070        }
071        
072        return StringUtils.isNotBlank(recipient) ? Collections.singleton(recipient) : Collections.EMPTY_SET;
073    }
074    
075    @Override
076    protected String getSender(Map transientVars, WorkflowAwareContent content) throws WorkflowException
077    {
078        if (content instanceof WebContent)
079        {
080            Site site = ((WebContent) content).getSite();
081            return site.getValue("site-mail-from", false, Config.getInstance().getValue("smtp.mail.from"));
082        }
083        else
084        {
085            return Config.getInstance().getValue("smtp.mail.from");
086        }
087    }
088    
089    @Override
090    protected List<String> getBodyI18nParams(User user, WorkflowAwareContent content)
091    {
092        List<String> params = new ArrayList<>();
093        
094        params.add(user != null ? user.getFullName() : ""); // {0}
095        params.add(_contentHelper.getTitle(content)); // {1}
096        
097        Site site = _getSite(content);
098        if (site != null)
099        {
100            params.add(site.getTitle());  // {2}
101        }
102        
103        params.add(_getContentUri(content, null, site));  // {3}
104        
105        // Creator {4}
106        if (user != null && !UserPopulationDAO.SYSTEM_USER_IDENTITY.equals(user.getIdentity()))
107        {
108            params.add(user.getFullName());
109        }
110        else if (_cTypeHelper.isInstanceOf(content, UGCConstants.UGC_MIXIN_TYPE) && content.hasValue(UGCConstants.ATTRIBUTE_PATH_UGC_MAIL))
111        {
112            params.add(content.getValue(UGCConstants.ATTRIBUTE_PATH_UGC_AUTHOR));
113        }
114        
115        return params;
116    }
117    
118    @Override
119    public I18nizableText getLabel()
120    {
121        return new I18nizableText("plugin.ugc", "PLUGINS_UGC_SEND_MAIL_TO_CREATOR_FUNCTION_LABEL");
122    }
123}