added period event

This commit is contained in:
Alyona 2018-11-09 14:48:21 +04:00
parent f449eeb999
commit 25707db6cd
5 changed files with 117 additions and 6 deletions

View File

@ -4,12 +4,7 @@ import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity;
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 javax.persistence.*;
import java.util.Date;
import java.util.List;
@ -18,6 +13,13 @@ public class Event extends BaseEntity {
@NotBlank
private String title;
@Enumerated(value = EnumType.STRING)
private PeriodEvent period;
@Column(name = "begin_date")
@Temporal(TemporalType.TIMESTAMP)
private Date beginDate;
@Column(name = "execute_date")
@Temporal(TemporalType.TIMESTAMP)
private Date executeDate;
@ -43,6 +45,14 @@ public class Event extends BaseEntity {
this.title = title;
}
public PeriodEvent getPeriod() {return period; }
public void setPeriod(PeriodEvent period) { this.period = period; }
public Date getBeginDate() {return beginDate; }
public void setBeginDate(Date beginDate){this.beginDate = beginDate; }
public Date getCreateDate() {
return createDate;
}

View File

@ -12,6 +12,8 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
public class EventDto {
private final Integer id;
private final String title;
private final PeriodEvent period;
private final Date beginDate;
private final Date executeDate;
private final Date createDate;
private final Date updateDate;
@ -21,6 +23,8 @@ public class EventDto {
@JsonCreator
public EventDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title,
@JsonProperty("period") PeriodEvent period,
@JsonProperty("beginDate") Date beginDate,
@JsonProperty("executeDate") Date executeDate,
@JsonProperty("createDate") Date createDate,
@JsonProperty("updateDate") Date updateDate,
@ -28,6 +32,8 @@ public class EventDto {
@JsonProperty("recipients") List<UserDto> recipients) {
this.id = id;
this.title = title;
this.period = period;
this.beginDate = beginDate;
this.executeDate = executeDate;
this.createDate = createDate;
this.updateDate = updateDate;
@ -38,6 +44,8 @@ public class EventDto {
public EventDto(Event event) {
this.id = event.getId();
this.title = event.getTitle();
this.period = event.getPeriod();
this.beginDate = event.getBeginDate();
this.executeDate = event.getExecuteDate();
this.createDate = event.getCreateDate();
this.updateDate = event.getUpdateDate();
@ -53,6 +61,10 @@ public class EventDto {
return title;
}
public PeriodEvent getPeriod() { return period; }
public Date getBeginDate() { return beginDate; }
public Date getCreateDate() {
return createDate;
}

View 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;
}
}

View File

@ -0,0 +1,63 @@
package ru.ulstu.timeline.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import ru.ulstu.core.util.DateUtils;
import ru.ulstu.timeline.model.Event;
import ru.ulstu.timeline.model.EventDto;
import ru.ulstu.timeline.repository.EventRepository;
import java.sql.Date;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
public class EventScheduler {
private final Logger log = LoggerFactory.getLogger(EventScheduler.class);
private final EventService eventService;
private final EventRepository eventRepository;
public EventScheduler(EventRepository eventRepository, EventService eventService) {
this.eventRepository = eventRepository;
this.eventService = eventService;
}
@Scheduled(cron = "0 0 8/12 * 1 ?")
public void checkPeriodEvents() {
log.debug("EventScheduler.checkPeriodEvents started");
for (Event event : eventRepository.findAll()) {
if(halfOfThePariodHasPassed(event)){
event.setCreateDate(Date.from((LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant())));
event.setBeginDate(DateUtils.addDays(event.getBeginDate(), event.getPeriod().getPeriod().getDays()));
event.setUpdateDate(null);
eventService.create(new EventDto(
event.getId(),
event.getTitle(),
event.getPeriod(),
event.getBeginDate(),
event.getExecuteDate(),
event.getCreateDate(),
event.getUpdateDate(),
event.getDescription(),
new ArrayList<>()
));
}
}
log.debug("EventScheduler.checkPeriodEvents finished");
}
private boolean halfOfThePariodHasPassed(Event event){
if (Period.between(event.getBeginDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
LocalDate.now()).getDays() <= event.getPeriod().getPeriod().getDays() / 2) {
return true;
}
return false;
}
}

View File

@ -46,6 +46,8 @@ public class EventService {
event.setDescription(eventDto.getDescription());
event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId)));
event.setTitle(eventDto.getTitle());
event.setPeriod(eventDto.getPeriod());
event.setBeginDate(eventDto.getBeginDate());
event.setUpdateDate(eventDto.getUpdateDate());
return event;
}