Merge branch '32-timeline-grants' into 'dev'

Resolve "Создать страницу таймлайна дедлайнов"

Closes #32

See merge request romanov73/ng-tracker!83
This commit is contained in:
Anton Romanov 2019-05-06 07:06:04 +00:00
commit 2dff183662
8 changed files with 104 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.project.model.Project;
import ru.ulstu.timeline.model.Event;
import ru.ulstu.user.model.User;
import javax.persistence.CascadeType;
@ -90,8 +91,13 @@ public class Grant extends BaseEntity implements UserContainer {
@JoinTable(name = "grants_papers",
joinColumns = {@JoinColumn(name = "grant_id")},
inverseJoinColumns = {@JoinColumn(name = "paper_id")})
@Fetch(FetchMode.SUBSELECT)
private List<Paper> papers = new ArrayList<>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "grant_id")
private List<Event> events = new ArrayList<>();
public GrantStatus getStatus() {
return status;
}
@ -169,6 +175,14 @@ public class Grant extends BaseEntity implements UserContainer {
this.papers = papers;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public Optional<Deadline> getNextDeadline() {
return deadlines
.stream()

View File

@ -15,6 +15,7 @@ import ru.ulstu.paper.service.PaperService;
import ru.ulstu.project.model.Project;
import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.service.ProjectService;
import ru.ulstu.timeline.service.EventService;
import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService;
@ -39,19 +40,22 @@ public class GrantService {
private final FileService fileService;
private final UserService userService;
private final PaperService paperService;
private final EventService eventService;
public GrantService(GrantRepository grantRepository,
FileService fileService,
DeadlineService deadlineService,
ProjectService projectService,
UserService userService,
PaperService paperService) {
PaperService paperService,
EventService eventService) {
this.grantRepository = grantRepository;
this.fileService = fileService;
this.deadlineService = deadlineService;
this.projectService = projectService;
this.userService = userService;
this.paperService = paperService;
this.eventService = eventService;
}
public List<Grant> findAll() {
@ -72,6 +76,7 @@ public class GrantService {
public Integer create(GrantDto grantDto) throws IOException {
Grant newGrant = copyFromDto(new Grant(), grantDto);
newGrant = grantRepository.save(newGrant);
eventService.createFromGrant(newGrant);
return newGrant.getId();
}
@ -115,6 +120,7 @@ public class GrantService {
}
grantDto.getRemovedDeadlineIds().forEach(deadlineService::remove);
grantRepository.save(copyFromDto(grant, grantDto));
eventService.updateGrantDeadlines(grant);
return grant.getId();
}
@ -140,6 +146,9 @@ public class GrantService {
grant.setLeader(user);
grant.getPapers().add(paper);
grant = grantRepository.save(grant);
eventService.createFromGrant(grant);
return grant;
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.timeline.model;
import org.hibernate.validator.constraints.NotBlank;
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.user.model.User;
@ -82,6 +83,10 @@ public class Event extends BaseEntity {
@JoinColumn(name = "conference_id")
private Conference conference;
@ManyToOne
@JoinColumn(name = "grant_id")
private Grant grant;
public String getTitle() {
return title;
}
@ -177,4 +182,12 @@ public class Event extends BaseEntity {
public void setConference(Conference conference) {
this.conference = conference;
}
public Grant getGrant() {
return grant;
}
public void setGrant(Grant grant) {
this.grant = grant;
}
}

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
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.user.model.UserDto;
@ -27,6 +28,7 @@ public class EventDto {
private final List<UserDto> recipients;
private PaperDto paperDto;
private ConferenceDto conferenceDto;
private GrantDto grantDto;
@JsonCreator
public EventDto(@JsonProperty("id") Integer id,
@ -39,7 +41,8 @@ public class EventDto {
@JsonProperty("description") String description,
@JsonProperty("paperDto") PaperDto paperDto,
@JsonProperty("recipients") List<UserDto> recipients,
@JsonProperty("conferenceDto") ConferenceDto conferenceDto) {
@JsonProperty("conferenceDto") ConferenceDto conferenceDto,
@JsonProperty("grantDto") GrantDto grantDto) {
this.id = id;
this.title = title;
this.period = period;
@ -51,6 +54,7 @@ public class EventDto {
this.recipients = recipients;
this.paperDto = paperDto;
this.conferenceDto = conferenceDto;
this.grantDto = grantDto;
}
public EventDto(Event event) {
@ -69,6 +73,9 @@ public class EventDto {
if (conferenceDto != null) {
this.conferenceDto = new ConferenceDto(event.getConference());
}
if (grantDto != null) {
this.grantDto = new GrantDto(event.getGrant());
}
}
public Integer getId() {
@ -122,4 +129,12 @@ public class EventDto {
public void setConferenceDto(ConferenceDto conferenceDto) {
this.conferenceDto = conferenceDto;
}
public GrantDto getGrantDto() {
return grantDto;
}
public void setGrantDto(GrantDto grantDto) {
this.grantDto = grantDto;
}
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.timeline.repository;
import org.springframework.data.jpa.repository.JpaRepository;
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.timeline.model.Event;
@ -18,4 +19,6 @@ public interface EventRepository extends JpaRepository<Event, Integer> {
List<Event> findAllByPaper(Paper paper);
List<Event> findAllByConference(Conference conference);
List<Event> findAllByGrant(Grant grant);
}

View File

@ -6,6 +6,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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.timeline.model.Event;
import ru.ulstu.timeline.model.EventDto;
@ -170,4 +171,36 @@ public class EventService {
eventRepository.delete(eventRepository.findAllByConference(conference));
createFromConference(conference);
}
public void createFromGrant(Grant newGrant) {
List<Timeline> timelines = timelineService.findAll();
Timeline timeline = timelines.isEmpty() ? new Timeline() : timelines.get(0);
for (Deadline deadline : newGrant.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() + "' гранта '" + newGrant.getTitle() + "'");
if (newGrant.getAuthors() != null) {
newEvent.setRecipients(new ArrayList(newGrant.getAuthors()));
}
newEvent.getRecipients().add(newGrant.getLeader());
newEvent.setGrant(newGrant);
eventRepository.save(newEvent);
timeline.getEvents().add(newEvent);
timelineService.save(timeline);
}
}
public void updateGrantDeadlines(Grant grant) {
eventRepository.delete(eventRepository.findAllByGrant(grant));
createFromGrant(grant);
}
}

View File

@ -0,0 +1,14 @@
<?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="tanya" id="20190505_000000-1">
<addColumn tableName="event">
<column name="grant_id" type="integer"/>
</addColumn>
<addForeignKeyConstraint baseTableName="event" baseColumnNames="grant_id"
constraintName="fk_event_grant_id" referencedTableName="grants"
referencedColumnNames="id"/>
</changeSet>
</databaseChangeLog>

View File

@ -37,4 +37,5 @@
<include file="db/changelog-20190426_000000-schema.xml"/>
<include file="db/changelog-20190428_000000-schema.xml"/>
<include file="db/changelog-20190430_000000-schema.xml"/>
<include file="db/changelog-20190505_000000-schema.xml"/>
</databaseChangeLog>