#101 EventService edited

This commit is contained in:
Васин Антон 2019-06-03 20:21:46 +03:00
parent dc52968ddf
commit 482964fe53
11 changed files with 139 additions and 143 deletions

View File

@ -5,8 +5,11 @@ import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.EventSource;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.timeline.model.Event;
import ru.ulstu.user.model.User;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
@ -28,7 +31,7 @@ import java.util.Optional;
@Entity @Entity
@Table(name = "conference") @Table(name = "conference")
public class Conference extends BaseEntity { public class Conference extends BaseEntity implements EventSource {
@NotBlank @NotBlank
private String title; private String title;
@ -71,6 +74,19 @@ public class Conference extends BaseEntity {
return title; return title;
} }
@Override
public List<User> getRecipients() {
List<User> list = new ArrayList<>();
getUsers().forEach(conferenceUser -> list.add(conferenceUser.getUser()));
return list;
}
@Override
public void addObjectToEvent(Event event) {
event.setConference(this);
}
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }

View File

@ -117,7 +117,7 @@ public class ConferenceService extends BaseService {
Conference newConference = copyFromDto(new Conference(), conferenceDto); Conference newConference = copyFromDto(new Conference(), conferenceDto);
newConference = conferenceRepository.save(newConference); newConference = conferenceRepository.save(newConference);
conferenceNotificationService.sendCreateNotification(newConference); conferenceNotificationService.sendCreateNotification(newConference);
eventService.createFromObject(newConference, Collections.emptyList()); eventService.createFromObject(newConference, Collections.emptyList(), false, "конференции");
return newConference; return newConference;
} }

View File

@ -0,0 +1,17 @@
package ru.ulstu.core.model;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.timeline.model.Event;
import ru.ulstu.user.model.User;
import java.util.List;
public interface EventSource {
List<Deadline> getDeadlines();
String getTitle();
List<User> getRecipients();
void addObjectToEvent(Event event);
}

View File

