From eb30d07d10f9d8072832e1077fcac22e481e3cbd Mon Sep 17 00:00:00 2001 From: "a.vasin" Date: Wed, 29 May 2019 18:27:34 +0400 Subject: [PATCH] #101 project deadlines on timeline --- .../ulstu/project/service/ProjectService.java | 6 ++++ .../java/ru/ulstu/timeline/model/Event.java | 13 +++++++ .../ru/ulstu/timeline/model/EventDto.java | 15 ++++++++ .../timeline/repository/EventRepository.java | 3 ++ .../ulstu/timeline/service/EventService.java | 34 +++++++++++++++++++ 5 files changed, 71 insertions(+) diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index fe8813c..30b5f01 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -9,6 +9,7 @@ import ru.ulstu.grant.repository.GrantRepository; import ru.ulstu.project.model.Project; import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.repository.ProjectRepository; +import ru.ulstu.timeline.service.EventService; import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; @@ -28,17 +29,20 @@ public class ProjectService { private final DeadlineService deadlineService; private final GrantRepository grantRepository; private final FileService fileService; + private final EventService eventService; private final UserService userService; public ProjectService(ProjectRepository projectRepository, DeadlineService deadlineService, GrantRepository grantRepository, FileService fileService, + EventService eventService, UserService userService) { this.projectRepository = projectRepository; this.deadlineService = deadlineService; this.grantRepository = grantRepository; this.fileService = fileService; + this.eventService = eventService; this.userService = userService; } @@ -64,6 +68,7 @@ public class ProjectService { public Project create(ProjectDto projectDto) throws IOException { Project newProject = copyFromDto(new Project(), projectDto); newProject = projectRepository.save(newProject); + eventService.createFromProject(newProject); return newProject; } @@ -74,6 +79,7 @@ public class ProjectService { fileService.deleteFile(project.getApplication()); } projectRepository.save(copyFromDto(project, projectDto)); + eventService.updateProjectDeadlines(project); return project; } diff --git a/src/main/java/ru/ulstu/timeline/model/Event.java b/src/main/java/ru/ulstu/timeline/model/Event.java index 8038f0b..a8b4571 100644 --- a/src/main/java/ru/ulstu/timeline/model/Event.java +++ b/src/main/java/ru/ulstu/timeline/model/Event.java @@ -5,6 +5,7 @@ import ru.ulstu.conference.model.Conference; import ru.ulstu.core.model.BaseEntity; import ru.ulstu.grant.model.Grant; import ru.ulstu.paper.model.Paper; +import ru.ulstu.project.model.Project; import ru.ulstu.students.model.Task; import ru.ulstu.user.model.User; @@ -88,6 +89,10 @@ public class Event extends BaseEntity { @JoinColumn(name = "grant_id") private Grant grant; + @ManyToOne + @JoinColumn(name = "project_id") + private Project project; + @ManyToOne @JoinColumn(name = "task_id") private Task task; @@ -196,6 +201,14 @@ public class Event extends BaseEntity { this.grant = grant; } + public Project getProject() { + return project; + } + + public void setProject(Project project) { + this.project = project; + } + public Task getTask() { return task; } diff --git a/src/main/java/ru/ulstu/timeline/model/EventDto.java b/src/main/java/ru/ulstu/timeline/model/EventDto.java index ccf0f0a..1f0076b 100644 --- a/src/main/java/ru/ulstu/timeline/model/EventDto.java +++ b/src/main/java/ru/ulstu/timeline/model/EventDto.java @@ -6,6 +6,7 @@ import org.hibernate.validator.constraints.NotBlank; import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.paper.model.PaperDto; +import ru.ulstu.project.model.ProjectDto; import ru.ulstu.students.model.TaskDto; import ru.ulstu.user.model.UserDto; @@ -30,6 +31,7 @@ public class EventDto { private PaperDto paperDto; private ConferenceDto conferenceDto; private GrantDto grantDto; + private ProjectDto projectDto; private TaskDto taskDto; @JsonCreator @@ -45,6 +47,7 @@ public class EventDto { @JsonProperty("recipients") List recipients, @JsonProperty("conferenceDto") ConferenceDto conferenceDto, @JsonProperty("grantDto") GrantDto grantDto, + @JsonProperty("projectDto") ProjectDto projectDto, @JsonProperty("taskDto") TaskDto taskDto) { this.id = id; this.title = title; @@ -58,6 +61,7 @@ public class EventDto { this.paperDto = paperDto; this.conferenceDto = conferenceDto; this.grantDto = grantDto; + this.projectDto = projectDto; this.taskDto = taskDto; } @@ -80,6 +84,9 @@ public class EventDto { if (grantDto != null) { this.grantDto = new GrantDto(event.getGrant()); } + if (projectDto != null) { + this.projectDto = new ProjectDto(event.getProject()); + } if (taskDto != null) { this.taskDto = new TaskDto(event.getTask()); } @@ -145,6 +152,14 @@ public class EventDto { this.grantDto = grantDto; } + public ProjectDto getProjectDto() { + return projectDto; + } + + public void setProjectDto(ProjectDto projectDto) { + this.projectDto = projectDto; + } + public TaskDto getTaskDto() { return taskDto; } diff --git a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java index 7ebd3c9..15e1355 100644 --- a/src/main/java/ru/ulstu/timeline/repository/EventRepository.java +++ b/src/main/java/ru/ulstu/timeline/repository/EventRepository.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.Query; import ru.ulstu.conference.model.Conference; import ru.ulstu.grant.model.Grant; import ru.ulstu.paper.model.Paper; +import ru.ulstu.project.model.Project; import ru.ulstu.students.model.Task; import ru.ulstu.timeline.model.Event; @@ -23,5 +24,7 @@ public interface EventRepository extends JpaRepository { List findAllByGrant(Grant grant); + List findAllByProject(Project project); + List findAllByTask(Task task); } diff --git a/src/main/java/ru/ulstu/timeline/service/EventService.java b/src/main/java/ru/ulstu/timeline/service/EventService.java index 8c68291..eef38eb 100644 --- a/src/main/java/ru/ulstu/timeline/service/EventService.java +++ b/src/main/java/ru/ulstu/timeline/service/EventService.java @@ -8,11 +8,13 @@ import ru.ulstu.conference.model.Conference; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.grant.model.Grant; import ru.ulstu.paper.model.Paper; +import ru.ulstu.project.model.Project; import ru.ulstu.students.model.Task; import ru.ulstu.timeline.model.Event; import ru.ulstu.timeline.model.EventDto; import ru.ulstu.timeline.model.Timeline; import ru.ulstu.timeline.repository.EventRepository; +import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.service.UserService; @@ -205,6 +207,38 @@ public class EventService { createFromGrant(grant); } + public void createFromProject(Project newProject) { + List timelines = timelineService.findAll(); + Timeline timeline = timelines.isEmpty() ? new Timeline() : timelines.get(0); + + 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.getRecipients().add((User) newProject.getExecutors()); + newEvent.setProject(newProject); + eventRepository.save(newEvent); + + timeline.getEvents().add(newEvent); + timelineService.save(timeline); + } + } + + public void updateProjectDeadlines(Project project) { + eventRepository.delete(eventRepository.findAllByProject(project)); + createFromProject(project); + } + public void removeConferencesEvent(Conference conference) { List eventList = eventRepository.findAllByConference(conference); eventList.forEach(event -> eventRepository.delete(event.getId()));