Skip to content

Commit

Permalink
Added: EmailSender repeatable
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Dec 29, 2023
1 parent e34713d commit f45812a
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Email Templates
- Email Sender Aspect
- Thymeleaf Template Engine Configuration
- EmailSender repeatable
6 changes: 2 additions & 4 deletions src/main/java/de/samply/annotations/EmailSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import de.samply.notification.smtp.EmailRecipientType;
import de.samply.notification.smtp.EmailTemplateType;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

@Repeatable(EmailSenders.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface EmailSender {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/de/samply/annotations/EmailSenders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.samply.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface EmailSenders {
EmailSender[] value();
}
32 changes: 24 additions & 8 deletions src/main/java/de/samply/aop/EmailSenderAspect.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.samply.aop;

import de.samply.annotations.EmailSender;
import de.samply.annotations.EmailSenders;
import de.samply.db.model.Project;
import de.samply.db.model.ProjectBridgehead;
import de.samply.db.model.ProjectBridgeheadUser;
Expand Down Expand Up @@ -55,26 +56,41 @@ public EmailSenderAspect(EmailService emailService,
this.projectRepository = projectRepository;
}

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

@Around("emailSenderPointcut()")
public Object aroundEmailSender(ProceedingJoinPoint joinPoint) throws Throwable {
try {
ResponseEntity responseEntity = (ResponseEntity) joinPoint.proceed();
if (responseEntity.getStatusCode().is2xxSuccessful()) {
Optional<ResponseEntity> responseEntity = fetchResult(joinPoint);
if (responseEntity.isPresent() && responseEntity.get().getStatusCode().is2xxSuccessful()) {
sendEmail(joinPoint);
}
return responseEntity;
return responseEntity.get();
} 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);
}
return Optional.empty();
}

private void sendEmail(ProceedingJoinPoint joinPoint) {
fetchEmailSender(joinPoint).ifPresent(emailSender ->
fetchEmailRecipients(emailSender, joinPoint).forEach(emailRecipient -> sendEmail(emailRecipient, emailSender)));
fetchEmailSenders(joinPoint).ifPresent(emailSenders ->
Arrays.stream(emailSenders.value()).forEach(emailSender ->
fetchEmailRecipients(emailSender, joinPoint).forEach(emailRecipient ->
sendEmail(emailRecipient, emailSender))));
}

private void sendEmail(EmailRecipient emailRecipient, EmailSender emailSender) {
Expand All @@ -85,8 +101,8 @@ 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);
}

private Set<EmailRecipient> fetchEmailRecipients(EmailSender emailSender, ProceedingJoinPoint joinPoint) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/de/samply/app/ProjectManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ public ResponseEntity<String> fetchActions(


@RoleConstraints(projectRoles = {ProjectRole.CREATOR, ProjectRole.PROJECT_MANAGER_ADMIN})
@StateConstraints(projectStates = {ProjectState.DRAFT, ProjectState.CREATED, ProjectState.ACCEPTED, ProjectState.DEVELOP})
@StateConstraints(projectStates = {ProjectState.DEVELOP})
@EmailSender(templateType = EmailTemplateType.INVITATION, recipients = {EmailRecipientType.EMAIL_ANNOTATION})
@EmailSender(templateType = EmailTemplateType.NEW_PROJECT, recipients = {EmailRecipientType.BRIDGEHEAD_ADMIN})
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE)
@FrontendAction(action = ProjectManagerConst.SET_DEVELOPER_USER_ACTION)
@PostMapping(value = ProjectManagerConst.SET_DEVELOPER_USER)
Expand All @@ -111,8 +112,9 @@ public ResponseEntity<String> setUserAsDeveloper(
}

@RoleConstraints(organisationRoles = {OrganisationRole.PROJECT_MANAGER_ADMIN})
@StateConstraints(projectStates = {ProjectState.ACCEPTED, ProjectState.DEVELOP, ProjectState.PILOT})
@StateConstraints(projectStates = {ProjectState.PILOT})
@EmailSender(templateType = EmailTemplateType.INVITATION, recipients = {EmailRecipientType.EMAIL_ANNOTATION})
@EmailSender(templateType = EmailTemplateType.NEW_PROJECT, recipients = {EmailRecipientType.BRIDGEHEAD_ADMIN})
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE)
@FrontendAction(action = ProjectManagerConst.SET_PILOT_USER_ACTION)
@PostMapping(value = ProjectManagerConst.SET_PILOT_USER)
Expand All @@ -126,7 +128,7 @@ public ResponseEntity<String> setUserAsPilot(
}

@RoleConstraints(organisationRoles = {OrganisationRole.PROJECT_MANAGER_ADMIN})
@StateConstraints(projectStates = {ProjectState.ACCEPTED, ProjectState.PILOT, ProjectState.FINAL})
@StateConstraints(projectStates = {ProjectState.FINAL})
@EmailSender(templateType = EmailTemplateType.INVITATION, recipients = {EmailRecipientType.EMAIL_ANNOTATION})
@FrontendSiteModule(site = ProjectManagerConst.PROJECT_VIEW_SITE, module = ProjectManagerConst.USER_MODULE)
@FrontendAction(action = ProjectManagerConst.SET_FINAL_USER_ACTION)
Expand Down

0 comments on commit f45812a

Please sign in to comment.