refactor and db changes
This commit is contained in:
parent
376c820b44
commit
f0ac06c900
@ -13,6 +13,7 @@ import ru.ulstu.core.model.response.Response;
|
||||
import ru.ulstu.timeline.model.EventDto;
|
||||
import ru.ulstu.timeline.service.EventService;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
import static ru.ulstu.timeline.controller.EventController.URL;
|
||||
@ -30,16 +31,16 @@ public class EventController {
|
||||
|
||||
@GetMapping
|
||||
public Response<List<EventDto>> getEvents() {
|
||||
return new Response<>(eventService.findAll());
|
||||
return new Response<>(eventService.findAllDto());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Response createEvent(@RequestBody EventDto timelineDto) {
|
||||
public Response createEvent(@RequestBody @Valid EventDto timelineDto) {
|
||||
return new Response(eventService.create(timelineDto));
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Response updateEvent(@RequestBody EventDto eventDto) {
|
||||
public Response updateEvent(@RequestBody @Valid EventDto eventDto) {
|
||||
return new Response(eventService.update(eventDto));
|
||||
}
|
||||
|
||||
|
@ -4,15 +4,18 @@ import org.hibernate.validator.constraints.NotBlank;
|
||||
import ru.ulstu.core.model.BaseEntity;
|
||||
import ru.ulstu.user.model.User;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -38,15 +41,12 @@ public class Event extends BaseEntity {
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
private PeriodEvent period;
|
||||
|
||||
@Column(name = "begin_date")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date beginDate;
|
||||
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
private EventStatus status;
|
||||
|
||||
@Column(name = "execute_date")
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
@NotNull
|
||||
private Date executeDate;
|
||||
|
||||
@Column(name = "create_date")
|
||||
@ -62,6 +62,10 @@ public class Event extends BaseEntity {
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
private List<User> recipients;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "child_id")
|
||||
private Event child;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
@ -78,13 +82,13 @@ public class Event extends BaseEntity {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public PeriodEvent getPeriod() {return period;}
|
||||
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 void setPeriod(PeriodEvent period) {
|
||||
this.period = period;
|
||||
}
|
||||
|
||||
public Date getCreateDate() {
|
||||
return createDate;
|
||||
@ -125,4 +129,12 @@ public class Event extends BaseEntity {
|
||||
public void setExecuteDate(Date 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.JsonProperty;
|
||||
import org.hibernate.validator.constraints.NotBlank;
|
||||
import ru.ulstu.user.model.UserDto;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -11,10 +13,11 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||
|
||||
public class EventDto {
|
||||
private final Integer id;
|
||||
@NotBlank
|
||||
private final String title;
|
||||
private final PeriodEvent period;
|
||||
private final Date beginDate;
|
||||
private final Event.EventStatus status;
|
||||
@NotNull
|
||||
private final Date executeDate;
|
||||
private final Date createDate;
|
||||
private final Date updateDate;
|
||||
@ -26,7 +29,6 @@ public class EventDto {
|
||||
@JsonProperty("title") String title,
|
||||
@JsonProperty("status") Event.EventStatus status,
|
||||
@JsonProperty("period") PeriodEvent period,
|
||||
@JsonProperty("beginDate") Date beginDate,
|
||||
@JsonProperty("executeDate") Date executeDate,
|
||||
@JsonProperty("createDate") Date createDate,
|
||||
@JsonProperty("updateDate") Date updateDate,
|
||||
@ -35,7 +37,6 @@ public class EventDto {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
this.period = period;
|
||||
this.beginDate = beginDate;
|
||||
this.status = status;
|
||||
this.executeDate = executeDate;
|
||||
this.createDate = createDate;
|
||||
@ -49,7 +50,6 @@ public class EventDto {
|
||||
this.title = event.getTitle();
|
||||
this.status = event.getStatus();
|
||||
this.period = event.getPeriod();
|
||||
this.beginDate = event.getBeginDate();
|
||||
this.executeDate = event.getExecuteDate();
|
||||
this.createDate = event.getCreateDate();
|
||||
this.updateDate = event.getUpdateDate();
|
||||
@ -67,8 +67,6 @@ public class EventDto {
|
||||
|
||||
public PeriodEvent getPeriod() { return period; }
|
||||
|
||||
public Date getBeginDate() { return beginDate; }
|
||||
|
||||
public Event.EventStatus getStatus() { return status; }
|
||||
|
||||
public Date getCreateDate() {
|
||||
|
@ -9,4 +9,7 @@ import java.util.List;
|
||||
public interface EventRepository extends JpaRepository<Event, Integer> {
|
||||
@Query("SELECT e FROM Event e WHERE e.executeDate = CURRENT_DATE")
|
||||
List<Event> findByCurrentDate();
|
||||
|
||||
@Query("SELECT e FROM Event e WHERE e.executeDate > CURRENT_DATE")
|
||||
List<Event> findAllFuture();
|
||||
}
|
||||
|
@ -1,67 +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.EventDto;
|
||||
import ru.ulstu.timeline.repository.EventRepository;
|
||||
import ru.ulstu.user.model.UserDto;
|
||||
import ru.ulstu.timeline.model.PeriodEvent;
|
||||
import ru.ulstu.user.service.MailService;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
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 EventRepository eventRepository;
|
||||
private final MailService mailService;
|
||||
|
||||
public EventScheduler(EventRepository eventRepository, EventService eventService) {
|
||||
this.eventRepository = eventRepository;
|
||||
public EventScheduler(EventService eventService,
|
||||
MailService mailService) {
|
||||
this.eventService = eventService;
|
||||
this.mailService = mailService;
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 8/12 * 1 ?")
|
||||
@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 */2 * * * ?")
|
||||
public void checkPeriodEvents() {
|
||||
log.debug("EventScheduler.checkPeriodEvents started");
|
||||
for (Event event : eventRepository.findAll()) {
|
||||
for (Event event : eventService.findAllFuture()) {
|
||||
if (halfOfThePeriodHasPassed(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.getStatus(),
|
||||
event.getPeriod(),
|
||||
event.getBeginDate(),
|
||||
event.getExecuteDate(),
|
||||
event.getCreateDate(),
|
||||
event.getUpdateDate(),
|
||||
event.getDescription(),
|
||||
new ArrayList<>()
|
||||
));
|
||||
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) {
|
||||
if (Period.between(event.getBeginDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
|
||||
LocalDate.now()).getDays() <= event.getPeriod().getPeriod().getDays() / 2) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
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;
|
||||
|
||||
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.UserDto;
|
||||
import ru.ulstu.user.service.MailService;
|
||||
import ru.ulstu.user.service.UserService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||
|
||||
@ -21,18 +18,19 @@ public class EventService {
|
||||
|
||||
private final EventRepository eventRepository;
|
||||
private final UserService userService;
|
||||
private final MailService mailService;
|
||||
|
||||
public EventService(EventRepository eventRepository,
|
||||
UserService userService,
|
||||
MailService mailService) {
|
||||
UserService userService) {
|
||||
this.eventRepository = eventRepository;
|
||||
this.userService = userService;
|
||||
this.mailService = mailService;
|
||||
}
|
||||
|
||||
public List<EventDto> findAll() {
|
||||
return convert(eventRepository.findAll(), EventDto::new);
|
||||
public List<EventDto> findAllDto() {
|
||||
return convert(findAll(), EventDto::new);
|
||||
}
|
||||
|
||||
public List<Event> findAll() {
|
||||
return eventRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -42,14 +40,13 @@ public class EventService {
|
||||
|
||||
private Event copyFromDto(Event event, EventDto eventDto) {
|
||||
event.setExecuteDate(eventDto.getExecuteDate());
|
||||
event.setCreateDate(eventDto.getCreateDate());
|
||||
event.setCreateDate(eventDto.getId() == null ? new Date() : eventDto.getCreateDate());
|
||||
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.setStatus(eventDto.getStatus());
|
||||
event.setUpdateDate(eventDto.getUpdateDate());
|
||||
event.setUpdateDate(new Date());
|
||||
return event;
|
||||
}
|
||||
|
||||
@ -69,13 +66,28 @@ public class EventService {
|
||||
return eventRepository.findAll(ids);
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 8 * * ?")
|
||||
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()));
|
||||
});
|
||||
public void createBasedOn(Event event, Date executeDate) {
|
||||
//backup event id
|
||||
Integer parentEventId = event.getId();
|
||||
|
||||
event.setId(null);
|
||||
event.setStatus(Event.EventStatus.POSSIBLE);
|
||||
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-20181031_000000-schema.xml"/>
|
||||
<include file="db/changelog-20181108_000000-data.xml"/>
|
||||
<include file="db/changelog-20181111_000000-schema.xml"/>
|
||||
</databaseChangeLog>
|
Loading…
Reference in New Issue
Block a user