|
|
|
@ -1,100 +1,68 @@
|
|
|
|
|
package ru.ulstu.activity.project.service;
|
|
|
|
|
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import org.thymeleaf.util.StringUtils;
|
|
|
|
|
import ru.ulstu.activity.common.service.ActivityService;
|
|
|
|
|
import ru.ulstu.activity.deadline.service.DeadlineService;
|
|
|
|
|
import ru.ulstu.activity.file.model.FileDataDto;
|
|
|
|
|
import ru.ulstu.activity.file.service.FileService;
|
|
|
|
|
import ru.ulstu.activity.grant.model.GrantDto;
|
|
|
|
|
import ru.ulstu.activity.grant.repository.GrantRepository;
|
|
|
|
|
import ru.ulstu.activity.grant.service.GrantService;
|
|
|
|
|
import ru.ulstu.activity.ping.service.PingService;
|
|
|
|
|
import ru.ulstu.activity.project.model.Project;
|
|
|
|
|
import ru.ulstu.activity.project.model.ProjectDashboardDto;
|
|
|
|
|
import ru.ulstu.activity.project.model.ProjectDto;
|
|
|
|
|
import ru.ulstu.activity.project.model.ProjectListDto;
|
|
|
|
|
import ru.ulstu.activity.project.repository.ProjectRepository;
|
|
|
|
|
import ru.ulstu.activity.timeline.service.EventService;
|
|
|
|
|
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
|
|
|
|
import ru.ulstu.core.model.response.PageableItems;
|
|
|
|
|
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.activity.project.model.Project.ProjectStatus.CLOSED;
|
|
|
|
|
import static ru.ulstu.activity.project.model.Project.ProjectStatus.FAILED;
|
|
|
|
|
import static ru.ulstu.activity.project.model.Project.ProjectStatus.TECHNICAL_TASK;
|
|
|
|
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
|
|
|
|
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class ProjectService {
|
|
|
|
|
private final static int MAX_DISPLAY_SIZE = 40;
|
|
|
|
|
|
|
|
|
|
public class ProjectService extends ActivityService<ProjectListDto, Project, ProjectDto> {
|
|
|
|
|
private final ProjectRepository projectRepository;
|
|
|
|
|
private final DeadlineService deadlineService;
|
|
|
|
|
private final GrantRepository grantRepository;
|
|
|
|
|
private final GrantService grantService;
|
|
|
|
|
private final FileService fileService;
|
|
|
|
|
private final EventService eventService;
|
|
|
|
|
private final UserService userService;
|
|
|
|
|
private final PingService pingService;
|
|
|
|
|
|
|
|
|
|
public ProjectService(ProjectRepository projectRepository,
|
|
|
|
|
ProjectNotificationService projectNotificationService,
|
|
|
|
|
DeadlineService deadlineService,
|
|
|
|
|
GrantRepository grantRepository,
|
|
|
|
|
GrantService grantService,
|
|
|
|
|
FileService fileService,
|
|
|
|
|
EventService eventService,
|
|
|
|
|
UserService userService,
|
|
|
|
|
PingService pingService) {
|
|
|
|
|
super(projectRepository, projectNotificationService, pingService);
|
|
|
|
|
this.projectRepository = projectRepository;
|
|
|
|
|
this.deadlineService = deadlineService;
|
|
|
|
|
this.grantRepository = grantRepository;
|
|
|
|
|
this.grantService = grantService;
|
|
|
|
|
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());
|
|
|
|
|
public PageableItems<ProjectDashboardDto> findAllActiveDto(int offset, int count) {
|
|
|
|
|
return convertPageable(findAllActive(offset, count), ProjectDashboardDto::new);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
private 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;
|
|
|
|
|
private PageableItems<Project> findAllActive(int offset, int count) {
|
|
|
|
|
Page<Project> activeProjectPage = projectRepository.findAllWithoutStatuses(new OffsetablePageRequest(offset, count), CLOSED, FAILED);
|
|
|
|
|
return new PageableItems<>(activeProjectPage.getTotalElements(), activeProjectPage.getContent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public boolean delete(Integer projectId) throws IOException {
|
|
|
|
|
public boolean delete(Integer projectId) {
|
|
|
|
|
if (projectRepository.existsById(projectId)) {
|
|
|
|
|
Project project = projectRepository.getOne(projectId);
|
|
|
|
|
projectRepository.delete(project);
|
|
|
|
@ -103,12 +71,12 @@ public class ProjectService {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Project copyFromDto(Project project, ProjectDto projectDto) throws IOException {
|
|
|
|
|
protected 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.setGrant(grantService.findById(projectDto.getGrant().getId()));
|
|
|
|
|
}
|
|
|
|
|
project.setRepository(projectDto.getRepository());
|
|
|
|
|
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
|
|
|
|
@ -117,17 +85,24 @@ public class ProjectService {
|
|
|
|
|
.collect(toList())));
|
|
|
|
|
project.getGrants().clear();
|
|
|
|
|
if (projectDto.getGrantIds() != null && !projectDto.getGrantIds().isEmpty()) {
|
|
|
|
|
projectDto.getGrantIds().forEach(grantIds -> project.getGrants().add(grantRepository.findGrantById(grantIds)));
|
|
|
|
|
projectDto.getGrantIds().forEach(grantIds -> project.getGrants().add(grantService.findById(grantIds)));
|
|
|
|
|
}
|
|
|
|
|
return project;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Project save(ProjectDto projectDto) throws IOException {
|
|
|
|
|
if (isEmpty(projectDto.getId())) {
|
|
|
|
|
return create(projectDto);
|
|
|
|
|
} else {
|
|
|
|
|
return update(projectDto);
|
|
|
|
|
@Override
|
|
|
|
|
protected ProjectListDto getActivityListDto(Project entity) {
|
|
|
|
|
return new ProjectListDto(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Project getNewActivity() {
|
|
|
|
|
return new Project();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected ProjectDto getNewActivityDto(Project entity) {
|
|
|
|
|
return new ProjectDto(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ProjectDto removeDeadline(ProjectDto projectDto, Integer deadlineId) {
|
|
|
|
@ -146,26 +121,7 @@ public class ProjectService {
|
|
|
|
|
return userService.findAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public void ping(int projectId) {
|
|
|
|
|
pingService.addPing(findById(projectId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<GrantDto> getAllGrants() {
|
|
|
|
|
return convert(grantRepository.findAll(), GrantDto::new);
|
|
|
|
|
public ProjectDto findProjectById(Integer projectId) {
|
|
|
|
|
return getNewActivityDto(findById(projectId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|