@ -4,6 +4,7 @@ import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.EventSource;
import ru.ulstu.core.model.UserContainer; import ru.ulstu.core.model.UserContainer;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileData;
@ -26,6 +27,7 @@ import javax.persistence.OrderBy;
import javax.persistence.Table; import javax.persistence.Table;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
@ -35,7 +37,7 @@ import java.util.Set;
@Entity @Entity
@Table(name = "grants") @Table(name = "grants")
public class Grant extends BaseEntity implements UserContainer { public class Grant extends BaseEntity implements UserContainer, EventSource {
public enum GrantStatus { public enum GrantStatus {
APPLICATION("Заявка"), APPLICATION("Заявка"),
ON_COMPETITION("Отправлен на конкурс"), ON_COMPETITION("Отправлен на конкурс"),
@ -134,6 +136,16 @@ public class Grant extends BaseEntity implements UserContainer {
return title; return title;
} }
@Override
public List<User> getRecipients() {
return authors != null ? new ArrayList<>(authors) : Collections.emptyList();
}
@Override
public void addObjectToEvent(Event event) {
event.setGrant(this);
}
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }

View File

@ -90,7 +90,7 @@ public class GrantService extends BaseService {
public Integer create(GrantDto grantDto) throws IOException { public Integer create(GrantDto grantDto) throws IOException {
Grant newGrant = copyFromDto(new Grant(), grantDto); Grant newGrant = copyFromDto(new Grant(), grantDto);
newGrant = grantRepository.save(newGrant); newGrant = grantRepository.save(newGrant);
eventService.createFromObject(newGrant, Collections.emptyList()); eventService.createFromObject(newGrant, Collections.emptyList(), false, "гранта");
grantNotificationService.sendCreateNotification(newGrant); grantNotificationService.sendCreateNotification(newGrant);
return newGrant.getId(); return newGrant.getId();
} }
@ -178,7 +178,7 @@ public class GrantService extends BaseService {
grant.getPapers().add(paper); grant.getPapers().add(paper);
grant = grantRepository.save(grant); grant = grantRepository.save(grant);
eventService.createFromObject(grant, Collections.emptyList()); eventService.createFromObject(grant, Collections.emptyList(), false, "гранта");
grantNotificationService.sendCreateNotification(grant); grantNotificationService.sendCreateNotification(grant);
return grant; return grant;

View File

@ -5,6 +5,7 @@ import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.conference.model.Conference; import ru.ulstu.conference.model.Conference;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.EventSource;
import ru.ulstu.core.model.UserContainer; import ru.ulstu.core.model.UserContainer;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileData;
@ -34,7 +35,7 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
@Entity @Entity
public class Paper extends BaseEntity implements UserContainer { public class Paper extends BaseEntity implements UserContainer, EventSource {
public enum PaperStatus { public enum PaperStatus {
ATTENTION("Обратить внимание"), ATTENTION("Обратить внимание"),
ON_PREPARATION("На подготовке"), ON_PREPARATION("На подготовке"),
@ -196,6 +197,16 @@ public class Paper extends BaseEntity implements UserContainer {
return title; return title;
} }
@Override
public List<User> getRecipients() {
return new ArrayList(authors);
}
@Override
public void addObjectToEvent(Event event) {
event.setPaper(this);
}
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }

View File

@ -4,6 +4,7 @@ import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.EventSource;
import ru.ulstu.core.model.UserContainer; import ru.ulstu.core.model.UserContainer;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileData;
@ -23,12 +24,13 @@ import javax.persistence.ManyToMany;
import javax.persistence.JoinTable; import javax.persistence.JoinTable;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@Entity @Entity
public class Project extends BaseEntity implements UserContainer { public class Project extends BaseEntity implements UserContainer, EventSource {
public enum ProjectStatus { public enum ProjectStatus {
TECHNICAL_TASK("Техническое задание"), TECHNICAL_TASK("Техническое задание"),
@ -92,6 +94,16 @@ public class Project extends BaseEntity implements UserContainer {
return title; return title;
} }
@Override
public List<User> getRecipients() {
return executors != null ? new ArrayList<>(executors) : Collections.emptyList();
}
@Override
public void addObjectToEvent(Event event) {
event.setProject(this);
}
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }

View File

@ -72,7 +72,7 @@ public class ProjectService {
public Project create(ProjectDto projectDto) throws IOException { public Project create(ProjectDto projectDto) throws IOException {
Project newProject = copyFromDto(new Project(), projectDto); Project newProject = copyFromDto(new Project(), projectDto);
newProject = projectRepository.save(newProject); newProject = projectRepository.save(newProject);
eventService.createFromObject(newProject, Collections.emptyList()); eventService.createFromObject(newProject, Collections.emptyList(), false, "проекта");
return newProject; return newProject;
} }

View File

@ -4,8 +4,17 @@ import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotBlank;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.BaseEntity;
import ru.ulstu.core.model.EventSource;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.students.repository.SchedulerRepository;
import ru.ulstu.students.repository.TaskRepository;
import ru.ulstu.tags.model.Tag; import ru.ulstu.tags.model.Tag;
import ru.ulstu.tags.service.TagService;
import ru.ulstu.timeline.model.Event;
import ru.ulstu.timeline.service.EventService;
import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Column; import javax.persistence.Column;
@ -20,12 +29,14 @@ import javax.persistence.OneToMany;
import javax.persistence.OrderBy; import javax.persistence.OrderBy;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
import javax.persistence.Transient;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@Entity @Entity
public class Task extends BaseEntity { public class Task extends BaseEntity implements EventSource {
public enum TaskStatus { public enum TaskStatus {
IN_WORK("В работе"), IN_WORK("В работе"),
@ -49,6 +60,17 @@ public class Task extends BaseEntity {
private String description; private String description;
@Transient
private UserService userService;
public Task() {
}
public Task(UserService userService) {
this.userService = userService;
}
@Enumerated(value = EnumType.STRING) @Enumerated(value = EnumType.STRING)
private TaskStatus status = TaskStatus.IN_WORK; private TaskStatus status = TaskStatus.IN_WORK;
@ -77,6 +99,16 @@ public class Task extends BaseEntity {
return title; return title;
} }
@Override
public List<User> getRecipients() {
return Collections.emptyList();
}
@Override
public void addObjectToEvent(Event event) {
event.setTask(this);
}
public void setTitle(String title) { public void setTitle(String title) {
this.title = title; this.title = title;
} }

View File

@ -84,7 +84,7 @@ public class TaskService {
public Integer create(TaskDto taskDto) throws IOException { public Integer create(TaskDto taskDto) throws IOException {
Task newTask = copyFromDto(new Task(), taskDto); Task newTask = copyFromDto(new Task(), taskDto);
newTask = taskRepository.save(newTask); newTask = taskRepository.save(newTask);
eventService.createFromObject(newTask, Collections.emptyList()); eventService.createFromObject(newTask, Collections.emptyList(), true, "задачи");
return newTask.getId(); return newTask.getId();
} }

View File

@ -1,12 +1,11 @@
package ru.ulstu.timeline.service; package ru.ulstu.timeline.service;
import com.sun.jna.platform.win32.COM.util.Convert;
import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.lang3.time.DateUtils;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
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.conference.model.Conference; import ru.ulstu.conference.model.Conference;
import ru.ulstu.core.model.BaseEntity; import ru.ulstu.core.model.EventSource;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.Grant;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
@ -16,12 +15,10 @@ import ru.ulstu.timeline.model.Event;
import ru.ulstu.timeline.model.EventDto; import ru.ulstu.timeline.model.EventDto;
import ru.ulstu.timeline.model.Timeline; import ru.ulstu.timeline.model.Timeline;
import ru.ulstu.timeline.repository.EventRepository; import ru.ulstu.timeline.repository.EventRepository;
import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -36,13 +33,6 @@ public class EventService {
private final TimelineService timelineService; private final TimelineService timelineService;
private final UserService userService; private final UserService userService;
private String objectClassName = "";
private Paper newPaper = new Paper();
private Conference newConference = new Conference();
private Grant newGrant = new Grant();
private Project newProject = new Project();
private Task newTask = new Task();
public EventService(EventRepository eventRepository, public EventService(EventRepository eventRepository,
@Lazy TimelineService timelineService, @Lazy TimelineService timelineService,
UserService userService) { UserService userService) {
@ -116,133 +106,39 @@ public class EventService {
} }
public void createFromPaper(Paper newPaper) { public void createFromPaper(Paper newPaper) {
createFromObject(newPaper, Collections.emptyList()); createFromObject(newPaper, Collections.emptyList(), false, "статьи");
} }
public void createFromObject(Object newObject, List<Event> events) { public void createFromObject(EventSource eventSource, List<Event> events, Boolean addCurrentUser, String suffix) {
List<Timeline> timelines = timelineService.findAll(); List<Timeline> timelines = timelineService.findAll();
Timeline timeline = timelines.isEmpty() ? new Timeline() : timelines.get(0); Timeline timeline = timelines.isEmpty() ? new Timeline() : timelines.get(0);
objectClassName = newObject.getClass().getName();
switch(objectClassName) {
case "ru.ulstu.paper.model.Paper":
newPaper = (Paper)newObject;
timeline.getEvents().removeAll(events);
for (Deadline deadline : newPaper.getDeadlines()
.stream()
.filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date()))
.collect(Collectors.toList())) {
Event newEvent = new Event();
newEvent.setTitle("Дедлайн статьи");
newEvent.setStatus(Event.EventStatus.NEW);
newEvent.setExecuteDate(deadline.getDate());
newEvent.setCreateDate(new Date());
newEvent.setUpdateDate(new Date());
newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' cтатьи '" + newPaper.getTitle() + "'");
newEvent.setRecipients(new ArrayList(newPaper.getAuthors()));
newEvent.setPaper(newPaper);
timeline.getEvents().add(eventRepository.save(newEvent));
}
timelineService.save(timeline);
break;
case "ru.ulstu.conference.model.Conference":
newConference = (Conference)newObject;
for (Deadline deadline : newConference.getDeadlines()
.stream()
.filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date()))
.collect(Collectors.toList())) {
Event newEvent = new Event();
newEvent.setTitle("Дедлайн конференции");
newEvent.setStatus(Event.EventStatus.NEW);
newEvent.setExecuteDate(deadline.getDate());
newEvent.setCreateDate(new Date());
newEvent.setUpdateDate(new Date());
newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' конференции '" + newConference.getTitle() + "'");
newConference.getUsers().forEach(conferenceUser -> newEvent.getRecipients().add(conferenceUser.getUser()));
newEvent.setConference(newConference);
save(newEvent);
timeline.getEvents().add(newEvent); timeline.getEvents().removeAll(events);
timelineService.save(timeline); for (Deadline deadline : eventSource.getDeadlines()
} .stream()
break; .filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date()))
case "ru.ulstu.grant.model.Grant": .collect(Collectors.toList())) {
newGrant = (Grant)newObject; Event newEvent = new Event();
for (Deadline deadline : newGrant.getDeadlines() newEvent.setTitle("Дедлайн " + suffix);
.stream() newEvent.setStatus(Event.EventStatus.NEW);
.filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date())) newEvent.setExecuteDate(deadline.getDate());
.collect(Collectors.toList())) { newEvent.setCreateDate(new Date());
Event newEvent = new Event(); newEvent.setUpdateDate(new Date());
newEvent.setTitle("Дедлайн гранта"); newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' " + suffix + " '"
newEvent.setStatus(Event.EventStatus.NEW); + eventSource.getTitle() + "'");
newEvent.setExecuteDate(deadline.getDate()); if (addCurrentUser) {
newEvent.setCreateDate(new Date()); newEvent.getRecipients().add(userService.getCurrentUser());
newEvent.setUpdateDate(new Date()); }
newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' гранта '" + newGrant.getTitle() + "'"); newEvent.setRecipients(eventSource.getRecipients());
if (newGrant.getAuthors() != null) { eventSource.addObjectToEvent(newEvent);
newEvent.setRecipients(new ArrayList(newGrant.getAuthors())); timeline.getEvents().add(eventRepository.save(newEvent));
}
newEvent.getRecipients().add(newGrant.getLeader());
newEvent.setGrant(newGrant);
eventRepository.save(newEvent);
timeline.getEvents().add(newEvent);
timelineService.save(timeline);
}
break;
case "ru.ulstu.project.model.Project":
newProject = (Project)newObject;
for (Deadline deadline : newProject.getDeadlines()
.stream()
.filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date()))
.collect(Collectors.toList())) {
Event newEvent = new Event();
newEvent.setTitle("Дедлайн проекта");
newEvent.setStatus(Event.EventStatus.NEW);
newEvent.setExecuteDate(deadline.getDate());
newEvent.setCreateDate(new Date());
newEvent.setUpdateDate(new Date());
newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' проекта '" + newProject.getTitle() + "'");
if (newProject.getExecutors() != null) {
newEvent.setRecipients(new ArrayList(newProject.getExecutors()));
}
newEvent.setProject(newProject);
eventRepository.save(newEvent);
timeline.getEvents().add(newEvent);
timelineService.save(timeline);
}
break;
case "ru.ulstu.students.model.Task":
newTask = (Task)newObject;
for (Deadline deadline : newTask.getDeadlines()
.stream()
.filter(d -> d.getDate().after(new Date()) || DateUtils.isSameDay(d.getDate(), new Date()))
.collect(Collectors.toList())) {
Event newEvent = new Event();
newEvent.setTitle("Дедлайн задачи");
newEvent.setStatus(Event.EventStatus.NEW);
newEvent.setExecuteDate(deadline.getDate());
newEvent.setCreateDate(new Date());
newEvent.setUpdateDate(new Date());
newEvent.setDescription("Дедлайн '" + deadline.getDescription() + "' задачи '" + newTask.getTitle() + "'");
newEvent.getRecipients().add(userService.getCurrentUser());
newEvent.setTask(newTask);
eventRepository.save(newEvent);
timeline.getEvents().add(newEvent);
timelineService.save(timeline);
}
System.out.println("5");
break;
default:
System.out.println("No such object");
break;
} }
timelineService.save(timeline);
} }
public void updatePaperDeadlines(Paper paper) { public void updatePaperDeadlines(Paper paper) {
List<Event> foundEvents = eventRepository.findAllByPaper(paper); List<Event> foundEvents = eventRepository.findAllByPaper(paper);
eventRepository.delete(foundEvents); eventRepository.delete(foundEvents);
createFromObject(paper, foundEvents); createFromObject(paper, foundEvents, false, "статьи");
} }
public List<Event> findByCurrentDate() { public List<Event> findByCurrentDate() {
@ -259,17 +155,17 @@ public class EventService {
public void updateConferenceDeadlines(Conference conference) { public void updateConferenceDeadlines(Conference conference) {
eventRepository.delete(eventRepository.findAllByConference(conference)); eventRepository.delete(eventRepository.findAllByConference(conference));
createFromObject(conference, Collections.emptyList()); createFromObject(conference, Collections.emptyList(), false, "конференции");
} }
public void updateGrantDeadlines(Grant grant) { public void updateGrantDeadlines(Grant grant) {
eventRepository.delete(eventRepository.findAllByGrant(grant)); eventRepository.delete(eventRepository.findAllByGrant(grant));
createFromObject(grant, Collections.emptyList()); createFromObject(grant, Collections.emptyList(), false, "гранта");
} }
public void updateProjectDeadlines(Project project) { public void updateProjectDeadlines(Project project) {
eventRepository.delete(eventRepository.findAllByProject(project)); eventRepository.delete(eventRepository.findAllByProject(project));
createFromObject(project, Collections.emptyList()); createFromObject(project, Collections.emptyList(), false, "проекта");
} }
public void removeConferencesEvent(Conference conference) { public void removeConferencesEvent(Conference conference) {
@ -279,6 +175,6 @@ public class EventService {
public void updateTaskDeadlines(Task task) { public void updateTaskDeadlines(Task task) {
eventRepository.delete(eventRepository.findAllByTask(task)); eventRepository.delete(eventRepository.findAllByTask(task));
createFromObject(task, Collections.emptyList()); createFromObject(task, Collections.emptyList(), true, "задачи");
} }
} }