Merge remote-tracking branch 'origin/master'
commit
4c0ccbe285
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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)));
|
||||||
|
}
|
||||||
|
}
|
@ -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>
|
Loading…
Reference in New Issue