EmailNotificationAspect.java
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.component.aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.UuidModel;
import org.genesys.server.model.dataset.Dataset;
import org.genesys.server.model.impl.Subset;
import org.genesys.server.model.impl.User;
import org.genesys.server.model.traits.DescriptorList;
import org.genesys.server.service.ArticleService;
import org.genesys.server.service.ContentService;
import org.genesys.server.service.EMailService;
import org.genesys.server.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* @author Maxym Borodenko
*/
@Aspect
@Component
public class EmailNotificationAspect {
private final static Logger LOG = LoggerFactory.getLogger(EmailNotificationAspect.class);
@Value("${mail.helpdesk}")
private String helpdeskEmail;
@Value("${frontend.url}")
private String frontendUrl;
@Autowired
private EMailService emailService;
@Autowired
private ContentService contentService;
@Autowired
private ArticleService articleService;
@Autowired
private UserService userService;
@AfterReturning(value = "execution(public * *(..)) && @annotation(NotifyForReview)", returning = "result")
public void afterSentToReview(final Object result) {
if (result == null) {
LOG.warn("@NotifyOnReview received null object");
return;
}
// Create the root hash
final Map<String, Object> root = new HashMap<>();
if (result instanceof Dataset) {
root.put("publicationType", "dataset");
root.put("title", ((Dataset) result).getTitle());
} else if (result instanceof Subset) {
root.put("publicationType", "subset");
root.put("title", ((Subset) result).getTitle());
} else if (result instanceof DescriptorList) {
root.put("publicationType", "descriptor list");
root.put("title", ((DescriptorList) result).getTitle());
} else {
LOG.warn("@NotifyOnReview not supported for {}", result.getClass());
return;
}
root.put("uuid", ((UuidModel) result).getUuid().toString());
root.put("baseUrl", frontendUrl);
try {
var article = articleService.getGlobalArticle(ContentService.REVIEW_PUBLICATION, Locale.ENGLISH);
String body;
String title;
var articleTranslation = article.getTranslation();
if (articleTranslation != null) {
body = articleTranslation.getBody();
title = articleTranslation.getTitle();
} else {
body = article.getEntity().getBody();
title = article.getEntity().getTitle();
}
final String mailBody = contentService.processTemplate(body, root);
final String mailSubject = title;
LOG.debug(">>>{}", mailBody);
emailService.sendMail(mailSubject, mailBody, helpdeskEmail);
} catch (Throwable e) {
LOG.warn("Error processing @NotifyForReview: {}", e.getMessage(), e);
}
}
@AfterReturning(value = "execution(public * *(..)) && @annotation(NotifyOnPublished)", returning = "result")
public void afterPublishing(final Object result) {
if (result == null) {
LOG.warn("@NotifyOnPublished received null object");
return;
}
Long creatorId = ((AuditedVersionedModel) result).getCreatedBy();
if (creatorId == null) {
LOG.warn("Skipping @NotifyOnPublished, no createdBy for {}", result);
return;
}
// Create the root hash
final Map<String, Object> root = new HashMap<>();
if (result instanceof Dataset) {
root.put("publicationType", "dataset");
root.put("title", ((Dataset) result).getTitle());
} else if (result instanceof Subset) {
root.put("publicationType", "subset");
root.put("title", ((Subset) result).getTitle());
} else if (result instanceof DescriptorList) {
root.put("publicationType", "descriptor list");
root.put("title", ((DescriptorList) result).getTitle());
} else {
LOG.warn("@NotifyOnPublished not supported for {}", result.getClass());
return;
}
root.put("uuid", ((UuidModel) result).getUuid().toString());
root.put("baseUrl", frontendUrl);
try {
var article = articleService.getGlobalArticle(ContentService.PUBLISH_PUBLICATION, LocaleContextHolder.getLocale());
String body;
String title;
var articleTranslation = article.getTranslation();
if (articleTranslation != null) {
body = articleTranslation.getBody();
title = articleTranslation.getTitle();
} else {
body = article.getEntity().getBody();
title = article.getEntity().getTitle();
}
final String mailBody = contentService.processTemplate(body, root);
final String mailSubject = title;
LOG.debug(">>>{}", mailBody);
User user = userService.getUser(creatorId);
emailService.sendMail(mailSubject, mailBody, user.getEmail());
} catch (Throwable e) {
LOG.warn("Error processing @NotifyForReview: {}", e.getMessage(), e);
}
}
}