From 410d0d96346c2ceeba065ad648c924e44c63aada Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 24 Dec 2018 13:05:37 +0400 Subject: [PATCH] Create service&repository for Project --- .../project/repository/ProjectRepository.java | 11 ++ .../ulstu/project/service/ProjectService.java | 158 ++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 src/main/java/ru/ulstu/project/repository/ProjectRepository.java create mode 100644 src/main/java/ru/ulstu/project/service/ProjectService.java diff --git a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java new file mode 100644 index 0000000..5a41fb1 --- /dev/null +++ b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java @@ -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 { + +// @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 filter(@Param("author") User author, @Param("year") Integer year); +} diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java new file mode 100644 index 0000000..8576e9b --- /dev/null +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -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 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 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 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 filter(PaperFilterDto filterDto) { +// return convert(sortPapers(paperRepository.filter( +// filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()), +// filterDto.getYear())), PaperDto::new); +// } + +// private List sortGrants(List 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 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)); +// } +}