Skip to content

Commit

Permalink
Bugfix: Repeatable EmailSender
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Dec 29, 2023
1 parent 1c528d2 commit 9aa3bb2
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/samply/annotations/EmailSender.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.samply.annotations;

import de.samply.notification.smtp.EmailRecipientType;
import de.samply.notification.smtp.EmailTemplateType;
import de.samply.email.EmailRecipientType;
import de.samply.email.EmailTemplateType;

import java.lang.annotation.*;

Expand Down
62 changes: 39 additions & 23 deletions src/main/java/de/samply/aop/EmailSenderAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import de.samply.db.model.ProjectBridgehead;
import de.samply.db.model.ProjectBridgeheadUser;
import de.samply.db.repository.*;
import de.samply.notification.smtp.EmailRecipient;
import de.samply.notification.smtp.EmailService;
import de.samply.notification.smtp.EmailServiceException;
import de.samply.email.EmailRecipient;
import de.samply.email.EmailService;
import de.samply.email.EmailServiceException;
import de.samply.project.state.ProjectBridgeheadState;
import de.samply.security.SessionUser;
import de.samply.user.roles.OrganisationRoleToProjectRoleMapper;
Expand All @@ -24,6 +24,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.util.*;

@Component
Expand Down Expand Up @@ -57,41 +58,52 @@ public EmailSenderAspect(EmailService emailService,
this.projectRepository = projectRepository;
}

@Pointcut("@annotation(de.samply.annotations.EmailSenders)")
@Pointcut("@annotation(de.samply.annotations.EmailSender)")
public void emailSenderPointcut() {
}

@Pointcut("@annotation(de.samply.annotations.EmailSenders)")
public void emailSendersPointcut() {
}

@Around("emailSenderPointcut()")
public Object aroundEmailSender(ProceedingJoinPoint joinPoint) throws Throwable {
return aroundEmailSender(joinPoint, true);
}

@Around("emailSendersPointcut()")
public Object aroundEmailSenders(ProceedingJoinPoint joinPoint) throws Throwable {
return aroundEmailSender(joinPoint, false);
}

