Create service&repository for Project

merge-requests/24/head
T-Midnight 6 years ago
parent 307b640e8e
commit 410d0d9634

@ -0,0 +1,11 @@
package ru.ulstu.project.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.project.model.Project;
public interface ProjectRepository extends JpaRepository<Project, Integer> {
// @Query("SELECT p FROM Paper p WHERE (:author IS NULL OR :author MEMBER OF p.authors) AND (YEAR(p.createDate) = :year OR :year IS NULL)")
// List<Paper> filter(@Param("author") User author, @Param("year") Integer year);
}

@ -0,0 +1,158 @@
package ru.ulstu.project.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.service.FileService;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.model.repository.GrantRepository;
import ru.ulstu.project.model.Project;
import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.repository.ProjectRepository;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION;
import static ru.ulstu.grant.model.Grant.GrantStatus.IN_WORK;
@Service
public class ProjectService {
private final static int MAX_DISPLAY_SIZE = 40;
private final ProjectRepository projectRepository;
private final DeadlineService deadlineService;
public ProjectService(ProjectRepository projectRepository,
DeadlineService deadlineService) {
this.projectRepository = projectRepository;
this.deadlineService = deadlineService;
}
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 GrantDto findOneDto(Integer id) {
// return new GrantDto(grantRepository.findOne(id));
// }
@Transactional
public Integer create(ProjectDto projectDto) throws IOException {
Project newProject = copyFromDto(new Project(), projectDto);
newProject = projectRepository.save(newProject);
//paperNotificationService.sendCreateNotification(newGrant);
return newProject.getId();
}
private Project copyFromDto(Project project, ProjectDto projectDto) throws IOException {
project.setTitle(projectDto.getTitle());
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
return project;
}
// public void createProject(GrantDto grantDto) {
// grantDto.setProject(new ProjectDto(grantDto.getTitle()));
// }
// @Transactional
// public Integer update(GrantDto grantDto) throws IOException {
// Grant grant = grantRepository.findOne(grantDto.getId());
// Grant.GrantStatus oldStatus = grant.getStatus();
// if (grantDto.getApplicationFileName() != null && grant.getApplication() != null) {
// fileService.deleteFile(grant.getApplication());
// }
//// if (paper.getStatus() != oldStatus) {
//// paperNotificationService.statusChangeNotification(paper, oldStatus);
//// }
// grantRepository.save(copyFromDto(grant, grantDto));
// return grant.getId();
// }
// @Transactional
// public void delete(Integer grantId) throws IOException {
// Grant grant = grantRepository.findOne(grantId);
// if (grant.getApplication() != null) {
// fileService.deleteFile(grant.getApplication());
// }
// //возможно при удалении гранта будет удаляться и проект, к нему привязанный
// grantRepository.delete(grant);
// }
// public List<Grant.GrantStatus> getGrantStatuses() {
// return Arrays.asList(Grant.GrantStatus.values());
// }
@Transactional
public Project create(String title, Date deadlineDate) {
Project project = new Project();
project.setTitle(title);
project.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн у проекта"));
project = projectRepository.save(project);
return project;
}
// public List<PaperDto> filter(PaperFilterDto filterDto) {
// return convert(sortPapers(paperRepository.filter(
// filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()),
// filterDto.getYear())), PaperDto::new);
// }
// private List<Grant> sortGrants(List<Grant> grants) {
// return grants.stream().sorted((grant1, grant2) -> {
// int statusCompareResult =
// Integer.valueOf(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant1.getStatus()))
// .compareTo(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant2.getStatus()));
// if (statusCompareResult != 0) {
// return statusCompareResult;
// }
// return grant1.getTitle().compareTo(grant2.getTitle());
// }).collect(Collectors.toList());
// }
//
// public GrantDto findGrant(int id) {
// return new GrantDto(grantRepository.getOne(id));
// }
//
// public void closeFailedGrants() {
// List<Grant> grants = grantRepository.findAll()
// .stream()
// .filter(grant -> grant.getNextDeadline().isPresent()
// && (grant.getStatus() == APPLICATION
// || grant.getStatus() == IN_WORK)
// && grant.getNextDeadline().get().getDate().before(new Date()))
// .collect(Collectors.toList());
// grants.forEach(grant -> {
// //Grant.GrantStatus oldStatus = grant.getStatus();
// grant.setStatus(Grant.GrantStatus.FAILED);
// grantRepository.save(grant);
// //paperNotificationService.sendFailedNotification(grant, oldStatus);
// });
// }
public void save(ProjectDto projectDto) throws IOException {
if (isEmpty(projectDto.getId())) {
create(projectDto);
} else {
//update(grantDto);
}
}
// public GrantDto findById(Integer grantId) {
// return new GrantDto(grantRepository.findOne(grantId));
// }
}
Loading…
Cancel
Save