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