Merge branch '4-show-time-line' into 'master'

Resolve "Список периодических событий"

Closes #4

See merge request romanov73/ng-tracker!2
This commit is contained in:
Anton Romanov 2018-10-30 19:32:00 +00:00
commit 9013baa209
17 changed files with 616 additions and 3 deletions

View File

@ -1,12 +1,15 @@
package ru.ulstu.core.util; package ru.ulstu.core.util;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class StreamApiUtils { public class StreamApiUtils {
public static <T, R> List<T> convert(List<R> entitites, Function<R, T> converter) { public static <T, R> List<T> convert(List<R> entities, Function<R, T> converter) {
return entitites.stream().map(e -> converter.apply(e)).collect(Collectors.toList()); return entities == null
? Collections.EMPTY_LIST
: entities.stream().map(e -> converter.apply(e)).collect(Collectors.toList());
} }
} }

View File

@ -0,0 +1,51 @@
package ru.ulstu.timeline.controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.timeline.model.EventDto;
import ru.ulstu.timeline.service.EventService;
import java.util.List;
import static ru.ulstu.timeline.controller.EventController.URL;
@RestController
@RequestMapping(URL)
public class EventController {
public static final String URL = Constants.API_1_0 + "events";
private final EventService eventService;
public EventController(EventService eventService) {
this.eventService = eventService;
}
@GetMapping
public Response<List<EventDto>> getEvents() {
return new Response<>(eventService.findAll());
}
@PostMapping
public Response createEvent(@RequestBody EventDto timelineDto) {
return new Response(eventService.create(timelineDto));
}
@PutMapping
public Response updateEvent(@RequestBody EventDto eventDto) {
return new Response(eventService.update(eventDto));
}
@DeleteMapping("/{event-id}")
public Response delete(@PathVariable("event-id") Integer eventId) {
eventService.delete(eventId);
return new Response(true);
}
}

View File

@ -0,0 +1,85 @@
package ru.ulstu.timeline.model;
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 java.util.Date;
import java.util.List;
@Entity
public class Event extends BaseEntity {
@NotBlank
private String title;
@Column(name = "execute_date")
@Temporal(TemporalType.TIMESTAMP)
private Date executeDate;
@Column(name = "create_date")
@Temporal(TemporalType.TIMESTAMP)
private Date createDate;
@Column(name = "update_date")
@Temporal(TemporalType.TIMESTAMP)
private Date updateDate;
private String description;
@ManyToMany(fetch = FetchType.EAGER)
private List<User> recipients;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<User> getRecipients() {
return recipients;
}
public void setRecipients(List<User> recipients) {
this.recipients = recipients;
}
public Date getExecuteDate() {
return executeDate;
}
public void setExecuteDate(Date executeDate) {
this.executeDate = executeDate;
}
}

View File

@ -0,0 +1,75 @@
package ru.ulstu.timeline.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import ru.ulstu.user.model.UserDto;
import java.util.Date;
import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class EventDto {
private final Integer id;
private final String title;
private final Date executeDate;
private final Date createDate;
private final Date updateDate;
private final String description;
private final List<UserDto> recipients;
@JsonCreator
public EventDto(@JsonProperty("id") Integer id,
@JsonProperty("title") String title,
@JsonProperty("executeDate") Date executeDate,
@JsonProperty("createDate") Date createDate,
@JsonProperty("updateDate") Date updateDate,
@JsonProperty("description") String description,
@JsonProperty("recipients") List<UserDto> recipients) {
this.id = id;
this.title = title;
this.executeDate = executeDate;
this.createDate = createDate;
this.updateDate = updateDate;
this.description = description;
this.recipients = recipients;
}
public EventDto(Event event) {
this.id = event.getId();
this.title = event.getTitle();
this.executeDate = event.getExecuteDate();
this.createDate = event.getCreateDate();
this.updateDate = event.getUpdateDate();
this.description = event.getDescription();
this.recipients = convert(event.getRecipients(), UserDto::new);
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
public Date getCreateDate() {
return createDate;
}
public Date getUpdateDate() {
return updateDate;
}
public String getDescription() {
return description;
}
public List<UserDto> getRecipients() {
return recipients;
}
public Date getExecuteDate() {
return executeDate;
}
}

View File

@ -0,0 +1,25 @@
package ru.ulstu.timeline.model;
import ru.ulstu.core.model.BaseEntity;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import java.util.List;
@Entity
public class Timeline extends BaseEntity {
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "timeline_id")
private List<Event> events;
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
}

View File

@ -0,0 +1,33 @@
package ru.ulstu.timeline.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert;
public class TimelineDto {
private final Integer id;
private final List<EventDto> events;
@JsonCreator
public TimelineDto(@JsonProperty("id") Integer id,
@JsonProperty("events") List<EventDto> events) {
this.id = id;
this.events = events;
}
public TimelineDto(Timeline timeline) {
this.id = timeline.getId();
this.events = convert(timeline.getEvents(), EventDto::new);
}
public Integer getId() {
return id;
}
public List<EventDto> getEvents() {
return events;
}
}

View File

