有两种方法可以解决这个问题。模型侦听器的方式有点棘手,因为文章和类别之间的关联是在模型侦听器被调用之后建立的。id 已经分配(article.getId()),但是需要一些技巧来克服这个问题。
直接的方法是创建一个服务包装器,调用原始代码,然后使用服务上下文来确定进一步的操作。
考虑使用 Liferay 消息总线上的异步消息将发送邮件的代码解耦。
如果您在 Liferay 中配置了 SMTP 服务器,您也可以使用MailService
从门户发送电子邮件。
查看示例代码
@Component(immediate = true, property = {}, service =
ServiceWrapper.class)
public class MailServiceWrapper extends JournalArticleServiceWrapper {
private static final Log _log = LogFactoryUtil.getLog(MailServiceWrapper.class);
@Reference
private MailService mailService;
@Reference
private AssetCategoryService assetCategoryService;
@Override
public JournalArticle addArticle(long groupId, long folderId, long classNameId, long classPK, String articleId, boolean autoArticleId, Map<Locale, String> titleMap, Map<Locale, String> descriptionMap, String content, String ddmStructureKey, String ddmTemplateKey, String layoutUuid, int displayDateMonth, int displayDateDay, int displayDateYear, int displayDateHour, int displayDateMinute, int expirationDateMonth, int expirationDateDay, int expirationDateYear, int expirationDateHour, int expirationDateMinute, boolean neverExpire, int reviewDateMonth, int reviewDateDay, int reviewDateYear, int reviewDateHour, int reviewDateMinute, boolean neverReview, boolean indexable, String articleURL, ServiceContext serviceContext) throws PortalException {
JournalArticle journalArticle = super.addArticle(groupId, folderId, classNameId, classPK, articleId, autoArticleId, titleMap, descriptionMap, content, ddmStructureKey, ddmTemplateKey, layoutUuid, displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute, expirationDateMonth, expirationDateDay, expirationDateYear, expirationDateHour, expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute, neverReview, indexable, articleURL, serviceContext);
sendNotification(serviceContext.getAssetCategoryIds(), journalArticle);
return journalArticle;
}
@Override
public JournalArticle addArticle(long groupId, long folderId, long classNameId, long classPK, String articleId, boolean autoArticleId, Map<Locale, String> titleMap, Map<Locale, String> descriptionMap, String content, String ddmStructureKey, String ddmTemplateKey, String layoutUuid, int displayDateMonth, int displayDateDay, int displayDateYear, int displayDateHour, int displayDateMinute, int expirationDateMonth, int expirationDateDay, int expirationDateYear, int expirationDateHour, int expirationDateMinute, boolean neverExpire, int reviewDateMonth, int reviewDateDay, int reviewDateYear, int reviewDateHour, int reviewDateMinute, boolean neverReview, boolean indexable, boolean smallImage, String smallImageURL, File smallFile, Map<String, byte[]> images, String articleURL, ServiceContext serviceContext) throws PortalException {
JournalArticle journalArticle = super.addArticle(groupId, folderId, classNameId, classPK, articleId, autoArticleId, titleMap, descriptionMap, content, ddmStructureKey, ddmTemplateKey, layoutUuid, displayDateMonth, displayDateDay, displayDateYear, displayDateHour, displayDateMinute, expirationDateMonth, expirationDateDay, expirationDateYear, expirationDateHour, expirationDateMinute, neverExpire, reviewDateMonth, reviewDateDay, reviewDateYear, reviewDateHour, reviewDateMinute, neverReview, indexable, smallImage, smallImageURL, smallFile, images, articleURL, serviceContext);
sendNotification(serviceContext.getAssetCategoryIds(), journalArticle);
return journalArticle;
}
private void sendNotification(long[] assetCategoryIds, JournalArticle journalArticle) throws PortalException {
// send mail
}
}