Merge branch '20-' into 'master'
Resolve "Реализовать периодические события" Closes #20 See merge request romanov73/ng-tracker!15
This commit is contained in:
commit
600d58d416
@ -13,6 +13,7 @@ import ru.ulstu.core.model.response.Response;
|
|||||||
import ru.ulstu.timeline.model.EventDto;
|
import ru.ulstu.timeline.model.EventDto;
|
||||||
import ru.ulstu.timeline.service.EventService;
|
import ru.ulstu.timeline.service.EventService;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static ru.ulstu.timeline.controller.EventController.URL;
|
import static ru.ulstu.timeline.controller.EventController.URL;
|
||||||
@ -30,16 +31,16 @@ public class EventController {
|
|||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public Response<List<EventDto>> getEvents() {
|
public Response<List<EventDto>> getEvents() {
|
||||||
return new Response<>(eventService.findAll());
|
return new Response<>(eventService.findAllDto());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Response createEvent(@RequestBody EventDto timelineDto) {
|
public Response createEvent(@RequestBody @Valid EventDto timelineDto) {
|
||||||
return new Response(eventService.create(timelineDto));
|
return new Response(eventService.create(timelineDto));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public Response updateEvent(@RequestBody EventDto eventDto) {
|
public Response updateEvent(@RequestBody @Valid EventDto eventDto) {
|
||||||
return new Response(eventService.update(eventDto));
|
return new Response(eventService.update(eventDto));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,9 +9,13 @@ import javax.persistence.Entity;
|
|||||||
import javax.persistence.EnumType;
|
import javax.persistence.EnumType;
|
||||||
import javax.persistence.Enumerated;
|
import javax.persistence.Enumerated;
|
||||||
import javax.persistence.FetchType;
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
import javax.persistence.ManyToMany;
|
import javax.persistence.ManyToMany;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
import javax.persistence.Temporal;
|
import javax.persistence.Temporal;
|
||||||
import javax.persistence.TemporalType;
|
import javax.persistence.TemporalType;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -34,11 +38,15 @@ public class Event extends BaseEntity {
|
|||||||
@NotBlank
|
@NotBlank
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
|
@Enumerated(value = EnumType.STRING)
|
||||||
|
private PeriodEvent period;
|
||||||
|
|
||||||
@Enumerated(value = EnumType.STRING)
|
@Enumerated(value = EnumType.STRING)
|
||||||
private EventStatus status;
|
private EventStatus status;
|
||||||
|
|
||||||
@Column(name = "execute_date")
|
@Column(name = "execute_date")
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
@NotNull
|
||||||
private Date executeDate;
|
private Date executeDate;
|
||||||
|
|
||||||
@Column(name = "create_date")
|
@Column(name = "create_date")
|
||||||
@ -54,6 +62,10 @@ public class Event extends BaseEntity {
|
|||||||
@ManyToMany(fetch = FetchType.EAGER)
|
@ManyToMany(fetch = FetchType.EAGER)
|
||||||
private List<User> recipients;
|
private List<User> recipients;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "child_id")
|
||||||
|
private Event child;
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
@ -62,7 +74,7 @@ public class Event extends BaseEntity {
|
|||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventStatus getStatus() {
|
public Event.EventStatus getStatus() {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,6 +82,14 @@ public class Event extends BaseEntity {
|
|||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PeriodEvent getPeriod() {
|
||||||
|
return period;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPeriod(PeriodEvent period) {
|
||||||
|
this.period = period;
|
||||||
|
}
|
||||||
|
|
||||||
public Date getCreateDate() {
|
public Date getCreateDate() {
|
||||||
return createDate;
|
return createDate;
|
||||||
}
|
}
|
||||||
@ -109,4 +129,12 @@ public class Event extends BaseEntity {
|
|||||||
public void setExecuteDate(Date executeDate) {
|
public void setExecuteDate(Date executeDate) {
|
||||||
this.executeDate = executeDate;
|
this.executeDate = executeDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Event getChild() {
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChild(Event child) {
|
||||||
|
this.child = child;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@ package ru.ulstu.timeline.model;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import org.hibernate.validator.constraints.NotBlank;
|
||||||
import ru.ulstu.user.model.UserDto;
|
import ru.ulstu.user.model.UserDto;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -11,8 +13,11 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
|
|||||||
|
|
||||||
public class EventDto {
|
public class EventDto {
|
||||||
private final Integer id;
|
private final Integer id;
|
||||||
|
@NotBlank
|
||||||
private final String title;
|
private final String title;
|
||||||
|
private final PeriodEvent period;
|
||||||
private final Event.EventStatus status;
|
private final Event.EventStatus status;
|
||||||
|
@NotNull
|
||||||
private final Date executeDate;
|
private final Date executeDate;
|
||||||
private final Date createDate;
|
private final Date createDate;
|
||||||
private final Date updateDate;
|
private final Date updateDate;
|
||||||
@ -23,6 +28,7 @@ public class EventDto {
|
|||||||
public EventDto(@JsonProperty("id") Integer id,
|
public EventDto(@JsonProperty("id") Integer id,
|
||||||
@JsonProperty("title") String title,
|
@JsonProperty("title") String title,
|
||||||
@JsonProperty("status") Event.EventStatus status,
|
@JsonProperty("status") Event.EventStatus status,
|
||||||
|
@JsonProperty("period") PeriodEvent period,
|
||||||
@JsonProperty("executeDate") Date executeDate,
|
@JsonProperty("executeDate") Date executeDate,
|
||||||
@JsonProperty("createDate") Date createDate,
|
@JsonProperty("createDate") Date createDate,
|
||||||
@JsonProperty("updateDate") Date updateDate,
|
@JsonProperty("updateDate") Date updateDate,
|
||||||
@ -30,6 +36,7 @@ public class EventDto {
|
|||||||
@JsonProperty("recipients") List<UserDto> recipients) {
|
@JsonProperty("recipients") List<UserDto> recipients) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
|
this.period = period;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.executeDate = executeDate;
|
this.executeDate = executeDate;
|
||||||
this.createDate = createDate;
|
this.createDate = createDate;
|
||||||
@ -42,6 +49,7 @@ public class EventDto {
|
|||||||
this.id = event.getId();
|
this.id = event.getId();
|
||||||
this.title = event.getTitle();
|
this.title = event.getTitle();
|
||||||
this.status = event.getStatus();
|
this.status = event.getStatus();
|
||||||
|
this.period = event.getPeriod();
|
||||||
this.executeDate = event.getExecuteDate();
|
this.executeDate = event.getExecuteDate();
|
||||||
this.createDate = event.getCreateDate();
|
this.createDate = event.getCreateDate();
|
||||||
this.updateDate = event.getUpdateDate();
|
this.updateDate = event.getUpdateDate();
|
||||||
@ -57,6 +65,8 @@ public class EventDto {
|
|||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PeriodEvent getPeriod() { return period; }
|
||||||
|
|
||||||
public Event.EventStatus getStatus() { return status; }
|
public Event.EventStatus getStatus() { return status; }
|
||||||
|
|
||||||
public Date getCreateDate() {
|
public Date getCreateDate() {
|
||||||
|
24
src/main/java/ru/ulstu/timeline/model/PeriodEvent.java
Normal file
24
src/main/java/ru/ulstu/timeline/model/PeriodEvent.java
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
package ru.ulstu.timeline.model;
|
||||||
|
|
||||||
|
import java.time.Period;
|
||||||
|
|
||||||
|
public enum PeriodEvent {
|
||||||
|
EVERY_YEAR(Period.ofYears(1), "Каждый год"),
|
||||||
|
EVERY_MONTH(Period.ofMonths(1), "Каждый месяц"),
|
||||||
|
EVERY_WEEK(Period.ofWeeks(1), "Каждую неделю"),
|
||||||
|
EVERY_DAY(Period.ofDays(1), "Каждый день");
|
||||||
|
|
||||||
|
private Period period;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
PeriodEvent(Period period, String message) {
|
||||||
|
this.period = period;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Period getPeriod() { return period; }
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
@ -9,4 +9,7 @@ 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")
|
@Query("SELECT e FROM Event e WHERE e.executeDate = CURRENT_DATE")
|
||||||
List<Event> findByCurrentDate();
|
List<Event> findByCurrentDate();
|
||||||
|
|
||||||
|
@Query("SELECT e FROM Event e WHERE e.executeDate > CURRENT_DATE")
|
||||||
|
List<Event> findAllFuture();
|
||||||
}
|
}
|
||||||
|
72
src/main/java/ru/ulstu/timeline/service/EventScheduler.java
Normal file
72
src/main/java/ru/ulstu/timeline/service/EventScheduler.java
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package ru.ulstu.timeline.service;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.core.util.DateUtils;
|
||||||
|
import ru.ulstu.timeline.model.Event;
|
||||||
|
import ru.ulstu.timeline.model.PeriodEvent;
|
||||||
|
import ru.ulstu.user.service.MailService;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class EventScheduler {
|
||||||
|
|
||||||
|
private final Logger log = LoggerFactory.getLogger(EventScheduler.class);
|
||||||
|
private final EventService eventService;
|
||||||
|
private final MailService mailService;
|
||||||
|
|
||||||
|
public EventScheduler(EventService eventService,
|
||||||
|
MailService mailService) {
|
||||||
|
this.eventService = eventService;
|
||||||
|
this.mailService = mailService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 0 8 * * ?")
|
||||||
|
public void sendNotifications() {
|
||||||
|
List<Event> events = eventService.findByCurrentDate();
|
||||||
|
events.forEach(event -> {
|
||||||
|
Map<String, Object> variables = ImmutableMap.of("description", event.getDescription());
|
||||||
|
event.getRecipients()
|
||||||
|
.forEach(recipient -> mailService.sendEmailFromTemplate(variables, recipient, "eventNotification", event.getTitle()));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "0 0 * * * ?")
|
||||||
|
public void checkPeriodEvents() {
|
||||||
|
log.debug("EventScheduler.checkPeriodEvents started");
|
||||||
|
for (Event event : eventService.findAllFuture()) {
|
||||||
|
if (halfOfThePeriodHasPassed(event)) {
|
||||||
|
eventService.createBasedOn(event, DateUtils.addDays(event.getExecuteDate(), getShiftInDays(event.getPeriod())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.debug("EventScheduler.checkPeriodEvents finished");
|
||||||
|
}
|
||||||
|
|
||||||
|
private int getShiftInDays(PeriodEvent periodEvent) {
|
||||||
|
switch (periodEvent) {
|
||||||
|
case EVERY_DAY:
|
||||||
|
return periodEvent.getPeriod().getDays();
|
||||||
|
case EVERY_WEEK:
|
||||||
|
return periodEvent.getPeriod().getDays();
|
||||||
|
case EVERY_MONTH:
|
||||||
|
return periodEvent.getPeriod().getMonths() * 30;
|
||||||
|
case EVERY_YEAR:
|
||||||
|
return periodEvent.getPeriod().getYears() * 365;
|
||||||
|
default:
|
||||||
|
throw new RuntimeException("period event not exists");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean halfOfThePeriodHasPassed(Event event) {
|
||||||
|
return event.getPeriod() != null && event.getChild() == null
|
||||||
|
&& new Date().after(
|
||||||
|
DateUtils.addDays(event.getExecuteDate(), (int) -Math.round((double) getShiftInDays(event.getPeriod()) / 2)));
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,15 @@
|
|||||||
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.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.Date;
|
||||||
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;
|
||||||
|
|
||||||
@ -21,18 +18,19 @@ 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> findAllDto() {
|
||||||
return convert(eventRepository.findAll(), EventDto::new);
|
return convert(findAll(), EventDto::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Event> findAll() {
|
||||||
|
return eventRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -42,12 +40,13 @@ public class EventService {
|
|||||||
|
|
||||||
private Event copyFromDto(Event event, EventDto eventDto) {
|
private Event copyFromDto(Event event, EventDto eventDto) {
|
||||||
event.setExecuteDate(eventDto.getExecuteDate());
|
event.setExecuteDate(eventDto.getExecuteDate());
|
||||||
event.setCreateDate(eventDto.getCreateDate());
|
event.setCreateDate(eventDto.getId() == null ? new Date() : 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)));
|
||||||
event.setTitle(eventDto.getTitle());
|
event.setTitle(eventDto.getTitle());
|
||||||
|
event.setPeriod(eventDto.getPeriod());
|
||||||
event.setStatus(eventDto.getStatus());
|
event.setStatus(eventDto.getStatus());
|
||||||
event.setUpdateDate(eventDto.getUpdateDate());
|
event.setUpdateDate(new Date());
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,13 +66,28 @@ public class EventService {
|
|||||||
return eventRepository.findAll(ids);
|
return eventRepository.findAll(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Scheduled(cron = "0 0 8 * * ?")
|
public void createBasedOn(Event event, Date executeDate) {
|
||||||
public void sendNotifications() {
|
//backup event id
|
||||||
List<Event> events = eventRepository.findByCurrentDate();
|
Integer parentEventId = event.getId();
|
||||||
events.forEach(event -> {
|
|
||||||
Map<String, Object> variables = ImmutableMap.of("description", event.getDescription());
|
event.setId(null);
|
||||||
event.getRecipients()
|
event.setStatus(Event.EventStatus.POSSIBLE);
|
||||||
.forEach(recipient -> mailService.sendEmailFromTemplate(variables, recipient, "eventNotification", event.getTitle()));
|
event.setExecuteDate(executeDate);
|
||||||
});
|
event.setCreateDate(new Date());
|
||||||
|
event.setUpdateDate(new Date());
|
||||||
|
event = eventRepository.save(event);
|
||||||
|
|
||||||
|
//set child to parent
|
||||||
|
Event parentEvent = eventRepository.findOne(parentEventId);
|
||||||
|
parentEvent.setChild(event);
|
||||||
|
eventRepository.save(parentEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Event> findByCurrentDate() {
|
||||||
|
return eventRepository.findByCurrentDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Event> findAllFuture() {
|
||||||
|
return eventRepository.findAllFuture();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
src/main/resources/db/changelog-20181111_000000-schema.xml
Normal file
26
src/main/resources/db/changelog-20181111_000000-schema.xml
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||||
|
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||||
|
<changeSet author="orion" id="20181111_000000-1">
|
||||||
|
<preConditions onFail="MARK_RAN">
|
||||||
|
<not><columnExists columnName="period" tableName="event"/></not>
|
||||||
|
</preConditions>
|
||||||
|
<addColumn tableName="event">
|
||||||
|
<column name="period" type="varchar(50)"/>
|
||||||
|
</addColumn>
|
||||||
|
</changeSet>
|
||||||
|
<changeSet author="orion" id="20181111_000000-2">
|
||||||
|
<preConditions onFail="MARK_RAN">
|
||||||
|
<not><columnExists columnName="child_id" tableName="event"/></not>
|
||||||
|
</preConditions>
|
||||||
|
<addColumn tableName="event">
|
||||||
|
<column name="child_id" type="integer"/>
|
||||||
|
</addColumn>
|
||||||
|
</changeSet>
|
||||||
|
<changeSet author="orion" id="20181111_000000-3">
|
||||||
|
<addForeignKeyConstraint baseTableName="event" baseColumnNames="child_id"
|
||||||
|
constraintName="fk_event_child_event" referencedTableName="event"
|
||||||
|
referencedColumnNames="id"/>
|
||||||
|
</changeSet>
|
||||||
|
</databaseChangeLog>
|
@ -15,4 +15,5 @@
|
|||||||
<include file="db/changelog-20181030_000000-schema.xml"/>
|
<include file="db/changelog-20181030_000000-schema.xml"/>
|
||||||
<include file="db/changelog-20181031_000000-schema.xml"/>
|
<include file="db/changelog-20181031_000000-schema.xml"/>
|
||||||
<include file="db/changelog-20181108_000000-data.xml"/>
|
<include file="db/changelog-20181108_000000-data.xml"/>
|
||||||
|
<include file="db/changelog-20181111_000000-schema.xml"/>
|
||||||
</databaseChangeLog>
|
</databaseChangeLog>
|
Loading…
Reference in New Issue
Block a user