@ -0,0 +1,12 @@
package ru.ulstu.timeline.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import ru.ulstu.timeline.model.Event;
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();
}

View File

@ -0,0 +1,7 @@
package ru.ulstu.timeline.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.timeline.model.Timeline;
public interface TimelineRepository extends JpaRepository<Timeline, Integer> {
}

View File

@ -0,0 +1,78 @@
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.List;
import java.util.Map;
import static ru.ulstu.core.util.StreamApiUtils.convert;
@Service
public class EventService {
private final EventRepository eventRepository;
private final UserService userService;
private final MailService mailService;
public EventService(EventRepository eventRepository,
UserService userService,
MailService mailService) {
this.eventRepository = eventRepository;
this.userService = userService;
this.mailService = mailService;
}
public List<EventDto> findAll() {
return convert(eventRepository.findAll(), EventDto::new);
}
@Transactional
public int create(EventDto eventDto) {
return eventRepository.save(copyFromDto(new Event(), eventDto)).getId();
}
private Event copyFromDto(Event event, EventDto eventDto) {
event.setExecuteDate(eventDto.getExecuteDate());
event.setCreateDate(eventDto.getCreateDate());
event.setDescription(eventDto.getDescription());
event.setRecipients(userService.findByIds(convert(eventDto.getRecipients(), UserDto::getId)));
event.setTitle(eventDto.getTitle());
event.setUpdateDate(eventDto.getUpdateDate());
return event;
}
@Transactional
public Integer update(EventDto eventDto) {
Event event = eventRepository.findOne(eventDto.getId());
return eventRepository.save(copyFromDto(event, eventDto)).getId();
}
@Transactional
public void delete(Integer timelineId) {
Event event = eventRepository.findOne(timelineId);
eventRepository.delete(event);
}
public List<Event> findByIds(List<Integer> ids) {
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()));
});
}
}

View File

@ -0,0 +1,51 @@
package ru.ulstu.timeline.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.timeline.model.EventDto;
import ru.ulstu.timeline.model.Timeline;
import ru.ulstu.timeline.model.TimelineDto;
import ru.ulstu.timeline.repository.TimelineRepository;
import java.util.List;
import static ru.ulstu.core.util.StreamApiUtils.convert;
@Service
public class TimelineService {
private final TimelineRepository timelineRepository;
private final EventService eventService;
public TimelineService(TimelineRepository timelineRepository,
EventService eventService) {
this.timelineRepository = timelineRepository;
this.eventService = eventService;
}
public List<TimelineDto> findAll() {
return convert(timelineRepository.findAll(), TimelineDto::new);
}
@Transactional
public int create(TimelineDto timelineDto) {
return timelineRepository.save(copyFromDto(new Timeline(), timelineDto)).getId();
}
private Timeline copyFromDto(Timeline timeline, TimelineDto timelineDto) {
timeline.setEvents(eventService.findByIds(convert(timelineDto.getEvents(), EventDto::getId)));
return timeline;
}
@Transactional
public Integer update(TimelineDto timelineDto) {
Timeline timeline = timelineRepository.findOne(timelineDto.getId());
return timelineRepository.save(copyFromDto(timeline, timelineDto)).getId();
}
@Transactional
public void delete(Integer timelineId) {
Timeline timeline = timelineRepository.findOne(timelineId);
timelineRepository.delete(timeline);
}
}

View File

@ -15,6 +15,7 @@ import ru.ulstu.user.model.User;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map;
@Service @Service
public class MailService { public class MailService {
@ -66,6 +67,17 @@ public class MailService {
sendEmail(user.getEmail(), subject, content); sendEmail(user.getEmail(), subject, content);
} }
//Todo: выделить сервис нотификаций
@Async
public void sendEmailFromTemplate(Map<String, Object> variables, User user, String templateName, String subject) {
Context context = new Context();
variables.entrySet().forEach(entry -> context.setVariable(entry.getKey(), entry.getValue()));
context.setVariable(USER, user);
context.setVariable(BASE_URL, applicationProperties.getBaseUrl());
String content = templateEngine.process(templateName, context);
sendEmail(user.getEmail(), subject, content);
}
@Async @Async
public void sendActivationEmail(User user) { public void sendActivationEmail(User user) {
sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE); sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE);

View File

@ -282,4 +282,8 @@ public class UserService implements UserDetailsService {
.map(role -> new SimpleGrantedAuthority(role.getName())) .map(role -> new SimpleGrantedAuthority(role.getName()))
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
public List<User> findByIds(List<Integer> ids) {
return userRepository.findAll(ids);
}
} }

View File

