fix another controller
This commit is contained in:
parent
7a8456a0aa
commit
8a245584ca
@ -1,9 +1,15 @@
|
|||||||
package ru.ulstu.activity.api;
|
package ru.ulstu.activity.api;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
public interface ActivityRepository<T> {
|
public interface ActivityRepository<T> {
|
||||||
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
|
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
|
||||||
|
|
||||||
T save(T t);
|
T save(T t);
|
||||||
|
|
||||||
|
T getById(Integer id);
|
||||||
|
|
||||||
|
Page<T> findAll(Pageable pageable);
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package ru.ulstu.activity.common.service;
|
package ru.ulstu.activity.common.service;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import ru.ulstu.activity.api.ActivityRepository;
|
import ru.ulstu.activity.api.ActivityRepository;
|
||||||
import ru.ulstu.activity.api.model.ActivityDto;
|
import ru.ulstu.activity.api.model.ActivityDto;
|
||||||
import ru.ulstu.activity.api.model.ActivityListDto;
|
import ru.ulstu.activity.api.model.ActivityListDto;
|
||||||
import ru.ulstu.activity.common.model.AbstractActivity;
|
import ru.ulstu.activity.common.model.AbstractActivity;
|
||||||
import ru.ulstu.activity.ping.service.PingService;
|
import ru.ulstu.activity.ping.service.PingService;
|
||||||
|
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
||||||
import ru.ulstu.core.model.response.PageableItems;
|
import ru.ulstu.core.model.response.PageableItems;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -37,6 +39,22 @@ public abstract class ActivityService<L extends ActivityListDto, T extends Abstr
|
|||||||
return getNewActivityDto(create(newEntity));
|
return getNewActivityDto(create(newEntity));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public T update(T entity) {
|
||||||
|
return activityRepository.save(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public D update(D entityDto) {
|
||||||
|
T newEntity;
|
||||||
|
try {
|
||||||
|
newEntity = copyFromDto(activityRepository.getById(entityDto.getId()), entityDto);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return getNewActivityDto(update(newEntity));
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public T create(T entity) {
|
public T create(T entity) {
|
||||||
T newEntity = activityRepository.save(entity);
|
T newEntity = activityRepository.save(entity);
|
||||||
@ -44,15 +62,15 @@ public abstract class ActivityService<L extends ActivityListDto, T extends Abstr
|
|||||||
return newEntity;
|
return newEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract T update(T entity);
|
|
||||||
|
|
||||||
public abstract D update(D entity);
|
|
||||||
|
|
||||||
public abstract boolean delete(Integer id);
|
public abstract boolean delete(Integer id);
|
||||||
|
|
||||||
public abstract T findById(Integer id);
|
public abstract T findById(Integer id);
|
||||||
|
|
||||||
public abstract PageableItems<T> findAll(int offset, int count);
|
|
||||||
|
public PageableItems<T> findAll(int offset, int count) {
|
||||||
|
final Page<T> page = activityRepository.findAll(new OffsetablePageRequest(offset, count));
|
||||||
|
return new PageableItems<>(page.getTotalElements(), page.getContent());
|
||||||
|
}
|
||||||
|
|
||||||
public PageableItems<L> findAllDto(int offset, int count) {
|
public PageableItems<L> findAllDto(int offset, int count) {
|
||||||
return convertPageable(findAll(offset, count), entity -> getActivityListDto(entity));
|
return convertPageable(findAll(offset, count), entity -> getActivityListDto(entity));
|
||||||
|
@ -71,11 +71,6 @@ public class ConferenceService extends ActivityService<ConferenceListDto, Confer
|
|||||||
return conferenceDto;
|
return conferenceDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PageableItems<Conference> findAll(int offset, int count) {
|
|
||||||
final Page<Conference> page = conferenceRepository.findAll(new OffsetablePageRequest(offset, count));
|
|
||||||
return new PageableItems<>(page.getTotalElements(), page.getContent());
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean save(ConferenceDto conferenceDto, Errors errors) throws IOException {
|
public boolean save(ConferenceDto conferenceDto, Errors errors) throws IOException {
|
||||||
conferenceDto.setTitle(conferenceDto.getTitle());
|
conferenceDto.setTitle(conferenceDto.getTitle());
|
||||||
filterEmptyDeadlines(conferenceDto);
|
filterEmptyDeadlines(conferenceDto);
|
||||||
|
@ -20,7 +20,6 @@ import ru.ulstu.activity.paper.model.Paper;
|
|||||||
import ru.ulstu.activity.paper.model.PaperDto;
|
import ru.ulstu.activity.paper.model.PaperDto;
|
||||||
import ru.ulstu.activity.paper.service.PaperService;
|
import ru.ulstu.activity.paper.service.PaperService;
|
||||||
import ru.ulstu.activity.ping.service.PingService;
|
import ru.ulstu.activity.ping.service.PingService;
|
||||||
import ru.ulstu.activity.project.model.ProjectDto;
|
|
||||||
import ru.ulstu.activity.project.service.ProjectService;
|
import ru.ulstu.activity.project.service.ProjectService;
|
||||||
import ru.ulstu.activity.timeline.service.EventService;
|
import ru.ulstu.activity.timeline.service.EventService;
|
||||||
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
||||||
@ -172,12 +171,6 @@ public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto>
|
|||||||
return grant;
|
return grant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void createProject(GrantDto grantDto) throws IOException {
|
|
||||||
grantDto.setProject(
|
|
||||||
new ProjectDto(projectService.save(new ProjectDto(grantDto.getTitle()))));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean delete(Integer grantId) {
|
public boolean delete(Integer grantId) {
|
||||||
Grant grant = findById(grantId);
|
Grant grant = findById(grantId);
|
||||||
|
@ -102,13 +102,6 @@ public class PaperService extends ActivityService<PaperListDto, Paper, PaperDto>
|
|||||||
return new PageableItems<>(activePapersPage.getTotalElements(), sortPapers(activePapersPage.getContent()));
|
return new PageableItems<>(activePapersPage.getTotalElements(), sortPapers(activePapersPage.getContent()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Paper update(Paper paper) {
|
|
||||||
paperRepository.save(paper);
|
|
||||||
paperNotificationService.sendCreateNotification(paper, oldAuthors);
|
|
||||||
paperNotificationService.statusChangeNotification(paper, oldStatus);
|
|
||||||
return paper;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public PaperDto update(PaperDto paperDto) {
|
public PaperDto update(PaperDto paperDto) {
|
||||||
Paper paper = findById(paperDto.getId());
|
Paper paper = findById(paperDto.getId());
|
||||||
|
@ -1,127 +1,68 @@
|
|||||||
package ru.ulstu.activity.project.controller;
|
package ru.ulstu.activity.project.controller;
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.ui.ModelMap;
|
|
||||||
import org.springframework.validation.Errors;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import ru.ulstu.activity.deadline.model.Deadline;
|
import ru.ulstu.activity.api.ActivityController;
|
||||||
import ru.ulstu.activity.grant.model.GrantDto;
|
import ru.ulstu.activity.project.model.ProjectDashboardDto;
|
||||||
import ru.ulstu.activity.project.model.Project;
|
|
||||||
import ru.ulstu.activity.project.model.ProjectDto;
|
import ru.ulstu.activity.project.model.ProjectDto;
|
||||||
|
import ru.ulstu.activity.project.model.ProjectListDto;
|
||||||
import ru.ulstu.activity.project.service.ProjectService;
|
import ru.ulstu.activity.project.service.ProjectService;
|
||||||
import ru.ulstu.user.model.User;
|
import ru.ulstu.configuration.Constants;
|
||||||
import springfox.documentation.annotations.ApiIgnore;
|
import ru.ulstu.core.model.response.PageableItems;
|
||||||
|
import ru.ulstu.core.model.response.Response;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static org.springframework.util.StringUtils.isEmpty;
|
@RestController
|
||||||
|
@RequestMapping(Constants.API_1_0 + "projects")
|
||||||
@Controller()
|
public class ProjectController implements ActivityController<ProjectListDto, ProjectDashboardDto, ProjectDto> {
|
||||||
@RequestMapping(value = "/projects")
|
|
||||||
@ApiIgnore
|
|
||||||
public class ProjectController {
|
|
||||||
private final ProjectService projectService;
|
private final ProjectService projectService;
|
||||||
|
|
||||||
public ProjectController(ProjectService projectService) {
|
public ProjectController(ProjectService projectService) {
|
||||||
this.projectService = projectService;
|
this.projectService = projectService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/dashboard")
|
@GetMapping("list")
|
||||||
public void getDashboard(ModelMap modelMap) {
|
public Response<PageableItems<ProjectListDto>> getList(@RequestParam(value = "offset", defaultValue = "0") int offset,
|
||||||
modelMap.put("projects", projectService.findAllDto());
|
@RequestParam(value = "count", defaultValue = "10") int count) {
|
||||||
|
return new Response<>(projectService.findAllDto(offset, count));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/projects")
|
@GetMapping("dashboard")
|
||||||
public void getProjects(ModelMap modelMap) {
|
public Response<PageableItems<ProjectDashboardDto>> getDashboard(@RequestParam(value = "offset", defaultValue = "0") int offset,
|
||||||
modelMap.put("projects", projectService.findAllDto());
|
@RequestParam(value = "count", defaultValue = "10") int count) {
|
||||||
|
return new Response<>(projectService.findAllActiveDto(offset, count));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/project")
|
@GetMapping("{project-id}")
|
||||||
public void getProject(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
|
public Response<ProjectDto> get(@PathVariable("project-id") Integer projectId) {
|
||||||
if (id != null && id > 0) {
|
return new Response<>(projectService.findProjectById(projectId));
|
||||||
ProjectDto projectDto = projectService.findOneDto(id);
|
|
||||||
attachGrant(projectDto);
|
|
||||||
modelMap.put("projectDto", projectService.findOneDto(id));
|
|
||||||
} else {
|
|
||||||
modelMap.put("projectDto", new ProjectDto());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute("allStatuses")
|
@PostMapping
|
||||||
public List<Project.ProjectStatus> getProjectStatuses() {
|
public Response<ProjectDto> create(@RequestBody @Valid ProjectDto projectDto) {
|
||||||
return projectService.getProjectStatuses();
|
return new Response<>(projectService.create(projectDto));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/project", params = "save")
|
@PutMapping
|
||||||
public String save(@Valid ProjectDto projectDto, Errors errors) throws IOException {
|
public Response<ProjectDto> update(@RequestBody @Valid ProjectDto projectDto) {
|
||||||
filterEmptyDeadlines(projectDto);
|
return new Response<>(projectService.update(projectDto));
|
||||||
if (projectDto.getDeadlines().isEmpty()) {
|
|
||||||
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
|
|
||||||
}
|
|
||||||
if (errors.hasErrors()) {
|
|
||||||
return "/projects/project";
|
|
||||||
}
|
|
||||||
projectService.save(projectDto);
|
|
||||||
return String.format("redirect:%s", "/projects/projects");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/project", params = "attachGrant")
|
@DeleteMapping("/{project-id}")
|
||||||
public String attachGrant(ProjectDto projectDto) {
|
public Response<Boolean> delete(@PathVariable("project-id") Integer projectId) {
|
||||||
projectService.attachGrant(projectDto);
|
return new Response<>(projectService.delete(projectId));
|
||||||
return "/projects/project";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/project", params = "addDeadline")
|
@PostMapping("ping/{project-id}")
|
||||||
public String addDeadline(@Valid ProjectDto projectDto, Errors errors) {
|
public void ping(@PathVariable("project-id") int projectId) {
|
||||||
filterEmptyDeadlines(projectDto);
|
|
||||||
if (errors.hasErrors()) {
|
|
||||||
return "/projects/project";
|
|
||||||
}
|
|
||||||
projectDto.getDeadlines().add(new Deadline());
|
|
||||||
return "/projects/project";
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping(value = "/project", params = "removeDeadline")
|
|
||||||
public String removeDeadline(ProjectDto projectDto,
|
|
||||||
@RequestParam(value = "removeDeadline") Integer deadlineId) {
|
|
||||||
projectService.removeDeadline(projectDto, deadlineId);
|
|
||||||
return "/projects/project";
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/delete/{project-id}")
|
|
||||||
public String delete(@PathVariable("project-id") Integer projectId) throws IOException {
|
|
||||||
projectService.delete(projectId);
|
|
||||||
return String.format("redirect:%s", "/projects/projects");
|
|
||||||
}
|
|
||||||
|
|
||||||
@ModelAttribute("allExecutors")
|
|
||||||
public List<User> getAllExecutors(ProjectDto projectDto) {
|
|
||||||
return projectService.getProjectExecutors(projectDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ModelAttribute("allGrants")
|
|
||||||
public List<GrantDto> getAllGrants() {
|
|
||||||
return projectService.getAllGrants();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void filterEmptyDeadlines(ProjectDto projectDto) {
|
|
||||||
projectDto.setDeadlines(projectDto.getDeadlines().stream()
|
|
||||||
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@ResponseBody
|
|
||||||
@PostMapping(value = "/ping")
|
|
||||||
public void ping(@RequestParam("projectId") int projectId) throws IOException {
|
|
||||||
projectService.ping(projectId);
|
projectService.ping(projectId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package ru.ulstu.activity.project.model;
|
||||||
|
|
||||||
|
import ru.ulstu.activity.api.model.ActivityDashboardDto;
|
||||||
|
import ru.ulstu.activity.common.model.ScienceGroupMemberDto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
|
public class ProjectDashboardDto extends ActivityDashboardDto {
|
||||||
|
private final Project.ProjectStatus status;
|
||||||
|
private final List<ScienceGroupMemberDto> executors;
|
||||||
|
|
||||||
|
public ProjectDashboardDto(Project project) {
|
||||||
|
super(project.getId(), project.getTitle());
|
||||||
|
this.status = project.getStatus();
|
||||||
|
this.executors = convert(project.getExecutors(), ScienceGroupMemberDto::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Project.ProjectStatus getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ScienceGroupMemberDto> getExecutors() {
|
||||||
|
return executors;
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@ package ru.ulstu.activity.project.model;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import org.thymeleaf.util.StringUtils;
|
import ru.ulstu.activity.api.model.ActivityDto;
|
||||||
import ru.ulstu.activity.deadline.model.Deadline;
|
import ru.ulstu.activity.deadline.model.Deadline;
|
||||||
import ru.ulstu.activity.file.model.FileDataDto;
|
import ru.ulstu.activity.file.model.FileDataDto;
|
||||||
import ru.ulstu.activity.grant.model.GrantDto;
|
import ru.ulstu.activity.grant.model.GrantDto;
|
||||||
@ -14,13 +14,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
public class ProjectDto {
|
public class ProjectDto extends ActivityDto {
|
||||||
private Integer id;
|
|
||||||
|
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
private String title;
|
private String title;
|
||||||
private Project.ProjectStatus status;
|
private Project.ProjectStatus status;
|
||||||
@ -35,15 +32,6 @@ public class ProjectDto {
|
|||||||
private List<Integer> grantIds;
|
private List<Integer> grantIds;
|
||||||
private List<GrantDto> grants;
|
private List<GrantDto> grants;
|
||||||
|
|
||||||
private final static int MAX_EXECUTORS_LENGTH = 40;
|
|
||||||
|
|
||||||
public ProjectDto() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public ProjectDto(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonCreator
|
@JsonCreator
|
||||||
public ProjectDto(@JsonProperty("id") Integer id,
|
public ProjectDto(@JsonProperty("id") Integer id,
|
||||||
@JsonProperty("title") String title,
|
@JsonProperty("title") String title,
|
||||||
@ -57,7 +45,7 @@ public class ProjectDto {
|
|||||||
@JsonProperty("executors") List<UserDto> executors,
|
@JsonProperty("executors") List<UserDto> executors,
|
||||||
@JsonProperty("grantIds") List<Integer> grantIds,
|
@JsonProperty("grantIds") List<Integer> grantIds,
|
||||||
@JsonProperty("grants") List<GrantDto> grants) {
|
@JsonProperty("grants") List<GrantDto> grants) {
|
||||||
this.id = id;
|
super(id);
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
@ -73,8 +61,8 @@ public class ProjectDto {
|
|||||||
|
|
||||||
|
|
||||||
public ProjectDto(Project project) {
|
public ProjectDto(Project project) {
|
||||||
Set<User> users = new HashSet<User>(project.getExecutors());
|
super(project.getId());
|
||||||
this.id = project.getId();
|
Set<User> users = new HashSet<>(project.getExecutors());
|
||||||
this.title = project.getTitle();
|
this.title = project.getTitle();
|
||||||
this.status = project.getStatus();
|
this.status = project.getStatus();
|
||||||
this.description = project.getDescription();
|
this.description = project.getDescription();
|
||||||
@ -88,14 +76,6 @@ public class ProjectDto {
|
|||||||
this.grants = convert(project.getGrants(), GrantDto::new);
|
this.grants = convert(project.getGrants(), GrantDto::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Integer id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
public String getTitle() {
|
||||||
return title;
|
return title;
|
||||||
}
|
}
|
||||||
@ -176,13 +156,6 @@ public class ProjectDto {
|
|||||||
this.executors = executors;
|
this.executors = executors;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExecutorsString() {
|
|
||||||
return StringUtils.abbreviate(executors
|
|
||||||
.stream()
|
|
||||||
.map(executor -> executor.getLastName())
|
|
||||||
.collect(Collectors.joining(", ")), MAX_EXECUTORS_LENGTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Integer> getGrantIds() {
|
public List<Integer> getGrantIds() {
|
||||||
return grantIds;
|
return grantIds;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package ru.ulstu.activity.project.model;
|
||||||
|
|
||||||
|
import ru.ulstu.activity.api.model.ActivityListDto;
|
||||||
|
import ru.ulstu.activity.common.model.ScienceGroupMemberDto;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
|
public class ProjectListDto extends ActivityListDto {
|
||||||
|
private final Project.ProjectStatus status;
|
||||||
|
private final List<ScienceGroupMemberDto> executors;
|
||||||
|
|
||||||
|
public ProjectListDto(Project project) {
|
||||||
|
super(project.getId(), project.getTitle());
|
||||||
|
this.status = project.getStatus();
|
||||||
|
this.executors = convert(project.getExecutors(), ScienceGroupMemberDto::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Project.ProjectStatus getStatus() {
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ScienceGroupMemberDto> getExecutors() {
|
||||||
|
return executors;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,14 @@
|
|||||||
package ru.ulstu.activity.project.repository;
|
package ru.ulstu.activity.project.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import ru.ulstu.activity.api.ActivityRepository;
|
||||||
import ru.ulstu.activity.project.model.Project;
|
import ru.ulstu.activity.project.model.Project;
|
||||||
|
|
||||||
public interface ProjectRepository extends JpaRepository<Project, Integer> {
|
public interface ProjectRepository extends JpaRepository<Project, Integer>, ActivityRepository<Project> {
|
||||||
|
@Query("SELECT p FROM Project p WHERE p.status NOT IN (:statuses)")
|
||||||
|
Page<Project> findAllWithoutStatuses(Pageable pageable, @Param("statuses") Project.ProjectStatus... statuses);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
package ru.ulstu.activity.project.service;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.activity.common.service.ActivityNotificationService;
|
||||||
|
import ru.ulstu.activity.project.model.Project;
|
||||||
|
import ru.ulstu.user.service.MailService;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ProjectNotificationService extends ActivityNotificationService<Project> {
|
||||||
|
|
||||||
|
private final MailService mailService;
|
||||||
|
|
||||||
|
public ProjectNotificationService(MailService mailService) {
|
||||||
|
this.mailService = mailService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendCreateNotification(Project project) {
|
||||||
|
throw new RuntimeException("not implemented yet");
|
||||||
|
}
|
||||||
|
}
|
@ -1,100 +1,68 @@
|
|||||||
package ru.ulstu.activity.project.service;
|
package ru.ulstu.activity.project.service;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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.deadline.service.DeadlineService;
|
||||||
import ru.ulstu.activity.file.model.FileDataDto;
|
|
||||||
import ru.ulstu.activity.file.service.FileService;
|
import ru.ulstu.activity.file.service.FileService;
|
||||||
import ru.ulstu.activity.grant.model.GrantDto;
|
import ru.ulstu.activity.grant.service.GrantService;
|
||||||
import ru.ulstu.activity.grant.repository.GrantRepository;
|
|
||||||
import ru.ulstu.activity.ping.service.PingService;
|
import ru.ulstu.activity.ping.service.PingService;
|
||||||
import ru.ulstu.activity.project.model.Project;
|
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.ProjectDto;
|
||||||
|
import ru.ulstu.activity.project.model.ProjectListDto;
|
||||||
import ru.ulstu.activity.project.repository.ProjectRepository;
|
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.model.User;
|
||||||
import ru.ulstu.user.service.UserService;
|
import ru.ulstu.user.service.UserService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static java.util.stream.Collectors.toList;
|
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.activity.project.model.Project.ProjectStatus.TECHNICAL_TASK;
|
||||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProjectService {
|
public class ProjectService extends ActivityService<ProjectListDto, Project, ProjectDto> {
|
||||||
private final static int MAX_DISPLAY_SIZE = 40;
|
|
||||||
|
|
||||||
private final ProjectRepository projectRepository;
|
private final ProjectRepository projectRepository;
|
||||||
private final DeadlineService deadlineService;
|
private final DeadlineService deadlineService;
|
||||||
private final GrantRepository grantRepository;
|
private final GrantService grantService;
|
||||||
private final FileService fileService;
|
private final FileService fileService;
|
||||||
private final EventService eventService;
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final PingService pingService;
|
private final PingService pingService;
|
||||||
|
|
||||||
public ProjectService(ProjectRepository projectRepository,
|
public ProjectService(ProjectRepository projectRepository,
|
||||||
|
ProjectNotificationService projectNotificationService,
|
||||||
DeadlineService deadlineService,
|
DeadlineService deadlineService,
|
||||||
GrantRepository grantRepository,
|
GrantService grantService,
|
||||||
FileService fileService,
|
FileService fileService,
|
||||||
EventService eventService,
|
|
||||||
UserService userService,
|
UserService userService,
|
||||||
PingService pingService) {
|
PingService pingService) {
|
||||||
|
super(projectRepository, projectNotificationService, pingService);
|
||||||
this.projectRepository = projectRepository;
|
this.projectRepository = projectRepository;
|
||||||
this.deadlineService = deadlineService;
|
this.deadlineService = deadlineService;
|
||||||
this.grantRepository = grantRepository;
|
this.grantService = grantService;
|
||||||
this.fileService = fileService;
|
this.fileService = fileService;
|
||||||
this.eventService = eventService;
|
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.pingService = pingService;
|
this.pingService = pingService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Project> findAll() {
|
public PageableItems<ProjectDashboardDto> findAllActiveDto(int offset, int count) {
|
||||||
return projectRepository.findAll();
|
return convertPageable(findAllActive(offset, count), ProjectDashboardDto::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ProjectDto> findAllDto() {
|
private PageableItems<Project> findAllActive(int offset, int count) {
|
||||||
List<ProjectDto> projects = convert(findAll(), ProjectDto::new);
|
Page<Project> activeProjectPage = projectRepository.findAllWithoutStatuses(new OffsetablePageRequest(offset, count), CLOSED, FAILED);
|
||||||
projects.forEach(projectDto -> projectDto.setTitle(StringUtils.abbreviate(projectDto.getTitle(), MAX_DISPLAY_SIZE)));
|
return new PageableItems<>(activeProjectPage.getTotalElements(), activeProjectPage.getContent());
|
||||||
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
|
@Transactional
|
||||||
public Project create(ProjectDto projectDto) throws IOException {
|
public boolean delete(Integer projectId) {
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public boolean delete(Integer projectId) throws IOException {
|
|
||||||
if (projectRepository.existsById(projectId)) {
|
if (projectRepository.existsById(projectId)) {
|
||||||
Project project = projectRepository.getOne(projectId);
|
Project project = projectRepository.getOne(projectId);
|
||||||
projectRepository.delete(project);
|
projectRepository.delete(project);
|
||||||
@ -103,12 +71,12 @@ public class ProjectService {
|
|||||||
return false;
|
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.setDescription(projectDto.getDescription());
|
||||||
project.setStatus(projectDto.getStatus() == null ? TECHNICAL_TASK : projectDto.getStatus());
|
project.setStatus(projectDto.getStatus() == null ? TECHNICAL_TASK : projectDto.getStatus());
|
||||||
project.setTitle(projectDto.getTitle());
|
project.setTitle(projectDto.getTitle());
|
||||||
if (projectDto.getGrant() != null && projectDto.getGrant().getId() != null) {
|
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.setRepository(projectDto.getRepository());
|
||||||
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
|
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
|
||||||
@ -117,17 +85,24 @@ public class ProjectService {
|
|||||||
.collect(toList())));
|
.collect(toList())));
|
||||||
project.getGrants().clear();
|
project.getGrants().clear();
|
||||||
if (projectDto.getGrantIds() != null && !projectDto.getGrantIds().isEmpty()) {
|
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;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Project save(ProjectDto projectDto) throws IOException {
|
@Override
|
||||||
if (isEmpty(projectDto.getId())) {
|
protected ProjectListDto getActivityListDto(Project entity) {
|
||||||
return create(projectDto);
|
return new ProjectListDto(entity);
|
||||||
} else {
|
}
|
||||||
return update(projectDto);
|
|
||||||
}
|
@Override
|
||||||
|
protected Project getNewActivity() {
|
||||||
|
return new Project();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ProjectDto getNewActivityDto(Project entity) {
|
||||||
|
return new ProjectDto(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProjectDto removeDeadline(ProjectDto projectDto, Integer deadlineId) {
|
public ProjectDto removeDeadline(ProjectDto projectDto, Integer deadlineId) {
|
||||||
@ -146,26 +121,7 @@ public class ProjectService {
|
|||||||
return userService.findAll();
|
return userService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
public ProjectDto findProjectById(Integer projectId) {
|
||||||
public void ping(int projectId) {
|
return getNewActivityDto(findById(projectId));
|
||||||
pingService.addPing(findById(projectId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<GrantDto> getAllGrants() {
|
|
||||||
return convert(grantRepository.findAll(), GrantDto::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -93,7 +93,7 @@ public class ProjectServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
public void findAll() {
|
public void findAll() {
|
||||||
when(projectRepository.findAll()).thenReturn(projects);
|
when(projectRepository.findAll()).thenReturn(projects);
|
||||||
assertEquals(projects, projectService.findAll());
|
assertEquals(projects, projectService.findAll(0, 100).getItems());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -137,8 +137,8 @@ public class ProjectServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void removeDeadline() throws IOException {
|
public void removeDeadline() {
|
||||||
ProjectDto newProjectDto = new ProjectDto();
|
ProjectDto newProjectDto = new ProjectDto(null);
|
||||||
newProjectDto.getRemovedDeadlineIds().add(INDEX);
|
newProjectDto.getRemovedDeadlineIds().add(INDEX);
|
||||||
projectDto.getDeadlines().add(deadline);
|
projectDto.getDeadlines().add(deadline);
|
||||||
ProjectDto result = projectService.removeDeadline(projectDto, INDEX);
|
ProjectDto result = projectService.removeDeadline(projectDto, INDEX);
|
||||||
|
Loading…
Reference in New Issue
Block a user