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.lang3.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 Map<String, Set<String>> getRecipientsByLanguage(Map transientVars, WorkflowAwareContent content, Set<String> rights) throws WorkflowException
058    {
059        String language = null;
060        String email = null;
061        
062        UserIdentity creator = content.getCreator();
063        if (!UserPopulationDAO.SYSTEM_USER_IDENTITY.equals(creator))
064        {
065            User user = _userManager.getUser(creator);
066            language = user.getLanguage();
067            email = user.getEmail();
068        }
069        else if (_cTypeHelper.isInstanceOf(content, UGCConstants.UGC_MIXIN_TYPE))
070        {
071            email = content.getValue(UGCConstants.ATTRIBUTE_PATH_UGC_MAIL, false, null);
072        }
073        
074        language = StringUtils.defaultIfBlank(language, _userLanguagesManager.getDefaultLanguage());
075        
076        return StringUtils.isNotBlank(email) ? Collections.singletonMap(language, Set.of(email)) : Collections.EMPTY_MAP;
077    }
078    
079    @Override
080    protected String getSender(Map transientVars, WorkflowAwareContent content) throws WorkflowException
081    {
082        if (content instanceof WebContent)
083        {
084            Site site = ((WebContent) content).getSite();
085            return site.getValue("site-mail-from", false, Config.getInstance().getValue("smtp.mail.from"));
086        }
087        else
088        {
089            return Config.getInstance().getValue("smtp.mail.from");
090        }
091    }
092    
093    @Override
094    protected List<String> getBodyI18nParams(User user, WorkflowAwareContent content)
095    {
096        List<String> params = new ArrayList<>();
097        
098        params.add(user != null ? user.getFullName() : ""); // {0}
099        params.add(_contentHelper.getTitle(content)); // {1}
100        
101        Site site = _getSite(content);
102        if (site != null)
103        {
104            params.add(site.getTitle());  // {2}
105        }
106        
107        params.add(_getContentUri(content, null, site));  // {3}
108        
109        // Creator {4}
110        if (user != null && !UserPopulationDAO.SYSTEM_USER_IDENTITY.equals(user.getIdentity()))
111        {
112            params.add(user.getFullName());
113        }
114        else if (_cTypeHelper.isInstanceOf(content, UGCConstants.UGC_MIXIN_TYPE) && content.hasValue(UGCConstants.ATTRIBUTE_PATH_UGC_MAIL))
115        {
116            params.add(content.getValue(UGCConstants.ATTRIBUTE_PATH_UGC_AUTHOR));
117        }
118        
119        return params;
120    }
121    
122    @Override
123    public I18nizableText getLabel()
124    {
125        return new I18nizableText("plugin.ugc", "PLUGINS_UGC_SEND_MAIL_TO_CREATOR_FUNCTION_LABEL");
126    }
127}