Skip to content

Commit

Permalink
Added: Send message while requesting changes in script or project
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Apr 15, 2024
1 parent 6b1496f commit 75935a3
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Send email to all involved users after a publication has been uploaded
- Project and project view url in email context
- Create notification after sending an email
- Send message while requesting changes in script or project
11 changes: 11 additions & 0 deletions src/main/java/de/samply/annotations/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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.PARAMETER)
public @interface Message {
}
12 changes: 9 additions & 3 deletions src/main/java/de/samply/aop/EmailSenderAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.samply.annotations.EmailSenderIfError;
import de.samply.annotations.EmailSenders;
import de.samply.annotations.EmailSendersIfError;
import de.samply.app.ProjectManagerConst;
import de.samply.db.model.Project;
import de.samply.db.model.ProjectBridgehead;
import de.samply.db.model.ProjectBridgeheadUser;
Expand Down Expand Up @@ -149,7 +150,8 @@ private void sendEmailFromEmailSenderIfError(ProceedingJoinPoint joinPoint, Opti

private void sendEmail(EmailRecipient emailRecipient, Supplier<EmailTemplateType> emailTemplateTypeSupplier) {
try {
emailService.sendEmail(emailRecipient.email(), emailRecipient.projectCode(), emailRecipient.bridgehead(), emailRecipient.role(), emailTemplateTypeSupplier.get());
Map<String, String> keyValues = (emailRecipient.getMessage().isPresent()) ? Map.of(ProjectManagerConst.EMAIL_CONTEXT_MESSAGE, emailRecipient.getMessage().get()) : new HashMap<>();
emailService.sendEmail(emailRecipient.getEmail(), emailRecipient.getProjectCode(), emailRecipient.getBridgehead(), emailRecipient.getRole(), emailTemplateTypeSupplier.get(), keyValues);
} catch (EmailServiceException e) {
throw new RuntimeException(e);
}
Expand All @@ -174,8 +176,9 @@ private Optional<EmailSendersIfError> fetchEmailSendersIfError(JoinPoint joinPoi
private Set<EmailRecipient> fetchEmailRecipients(Supplier<EmailRecipientType[]> emailRecipientTypesSupplier, ProceedingJoinPoint joinPoint) {
Set<EmailRecipient> result = new HashSet<>();
Optional<String> projectCode = AspectUtils.fetchProjectCode(joinPoint);
Optional<String> bridgehead = AspectUtils.fetchBridghead(joinPoint);
Optional<String> bridgehead = AspectUtils.fetchBridgehead(joinPoint);
Optional<String> email = AspectUtils.fetchEmail(joinPoint);
Optional<String> message = AspectUtils.fetchMessage(joinPoint);
Arrays.stream(emailRecipientTypesSupplier.get()).forEach(emailRecipientType ->
result.addAll(switch (emailRecipientType) {
case SESSION_USER -> fetchEmailRecipientsForSessionUser(projectCode, bridgehead);
Expand All @@ -191,6 +194,9 @@ private Set<EmailRecipient> fetchEmailRecipients(Supplier<EmailRecipientType[]>
case PROJECT_MANAGER_ADMIN -> fetchEmailRecipientsForProjectManagerAdmin(projectCode, bridgehead);
case PROJECT_ALL -> fetchEmailRecipientsForAllProjectUsers(projectCode, bridgehead);
}));
if (message.isPresent()) {
result.forEach(emailRecipient -> emailRecipient.setMessage(message));
}
return result;
}

Expand Down Expand Up @@ -361,7 +367,7 @@ private Set<EmailRecipient> fetchEmailRecipientsForAllProjectUsers(Optional<Stri
boolean addUser;
EmailRecipient emailRecipient = userEmailRecipientMap.get(projectBridgeheadUser.getEmail());
if (emailRecipient != null) {
addUser = ProjectRolesUtils.compare(emailRecipient.role(), projectBridgeheadUser.getProjectRole()) > 0;
addUser = ProjectRolesUtils.compare(emailRecipient.getRole(), projectBridgeheadUser.getProjectRole()) > 0;
} else {
addUser = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void roleConstraintsPointcut() {
public Object aroundRoleConstraints(ProceedingJoinPoint joinPoint) throws Throwable {
Optional<RoleConstraints> roleConstraints = fetchRoleConstrains(joinPoint);
Optional<String> projectCode = AspectUtils.fetchProjectCode(joinPoint);
Optional<String> bridgehead = AspectUtils.fetchBridghead(joinPoint);
Optional<String> bridgehead = AspectUtils.fetchBridgehead(joinPoint);
Optional<ResponseEntity> result = this.constraintsService.checkProjectRoleConstraints(roleConstraints, projectCode, bridgehead);
return (result.isEmpty()) ? joinPoint.proceed() : result.get();
}
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 @@ -30,7 +30,7 @@ public void stateConstraintsPointcut() {
public Object aroundStateConstraints(ProceedingJoinPoint joinPoint) throws Throwable {
Optional<StateConstraints> stateConstraints = fetchStateConstraints(joinPoint);
Optional<String> projectCode = AspectUtils.fetchProjectCode(joinPoint);
Optional<String> bridghead = AspectUtils.fetchBridghead(joinPoint);
Optional<String> bridghead = AspectUtils.fetchBridgehead(joinPoint);
Optional<ResponseEntity> result = this.constraintsService.checkStateConstraints(stateConstraints, projectCode, bridghead);
return (result.isEmpty()) ? joinPoint.proceed() : result.get();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/samply/app/ProjectManagerConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public class ProjectManagerConst {
public final static String PROJECT_CONFIGURATION = "project-configuration";
public final static String NOTIFICATION_ID = "notification-id";
public final static String BRIDGEHEAD = "bridgehead";
public final static String MESSAGE = "message";
public final static String BRIDGEHEADS = "bridgeheads";
public final static String EXPLORER_IDS = "explorer-ids";
public final static String PARTIAL_EMAIL = "partial-email";
Expand Down Expand Up @@ -214,6 +215,7 @@ public class ProjectManagerConst {
public final static String EMAIL_CONTEXT_BRIDGEHEAD = "bridgehead";
public final static String EMAIL_CONTEXT_PROJECT_CODE = "projectCode";
public final static String EMAIL_CONTEXT_PROJECT_VIEW_URL = "projectViewUrl";
public final static String EMAIL_CONTEXT_MESSAGE = "message";


// Application Properties
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/de/samply/app/ProjectManagerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import de.samply.project.ProjectType;
import de.samply.project.event.ProjectEventActionsException;
import de.samply.project.event.ProjectEventService;
import de.samply.project.state.ProjectBridgeheadState;
import de.samply.project.state.ProjectState;
import de.samply.query.OutputFormat;
import de.samply.query.QueryFormat;
Expand Down Expand Up @@ -552,7 +551,9 @@ public ResponseEntity<String> rejectScript(
@PostMapping(value = ProjectManagerConst.REQUEST_SCRIPT_CHANGES)
public ResponseEntity<String> requestChangesInScript(
@ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode,
@Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead
@Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead,
// Message is sent per email
@Message @RequestParam(name = ProjectManagerConst.MESSAGE, required = false) String message
) {
return convertToResponseEntity(() -> userService.requestChangesInProject(projectCode, bridgehead));
}
Expand Down Expand Up @@ -591,7 +592,9 @@ public ResponseEntity<String> rejectProjectResults(
@PostMapping(value = ProjectManagerConst.REQUEST_CHANGES_IN_PROJECT)
public ResponseEntity<String> requestChangesInProject(
@ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode,
@Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead
@Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD) String bridgehead,
// Message is sent per email
@Message @RequestParam(name = ProjectManagerConst.MESSAGE, required = false) String message
) {
return convertToResponseEntity(() -> userService.requestChangesInProject(projectCode, bridgehead));
}
Expand Down Expand Up @@ -753,7 +756,7 @@ public ResponseEntity<String> fetchScriptLabel(
@ProjectCode @RequestParam(name = ProjectManagerConst.PROJECT_CODE) String projectCode,
// bridgehead required for identifying developer user or bridgehead admin in role constraints
@Bridgehead @RequestParam(name = ProjectManagerConst.BRIDGEHEAD, required = false) String bridgehead
){
) {
return convertToResponseEntity(() -> this.documentService.fetchLabelOfLastDocumentOfThisType(projectCode, Optional.empty(), DocumentType.SCRIPT));
}

Expand Down
22 changes: 21 additions & 1 deletion src/main/java/de/samply/email/EmailRecipient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
package de.samply.email;

import de.samply.user.roles.ProjectRole;
import lombok.Getter;

import java.util.Optional;

public record EmailRecipient(String email, Optional<String> projectCode, Optional<String> bridgehead, ProjectRole role) {
@Getter
public class EmailRecipient {

private String email;
private Optional<String> projectCode;
private Optional<String> bridgehead;
private ProjectRole role;
private Optional<String> message = Optional.empty();

public EmailRecipient(String email, Optional<String> projectCode, Optional<String> bridgehead, ProjectRole role) {
this.email = email;
this.projectCode = projectCode;
this.bridgehead = bridgehead;
this.role = role;
}

public void setMessage(Optional<String> message) {
this.message = message;
}

}
7 changes: 6 additions & 1 deletion src/main/java/de/samply/email/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ public void sendEmail(@NotNull String email, Optional<String> project, Optional<
if (messageSubject.isPresent()) {
mailSender.send(createMimeMessage(email, emailFrom, messageSubject.get()));
if (project.isPresent()) {
String details = type.toString();
String message = keyValues.get(ProjectManagerConst.EMAIL_CONTEXT_MESSAGE);
if (message != null) {
details += " : " + message;
}
notificationService.createNotification(project.get(), bridgehead.isPresent() ? bridgehead.get() : null,
email, OperationType.SEND_EMAIL, type.toString(), null, null);
email, OperationType.SEND_EMAIL, details, null, null);
}
} else {
throw new EmailServiceException("Template not found for " + type.name() + " of role " + role.name());
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/de/samply/utils/AspectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.samply.annotations.Bridgehead;
import de.samply.annotations.Email;
import de.samply.annotations.Message;
import de.samply.annotations.ProjectCode;
import de.samply.db.model.Project;
import de.samply.db.repository.ProjectRepository;
Expand All @@ -19,7 +20,7 @@

public class AspectUtils {

public static Optional<String> fetchBridghead(JoinPoint joinPoint) {
public static Optional<String> fetchBridgehead(JoinPoint joinPoint) {
return fetchStringParameterAnnotation(joinPoint, Bridgehead.class);
}

Expand All @@ -31,6 +32,10 @@ public static Optional<String> fetchEmail(JoinPoint joinPoint) {
return fetchStringParameterAnnotation(joinPoint, Email.class);
}

public static Optional<String> fetchMessage(JoinPoint joinPoint) {
return fetchStringParameterAnnotation(joinPoint, Message.class);
}

private static Optional<String> fetchStringParameterAnnotation(JoinPoint joinPoint, Class annotationClass) {
Annotation[][] parameterAnnotations = fetchMethod(joinPoint).getParameterAnnotations();
Object[] args = joinPoint.getArgs();
Expand Down

0 comments on commit 75935a3

Please sign in to comment.