From de96be2471eeeef34b59b008344182853c466f79 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 15 Mar 2019 12:19:01 +0400 Subject: [PATCH 1/4] Create Navigation class to avoid "magic strings" and code duplication --- .../grant/controller/GrantController.java | 24 +++++++++---------- .../ru/ulstu/grant/controller/Navigation.java | 16 +++++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/main/java/ru/ulstu/grant/controller/Navigation.java diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 415dd0b..8310f84 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -20,6 +20,10 @@ import java.util.List; import java.util.stream.Collectors; import static org.springframework.util.StringUtils.isEmpty; +import static ru.ulstu.grant.controller.Navigation.GRANTS_PAGE; +import static ru.ulstu.grant.controller.Navigation.GRANT_PAGE; +import static ru.ulstu.grant.controller.Navigation.REDIRECT_TO; +import static ru.ulstu.grant.controller.Navigation.hasErrors; @Controller() @@ -56,36 +60,30 @@ public class GrantController { if (grantDto.getDeadlines().isEmpty()) { errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); } - if (errors.hasErrors()) { - return "/grants/grant"; - } + hasErrors(errors, GRANT_PAGE); grantService.save(grantDto); - return "redirect:/grants/grants"; + return String.format(REDIRECT_TO, GRANTS_PAGE); } @PostMapping(value = "/grant", params = "addDeadline") public String addDeadline(@Valid GrantDto grantDto, Errors errors) { filterEmptyDeadlines(grantDto); - if (errors.hasErrors()) { - return "/grants/grant"; - } + hasErrors(errors, GRANT_PAGE); grantDto.getDeadlines().add(new DeadlineDto()); - return "/grants/grant"; + return GRANT_PAGE; } @PostMapping(value = "/grant", params = "createProject") public String createProject(@Valid GrantDto grantDto, Errors errors) { - if (errors.hasErrors()) { - return "/grants/grant"; - } + hasErrors(errors, GRANT_PAGE); grantService.createProject(grantDto); - return "/grants/grant"; + return GRANT_PAGE; } @GetMapping("/delete/{grant-id}") public String delete(@PathVariable("grant-id") Integer grantId) throws IOException { grantService.delete(grantId); - return "redirect:/grants/grants"; + return String.format(REDIRECT_TO, GRANTS_PAGE); } @ModelAttribute("allStatuses") diff --git a/src/main/java/ru/ulstu/grant/controller/Navigation.java b/src/main/java/ru/ulstu/grant/controller/Navigation.java new file mode 100644 index 0000000..bf1626f --- /dev/null +++ b/src/main/java/ru/ulstu/grant/controller/Navigation.java @@ -0,0 +1,16 @@ +package ru.ulstu.grant.controller; + +import org.springframework.validation.Errors; + +public class Navigation { + public static final String REDIRECT_TO = "redirect:%s"; + public static final String GRANTS_PAGE = "/grants/grants"; + public static final String GRANT_PAGE = "/grants/grant"; + + public static String hasErrors(Errors errors, String page) { + if (errors.hasErrors()) { + return page; + } + return null; + } +} From c25c17d8bac4bf1cf98017916640028d80e1a553 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 15 Mar 2019 12:19:25 +0400 Subject: [PATCH 2/4] Rename title --- src/main/resources/templates/default.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index c440bb7..9aae873 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -8,7 +8,7 @@ - NG-Tacker + NG-Tracker From 99d85a5648615fd648f0c0ab779f2e38299767b4 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 15 Mar 2019 12:24:02 +0400 Subject: [PATCH 3/4] Add new status for grant --- src/main/java/ru/ulstu/grant/model/Grant.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/grant/model/Grant.java b/src/main/java/ru/ulstu/grant/model/Grant.java index 8d40e3a..685a1d5 100644 --- a/src/main/java/ru/ulstu/grant/model/Grant.java +++ b/src/main/java/ru/ulstu/grant/model/Grant.java @@ -30,7 +30,8 @@ public class Grant extends BaseEntity { SUCCESSFUL_PASSAGE("Успешное прохождение"), IN_WORK("В работе"), COMPLETED("Завершен"), - FAILED("Провалены сроки"); + FAILED("Провалены сроки"), + LOADED_FROM_KIAS("Загружен автоматически"); private String statusName; From 29df7120bf820ae855f25c81bea4d50e83c013dd Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 18 Mar 2019 14:27:54 +0400 Subject: [PATCH 4/4] Delete DeadlineDTO and update usages --- .../ru/ulstu/deadline/model/Deadline.java | 13 +++++ .../ru/ulstu/deadline/model/DeadlineDto.java | 58 ------------------- .../deadline/service/DeadlineService.java | 33 +++++------ .../grant/controller/GrantController.java | 4 +- .../java/ru/ulstu/grant/model/GrantDto.java | 16 +++-- .../paper/controller/PaperController.java | 4 +- .../java/ru/ulstu/paper/model/PaperDto.java | 14 ++--- .../ru/ulstu/project/model/ProjectDto.java | 14 ++--- 8 files changed, 53 insertions(+), 103 deletions(-) delete mode 100644 src/main/java/ru/ulstu/deadline/model/DeadlineDto.java diff --git a/src/main/java/ru/ulstu/deadline/model/Deadline.java b/src/main/java/ru/ulstu/deadline/model/Deadline.java index 1a73e59..404e5c8 100644 --- a/src/main/java/ru/ulstu/deadline/model/Deadline.java +++ b/src/main/java/ru/ulstu/deadline/model/Deadline.java @@ -1,5 +1,8 @@ package ru.ulstu.deadline.model; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import org.springframework.format.annotation.DateTimeFormat; import ru.ulstu.core.model.BaseEntity; import javax.persistence.Entity; @@ -13,6 +16,7 @@ public class Deadline extends BaseEntity { private String description; @Temporal(value = TemporalType.TIMESTAMP) + @DateTimeFormat(pattern = "yyyy-MM-dd") private Date date; public Deadline() { @@ -23,6 +27,15 @@ public class Deadline extends BaseEntity { this.description = description; } + @JsonCreator + public Deadline(@JsonProperty("id") Integer id, + @JsonProperty("description") String description, + @JsonProperty("date") Date date) { + this.setId(id); + this.description = description; + this.date = date; + } + public String getDescription() { return description; } diff --git a/src/main/java/ru/ulstu/deadline/model/DeadlineDto.java b/src/main/java/ru/ulstu/deadline/model/DeadlineDto.java deleted file mode 100644 index 7f724aa..0000000 --- a/src/main/java/ru/ulstu/deadline/model/DeadlineDto.java +++ /dev/null @@ -1,58 +0,0 @@ -package ru.ulstu.deadline.model; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; -import org.springframework.format.annotation.DateTimeFormat; - -import java.util.Date; - -public class DeadlineDto { - private Integer id; - - private String description; - - @DateTimeFormat(pattern = "yyyy-MM-dd") - private Date date; - - public DeadlineDto() { - } - - @JsonCreator - public DeadlineDto(@JsonProperty("id") Integer id, - @JsonProperty("description") String description, - @JsonProperty("date") Date date) { - this.id = id; - this.description = description; - this.date = date; - } - - public DeadlineDto(Deadline deadline) { - this.id = deadline.getId(); - this.description = deadline.getDescription(); - this.date = deadline.getDate(); - } - - public Integer getId() { - return id; - } - - public String getDescription() { - return description; - } - - public Date getDate() { - return date; - } - - public void setId(Integer id) { - this.id = id; - } - - public void setDescription(String description) { - this.description = description; - } - - public void setDate(Date date) { - this.date = date; - } -} diff --git a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java index c371d87..2180025 100644 --- a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java +++ b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java @@ -3,7 +3,6 @@ package ru.ulstu.deadline.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.deadline.model.Deadline; -import ru.ulstu.deadline.model.DeadlineDto; import ru.ulstu.deadline.repository.DeadlineRepository; import java.util.List; @@ -17,29 +16,29 @@ public class DeadlineService { this.deadlineRepository = deadlineRepository; } - public List saveOrCreate(List deadlines) { - return deadlines.stream().map(deadlineDto -> { - return deadlineDto.getId() != null ? update(deadlineDto) : create(deadlineDto); - }).collect(Collectors.toList()); + public List saveOrCreate(List deadlines) { + return deadlines + .stream() + .map(deadline -> { + return deadline.getId() != null ? update(deadline) : create(deadline); + }).collect(Collectors.toList()); } @Transactional - public Deadline update(DeadlineDto deadlineDto) { - Deadline deadline = deadlineRepository.findOne(deadlineDto.getId()); - deadlineRepository.save(copyFromDto(deadline, deadlineDto)); - return deadline; + public Deadline update(Deadline deadline) { + Deadline updateDeadline = deadlineRepository.findOne(deadline.getId()); + updateDeadline.setDate(deadline.getDate()); + updateDeadline.setDescription(deadline.getDescription()); + deadlineRepository.save(updateDeadline); + return updateDeadline; } @Transactional - public Deadline create(DeadlineDto deadlineDto) { - Deadline newDeadline = copyFromDto(new Deadline(), deadlineDto); + public Deadline create(Deadline deadline) { + Deadline newDeadline = new Deadline(); + newDeadline.setDate(deadline.getDate()); + newDeadline.setDescription(deadline.getDescription()); newDeadline = deadlineRepository.save(newDeadline); return newDeadline; } - - private Deadline copyFromDto(Deadline deadline, DeadlineDto deadlineDto) { - deadline.setDate(deadlineDto.getDate()); - deadline.setDescription(deadlineDto.getDescription()); - return deadline; - } } diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 8310f84..c786ffa 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -9,7 +9,7 @@ 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.Deadline; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.service.GrantService; @@ -69,7 +69,7 @@ public class GrantController { public String addDeadline(@Valid GrantDto grantDto, Errors errors) { filterEmptyDeadlines(grantDto); hasErrors(errors, GRANT_PAGE); - grantDto.getDeadlines().add(new DeadlineDto()); + grantDto.getDeadlines().add(new Deadline()); return GRANT_PAGE; } diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index e56896a..fb29164 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -3,33 +3,31 @@ package ru.ulstu.grant.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotEmpty; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.project.model.ProjectDto; import java.util.ArrayList; import java.util.List; -import static ru.ulstu.core.util.StreamApiUtils.convert; - public class GrantDto { private Integer id; @NotEmpty private String title; private Grant.GrantStatus status; - private List deadlines = new ArrayList<>(); + private List deadlines = new ArrayList<>(); private String comment; private String applicationFileName; private ProjectDto project; public GrantDto() { - deadlines.add(new DeadlineDto()); + deadlines.add(new Deadline()); } @JsonCreator public GrantDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, @JsonProperty("status") Grant.GrantStatus status, - @JsonProperty("deadlines") List deadlines, + @JsonProperty("deadlines") List deadlines, @JsonProperty("comment") String comment, @JsonProperty("project") ProjectDto project) { this.id = id; @@ -45,7 +43,7 @@ public class GrantDto { this.id = grant.getId(); this.title = grant.getTitle(); this.status = grant.getStatus(); - this.deadlines = convert(grant.getDeadlines(), DeadlineDto::new); + this.deadlines = grant.getDeadlines(); this.comment = grant.getComment(); this.project = grant.getProject() == null ? null : new ProjectDto(grant.getProject()); this.applicationFileName = grant.getApplication() == null ? null : grant.getApplication().getName(); @@ -75,11 +73,11 @@ public class GrantDto { this.status = status; } - public List getDeadlines() { + public List getDeadlines() { return deadlines; } - public void setDeadlines(List deadlines) { + public void setDeadlines(List deadlines) { this.deadlines = deadlines; } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index cb34a8a..31df0e3 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -9,7 +9,7 @@ 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.Deadline; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperFilterDto; @@ -80,7 +80,7 @@ public class PaperController { if (errors.hasErrors()) { return "/papers/paper"; } - paperDto.getDeadlines().add(new DeadlineDto()); + paperDto.getDeadlines().add(new Deadline()); return "/papers/paper"; } diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index 545b432..3405768 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.lang3.StringUtils; import org.hibernate.validator.constraints.NotEmpty; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.user.model.UserDto; import javax.validation.constraints.Size; @@ -27,7 +27,7 @@ public class PaperDto { private Date createDate; private Date updateDate; @NotEmpty - private List deadlines = new ArrayList<>(); + private List deadlines = new ArrayList<>(); private String comment; private Boolean locked; private String tmpFileName; @@ -39,7 +39,7 @@ public class PaperDto { private Integer filterAuthorId; public PaperDto() { - deadlines.add(new DeadlineDto()); + deadlines.add(new Deadline()); } @JsonCreator @@ -48,7 +48,7 @@ public class PaperDto { @JsonProperty("status") Paper.PaperStatus status, @JsonProperty("createDate") Date createDate, @JsonProperty("updateDate") Date updateDate, - @JsonProperty("deadlines") List deadlines, + @JsonProperty("deadlines") List deadlines, @JsonProperty("comment") String comment, @JsonProperty("locked") Boolean locked, @JsonProperty("tmpFileName") String tmpFileName, @@ -75,7 +75,7 @@ public class PaperDto { this.status = paper.getStatus(); this.createDate = paper.getCreateDate(); this.updateDate = paper.getUpdateDate(); - this.deadlines = convert(paper.getDeadlines(), DeadlineDto::new); + this.deadlines = paper.getDeadlines(); this.comment = paper.getComment(); this.locked = paper.getLocked(); this.tmpFileName = null; @@ -126,11 +126,11 @@ public class PaperDto { this.updateDate = updateDate; } - public List getDeadlines() { + public List getDeadlines() { return deadlines; } - public void setDeadlines(List deadlines) { + public void setDeadlines(List deadlines) { this.deadlines = deadlines; } diff --git a/src/main/java/ru/ulstu/project/model/ProjectDto.java b/src/main/java/ru/ulstu/project/model/ProjectDto.java index fdce26b..db7fc75 100644 --- a/src/main/java/ru/ulstu/project/model/ProjectDto.java +++ b/src/main/java/ru/ulstu/project/model/ProjectDto.java @@ -3,20 +3,18 @@ package ru.ulstu.project.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotEmpty; -import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.deadline.model.Deadline; import java.util.ArrayList; import java.util.List; -import static ru.ulstu.core.util.StreamApiUtils.convert; - public class ProjectDto { private Integer id; @NotEmpty private String title; - private List deadlines = new ArrayList<>(); + private List deadlines = new ArrayList<>(); public ProjectDto() { } @@ -28,7 +26,7 @@ public class ProjectDto { @JsonCreator public ProjectDto(@JsonProperty("id") Integer id, @JsonProperty("title") String title, - @JsonProperty("deadlines") List deadlines) { + @JsonProperty("deadlines") List deadlines) { this.id = id; this.title = title; this.deadlines = deadlines; @@ -38,7 +36,7 @@ public class ProjectDto { public ProjectDto(Project project) { this.id = project.getId(); this.title = project.getTitle(); - this.deadlines = convert(project.getDeadlines(), DeadlineDto::new); + this.deadlines = project.getDeadlines(); } public Integer getId() { @@ -57,11 +55,11 @@ public class ProjectDto { this.title = title; } - public List getDeadlines() { + public List getDeadlines() { return deadlines; } - public void setDeadlines(List deadlines) { + public void setDeadlines(List deadlines) { this.deadlines = deadlines; } }