private <T extends Annotation> Object aroundEmailSender(ProceedingJoinPoint joinPoint, boolean isSingleEmailSender) throws Throwable {
try {
Optional<ResponseEntity> responseEntity = fetchResult(joinPoint);
if (responseEntity.isPresent() && responseEntity.get().getStatusCode().is2xxSuccessful()) {
sendEmail(joinPoint);
ResponseEntity responseEntity = (ResponseEntity) joinPoint.proceed();
if (responseEntity.getStatusCode().is2xxSuccessful()) {
sendEmail(joinPoint, isSingleEmailSender);
}
return responseEntity.get();
return responseEntity;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private Optional<ResponseEntity> fetchResult(ProceedingJoinPoint joinPoint) throws Throwable {
Object result = joinPoint.proceed();
if (result instanceof Optional) {
Object result2 = ((Optional) result).get();
if (result2 instanceof ResponseEntity) {
return (Optional<ResponseEntity>) result;
}
} else if (result instanceof ResponseEntity) {
return Optional.ofNullable((ResponseEntity) result);
private void sendEmail(ProceedingJoinPoint joinPoint, boolean isSingleEmailSender) {
if (isSingleEmailSender) {
sendEmailFromEmailSender(joinPoint, fetchEmailSender(joinPoint));
} else {
sendEmailFromEmailSenders(joinPoint, fetchEmailSenders(joinPoint));
}
return Optional.empty();
}

private void sendEmail(ProceedingJoinPoint joinPoint) {
fetchEmailSenders(joinPoint).ifPresent(emailSenders ->
Arrays.stream(emailSenders.value()).forEach(emailSender ->
fetchEmailRecipients(emailSender, joinPoint).forEach(emailRecipient ->
sendEmail(emailRecipient, emailSender))));
private void sendEmailFromEmailSenders(ProceedingJoinPoint joinPoint, Optional<EmailSenders> emailSendersOptional) {
emailSendersOptional.ifPresent(emailSenders -> Arrays.stream(emailSenders.value()).forEach(emailSender ->
sendEmailFromEmailSender(joinPoint, Optional.of(emailSender))));
}

private void sendEmailFromEmailSender(ProceedingJoinPoint joinPoint, Optional<EmailSender> emailSenderOptional) {
emailSenderOptional.ifPresent(emailSender -> fetchEmailRecipients(emailSender, joinPoint)
.forEach(emailRecipient -> sendEmail(emailRecipient, emailSender)));
}

private void sendEmail(EmailRecipient emailRecipient, EmailSender emailSender) {
Expand All @@ -102,6 +114,10 @@ private void sendEmail(EmailRecipient emailRecipient, EmailSender emailSender) {
}
}

private Optional<EmailSender> fetchEmailSender(JoinPoint joinPoint) {
return AspectUtils.fetchT(joinPoint, EmailSender.class);
}

private Optional<EmailSenders> fetchEmailSenders(JoinPoint joinPoint) {
return AspectUtils.fetchT(joinPoint, EmailSenders.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Object aroundRoleConstraints(ProceedingJoinPoint joinPoint) throws Throwa
Optional<String> projectCode = AspectUtils.fetchProjectCode(joinPoint);
Optional<String> bridgehead = AspectUtils.fetchBridghead(joinPoint);
Optional<ResponseEntity> result = this.constraintsService.checkProjectRoleConstraints(roleConstraints, projectCode, bridgehead);
return (result.isEmpty()) ? joinPoint.proceed() : result;
return (result.isEmpty()) ? joinPoint.proceed() : result.get();
}

private Optional<RoleConstraints> fetchRoleConstrains(JoinPoint joinPoint) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/samply/aop/StateConstraintsAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Object aroundStateConstraints(ProceedingJoinPoint joinPoint) throws Throw
Optional<String> projectCode = AspectUtils.fetchProjectCode(joinPoint);
Optional<String> bridghead = AspectUtils.fetchBridghead(joinPoint);
Optional<ResponseEntity> result = this.constraintsService.checkStateConstraints(stateConstraints, projectCode, bridghead);
return (result.isEmpty()) ? joinPoint.proceed() : result;
return (result.isEmpty()) ? joinPoint.proceed() : result.get();
}

private Optional<StateConstraints> fetchStateConstrains(JoinPoint joinPoint) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/de/samply/app/ProjectManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import de.samply.document.DocumentService;
import de.samply.document.DocumentServiceException;
import de.samply.document.DocumentType;
import de.samply.email.EmailRecipientType;
import de.samply.email.EmailTemplateType;
import de.samply.exporter.ExporterService;
import de.samply.frontend.FrontendService;
import de.samply.notification.smtp.EmailRecipientType;
import de.samply.notification.smtp.EmailTemplateType;
import de.samply.project.ProjectType;
import de.samply.project.event.ProjectEventActionsException;
import de.samply.project.event.ProjectEventService;
Expand Down Expand Up @@ -274,6 +274,7 @@ public ResponseEntity<String> archiveProject(

@RoleConstraints(organisationRoles = {OrganisationRole.PROJECT_MANAGER_ADMIN})
@StateConstraints(projectStates = {ProjectState.FINAL})
@EmailSender(templateType = EmailTemplateType.FINISHED_PROJECT, recipients = {EmailRecipientType.PROJECT_ALL})
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.PROJECT_STATE_MODULE)
@FrontendAction(action = ProjectManagerConst.FINISH_PROJECT_ACTION)
@PostMapping(value = ProjectManagerConst.FINISH_PROJECT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

import lombok.Getter;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

import com.fasterxml.jackson.annotation.JsonIgnore;
import de.samply.user.roles.ProjectRole;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

import de.samply.user.roles.ProjectRole;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

public enum EmailRecipientType {
SESSION_USER,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

import de.samply.app.ProjectManagerConst;
import de.samply.user.roles.ProjectRole;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

public class EmailServiceException extends Exception {

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/de/samply/email/EmailTemplateType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.samply.email;

public enum EmailTemplateType {
INVITATION,
NEW_PROJECT,
FINISHED_PROJECT
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

import com.fasterxml.jackson.annotation.JsonIgnore;
import de.samply.user.roles.ProjectRole;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

public record MessageSubject (String message, String subject) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

public record TemplateSubject (String template, String subject) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.samply.notification.smtp;
package de.samply.email;

import de.samply.app.ProjectManagerConst;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -48,5 +48,5 @@ public SpringTemplateEngine templateEngine() {
engine.addTemplateResolver(externalTemplateResolver());
return engine;
}

}

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/resources/email-templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"PROJECT_MANAGER_ADMIN": "project-manager-admin-new-project.html",
"DEFAULT": "default-new-project.html"
}
},
"FINISHED_PROJECT": {
"subject": "Project finished",
"files": {
"DEFAULT": "default-project-finished.html"
}
}
}
}
10 changes: 10 additions & 0 deletions src/main/resources/templates/default-project-finished.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Project finished</title>
</head>
<body>
<h1>The project is finished</h1>
</body>
</html>

0 comments on commit 9aa3bb2

Please sign in to comment.