From 91d216801243eca967042c17ccd7b0c224dbc4b2 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 5 Apr 2019 13:34:38 +0400 Subject: [PATCH 1/2] #82 show paper formatted lines --- .../core/controller/AdviceController.java | 8 +---- .../paper/controller/PaperController.java | 2 +- .../paper/controller/PaperRestController.java | 5 +++ .../ru/ulstu/paper/service/PaperService.java | 31 +++++++++++++++++-- src/main/java/ru/ulstu/user/model/User.java | 9 ++++++ 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/ulstu/core/controller/AdviceController.java b/src/main/java/ru/ulstu/core/controller/AdviceController.java index 1c71df1..8238797 100644 --- a/src/main/java/ru/ulstu/core/controller/AdviceController.java +++ b/src/main/java/ru/ulstu/core/controller/AdviceController.java @@ -20,7 +20,6 @@ import ru.ulstu.user.error.UserNotActivatedException; import ru.ulstu.user.error.UserNotFoundException; import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException; import ru.ulstu.user.error.UserResetKeyError; -import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; import java.util.Set; @@ -28,7 +27,6 @@ import java.util.stream.Collectors; @ControllerAdvice public class AdviceController { - private final static String USER_NAME_TEMPLATE = "%s %s %s"; private final Logger log = LoggerFactory.getLogger(AdviceController.class); private final UserService userService; @@ -38,11 +36,7 @@ public class AdviceController { @ModelAttribute("currentUser") public String getCurrentUser() { - User user = userService.getCurrentUser(); - return String.format(USER_NAME_TEMPLATE, - user.getLastName(), - user.getFirstName().substring(0, 1), - user.getPatronymic().substring(0, 1)); + return userService.getCurrentUser().getUserAbbreviate(); } private Response handleException(ErrorConstants error) { diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 9169bd9..27fec4e 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -51,7 +51,7 @@ public class PaperController { @GetMapping("/dashboard") public void getDashboard(ModelMap modelMap) { - modelMap.put("papers", paperService.findAllActive()); + modelMap.put("papers", paperService.findAllActiveDto()); } @GetMapping("/paper") diff --git a/src/main/java/ru/ulstu/paper/controller/PaperRestController.java b/src/main/java/ru/ulstu/paper/controller/PaperRestController.java index 5dd6b9f..2bd8384 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperRestController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperRestController.java @@ -61,4 +61,9 @@ public class PaperRestController { public Response> filter(@RequestBody @Valid PaperFilterDto paperFilterDto) throws IOException { return new Response<>(paperService.filter(paperFilterDto)); } + + @GetMapping("formatted-list") + public Response> getFormattedPaperList() { + return new Response<>(paperService.getFormattedPaperList()); + } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index aab62d1..a93c9c6 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -21,6 +21,7 @@ import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import static java.util.stream.Collectors.toList; import static org.springframework.util.ObjectUtils.isEmpty; @@ -34,6 +35,7 @@ import static ru.ulstu.paper.model.Paper.PaperStatus.ON_PREPARATION; @Service public class PaperService { private final static int MAX_DISPLAY_SIZE = 40; + private final static String PAPER_FORMATTED_TEMPLATE = "%s %s"; private final PaperNotificationService paperNotificationService; private final PaperRepository paperRepository; @@ -66,13 +68,17 @@ public class PaperService { return papers; } - public List findAllActive() { - return findAllDto() + public List findAllActive() { + return findAll() .stream() .filter(paper -> paper.getStatus() != COMPLETED && paper.getStatus() != FAILED) .collect(toList()); } + public List findAllActiveDto() { + return convert(findAllActive(), PaperDto::new); + } + public PaperDto findOneDto(Integer id) { return new PaperDto(paperRepository.findOne(id)); } @@ -212,4 +218,25 @@ public class PaperService { public List getPaperAuthors() { return userService.findAll(); } + + public List getFormattedPaperList() { + return findAllCompleted() + .stream() + .map(paper -> String.format(PAPER_FORMATTED_TEMPLATE, paper.getTitle(), getAuthors(paper))) + .collect(toList()); + } + + private List findAllCompleted() { + return findAll() + .stream() + .filter(paper -> paper.getStatus() == COMPLETED) + .collect(toList()); + } + + private String getAuthors(Paper paper) { + return paper.getAuthors() + .stream() + .map(User::getUserAbbreviate) + .collect(Collectors.joining(", ")); + } } diff --git a/src/main/java/ru/ulstu/user/model/User.java b/src/main/java/ru/ulstu/user/model/User.java index de9f028..089d1c9 100644 --- a/src/main/java/ru/ulstu/user/model/User.java +++ b/src/main/java/ru/ulstu/user/model/User.java @@ -24,6 +24,8 @@ import java.util.Set; @Entity @Table(name = "users") public class User extends BaseEntity { + private final static String USER_ABBREVIATE_TEMPLATE = "%s %s %s"; + @NotNull @Pattern(regexp = Constants.LOGIN_REGEX) @Size(min = 1, max = 50) @@ -186,4 +188,11 @@ public class User extends BaseEntity { public void setPatronymic(String patronymic) { this.patronymic = patronymic; } + + public String getUserAbbreviate() { + return String.format(USER_ABBREVIATE_TEMPLATE, + lastName == null ? "" : lastName, + firstName == null ? "" : firstName.substring(0, 1), + patronymic == null ? "" : patronymic.substring(0, 1)); + } } From 54edeb0559bbfc159a3e61f0ca79a35093f61e67 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 5 Apr 2019 13:39:53 +0400 Subject: [PATCH 2/2] #82 fix user abbreviate --- src/main/java/ru/ulstu/user/model/User.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/user/model/User.java b/src/main/java/ru/ulstu/user/model/User.java index 089d1c9..06f407b 100644 --- a/src/main/java/ru/ulstu/user/model/User.java +++ b/src/main/java/ru/ulstu/user/model/User.java @@ -24,7 +24,7 @@ import java.util.Set; @Entity @Table(name = "users") public class User extends BaseEntity { - private final static String USER_ABBREVIATE_TEMPLATE = "%s %s %s"; + private final static String USER_ABBREVIATE_TEMPLATE = "%s %s%s"; @NotNull @Pattern(regexp = Constants.LOGIN_REGEX) @@ -192,7 +192,7 @@ public class User extends BaseEntity { public String getUserAbbreviate() { return String.format(USER_ABBREVIATE_TEMPLATE, lastName == null ? "" : lastName, - firstName == null ? "" : firstName.substring(0, 1), - patronymic == null ? "" : patronymic.substring(0, 1)); + firstName == null ? "" : firstName.substring(0, 1) + ".", + patronymic == null ? "" : patronymic.substring(0, 1) + "."); } }