some refactor

This commit is contained in:
Anton Romanov 2018-12-28 13:57:20 +04:00
parent bcc64cda96
commit ce78f8ad66
8 changed files with 42 additions and 151 deletions

View File

@ -3,7 +3,12 @@ package ru.ulstu.grant.controller;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; 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.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.deadline.model.DeadlineDto; import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.model.GrantDto;
@ -49,7 +54,7 @@ public class GrantController {
public String save(@Valid GrantDto grantDto, Errors errors) throws IOException { public String save(@Valid GrantDto grantDto, Errors errors) throws IOException {
filterEmptyDeadlines(grantDto); filterEmptyDeadlines(grantDto);
if (grantDto.getDeadlines().isEmpty()) { if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode","Не может быть пустым"); errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
} }
if (errors.hasErrors()) { if (errors.hasErrors()) {
return "/grants/grant"; return "/grants/grant";

View File

@ -6,9 +6,20 @@ 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 javax.persistence.*; import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.*; import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
@Entity @Entity
@Table(name = "grants") @Table(name = "grants")
@ -21,12 +32,14 @@ public class Grant extends BaseEntity {
COMPLETED("Завершен"), COMPLETED("Завершен"),
FAILED("Провалены сроки"); FAILED("Провалены сроки");
private String name; private String statusName;
GrantStatus(String name) {
this.name = name; GrantStatus(String statusName) {
this.statusName = statusName;
} }
public String getName() {
return name; public String getStatusName() {
return statusName;
} }
} }
@ -81,13 +94,17 @@ public class Grant extends BaseEntity {
return application; return application;
} }
public void setApplication(FileData application) { this.application = application; } public void setApplication(FileData application) {
this.application = application;
}
public String getTitle() { public String getTitle() {
return title; return title;
} }
public void setTitle(String title) { this.title = title; } public void setTitle(String title) {
this.title = title;
}
public Project getProject() { public Project getProject() {
return project; return project;

View File

@ -1,14 +1,12 @@
package ru.ulstu.grant.model; package ru.ulstu.grant.model;
import ru.ulstu.grant.model.Grant;
public class GrantStatusDto { public class GrantStatusDto {
private final String id; private final String id;
private final String name; private final String name;
public GrantStatusDto(Grant.GrantStatus status) { public GrantStatusDto(Grant.GrantStatus status) {
this.id = status.name(); this.id = status.name();
this.name = status.getName(); this.name = status.getStatusName();
} }
public String getId() { public String getId() {

View File

@ -14,13 +14,13 @@ import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.service.ProjectService; import ru.ulstu.project.service.ProjectService;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.Date;
import java.util.List;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert; 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.APPLICATION;
import static ru.ulstu.grant.model.Grant.GrantStatus.IN_WORK;
@Service @Service
public class GrantService { public class GrantService {
@ -59,7 +59,6 @@ public class GrantService {
public Integer create(GrantDto grantDto) throws IOException { public Integer create(GrantDto grantDto) throws IOException {
Grant newGrant = copyFromDto(new Grant(), grantDto); Grant newGrant = copyFromDto(new Grant(), grantDto);
newGrant = grantRepository.save(newGrant); newGrant = grantRepository.save(newGrant);
//paperNotificationService.sendCreateNotification(newGrant);
return newGrant.getId(); return newGrant.getId();
} }
@ -67,10 +66,9 @@ 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());
if (grantDto.getProject() != null) { if (grantDto.getProject() != null && grantDto.getProject().getId() != null) {
grant.setProject(projectService.findById(grantDto.getProject().getId())); 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()));
@ -90,9 +88,6 @@ public class GrantService {
if (grantDto.getApplicationFileName() != null && grant.getApplication() != null) { if (grantDto.getApplicationFileName() != null && grant.getApplication() != null) {
fileService.deleteFile(grant.getApplication()); fileService.deleteFile(grant.getApplication());
} }
// if (paper.getStatus() != oldStatus) { //TODO: доделать потом все уведомления
// paperNotificationService.statusChangeNotification(paper, oldStatus);
// }
grantRepository.save(copyFromDto(grant, grantDto)); grantRepository.save(copyFromDto(grant, grantDto));
return grant.getId(); return grant.getId();
} }
@ -120,48 +115,9 @@ public class GrantService {
grant.setStatus(APPLICATION); grant.setStatus(APPLICATION);
grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн")); grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
grant = grantRepository.save(grant); grant = grantRepository.save(grant);
// paperNotificationService.sendCreateNotification(paper);
return grant; return grant;
} }
// 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(GrantDto grantDto) throws IOException { public void save(GrantDto grantDto) throws IOException {
if (isEmpty(grantDto.getId())) { if (isEmpty(grantDto.getId())) {
create(grantDto); create(grantDto);
@ -169,8 +125,4 @@ public class GrantService {
update(grantDto); update(grantDto);
} }
} }
public GrantDto findById(Integer grantId) {
return new GrantDto(grantRepository.findOne(grantId));
}
} }

View File

@ -5,6 +5,4 @@ import ru.ulstu.project.model.Project;
public interface ProjectRepository extends JpaRepository<Project, Integer> { 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);
} }

View File

@ -10,11 +10,9 @@ import ru.ulstu.project.repository.ProjectRepository;
import java.util.List; import java.util.List;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert;
@Service @Service
public class ProjectService { public class ProjectService {
private final static int MAX_DISPLAY_SIZE = 40;
private final ProjectRepository projectRepository; private final ProjectRepository projectRepository;
private final DeadlineService deadlineService; private final DeadlineService deadlineService;
@ -33,7 +31,6 @@ public class ProjectService {
public Project create(ProjectDto projectDto) { 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);
return newProject; return newProject;
} }
@ -52,87 +49,11 @@ public class ProjectService {
} }
private Project update(ProjectDto projectDto) { private Project update(ProjectDto projectDto) {
return null; throw new RuntimeException("not implemented yet");
} }
public Project findById(Integer id) { public Project findById(Integer id) {
return projectRepository.findOne(id); return projectRepository.findOne(id);
} }
// 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());
// }
// 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 GrantDto findById(Integer grantId) {
// return new GrantDto(grantRepository.findOne(grantId));
// }
} }

View File

@ -17,4 +17,5 @@
<include file="db/changelog-20181108_000000-data.xml"/> <include file="db/changelog-20181108_000000-data.xml"/>
<include file="db/changelog-20181111_000000-schema.xml"/> <include file="db/changelog-20181111_000000-schema.xml"/>
<include file="db/changelog-20181208_000000-schema.xml"/> <include file="db/changelog-20181208_000000-schema.xml"/>
<include file="db/changelog-20181224_000000-schema.xml"/>
</databaseChangeLog> </databaseChangeLog>

View File

@ -11,8 +11,7 @@
</div> </div>
<div class="col col-10 text-right"> <div class="col col-10 text-right">
<h7 class="service-heading" th:text="${grant.title}"> title</h7> <h7 class="service-heading" th:text="${grant.title}"> title</h7>
<p class="text-muted" th:text="${grant.comment}"> comment</p> <p class="text-muted" th:text="${grant.status.statusName}"> status</p>
<p class="text-muted" th:text="${grant.status}"> status</p>
</div> </div>
</div> </div>
</div> </div>