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.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.List; import java.util.Set; 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; 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; } public List findAll() { return projectRepository.findAll(); } public List findAllDto() { List 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.findOne(id)); } public List 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.createFromProject(newProject); return newProject; } @Transactional public Project update(ProjectDto projectDto) throws IOException { Project project = projectRepository.findOne(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 void delete(Integer projectId) throws IOException { Project project = projectRepository.findOne(projectId); projectRepository.delete(project); } 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.findOne(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 void removeDeadline(ProjectDto projectDto, Integer deadlineId) { if (deadlineId != null) { projectDto.getRemovedDeadlineIds().add(deadlineId); } projectDto.getDeadlines().remove((int) deadlineId); } public Project findById(Integer id) { return projectRepository.findOne(id); } public List getProjectExecutors(ProjectDto projectDto) { List users = userService.findAll(); return users; } public List getAllGrants() { List grants = convert(grantRepository.findAll(), GrantDto::new); return grants; } public List getProjectGrants(List grantIds) { return convert(grantRepository.findAll(grantIds), GrantDto::new); } public void attachGrant(ProjectDto projectDto) { if (!projectDto.getGrantIds().isEmpty()) { projectDto.getGrants().clear(); projectDto.setGrants(getProjectGrants(projectDto.getGrantIds())); } else { projectDto.getGrants().clear(); } } }