From 970694451505a22b056f4501b1489a9ccc08f2d8 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 9 Jun 2018 22:41:00 +0400 Subject: [PATCH 01/11] fix ports --- src/main/resources/application.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ea20ef6..e5b11f2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,7 +1,7 @@ # Server Settings spring.main.banner-mode=off -server.port=8443 -server.http.port=8080 +server.port=443 +server.http.port=80 spring.http.multipart.maxFileSize=20MB spring.http.multipart.maxRequestSize=20MB # Thymeleaf Settings From 43917c043d422d1af60c78684bca545a9d99ec2c Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 9 Jun 2018 22:58:32 +0400 Subject: [PATCH 02/11] remove liquibase --- build.gradle | 3 - src/main/resources/application.properties | 6 +- .../db/changelog-20180301_130000-schema.xml | 15 ----- .../db/changelog-20180301_140000-data.xml | 38 ------------- .../db/changelog-20180301_140000-schema.xml | 55 ------------------- .../db/changelog-20180305_100000-schema.xml | 16 ------ .../db/changelog-20180321_193000-data.xml | 11 ---- .../db/changelog-20180405_110000-schema.xml | 43 --------------- .../db/changelog-20180428_110000-schema.xml | 15 ----- .../db/changelog-20180505_000000-schema.xml | 49 ----------------- src/main/resources/db/changelog-master.xml | 14 ----- 11 files changed, 1 insertion(+), 264 deletions(-) delete mode 100644 src/main/resources/db/changelog-20180301_130000-schema.xml delete mode 100644 src/main/resources/db/changelog-20180301_140000-data.xml delete mode 100644 src/main/resources/db/changelog-20180301_140000-schema.xml delete mode 100644 src/main/resources/db/changelog-20180305_100000-schema.xml delete mode 100644 src/main/resources/db/changelog-20180321_193000-data.xml delete mode 100644 src/main/resources/db/changelog-20180405_110000-schema.xml delete mode 100644 src/main/resources/db/changelog-20180428_110000-schema.xml delete mode 100644 src/main/resources/db/changelog-20180505_000000-schema.xml delete mode 100644 src/main/resources/db/changelog-master.xml diff --git a/build.gradle b/build.gradle index 13e3454..c396c77 100644 --- a/build.gradle +++ b/build.gradle @@ -104,9 +104,6 @@ dependencies { compile group: 'postgresql', name: 'postgresql', version: '9.1-901.jdbc4' - compile group: 'org.liquibase', name: 'liquibase-core', version: '3.5.3' - compile group: 'com.mattbertolini', name: 'liquibase-slf4j', version: '2.0.0' - compile group: 'org.apache.poi', name: 'poi', version: '3.9' compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.9' diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e5b11f2..a2821bd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -26,11 +26,7 @@ spring.datasource.url=jdbc:postgresql://localhost:5432/ng-tracker spring.datasource.username=postgres spring.datasource.password=postgres spring.datasource.driverclassName=org.postgresql.Driver -spring.jpa.hibernate.ddl-auto=validate -# Liquibase Settings -liquibase.drop-first=false -liquibase.enabled=true -liquibase.change-log=classpath:db/changelog-master.xml +spring.jpa.hibernate.ddl-auto=update # Application Settings ng-tracker.base-url=https://127.0.0.1:8443 ng-tracker.undead-user-login=admin diff --git a/src/main/resources/db/changelog-20180301_130000-schema.xml b/src/main/resources/db/changelog-20180301_130000-schema.xml deleted file mode 100644 index 47c29e2..0000000 --- a/src/main/resources/db/changelog-20180301_130000-schema.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog-20180301_140000-data.xml b/src/main/resources/db/changelog-20180301_140000-data.xml deleted file mode 100644 index 6d23cba..0000000 --- a/src/main/resources/db/changelog-20180301_140000-data.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog-20180301_140000-schema.xml b/src/main/resources/db/changelog-20180301_140000-schema.xml deleted file mode 100644 index 07ab917..0000000 --- a/src/main/resources/db/changelog-20180301_140000-schema.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog-20180305_100000-schema.xml b/src/main/resources/db/changelog-20180305_100000-schema.xml deleted file mode 100644 index a9e1577..0000000 --- a/src/main/resources/db/changelog-20180305_100000-schema.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog-20180321_193000-data.xml b/src/main/resources/db/changelog-20180321_193000-data.xml deleted file mode 100644 index d4d9812..0000000 --- a/src/main/resources/db/changelog-20180321_193000-data.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - update users - set password_hash='$2a$10$5UCtAX/UcNSAcLnHUJDqUO6GR4hyPwCzBfuFI81nsoaYkvNF9SGxG' where id = 1; - - - \ No newline at end of file diff --git a/src/main/resources/db/changelog-20180405_110000-schema.xml b/src/main/resources/db/changelog-20180405_110000-schema.xml deleted file mode 100644 index e174300..0000000 --- a/src/main/resources/db/changelog-20180405_110000-schema.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog-20180428_110000-schema.xml b/src/main/resources/db/changelog-20180428_110000-schema.xml deleted file mode 100644 index 2f1917f..0000000 --- a/src/main/resources/db/changelog-20180428_110000-schema.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog-20180505_000000-schema.xml b/src/main/resources/db/changelog-20180505_000000-schema.xml deleted file mode 100644 index 8f5240d..0000000 --- a/src/main/resources/db/changelog-20180505_000000-schema.xml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml deleted file mode 100644 index 4faae2e..0000000 --- a/src/main/resources/db/changelog-master.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - \ No newline at end of file From aa919906aa9c7f04c9363f054590183c86af8da3 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sun, 10 Jun 2018 01:12:28 +0400 Subject: [PATCH 03/11] add event controller --- .../ru/ulstu/core/util/StreamApiUtils.java | 7 +- .../timeline/controller/EventController.java | 51 ++++++++++++++ .../java/ru/ulstu/timeline/model/Event.java | 68 +++++++++++++++++++ .../ru/ulstu/timeline/model/EventDto.java | 67 ++++++++++++++++++ .../ru/ulstu/timeline/model/Timeline.java | 25 +++++++ .../ru/ulstu/timeline/model/TimelineDto.java | 33 +++++++++ .../timeline/repository/EventRepository.java | 7 ++ .../repository/TimelineRepository.java | 7 ++ .../ulstu/timeline/service/EventService.java | 61 +++++++++++++++++ .../timeline/service/TimelineService.java | 51 ++++++++++++++ .../ru/ulstu/user/service/UserService.java | 4 ++ 11 files changed, 379 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ru/ulstu/timeline/controller/EventController.java create mode 100644 src/main/java/ru/ulstu/timeline/model/Event.java create mode 100644 src/main/java/ru/ulstu/timeline/model/EventDto.java create mode 100644 src/main/java/ru/ulstu/timeline/model/Timeline.java create mode 100644 src/main/java/ru/ulstu/timeline/model/TimelineDto.java create mode 100644 src/main/java/ru/ulstu/timeline/repository/EventRepository.java create mode 100644 src/main/java/ru/ulstu/timeline/repository/TimelineRepository.java create mode 100644 src/main/java/ru/ulstu/timeline/service/EventService.java create mode 100644 src/main/java/ru/ulstu/timeline/service/TimelineService.java diff --git a/src/main/java/ru/ulstu/core/util/StreamApiUtils.java b/src/main/java/ru/ulstu/core/util/StreamApiUtils.java index 282601c..e1fb55a 100644 --- a/src/main/java/ru/ulstu/core/util/StreamApiUtils.java +++ b/src/main/java/ru/ulstu/core/util/StreamApiUtils.java @@ -1,12 +1,15 @@ package ru.ulstu.core.util; +import java.util.Collections; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class StreamApiUtils { - public static List convert(List entitites, Function converter) { - return entitites.stream().map(e -> converter.apply(e)).collect(Collectors.toList()); + public static List convert(List entities, Function converter) { + return entities == null + ? Collections.EMPTY_LIST + : entities.stream().map(e -> converter.apply(e)).collect(Collectors.toList()); } } diff --git a/src/main/java/ru/ulstu/timeline/controller/EventController.java b/src/main/java/ru/ulstu/timeline/controller/EventController.java new file mode 100644 index 0000000..6f79b31 --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/controller/EventController.java @@ -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> 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); + } +} diff --git a/src/main/java/ru/ulstu/timeline/model/Event.java b/src/main/java/ru/ulstu/timeline/model/Event.java new file mode 100644 index 0000000..45a2499 --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/model/Event.java @@ -0,0 +1,68 @@ +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.ManyToMany; +import java.util.Date; +import java.util.List; + +@Entity +public class Event extends BaseEntity { + @NotBlank + private String title; + + @Column(name = "create_date") + private Date createDate; + + @Column(name = "update_date") + private Date updateDate; + + private String description; + + @ManyToMany + private List 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 getRecipients() { + return recipients; + } + + public void setRecipients(List recipients) { + this.recipients = recipients; + } +} diff --git a/src/main/java/ru/ulstu/timeline/model/EventDto.java b/src/main/java/ru/ulstu/timeline/model/EventDto.java new file mode 100644 index 0000000..12d2457 --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/model/EventDto.java @@ -0,0 +1,67 @@ +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 createDate; + private final Date updateDate; + private final String description; + private final List recipients; + + @JsonCreator + public EventDto(@JsonProperty("id") Integer id, + @JsonProperty("title") String title, + @JsonProperty("createDate") Date createDate, + @JsonProperty("updateDate") Date updateDate, + @JsonProperty("description") String description, + @JsonProperty("recipients") List recipients) { + this.id = id; + this.title = title; + 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.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 getRecipients() { + return recipients; + } +} diff --git a/src/main/java/ru/ulstu/timeline/model/Timeline.java b/src/main/java/ru/ulstu/timeline/model/Timeline.java new file mode 100644 index 0000000..8e588cb --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/model/Timeline.java @@ -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 events; + + public List getEvents() { + return events; + } + + public void setEvents(List events) { + this.events = events; + } +} diff --git a/src/main/java/ru/ulstu/timeline/model/TimelineDto.java b/src/main/java/ru/ulstu/timeline/model/TimelineDto.java new file mode 100644 index 0000000..1c651a7 --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/model/TimelineDto.java @@ -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 events; + + @JsonCreator + public TimelineDto(@JsonProperty("id") Integer id, + @JsonProperty("events") List 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 getEvents() { + return events; + } +} diff --git a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java new file mode 100644 index 0000000..3442f10 --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java @@ -0,0 +1,7 @@ +package ru.ulstu.timeline.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.timeline.model.Event; + +public interface EventRepository extends JpaRepository { +} diff --git a/src/main/java/ru/ulstu/timeline/repository/TimelineRepository.java b/src/main/java/ru/ulstu/timeline/repository/TimelineRepository.java new file mode 100644 index 0000000..6b1be39 --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/repository/TimelineRepository.java @@ -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 { +} diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java new file mode 100644 index 0000000..b25f71b --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -0,0 +1,61 @@ +package ru.ulstu.timeline.service; + +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.User; +import ru.ulstu.user.model.UserDto; +import ru.ulstu.user.service.UserService; + +import java.util.List; + +import static ru.ulstu.core.util.StreamApiUtils.convert; + +@Service +public class EventService { + + private final EventRepository eventRepository; + private final UserService userService; + + public EventService(EventRepository eventRepository, + UserService userService) { + this.eventRepository = eventRepository; + this.userService = userService; + } + + public List 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.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 findByIds(List ids) { + return eventRepository.findAll(ids); + } +} diff --git a/src/main/java/ru/ulstu/timeline/service/TimelineService.java b/src/main/java/ru/ulstu/timeline/service/TimelineService.java new file mode 100644 index 0000000..94388ee --- /dev/null +++ b/src/main/java/ru/ulstu/timeline/service/TimelineService.java @@ -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 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); + } +} diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 0c51e5a..0603620 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -282,4 +282,8 @@ public class UserService implements UserDetailsService { .map(role -> new SimpleGrantedAuthority(role.getName())) .collect(Collectors.toList())); } + + public List findByIds(List ids) { + return userRepository.findAll(ids); + } } From da1fbc3124c07b881a7cb2259c83c652b1ca0a0b Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sun, 10 Jun 2018 01:49:38 +0400 Subject: [PATCH 04/11] send notification --- .../java/ru/ulstu/timeline/model/Event.java | 19 ++++++++++++++++- .../ru/ulstu/timeline/model/EventDto.java | 8 +++++++ .../timeline/repository/EventRepository.java | 5 +++++ .../ulstu/timeline/service/EventService.java | 21 +++++++++++++++++-- .../ru/ulstu/user/service/MailService.java | 12 +++++++++++ .../mail_templates/eventNotification.html | 21 +++++++++++++++++++ 6 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/mail_templates/eventNotification.html diff --git a/src/main/java/ru/ulstu/timeline/model/Event.java b/src/main/java/ru/ulstu/timeline/model/Event.java index 45a2499..6021ccb 100644 --- a/src/main/java/ru/ulstu/timeline/model/Event.java +++ b/src/main/java/ru/ulstu/timeline/model/Event.java @@ -6,7 +6,10 @@ 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; @@ -15,15 +18,21 @@ public class Event extends BaseEntity { @NotBlank private String title; + @Column(name = "execute_date") + @Temporal(TemporalType.DATE) + 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 + @ManyToMany(fetch = FetchType.EAGER) private List recipients; public String getTitle() { @@ -65,4 +74,12 @@ public class Event extends BaseEntity { public void setRecipients(List recipients) { this.recipients = recipients; } + + public Date getExecuteDate() { + return executeDate; + } + + public void setExecuteDate(Date executeDate) { + this.executeDate = executeDate; + } } diff --git a/src/main/java/ru/ulstu/timeline/model/EventDto.java b/src/main/java/ru/ulstu/timeline/model/EventDto.java index 12d2457..59cef34 100644 --- a/src/main/java/ru/ulstu/timeline/model/EventDto.java +++ b/src/main/java/ru/ulstu/timeline/model/EventDto.java @@ -12,6 +12,7 @@ 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; @@ -20,12 +21,14 @@ public class EventDto { @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 recipients) { this.id = id; this.title = title; + this.executeDate = executeDate; this.createDate = createDate; this.updateDate = updateDate; this.description = description; @@ -35,6 +38,7 @@ public class EventDto { 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(); @@ -64,4 +68,8 @@ public class EventDto { public List getRecipients() { return recipients; } + + public Date getExecuteDate() { + return executeDate; + } } diff --git a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java index 3442f10..6267d51 100644 --- a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java +++ b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java @@ -1,7 +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 { + @Query("SELECT e FROM Event e WHERE e.executeDate = CURRENT_DATE") + List findByCurrentDate(); } diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index b25f71b..92c8e20 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -1,15 +1,18 @@ 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.User; 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; @@ -18,11 +21,14 @@ public class EventService { private final EventRepository eventRepository; private final UserService userService; + private final MailService mailService; public EventService(EventRepository eventRepository, - UserService userService) { + UserService userService, + MailService mailService) { this.eventRepository = eventRepository; this.userService = userService; + this.mailService = mailService; } public List findAll() { @@ -35,6 +41,7 @@ public class EventService { } 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))); @@ -58,4 +65,14 @@ public class EventService { public List findByIds(List ids) { return eventRepository.findAll(ids); } + + @Scheduled(cron = "0 8 1 * * ?") + public void sendNotifications() { + List events = eventRepository.findByCurrentDate(); + events.forEach(event -> { + Map variables = ImmutableMap.of("description", event.getDescription()); + event.getRecipients() + .forEach(recipient -> mailService.sendEmailFromTemplate(variables, recipient, "eventNotification", event.getTitle())); + }); + } } diff --git a/src/main/java/ru/ulstu/user/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index 0708450..da1da6d 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -15,6 +15,7 @@ import ru.ulstu.user.model.User; import javax.mail.internet.MimeMessage; import java.nio.charset.StandardCharsets; +import java.util.Map; @Service public class MailService { @@ -66,6 +67,17 @@ public class MailService { sendEmail(user.getEmail(), subject, content); } + //Todo: выделить сервис нотификаций + @Async + public void sendEmailFromTemplate(Map 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 public void sendActivationEmail(User user) { sendEmailFromTemplate(user, "activationEmail", Constants.MAIL_ACTIVATE); diff --git a/src/main/resources/mail_templates/eventNotification.html b/src/main/resources/mail_templates/eventNotification.html new file mode 100644 index 0000000..0500e26 --- /dev/null +++ b/src/main/resources/mail_templates/eventNotification.html @@ -0,0 +1,21 @@ + + + + Уведомление о событии + + + + +

