001/* 002 * Copyright 2019 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.observation; 017 018import java.text.DateFormat; 019import java.util.Collection; 020import java.util.Date; 021import java.util.HashMap; 022import java.util.Locale; 023import java.util.Map; 024 025import javax.mail.MessagingException; 026 027import org.apache.avalon.framework.configuration.Configurable; 028import org.apache.avalon.framework.configuration.Configuration; 029import org.apache.avalon.framework.configuration.ConfigurationException; 030import org.apache.avalon.framework.logger.AbstractLogEnabled; 031import org.apache.avalon.framework.service.ServiceException; 032import org.apache.avalon.framework.service.ServiceManager; 033import org.apache.avalon.framework.service.Serviceable; 034import org.apache.commons.lang.StringUtils; 035 036import org.ametys.cms.contenttype.ContentTypesHelper; 037import org.ametys.cms.repository.Content; 038import org.ametys.core.observation.Event; 039import org.ametys.core.observation.Observer; 040import org.ametys.core.util.I18nUtils; 041import org.ametys.core.util.mail.SendMailHelper; 042import org.ametys.plugins.ugc.UGCConstants; 043import org.ametys.runtime.config.Config; 044import org.ametys.runtime.i18n.I18nizableTextParameter; 045import org.ametys.runtime.i18n.I18nizableText; 046import org.ametys.web.repository.content.WebContent; 047import org.ametys.web.repository.page.Page; 048import org.ametys.web.repository.site.Site; 049 050/** 051 * Abstract observer to send mail to UGC author on UGC content event 052 * 053 */ 054public abstract class AbstractUGCContentObserver extends AbstractLogEnabled implements Observer, Serviceable, Configurable 055{ 056 /** The content type helper */ 057 protected ContentTypesHelper _cTypeHelper; 058 /** The i18n utils */ 059 protected I18nUtils _i18nUtils; 060 061 /** The i18n key for mail subject */ 062 protected String _subjectI18nKey; 063 /** The i18n key for mail body */ 064 protected String _bodyI18nKey; 065 066 @Override 067 public void service(ServiceManager smanager) throws ServiceException 068 { 069 _cTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE); 070 _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE); 071 } 072 073 public void configure(Configuration configuration) throws ConfigurationException 074 { 075 _subjectI18nKey = configuration.getChild("mailSubjectKey").getValue(null); 076 _bodyI18nKey = configuration.getChild("mailBodyKey").getValue(null); 077 } 078 079 @Override 080 public int getPriority(Event event) 081 { 082 return MIN_PRIORITY; 083 } 084 085 /** 086 * Send mail to the UGC author 087 * @param ugcContent The UGC content 088 * @param comment The comment. Can be null. 089 */ 090 protected void sendMailToUGCAuthor(Content ugcContent, String comment) 091 { 092 String recipient = ugcContent.getValue(UGCConstants.ATTRIBUTE_PATH_UGC_MAIL, false, null); 093 094 if (StringUtils.isNotBlank(recipient)) 095 { 096 String sender = Config.getInstance().getValue("smtp.mail.from"); 097 if (ugcContent instanceof WebContent) 098 { 099 Site site = ((WebContent) ugcContent).getSite(); 100 sender = site.getValue("site-mail-from", false, Config.getInstance().getValue("smtp.mail.from")); 101 } 102 103 String subject = getMailSubject(ugcContent); 104 String body = getMailBody(ugcContent, comment); 105 106 try 107 { 108 SendMailHelper.sendMail(subject, null, body, recipient, sender, true); 109 } 110 catch (MessagingException e) 111 { 112 getLogger().warn("Fail to send UGC content notification mail to " + recipient, e); 113 } 114 } 115 } 116 117 /** 118 * Get the mail subject 119 * @param ugcContent The UGC content 120 * @return the mail text subject 121 */ 122 protected String getMailSubject (Content ugcContent) 123 { 124 I18nizableText subjectKey = new I18nizableText(null, _subjectI18nKey, getSubjectI18nParams(ugcContent)); 125 return _i18nUtils.translate(subjectKey, null); 126 } 127 128 /** 129 * Get the mail body 130 * @param ugcContent The UGC content 131 * @param comment The comment. Can be null. 132 * @return the mail text body 133 */ 134 protected String getMailBody (Content ugcContent, String comment) 135 { 136 I18nizableText bodyKey = new I18nizableText(null, _bodyI18nKey, getBodyI18nParams(ugcContent, comment)); 137 return _i18nUtils.translate(bodyKey, null); 138 } 139 140 /** 141 * Get the i18n parameters for mail subject 142 * @param ugcContent The UGC content 143 * @return the i18n parameters for mail subject 144 */ 145 protected Map<String, I18nizableTextParameter> getSubjectI18nParams(Content ugcContent) 146 { 147 Map<String, I18nizableTextParameter> params = new HashMap<>(); 148 149 params.put("contentTitle", new I18nizableText(ugcContent.getTitle())); 150 if (ugcContent instanceof WebContent) 151 { 152 Site site = ((WebContent) ugcContent).getSite(); 153 params.put("siteTitle", new I18nizableText(site.getTitle())); 154 } 155 156 return params; 157 } 158 159 /** 160 * Get the i18n parameters for mail body 161 * @param ugcContent The UGC content 162 * @param comment The comment. Can be null. 163 * @return the i18n parameters for mail body 164 */ 165 protected Map<String, I18nizableTextParameter> getBodyI18nParams(Content ugcContent, String comment) 166 { 167 Map<String, I18nizableTextParameter> params = new HashMap<>(); 168 169 String authorFullName = ugcContent.getValue(UGCConstants.ATTRIBUTE_PATH_UGC_AUTHOR, false, null); 170 params.put("authorFullName", new I18nizableText(authorFullName)); 171 172 params.put("contentTitle", new I18nizableText(ugcContent.getTitle())); 173 174 if (ugcContent instanceof WebContent) 175 { 176 Site site = ((WebContent) ugcContent).getSite(); 177 params.put("siteTitle", new I18nizableText(site.getTitle())); 178 params.put("siteUrl", new I18nizableText(site.getUrl())); 179 180 Collection<Page> pages = ((WebContent) ugcContent).getReferencingPages(); 181 if (!pages.isEmpty()) 182 { 183 Page page = pages.iterator().next(); 184 String uri = site.getUrl() + "/" + page.getSitemapName() + "/" + page.getPathInSitemap() + ".html"; 185 params.put("uri", new I18nizableText(uri)); 186 } 187 } 188 189 Date creationDate = ugcContent.getCreationDate(); 190 DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT, new Locale(ugcContent.getLanguage())); 191 params.put("creationDate", new I18nizableText(df.format(creationDate))); 192 193 if (StringUtils.isNotBlank(comment)) 194 { 195 params.put("comment", new I18nizableText(comment)); 196 } 197 198 return params; 199 } 200}