diff --git a/src/main/java/ru/ulstu/timeline/model/Event.java b/src/main/java/ru/ulstu/timeline/model/Event.java index 45a2499..6021ccb 100644 --- a/src/main/java/ru/ulstu/timeline/model/Event.java +++ b/src/main/java/ru/ulstu/timeline/model/Event.java @@ -6,7 +6,10 @@ import ru.ulstu.user.model.User; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.ManyToMany; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import java.util.Date; import java.util.List; @@ -15,15 +18,21 @@ public class Event extends BaseEntity { @NotBlank private String title; + @Column(name = "execute_date") + @Temporal(TemporalType.DATE) + private Date executeDate; + @Column(name = "create_date") + @Temporal(TemporalType.TIMESTAMP) private Date createDate; @Column(name = "update_date") + @Temporal(TemporalType.TIMESTAMP) private Date updateDate; private String description; - @ManyToMany + @ManyToMany(fetch = FetchType.EAGER) private List recipients; public String getTitle() { @@ -65,4 +74,12 @@ public class Event extends BaseEntity { public void setRecipients(List recipients) { this.recipients = recipients; } + + public Date getExecuteDate() { + return executeDate; + } + + public void setExecuteDate(Date executeDate) { + this.executeDate = executeDate; + } } diff --git a/src/main/java/ru/ulstu/timeline/model/EventDto.java b/src/main/java/ru/ulstu/timeline/model/EventDto.java index 12d2457..59cef34 100644 --- a/src/main/java/ru/ulstu/timeline/model/EventDto.java +++ b/src/main/java/ru/ulstu/timeline/model/EventDto.java @@ -12,6 +12,7 @@ import static ru.ulstu.core.util.StreamApiUtils.convert; public class EventDto { private final Integer id; private final String title; + private final Date executeDate; private final Date createDate; private final Date updateDate; private final String description; @@ -20,12 +21,14 @@ public class EventDto { @JsonCreator public EventDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, + @JsonProperty("executeDate") Date executeDate, @JsonProperty("createDate") Date createDate, @JsonProperty("updateDate") Date updateDate, @JsonProperty("description") String description, @JsonProperty("recipients") List recipients) { this.id = id; this.title = title; + this.executeDate = executeDate; this.createDate = createDate; this.updateDate = updateDate; this.description = description; @@ -35,6 +38,7 @@ public class EventDto { public EventDto(Event event) { this.id = event.getId(); this.title = event.getTitle(); + this.executeDate = event.getExecuteDate(); this.createDate = event.getCreateDate(); this.updateDate = event.getUpdateDate(); this.description = event.getDescription(); @@ -64,4 +68,8 @@ public class EventDto { public List getRecipients() { return recipients; } + + public Date getExecuteDate() { + return executeDate; + } } diff --git a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java index 3442f10..6267d51 100644 --- a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java +++ b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java @@ -1,7 +1,12 @@ package ru.ulstu.timeline.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import ru.ulstu.timeline.model.Event; +import java.util.List; + public interface EventRepository extends JpaRepository { + @Query("SELECT e FROM Event e WHERE e.executeDate = CURRENT_DATE") + List findByCurrentDate(); } diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index b25f71b..92c8e20 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -1,15 +1,18 @@ package ru.ulstu.timeline.service; +import com.google.common.collect.ImmutableMap; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.timeline.model.Event; import ru.ulstu.timeline.model.EventDto; import ru.ulstu.timeline.repository.EventRepository; -import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; +import ru.ulstu.user.service.MailService; import ru.ulstu.user.service.UserService; import java.util.List; +import java.util.Map; import static ru.ulstu.core.util.StreamApiUtils.convert; @@ -18,11 +21,14 @@ public class EventService { private final EventRepository eventRepository; private final UserService userService; + private final MailService mailService; public EventService(EventRepository eventRepository, - UserService userService) { + UserService userService, + MailService mailService) { this.eventRepository = eventRepository; this.userService = userService; + this.mailService = mailService; } public List findAll() { @@ -35,6 +41,7 @@ public class EventService { } private Event copyFromDto(Event event, EventDto eventDto) { + event.setExecuteDate(eventDto.getExecuteDate()); event.setCreateDate(eventDto.getCreateDate()); event.setDescription(eventDto.getDescription()); event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId))); @@ -58,4 +65,14 @@ public class EventService { public List findByIds(List ids) { return eventRepository.findAll(ids); } + + @Scheduled(cron = "0 8 1 * * ?") + public void sendNotifications() { + List events = eventRepository.findByCurrentDate(); + events.forEach(event -> { + Map variables = ImmutableMap.of("description", event.getDescription()); + event.getRecipients() + .forEach(recipient -> mailService.sendEmailFromTemplate(variables, recipient, "eventNotification", event.getTitle())); + }); + } } diff --git a/src/main/java/ru/ulstu/user/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index 0708450..da1da6d 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -15,6 +15,7 @@ import ru.ulstu.user.model.User; import javax.mail.internet.MimeMessage; import java.nio.charset.StandardCharsets; +import java.util.Map; @Service public class MailService { @@ -66,6 +67,17 @@ public class MailService { sendEmail(user.getEmail(), subject, content); } + //Todo: выделить сервис нотификаций + @Async + public void sendEmailFromTemplate(Map variables, User user, String templateName, String subject) { + Context context = new Context(); + variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue())); + context.setVariable(USER, user); + context.setVariable(BASE_URL, applicationProperties.getBaseUrl()); + String content = templateEngine.process(templateName, context); + sendEmail(user.getEmail(), subject, content); + } + @Async public void sendActivationEmail(User user) { sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE); diff --git a/src/main/resources/mail_templates/eventNotification.html b/src/main/resources/mail_templates/eventNotification.html new file mode 100644 index 0000000..0500e26 --- /dev/null +++ b/src/main/resources/mail_templates/eventNotification.html @@ -0,0 +1,21 @@ + + + + Уведомление о событии + + + + +

+ Уважаемый Ivan Ivanov +

+

+ some text +

+

+ Regards, +
+ Balance Team. +

+ +