Update classes
This commit is contained in:
parent
2f68339ba2
commit
d7721ce19d
@ -2,17 +2,16 @@ package ru.ulstu.grant.model;
|
|||||||
|
|
||||||
import org.hibernate.validator.constraints.NotBlank;
|
import org.hibernate.validator.constraints.NotBlank;
|
||||||
import ru.ulstu.core.model.BaseEntity;
|
import ru.ulstu.core.model.BaseEntity;
|
||||||
import ru.ulstu.core.model.UserContainer;
|
|
||||||
import ru.ulstu.deadline.model.Deadline;
|
import ru.ulstu.deadline.model.Deadline;
|
||||||
import ru.ulstu.file.model.FileData;
|
import ru.ulstu.file.model.FileData;
|
||||||
import ru.ulstu.project.model.Project;
|
import ru.ulstu.project.model.Project;
|
||||||
import ru.ulstu.user.model.User;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
|
@Table(name = "grants")
|
||||||
public class Grant extends BaseEntity {
|
public class Grant extends BaseEntity {
|
||||||
public enum GrantStatus {
|
public enum GrantStatus {
|
||||||
APPLICATION("Заявка"),
|
APPLICATION("Заявка"),
|
||||||
@ -23,11 +22,9 @@ public class Grant extends BaseEntity {
|
|||||||
FAILED("Провалены сроки");
|
FAILED("Провалены сроки");
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
GrantStatus(String name) {
|
GrantStatus(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import org.hibernate.validator.constraints.NotEmpty;
|
|||||||
import ru.ulstu.deadline.model.DeadlineDto;
|
import ru.ulstu.deadline.model.DeadlineDto;
|
||||||
import ru.ulstu.project.model.ProjectDto;
|
import ru.ulstu.project.model.ProjectDto;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
@ -15,7 +16,7 @@ public class GrantDto {
|
|||||||
@NotEmpty
|
@NotEmpty
|
||||||
private String title;
|
private String title;
|
||||||
private Grant.GrantStatus status;
|
private Grant.GrantStatus status;
|
||||||
private List<DeadlineDto> deadlines;
|
private List<DeadlineDto> deadlines = new ArrayList<>();
|
||||||
private String comment;
|
private String comment;
|
||||||
private String applicationFileName;
|
private String applicationFileName;
|
||||||
private ProjectDto project;
|
private ProjectDto project;
|
||||||
|
@ -5,19 +5,12 @@ import org.springframework.ui.ModelMap;
|
|||||||
import org.springframework.validation.Errors;
|
import org.springframework.validation.Errors;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import ru.ulstu.deadline.model.DeadlineDto;
|
import ru.ulstu.deadline.model.DeadlineDto;
|
||||||
|
import ru.ulstu.grant.model.Grant;
|
||||||
import ru.ulstu.grant.model.GrantDto;
|
import ru.ulstu.grant.model.GrantDto;
|
||||||
import ru.ulstu.grant.model.service.GrantService;
|
import ru.ulstu.grant.model.service.GrantService;
|
||||||
import ru.ulstu.paper.model.Paper;
|
|
||||||
import ru.ulstu.paper.model.PaperDto;
|
|
||||||
import ru.ulstu.paper.model.PaperFilterDto;
|
|
||||||
import ru.ulstu.paper.service.PaperService;
|
|
||||||
import ru.ulstu.project.model.ProjectDto;
|
|
||||||
import ru.ulstu.user.model.User;
|
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -40,7 +33,7 @@ public class GrantController {
|
|||||||
|
|
||||||
@GetMapping("/dashboard")
|
@GetMapping("/dashboard")
|
||||||
public void getDashboard(ModelMap modelMap) {
|
public void getDashboard(ModelMap modelMap) {
|
||||||
modelMap.put("dashboard", grantService.findAllDto());
|
modelMap.put("grants", grantService.findAllDto());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/grant")
|
@GetMapping("/grant")
|
||||||
@ -75,12 +68,12 @@ public class GrantController {
|
|||||||
return "/grants/grant";
|
return "/grants/grant";
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/grant", params = "addProject")
|
@PostMapping(value = "/grant", params = "createProject")
|
||||||
public String addProject(@Valid GrantDto grantDto, Errors errors) {
|
public String createProject(@Valid GrantDto grantDto, Errors errors) {
|
||||||
if (errors.hasErrors()) {
|
if (errors.hasErrors()) {
|
||||||
return "/grants/grant";
|
return "/grants/grant";
|
||||||
}
|
}
|
||||||
grantDto.setProject(new ProjectDto());
|
grantService.createProject(grantDto);
|
||||||
return "/grants/grant";
|
return "/grants/grant";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +83,11 @@ public class GrantController {
|
|||||||
return "redirect:/grants/grants";
|
return "redirect:/grants/grants";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ModelAttribute("allStatuses")
|
||||||
|
public List<Grant.GrantStatus> getGrantStatuses() {
|
||||||
|
return grantService.getGrantStatuses();
|
||||||
|
}
|
||||||
|
|
||||||
private void filterEmptyDeadlines(GrantDto grantDto) {
|
private void filterEmptyDeadlines(GrantDto grantDto) {
|
||||||
grantDto.setDeadlines(grantDto.getDeadlines().stream()
|
grantDto.setDeadlines(grantDto.getDeadlines().stream()
|
||||||
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
|
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
|
||||||
|
@ -9,8 +9,10 @@ import ru.ulstu.file.service.FileService;
|
|||||||
import ru.ulstu.grant.model.Grant;
|
import ru.ulstu.grant.model.Grant;
|
||||||
import ru.ulstu.grant.model.GrantDto;
|
import ru.ulstu.grant.model.GrantDto;
|
||||||
import ru.ulstu.grant.model.repository.GrantRepository;
|
import ru.ulstu.grant.model.repository.GrantRepository;
|
||||||
import ru.ulstu.paper.model.PaperDto;
|
|
||||||
import ru.ulstu.project.model.Project;
|
import ru.ulstu.project.model.Project;
|
||||||
|
import ru.ulstu.project.model.ProjectDto;
|
||||||
|
import ru.ulstu.project.repository.ProjectRepository;
|
||||||
|
import ru.ulstu.project.service.ProjectService;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -26,29 +28,32 @@ public class GrantService {
|
|||||||
private final static int MAX_DISPLAY_SIZE = 40;
|
private final static int MAX_DISPLAY_SIZE = 40;
|
||||||
|
|
||||||
private final GrantRepository grantRepository;
|
private final GrantRepository grantRepository;
|
||||||
|
private final ProjectService projectService;
|
||||||
private final DeadlineService deadlineService;
|
private final DeadlineService deadlineService;
|
||||||
private final FileService fileService;
|
private final FileService fileService;
|
||||||
|
|
||||||
public GrantService(GrantRepository grantRepository,
|
public GrantService(GrantRepository grantRepository,
|
||||||
FileService fileService,
|
FileService fileService,
|
||||||
DeadlineService deadlineService) {
|
DeadlineService deadlineService,
|
||||||
|
ProjectService projectService) {
|
||||||
this.grantRepository = grantRepository;
|
this.grantRepository = grantRepository;
|
||||||
|
this.projectService = projectService;
|
||||||
this.fileService = fileService;
|
this.fileService = fileService;
|
||||||
this.deadlineService = deadlineService;
|
this.deadlineService = deadlineService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Grant> findAll() {
|
public List<Grant> findAll() {
|
||||||
return sortGrants(grantRepository.findAll());
|
return grantRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<GrantDto> findAllDto() {
|
public List<GrantDto> findAllDto() {
|
||||||
List<GrantDto> grants = convert(findAll(), ru.ulstu.grant.model.GrantDto::new);
|
List<GrantDto> grants = convert(findAll(), GrantDto::new);
|
||||||
grants.forEach(grantDto -> grantDto.setTitle(StringUtils.abbreviate(grantDto.getTitle(), MAX_DISPLAY_SIZE)));
|
grants.forEach(grantDto -> grantDto.setTitle(StringUtils.abbreviate(grantDto.getTitle(), MAX_DISPLAY_SIZE)));
|
||||||
return grants;
|
return grants;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GrantDto findOneDto(Integer id) {
|
public GrantDto findOneDto(Integer id) {
|
||||||
return new ru.ulstu.grant.model.GrantDto(grantRepository.findOne(id));
|
return new GrantDto(grantRepository.findOne(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -63,7 +68,10 @@ public class GrantService {
|
|||||||
grant.setComment(grantDto.getComment());
|
grant.setComment(grantDto.getComment());
|
||||||
grant.setStatus(grantDto.getStatus() == null ? APPLICATION : grantDto.getStatus());
|
grant.setStatus(grantDto.getStatus() == null ? APPLICATION : grantDto.getStatus());
|
||||||
grant.setTitle(grantDto.getTitle());
|
grant.setTitle(grantDto.getTitle());
|
||||||
//grant.setProject(grantDto.getProject()); //TODO: Исправить!
|
if (grantDto.getProject() != null) {
|
||||||
|
grant.setProject(projectService.findById(grantDto.getProject().getId()));
|
||||||
|
}
|
||||||
|
//grant. setdeadlineDate(grant.getdeadlineDate() == null ? new Date() : grant.getdeadlineDate());
|
||||||
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
|
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
|
||||||
if (grantDto.getApplicationFileName() != null) {
|
if (grantDto.getApplicationFileName() != null) {
|
||||||
grant.setApplication(fileService.createFileFromTmp(grantDto.getApplicationFileName()));
|
grant.setApplication(fileService.createFileFromTmp(grantDto.getApplicationFileName()));
|
||||||
@ -71,6 +79,11 @@ public class GrantService {
|
|||||||
return grant;
|
return grant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createProject(GrantDto grantDto) {
|
||||||
|
grantDto.setProject(
|
||||||
|
new ProjectDto(projectService.save(new ProjectDto(grantDto.getTitle()))));
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Integer update(GrantDto grantDto) throws IOException {
|
public Integer update(GrantDto grantDto) throws IOException {
|
||||||
Grant grant = grantRepository.findOne(grantDto.getId());
|
Grant grant = grantRepository.findOne(grantDto.getId());
|
||||||
@ -130,7 +143,7 @@ public class GrantService {
|
|||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ru.ulstu.grant.model.GrantDto findGrant(int id) {
|
public GrantDto findGrant(int id) {
|
||||||
return new ru.ulstu.grant.model.GrantDto(grantRepository.getOne(id));
|
return new ru.ulstu.grant.model.GrantDto(grantRepository.getOne(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,19 +4,27 @@ import com.fasterxml.jackson.annotation.JsonCreator;
|
|||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import org.hibernate.validator.constraints.NotEmpty;
|
import org.hibernate.validator.constraints.NotEmpty;
|
||||||
import ru.ulstu.deadline.model.DeadlineDto;
|
import ru.ulstu.deadline.model.DeadlineDto;
|
||||||
|
import ru.ulstu.project.model.Project;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
import static ru.ulstu.core.util.StreamApiUtils.convert;
|
||||||
|
|
||||||
public class ProjectDto {
|
public class ProjectDto {
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
private String title;
|
private String title;
|
||||||
private List<DeadlineDto> deadlines;
|
|
||||||
|
private List<DeadlineDto> deadlines = new ArrayList<>();
|
||||||
|
|
||||||
public ProjectDto() {}
|
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,
|
||||||
|
@ -41,30 +41,35 @@ public class ProjectService {
|
|||||||
return projectRepository.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
|
@Transactional
|
||||||
public Integer create(ProjectDto projectDto) throws IOException {
|
public Project create(ProjectDto projectDto) {
|
||||||
Project newProject = copyFromDto(new Project(), projectDto);
|
Project newProject = copyFromDto(new Project(), projectDto);
|
||||||
newProject = projectRepository.save(newProject);
|
newProject = projectRepository.save(newProject);
|
||||||
//paperNotificationService.sendCreateNotification(newGrant);
|
//paperNotificationService.sendCreateNotification(newGrant);
|
||||||
return newProject.getId();
|
return newProject;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Project copyFromDto(Project project, ProjectDto projectDto) throws IOException {
|
private Project copyFromDto(Project project, ProjectDto projectDto) {
|
||||||
project.setTitle(projectDto.getTitle());
|
project.setTitle(projectDto.getTitle());
|
||||||
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
|
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines()));
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Project save(ProjectDto projectDto) {
|
||||||
|
if (isEmpty(projectDto.getId())) {
|
||||||
|
return create(projectDto);
|
||||||
|
} else {
|
||||||
|
return update(projectDto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Project update(ProjectDto projectDto) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Project findById(Integer id) {
|
||||||
|
return projectRepository.findOne(id);
|
||||||
|
}
|
||||||
// public void createProject(GrantDto grantDto) {
|
// public void createProject(GrantDto grantDto) {
|
||||||
// grantDto.setProject(new ProjectDto(grantDto.getTitle()));
|
// grantDto.setProject(new ProjectDto(grantDto.getTitle()));
|
||||||
// }
|
// }
|
||||||
@ -97,14 +102,7 @@ public class ProjectService {
|
|||||||
// return Arrays.asList(Grant.GrantStatus.values());
|
// 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) {
|
// public List<PaperDto> filter(PaperFilterDto filterDto) {
|
||||||
// return convert(sortPapers(paperRepository.filter(
|
// return convert(sortPapers(paperRepository.filter(
|
||||||
@ -144,13 +142,7 @@ public class ProjectService {
|
|||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
public void save(ProjectDto projectDto) throws IOException {
|
|
||||||
if (isEmpty(projectDto.getId())) {
|
|
||||||
create(projectDto);
|
|
||||||
} else {
|
|
||||||
//update(grantDto);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// public GrantDto findById(Integer grantId) {
|
// public GrantDto findById(Integer grantId) {
|
||||||
// return new GrantDto(grantRepository.findOne(grantId));
|
// return new GrantDto(grantRepository.findOne(grantId));
|
||||||
|
Loading…
Reference in New Issue
Block a user