# Conflicts: # src/main/java/ru/ulstu/conference/service/ConferenceService.java # src/main/java/ru/ulstu/grant/model/Grant.java # src/main/java/ru/ulstu/grant/service/GrantService.java # src/main/java/ru/ulstu/paper/model/Paper.java # src/main/java/ru/ulstu/paper/service/PaperService.java # src/main/java/ru/ulstu/project/model/Project.java # src/main/java/ru/ulstu/project/model/ProjectDto.java # src/main/java/ru/ulstu/students/model/Task.java # src/main/java/ru/ulstu/students/service/TaskService.java # src/main/java/ru/ulstu/timeline/model/Event.java # src/main/java/ru/ulstu/timeline/model/EventDto.java # src/main/java/ru/ulstu/timeline/service/EventService.java # src/main/java/ru/ulstu/user/service/UserService.java # src/main/resources/application.properties # src/main/resources/db/changelog-master.xml # src/main/resources/public/css/conference.css # src/main/resources/public/js/conference.js # src/main/resources/public/js/tasks.js # src/main/resources/templates/conferences/conference.html # src/main/resources/templates/grants/grant.html # src/main/resources/templates/papers/paper.html
174 lines
6.4 KiB
Java
174 lines
6.4 KiB
Java
package ru.ulstu.project.service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.thymeleaf.util.StringUtils;
|
|
import ru.ulstu.deadline.service.DeadlineService;
|
|
import ru.ulstu.file.model.FileDataDto;
|
|
import ru.ulstu.file.service.FileService;
|
|
import ru.ulstu.grant.model.GrantDto;
|
|
import ru.ulstu.grant.repository.GrantRepository;
|
|
import ru.ulstu.ping.service.PingService;
|
|
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;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
import static java.util.stream.Collectors.toList;
|
|
import static org.springframework.util.ObjectUtils.isEmpty;
|
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
|
import static ru.ulstu.project.model.Project.ProjectStatus.TECHNICAL_TASK;
|
|
|
|
@Service
|
|
public class ProjectService {
|
|
private final static int MAX_DISPLAY_SIZE = 40;
|
|
|
|
private final ProjectRepository projectRepository;
|
|
private final DeadlineService deadlineService;
|
|
private final GrantRepository grantRepository;
|
|
private final FileService fileService;
|
|
private final EventService eventService;
|
|
private final UserService userService;
|
|
private final PingService pingService;
|
|
|
|
public ProjectService(ProjectRepository projectRepository,
|
|
DeadlineService deadlineService,
|
|
GrantRepository grantRepository,
|
|
FileService fileService,
|
|
EventService eventService,
|
|
UserService userService,
|
|
PingService pingService) {
|
|
this.projectRepository = projectRepository;
|
|
this.deadlineService = deadlineService;
|
|
this.grantRepository = grantRepository;
|
|
this.fileService = fileService;
|
|
this.eventService = eventService;
|
|
this.userService = userService;
|
|
this.pingService = pingService;
|
|
}
|
|
|
|
public List<Project> findAll() {
|
|
return projectRepository.findAll();
|
|
}
|
|
|
|
public List<ProjectDto> findAllDto() {
|
|
List<ProjectDto> projects = convert(findAll(), ProjectDto::new);
|
|
projects.forEach(projectDto -> projectDto.setTitle(StringUtils.abbreviate(projectDto.getTitle(), MAX_DISPLAY_SIZE)));
|
|
return projects;
|
|
}
|
|
|
|
public ProjectDto findOneDto(Integer id) {
|
|
return new ProjectDto(projectRepository.getOne(id));
|
|
}
|
|
|
|
public List<Project.ProjectStatus> getProjectStatuses() {
|
|
return Arrays.asList(Project.ProjectStatus.values());
|
|
}
|
|
|
|
@Transactional
|
|
public Project create(ProjectDto projectDto) throws IOException {
|
|
Project newProject = copyFromDto(new Project(), projectDto);
|
|
newProject = projectRepository.save(newProject);
|
|
eventService.createFromObject(newProject, Collections.emptyList(), false, "проекта");
|
|
return newProject;
|
|
}
|
|
|
|
@Transactional
|
|
public Project update(ProjectDto projectDto) throws IOException {
|
|
Project project = projectRepository.getOne(projectDto.getId());
|
|
projectRepository.save(copyFromDto(project, projectDto));
|
|
eventService.updateProjectDeadlines(project);
|
|
for (FileDataDto file : projectDto.getFiles().stream()
|
|
.filter(f -> f.isDeleted() && f.getId() != null)
|
|
.collect(toList())) {
|
|
fileService.delete(file.getId());
|
|
}
|
|
return project;
|
|
}
|
|
|
|
@Transactional
|
|
public boolean delete(Integer projectId) throws IOException {
|
|
if (projectRepository.existsById(projectId)) {
|
|
Project project = projectRepository.getOne(projectId);
|
|
projectRepository.delete(project);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private Project copyFromDto(Project project, ProjectDto projectDto) throws IOException {
|
|
project.setDescription(projectDto.getDescription());
|
|
project.setStatus(projectDto.getStatus() == null ? TECHNICAL_TASK : projectDto.getStatus());
|
|
project.setTitle(projectDto.getTitle());
|
|
if (projectDto.getGrant() != null && projectDto.getGrant().getId() != null) {
|
|
project.setGrant(grantRepository.getOne(projectDto.getGrant().getId()));
|
|
}
|
|
project.setRepository(projectDto.getRepository());
|
|
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
|
|
project.setFiles(fileService.saveOrCreate(projectDto.getFiles().stream()
|
|
.filter(f -> !f.isDeleted())
|
|
.collect(toList())));
|
|
project.getGrants().clear();
|
|
if (projectDto.getGrantIds() != null && !projectDto.getGrantIds().isEmpty()) {
|
|
projectDto.getGrantIds().forEach(grantIds -> project.getGrants().add(grantRepository.findGrantById(grantIds)));
|
|
}
|
|
return project;
|
|
}
|
|
|
|
public Project save(ProjectDto projectDto) throws IOException {
|
|
if (isEmpty(projectDto.getId())) {
|
|
return create(projectDto);
|
|
} else {
|
|
return update(projectDto);
|
|
}
|
|
}
|
|
|
|
public ProjectDto removeDeadline(ProjectDto projectDto, Integer deadlineId) {
|
|
if (deadlineId != null) {
|
|
projectDto.getRemovedDeadlineIds().add(deadlineId);
|
|
}
|
|
projectDto.getDeadlines().remove((int) deadlineId);
|
|
return projectDto;
|
|
}
|
|
|
|
public Project findById(Integer id) {
|
|
return projectRepository.getOne(id);
|
|
}
|
|
|
|
public List<User> getProjectExecutors(ProjectDto projectDto) {
|
|
List<User> users = userService.findAll();
|
|
return users;
|
|
}
|
|
|
|
@Transactional
|
|
public void ping(int projectId) throws IOException {
|
|
pingService.addPing(findById(projectId));
|
|
}
|
|
|
|
public List<GrantDto> getAllGrants() {
|
|
List<GrantDto> grants = convert(grantRepository.findAll(), GrantDto::new);
|
|
return grants;
|
|
}
|
|
|
|
public List<GrantDto> getProjectGrants(List<Integer> grantIds) {
|
|
return convert(grantRepository.findAllById(grantIds), GrantDto::new);
|
|
}
|
|
|
|
public void attachGrant(ProjectDto projectDto) {
|
|
if (!projectDto.getGrantIds().isEmpty()) {
|
|
projectDto.getGrants().clear();
|
|
projectDto.setGrants(getProjectGrants(projectDto.getGrantIds()));
|
|
} else {
|
|
projectDto.getGrants().clear();
|
|
}
|
|
}
|
|
|
|
}
|