@ -0,0 +1,44 @@
<?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="20181030_000000-1">
<createTable tableName="timeline">
<column name="id" type="integer">
<constraints nullable="false"/>
</column>
<column name="version" type="integer"/>
</createTable>
<addPrimaryKey columnNames="id" constraintName="pk_timeline" tableName="timeline"/>
</changeSet>
<changeSet author="orion" id="20181030_000000-4">
<createTable tableName="event">
<column name="id" type="integer">
<constraints nullable="false"/>
</column>
<column name="title" type="varchar(255)"/>
<column name="create_date" type="timestamp"/>
<column name="execute_date" type="timestamp"/>
<column name="update_date" type="timestamp"/>
<column name="description" type="varchar(255)"/>
<column name="timeline_id" type="integer"/>
<column name="version" type="integer"/>
</createTable>
<addPrimaryKey columnNames="id" constraintName="pk_event" tableName="event"/>
<addForeignKeyConstraint baseTableName="event" baseColumnNames="timeline_id"
constraintName="fk_event_timeline" referencedTableName="timeline"
referencedColumnNames="id"/>
</changeSet>
<changeSet author="orion" id="20181030_000000-5">
<createTable tableName="event_recipients">
<column name="event_id" type="integer"/>
<column name="recipients_id" type="integer"/>
</createTable>
<addForeignKeyConstraint baseTableName="event_recipients" baseColumnNames="event_id"
constraintName="fk_event_event_recipients" referencedTableName="event"
referencedColumnNames="id"/>
<addForeignKeyConstraint baseTableName="event_recipients" baseColumnNames="recipients_id"
constraintName="fk_user_event_recipients" referencedTableName="users"
referencedColumnNames="id"/>
</changeSet>
</databaseChangeLog>

View File

@ -12,4 +12,5 @@
<include file="db/changelog-20180428_110000-schema.xml"/> <include file="db/changelog-20180428_110000-schema.xml"/>
<include file="db/changelog-20180505_000000-schema.xml"/> <include file="db/changelog-20180505_000000-schema.xml"/>
<include file="db/changelog-20181027_000000-schema.xml"/> <include file="db/changelog-20181027_000000-schema.xml"/>
<include file="db/changelog-20181030_000000-schema.xml"/>
</databaseChangeLog> </databaseChangeLog>

View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Уведомление о событии</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="shortcut icon" th:href="@{|${baseUrl}/favicon.ico|}"/>
</head>
<body>
<p>
Уважаемый <span th:text="${user.firstName + ' ' + user.lastName}">Ivan Ivanov</span>
</p>
<p th:text="${description}">
some text
</p>
<p>
Regards,
<br/>
<em>Balance Team.</em>
</p>
</body>
</html>

View File

@ -102,7 +102,7 @@
</div> </div>
</div> </div>
<div class="col-md-4 col-sm-6 portfolio-item"> <div class="col-md-4 col-sm-6 portfolio-item">
<a class="portfolio-link" data-toggle="modal" href="./timeline"> <a class="portfolio-link" href="./timeline">
<div class="portfolio-hover"> <div class="portfolio-hover">
<div class="portfolio-hover-content"> <div class="portfolio-hover-content">
<i class="fa fa-arrow-right fa-3x"></i> <i class="fa fa-arrow-right fa-3x"></i>

View File

@ -0,0 +1,111 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default">
<head>
</head>
<body>
<div class="container" layout:fragment="content">
<section id="timeline">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading text-uppercase">События</h2>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<ul class="timeline">
<li>
<div class="timeline-image">
<h4>
<br/>
02.07.2018
</h4>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>Уход в отпуск</h4>
</div>
<div class="timeline-body">
<p class="text-muted">Наконец-то!</p>
</div>
</div>
</li>
<li class="timeline-inverted">
<div class="timeline-image">
<img class="rounded-circle img-fluid" src="img/about/2.jpg" alt=""/>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>March 2011</h4>
<h4 class="subheading">An Agency is Born</h4>
</div>
<div class="timeline-body">
<p class="text-muted">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt
ut
voluptatum eius sapiente, totam reiciendis temporibus qui quibusdam, recusandae
sit
vero unde, sed, incidunt et ea quo dolore laudantium consectetur!</p>
</div>
</div>
</li>
<li>
<div class="timeline-image">
<img class="rounded-circle img-fluid" src="img/about/3.jpg" alt=""/>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>December 2012</h4>
<h4 class="subheading">Transition to Full Service</h4>
</div>
<div class="timeline-body">
<p class="text-muted">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt
ut
voluptatum eius sapiente, totam reiciendis temporibus qui quibusdam, recusandae
sit
vero unde, sed, incidunt et ea quo dolore laudantium consectetur!</p>
</div>
</div>
</li>
<li class="timeline-inverted">
<div class="timeline-image">
<img class="rounded-circle img-fluid" src="img/about/4.jpg" alt=""/>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>July 2014</h4>
<h4 class="subheading">Phase Two Expansion</h4>
</div>
<div class="timeline-body">
<p class="text-muted">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt
ut
voluptatum eius sapiente, totam reiciendis temporibus qui quibusdam, recusandae
sit
vero unde, sed, incidunt et ea quo dolore laudantium consectetur!</p>
</div>
</div>
</li>
<li class="timeline-inverted">
<div class="timeline-image">
<h4>Be Part
<br/>Of Our
<br/>Story!</h4>
</div>
</li>
</ul>
</div>
</div>
</div>
</section>
<!-- Plugin JavaScript -->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Custom scripts for this template -->
<script src="js/agency.js"></script>
</div>
</body>
</html>