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