send notification
This commit is contained in:
parent
aa919906aa
commit
da1fbc3124
@ -6,7 +6,10 @@ import ru.ulstu.user.model.User;
|
|||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
import javax.persistence.ManyToMany;
|
import javax.persistence.ManyToMany;
|
||||||
|
import javax.persistence.Temporal;
|
||||||
|
import javax.persistence.TemporalType;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -15,15 +18,21 @@ public class Event extends BaseEntity {
|
|||||||
@NotBlank
|
@NotBlank
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
|
@Column(name = "execute_date")
|
||||||
|
@Temporal(TemporalType.DATE)
|
||||||
|
private Date executeDate;
|
||||||
|
|
||||||
@Column(name = "create_date")
|
@Column(name = "create_date")
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Date createDate;
|
private Date createDate;
|
||||||
|
|
||||||
@Column(name = "update_date")
|
@Column(name = "update_date")
|
||||||
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
private Date updateDate;
|
private Date updateDate;
|
||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@ManyToMany
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
private List<User> recipients;
|
private List<User> recipients;
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
@ -65,4 +74,12 @@ public class Event extends BaseEntity {
|
|||||||
public void setRecipients(List<User> recipients) {
|
public void setRecipients(List<User> recipients) {
|
||||||
this.recipients = recipients;
|
this.recipients = recipients;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date getExecuteDate() {
|
||||||
|
return executeDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExecuteDate(Date executeDate) {
|
||||||
|
this.executeDate = executeDate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
|
|||||||
public class EventDto {
|
public class EventDto {
|
||||||
private final Integer id;
|
private final Integer id;
|
||||||
private final String title;
|
private final String title;
|
||||||
|
private final Date executeDate;
|
||||||
private final Date createDate;
|
private final Date createDate;
|
||||||
private final Date updateDate;
|
private final Date updateDate;
|
||||||
private final String description;
|
private final String description;
|
||||||
@ -20,12 +21,14 @@ public class EventDto {
|
|||||||
@JsonCreator
|
@JsonCreator
|
||||||
public EventDto(@JsonProperty("id") Integer id,
|
public EventDto(@JsonProperty("id") Integer id,
|
||||||
@JsonProperty("title") String title,
|
@JsonProperty("title") String title,
|
||||||
|
@JsonProperty("executeDate") Date executeDate,
|
||||||
@JsonProperty("createDate") Date createDate,
|
@JsonProperty("createDate") Date createDate,
|
||||||
@JsonProperty("updateDate") Date updateDate,
|
@JsonProperty("updateDate") Date updateDate,
|
||||||
@JsonProperty("description") String description,
|
@JsonProperty("description") String description,
|
||||||
@JsonProperty("recipients") List<UserDto> recipients) {
|
@JsonProperty("recipients") List<UserDto> recipients) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
|
this.executeDate = executeDate;
|
||||||
this.createDate = createDate;
|
this.createDate = createDate;
|
||||||
this.updateDate = updateDate;
|
this.updateDate = updateDate;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
@ -35,6 +38,7 @@ public class EventDto {
|
|||||||
public EventDto(Event event) {
|
public EventDto(Event event) {
|
||||||
this.id = event.getId();
|
this.id = event.getId();
|
||||||
this.title = event.getTitle();
|
this.title = event.getTitle();
|
||||||
|
this.executeDate = event.getExecuteDate();
|
||||||
this.createDate = event.getCreateDate();
|
this.createDate = event.getCreateDate();
|
||||||
this.updateDate = event.getUpdateDate();
|
this.updateDate = event.getUpdateDate();
|
||||||
this.description = event.getDescription();
|
this.description = event.getDescription();
|
||||||
@ -64,4 +68,8 @@ public class EventDto {
|
|||||||
public List<UserDto> getRecipients() {
|
public List<UserDto> getRecipients() {
|
||||||
return recipients;
|
return recipients;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date getExecuteDate() {
|
||||||
|
return executeDate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package ru.ulstu.timeline.repository;
|
package ru.ulstu.timeline.repository;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import ru.ulstu.timeline.model.Event;
|
import ru.ulstu.timeline.model.Event;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface EventRepository extends JpaRepository<Event, Integer> {
|
public interface EventRepository extends JpaRepository<Event, Integer> {
|
||||||
|
@Query("SELECT e FROM Event e WHERE e.executeDate = CURRENT_DATE")
|
||||||
|
List<Event> findByCurrentDate();
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,18 @@
|
|||||||
package ru.ulstu.timeline.service;
|
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.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.ulstu.timeline.model.Event;
|
import ru.ulstu.timeline.model.Event;
|
||||||
import ru.ulstu.timeline.model.EventDto;
|
import ru.ulstu.timeline.model.EventDto;
|
||||||
import ru.ulstu.timeline.repository.EventRepository;
|
import ru.ulstu.timeline.repository.EventRepository;
|
||||||
import ru.ulstu.user.model.User;
|
|
||||||
import ru.ulstu.user.model.UserDto;
|
import ru.ulstu.user.model.UserDto;
|
||||||
|
import ru.ulstu.user.service.MailService;
|
||||||
import ru.ulstu.user.service.UserService;
|
import ru.ulstu.user.service.UserService;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
@ -18,11 +21,14 @@ public class EventService {
|
|||||||
|
|
||||||
private final EventRepository eventRepository;
|
private final EventRepository eventRepository;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
private final MailService mailService;
|
||||||
|
|
||||||
public EventService(EventRepository eventRepository,
|
public EventService(EventRepository eventRepository,
|
||||||
UserService userService) {
|
UserService userService,
|
||||||
|
MailService mailService) {
|
||||||
this.eventRepository = eventRepository;
|
this.eventRepository = eventRepository;
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
this.mailService = mailService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<EventDto> findAll() {
|
public List<EventDto> findAll() {
|
||||||
@ -35,6 +41,7 @@ public class EventService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Event copyFromDto(Event event, EventDto eventDto) {
|
private Event copyFromDto(Event event, EventDto eventDto) {
|
||||||
|
event.setExecuteDate(eventDto.getExecuteDate());
|
||||||
event.setCreateDate(eventDto.getCreateDate());
|
event.setCreateDate(eventDto.getCreateDate());
|
||||||
event.setDescription(eventDto.getDescription());
|
event.setDescription(eventDto.getDescription());
|
||||||
event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId)));
|
event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId)));
|
||||||
@ -58,4 +65,14 @@ public class EventService {
|
|||||||
public List<Event> findByIds(List<Integer> ids) {
|
public List<Event> findByIds(List<Integer> ids) {
|
||||||
return eventRepository.findAll(ids);
|
return eventRepository.findAll(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 8 1 * * ?")
|
||||||
|
public void sendNotifications() {
|
||||||
|
List<Event> events = eventRepository.findByCurrentDate();
|
||||||
|
events.forEach(event -> {
|
||||||
|
Map<String, Object> variables = ImmutableMap.of("description", event.getDescription());
|
||||||
|
event.getRecipients()
|
||||||
|
.forEach(recipient -> mailService.sendEmailFromTemplate(variables, recipient, "eventNotification", event.getTitle()));
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import ru.ulstu.user.model.User;
|
|||||||
|
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MailService {
|
public class MailService {
|
||||||
@ -66,6 +67,17 @@ public class MailService {
|
|||||||
sendEmail(user.getEmail(), subject, content);
|
sendEmail(user.getEmail(), subject, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Todo: выделить сервис нотификаций
|
||||||
|
@Async
|
||||||
|
public void sendEmailFromTemplate(Map<String, Object> 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
|
@Async
|
||||||
public void sendActivationEmail(User user) {
|
public void sendActivationEmail(User user) {
|
||||||
sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE);
|
sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE);
|
||||||
|
21
src/main/resources/mail_templates/eventNotification.html
Normal file
21
src/main/resources/mail_templates/eventNotification.html
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<title>Уведомление о событии</title>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||||
|
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>
|
||||||
|
Уважаемый <span th:text="${user.firstName + ' ' + user.lastName}">Ivan Ivanov</span>
|
||||||
|
</p>
|
||||||
|
<p th:text="${description}">
|
||||||
|
some text
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Regards,
|
||||||
|
<br/>
|
||||||
|
<em>Balance Team.</em>
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user