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