+ Уважаемый Ivan Ivanov +

+

+ some text +

+

+ Regards, +
+ Balance Team. +

+ + From e27e56aa9bca4bdf1e08ef9b3b0be70ccbd479bb Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sun, 10 Jun 2018 01:53:32 +0400 Subject: [PATCH 05/11] fix time --- src/main/java/ru/ulstu/timeline/service/EventService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index 92c8e20..0c1cf6c 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -66,7 +66,7 @@ public class EventService { return eventRepository.findAll(ids); } - @Scheduled(cron = "0 8 1 * * ?") + @Scheduled(cron = "0 0 8 * * ?") public void sendNotifications() { List events = eventRepository.findByCurrentDate(); events.forEach(event -> { From 1d74a7cab991f499d4d250005ea4aa9d3febb36b Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sun, 10 Jun 2018 13:43:40 +0400 Subject: [PATCH 06/11] add timeline page --- src/main/resources/templates/timeline.html | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/main/resources/templates/timeline.html diff --git a/src/main/resources/templates/timeline.html b/src/main/resources/templates/timeline.html new file mode 100644 index 0000000..a480066 --- /dev/null +++ b/src/main/resources/templates/timeline.html @@ -0,0 +1,123 @@ + + + + + + +
+
+
+
+
+

События

+
+
+
+
+
    +
  • +
    +

    Be Part +
    Of Our +
    Story!

    +
    +
    +
    +

    2009-2011

    +

    Our Humble Beginnings

    +
    +
    +

    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!

    +
    +
    +
  • +
  • +
    + +
    +
    +
    +

    March 2011

    +

    An Agency is Born

    +
    +
    +

    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!

    +
    +
    +
  • +
  • +
    + +
    +
    +
    +

    December 2012

    +

    Transition to Full Service

    +
    +
    +

    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!

    +
    +
    +
  • +
  • +
    + +
    +
    +
    +

    July 2014

    +

    Phase Two Expansion

    +
    +
    +

    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!

    +
    +
    +
  • +
  • +
    +

    Be Part +
    Of Our +
    Story!

    +
    +
  • +
+
+
+
+
+ + + + + + + + + + + + + + +
+ + + From 09e8618bb37ed93caae395c6fb89a918c794aeab Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 5 Sep 2018 21:58:04 +0400 Subject: [PATCH 07/11] add timeline page --- src/main/resources/templates/timeline.html | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/resources/templates/timeline.html b/src/main/resources/templates/timeline.html index a480066..4d2acf7 100644 --- a/src/main/resources/templates/timeline.html +++ b/src/main/resources/templates/timeline.html @@ -19,21 +19,17 @@