refactor and db changes

This commit is contained in:
Anton Romanov 2018-11-11 01:29:06 +04:00
parent 376c820b44
commit f0ac06c900
8 changed files with 137 additions and 79 deletions

View File

@ -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));
} }

View File

@ -4,15 +4,18 @@ import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import javax.persistence.*;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; 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;
@ -38,15 +41,12 @@ public class Event extends BaseEntity {
@Enumerated(value = EnumType.STRING) @Enumerated(value = EnumType.STRING)
private PeriodEvent period; private PeriodEvent period;
@Column(name = "begin_date")
@Temporal(TemporalType.TIMESTAMP)
private Date beginDate;
@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")
@ -62,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;
} }
@ -78,13 +82,13 @@ public class Event extends BaseEntity {
this.status = status; this.status = status;
} }
public PeriodEvent getPeriod() {return period;} public PeriodEvent getPeriod() {
return period;
}
public void setPeriod(PeriodEvent period){this.period = 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() { public Date getCreateDate() {
return createDate; return createDate;
@ -125,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;
}
} }

View File

@ -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,10 +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 PeriodEvent period;
private final Date beginDate;
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;
@ -26,7 +29,6 @@ public class EventDto {
@JsonProperty("title") String title, @JsonProperty("title") String title,
@JsonProperty("status") Event.EventStatus status, @JsonProperty("status") Event.EventStatus status,
@JsonProperty("period") PeriodEvent period, @JsonProperty("period") PeriodEvent period,
@JsonProperty("beginDate") Date beginDate,
@JsonProperty("executeDate") Date executeDate, @JsonProperty("executeDate") Date executeDate,
@JsonProperty("createDate") Date createDate, @JsonProperty("createDate") Date createDate,
@JsonProperty("updateDate") Date updateDate, @JsonProperty("updateDate") Date updateDate,
@ -35,7 +37,6 @@ public class EventDto {
this.id = id; this.id = id;
this.title = title; this.title = title;
this.period = period; this.period = period;
this.beginDate = beginDate;
this.status = status; this.status = status;
this.executeDate = executeDate; this.executeDate = executeDate;
this.createDate = createDate; this.createDate = createDate;
@ -49,7 +50,6 @@ public class EventDto {
this.title = event.getTitle(); this.title = event.getTitle();
this.status = event.getStatus(); this.status = event.getStatus();
this.period = event.getPeriod(); this.period = event.getPeriod();
this.beginDate = event.getBeginDate();
this.executeDate = event.getExecuteDate(); this.executeDate = event.getExecuteDate();
this.createDate = event.getCreateDate(); this.createDate = event.getCreateDate();
this.updateDate = event.getUpdateDate(); this.updateDate = event.getUpdateDate();
@ -67,8 +67,6 @@ public class EventDto {
public PeriodEvent getPeriod() { return period; } public PeriodEvent getPeriod() { return period; }
public Date getBeginDate() { return beginDate; }
public Event.EventStatus getStatus() { return status; } public Event.EventStatus getStatus() { return status; }
public Date getCreateDate() { public Date getCreateDate() {

View File

@ -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();
} }

View File

@ -1,67 +1,72 @@
package ru.ulstu.timeline.service; package ru.ulstu.timeline.service;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import ru.ulstu.core.util.DateUtils; import ru.ulstu.core.util.DateUtils;
import ru.ulstu.timeline.model.Event; import ru.ulstu.timeline.model.Event;
import ru.ulstu.timeline.model.EventDto; import ru.ulstu.timeline.model.PeriodEvent;
import ru.ulstu.timeline.repository.EventRepository; import ru.ulstu.user.service.MailService;
import ru.ulstu.user.model.UserDto;
import java.sql.Date; import java.util.Date;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Map;
@Service
public class EventScheduler { public class EventScheduler {
private final Logger log = LoggerFactory.getLogger(EventScheduler.class); private final Logger log = LoggerFactory.getLogger(EventScheduler.class);
private final EventService eventService; private final EventService eventService;
private final EventRepository eventRepository; private final MailService mailService;
public EventScheduler(EventRepository eventRepository, EventService eventService) { public EventScheduler(EventService eventService,
this.eventRepository = eventRepository; MailService mailService) {
this.eventService = eventService; 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() { public void checkPeriodEvents() {
log.debug("EventScheduler.checkPeriodEvents started"); log.debug("EventScheduler.checkPeriodEvents started");
for (Event event : eventRepository.findAll()) { for (Event event : eventService.findAllFuture()) {
if (halfOfThePeriodHasPassed(event)) { if (halfOfThePeriodHasPassed(event)) {
event.setCreateDate(Date.from((LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()))); eventService.createBasedOn(event, DateUtils.addDays(event.getExecuteDate(), getShiftInDays(event.getPeriod())));
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<>()
));
} }
} }
log.debug("EventScheduler.checkPeriodEvents finished"); 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) { private boolean halfOfThePeriodHasPassed(Event event) {
if (Period.between(event.getBeginDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), return event.getPeriod() != null && event.getChild() == null
LocalDate.now()).getDays() <= event.getPeriod().getPeriod().getDays() / 2) { && new Date().after(
return true; DateUtils.addDays(event.getExecuteDate(), (int) -Math.round((double) getShiftInDays(event.getPeriod()) / 2)));
}
return false;
} }
} }

View File

@ -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,14 +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.setPeriod(eventDto.getPeriod());
event.setBeginDate(eventDto.getBeginDate());
event.setStatus(eventDto.getStatus()); event.setStatus(eventDto.getStatus());
event.setUpdateDate(eventDto.getUpdateDate()); event.setUpdateDate(new Date());
return event; return event;
} }
@ -69,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();
} }
} }

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

View File

@ -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>