From bd78be93ab90b312ea0cd7da9e2e89687f7939ad Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 8 Nov 2018 19:36:50 +0400 Subject: [PATCH] add paper create strategy --- .../ControllersConfiguration.java | 2 + .../ru/ulstu/core/model/UserContainer.java | 9 ++++ .../java/ru/ulstu/core/util/DateUtils.java | 6 +++ .../paper/controller/PaperController.java | 2 +- src/main/java/ru/ulstu/paper/model/Paper.java | 21 ++++++--- .../ru/ulstu/paper/service/PaperService.java | 43 ++++++++++++++----- .../strategy/api/EntityCreateStrategy.java | 28 ++++++++++++ .../strategy/api/PaperCreateStrategy.java | 35 +++++++++++++++ .../api/StrategyEntityCreateExecutor.java | 26 +++++++++++ .../ru/ulstu/user/service/UserService.java | 5 +++ src/main/resources/application.properties | 4 +- .../paperCreateNotification.html | 24 +++++++++++ 12 files changed, 184 insertions(+), 21 deletions(-) create mode 100644 src/main/java/ru/ulstu/core/model/UserContainer.java create mode 100644 src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java create mode 100644 src/main/java/ru/ulstu/strategy/api/PaperCreateStrategy.java create mode 100644 src/main/java/ru/ulstu/strategy/api/StrategyEntityCreateExecutor.java create mode 100644 src/main/resources/mail_templates/paperCreateNotification.html diff --git a/src/main/java/ru/ulstu/configuration/ControllersConfiguration.java b/src/main/java/ru/ulstu/configuration/ControllersConfiguration.java index 2d92313..a2beab7 100644 --- a/src/main/java/ru/ulstu/configuration/ControllersConfiguration.java +++ b/src/main/java/ru/ulstu/configuration/ControllersConfiguration.java @@ -17,6 +17,8 @@ class ControllersConfiguration { config.addAllowedHeader("*"); config.addAllowedMethod("GET"); config.addAllowedMethod("POST"); + config.addAllowedMethod("PUT"); + config.addAllowedMethod("OPTIONAL"); config.addAllowedMethod("DELETE"); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); diff --git a/src/main/java/ru/ulstu/core/model/UserContainer.java b/src/main/java/ru/ulstu/core/model/UserContainer.java new file mode 100644 index 0000000..87dc97d --- /dev/null +++ b/src/main/java/ru/ulstu/core/model/UserContainer.java @@ -0,0 +1,9 @@ +package ru.ulstu.core.model; + +import ru.ulstu.user.model.User; + +import java.util.List; + +public interface UserContainer { + List getUsers(); +} diff --git a/src/main/java/ru/ulstu/core/util/DateUtils.java b/src/main/java/ru/ulstu/core/util/DateUtils.java index a8853b4..b24b09c 100644 --- a/src/main/java/ru/ulstu/core/util/DateUtils.java +++ b/src/main/java/ru/ulstu/core/util/DateUtils.java @@ -43,4 +43,10 @@ public class DateUtils { public static Date localTimeToDate(LocalTime localTime) { return Date.from(localTime.atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant()); } + + public static Date addDays(Date date, int count) { + Calendar cal = getCalendar(date); + cal.add(Calendar.DAY_OF_MONTH, count); + return cal.getTime(); + } } diff --git a/src/main/java/ru/ulstu/paper/controller/PaperController.java b/src/main/java/ru/ulstu/paper/controller/PaperController.java index 0672f82..ccf0dda 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperController.java @@ -32,7 +32,7 @@ public class PaperController { @GetMapping public Response> getPapers() { - return new Response<>(paperService.findAll()); + return new Response<>(paperService.findAllDto()); } @PostMapping diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index 77b8e0c..6b16ee3 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -2,15 +2,17 @@ package ru.ulstu.paper.model; import org.hibernate.validator.constraints.NotBlank; import ru.ulstu.core.model.BaseEntity; +import ru.ulstu.core.model.UserContainer; import ru.ulstu.file.model.FileData; import ru.ulstu.user.model.User; import javax.persistence.*; +import java.util.ArrayList; import java.util.Date; import java.util.List; @Entity -public class Paper extends BaseEntity { +public class Paper extends BaseEntity implements UserContainer { public enum PaperStatus { ATTENTION("Обратить внимание"), ON_PREPARATION("На подготовке"), DRAFT("Черновик"), COMPLETED("Завершена"); @@ -29,27 +31,27 @@ public class Paper extends BaseEntity { private String title; @Enumerated(value = EnumType.STRING) - private PaperStatus status; + private PaperStatus status = PaperStatus.DRAFT; @Column(name = "create_date") - private Date createDate; + private Date createDate = new Date(); @Column(name = "update_date") - private Date updateDate; + private Date updateDate = new Date(); @Column(name = "deadline_date") private Date deadlineDate; private String comment; - private Boolean locked; + private Boolean locked = false; @ManyToOne @JoinColumn(name = "file_id") private FileData fileData; - @ManyToMany - private List authors; + @ManyToMany(fetch = FetchType.EAGER) + private List authors = new ArrayList<>(); public PaperStatus getStatus() { return status; @@ -122,4 +124,9 @@ public class Paper extends BaseEntity { public void setAuthors(List authors) { this.authors = authors; } + + @Override + public List getUsers() { + return getAuthors(); + } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 4498b32..6c4cb1d 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -1,5 +1,6 @@ package ru.ulstu.paper.service; +import com.google.common.collect.ImmutableMap; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import ru.ulstu.file.service.FileService; @@ -7,22 +8,24 @@ import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperStatusDto; import ru.ulstu.paper.repository.PaperRepository; +import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; import ru.ulstu.user.service.MailService; import java.io.IOException; import java.util.Arrays; +import java.util.Date; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import static ru.ulstu.core.util.StreamApiUtils.convert; @Service public class PaperService { - + public final MailService mailService; private final PaperRepository paperRepository; private final FileService fileService; - public final MailService mailService; public PaperService(PaperRepository paperRepository, @@ -32,9 +35,8 @@ public class PaperService { this.mailService = mailService; } - public List findAll() { - List allPapers = paperRepository.findAll(); - allPapers = allPapers.stream().sorted((paper1, paper2) -> { + public List findAll() { + return paperRepository.findAll().stream().sorted((paper1, paper2) -> { int statusCompareResult = Integer.valueOf(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper1.getStatus())) .compareTo(Integer.valueOf(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper2.getStatus()))); @@ -43,7 +45,10 @@ public class PaperService { } return paper1.getTitle().compareTo(paper2.getTitle()); }).collect(Collectors.toList()); - return convert(allPapers, PaperDto::new); + } + + public List findAllDto() { + return convert(findAll(), PaperDto::new); } @Transactional @@ -67,8 +72,8 @@ public class PaperService { @Transactional public Integer update(PaperDto paperDto) throws IOException { Paper paper = paperRepository.findOne(paperDto.getId()); - if(paper != null && paper.getStatus() != paperDto.getStatus()){ - sendMessageAboutStatusChange(paper.getStatus(),paperDto); + if (paper != null && paper.getStatus() != paperDto.getStatus()) { + sendMessageAboutStatusChange(paper.getStatus(), paperDto); } if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { fileService.deleteFile(paper.getFileData()); @@ -86,14 +91,30 @@ public class PaperService { } public List getPaperStatuses() { - return convert(Arrays.asList(Paper.PaperStatus.values()), status ->new PaperStatusDto(status)); + return convert(Arrays.asList(Paper.PaperStatus.values()), status -> new PaperStatusDto(status)); } - private void sendMessageAboutStatusChange(Paper.PaperStatus oldStatus, PaperDto paper){ - for (UserDto user: paper.getAuthors()) { + private void sendMessageAboutStatusChange(Paper.PaperStatus oldStatus, PaperDto paper) { + for (UserDto user : paper.getAuthors()) { mailService.sendEmail(user.getEmail(), "Обновление статуса статьи", "Статус статьи " + paper.getTitle() + " сменился с " + oldStatus.getName() + " на " + paper.getStatus().getName()); } } + + @Transactional + public Paper create(String title, User user, Date deadlineDate) { + Paper paper = new Paper(); + paper.setTitle(title); + paper.getAuthors().add(user); + paper.setDeadlineDate(deadlineDate); + paper.setCreateDate(new Date()); + paper.setUpdateDate(new Date()); + paper.setStatus(Paper.PaperStatus.DRAFT); + paper = paperRepository.save(paper); + + Map variables = ImmutableMap.of("paper", paper); + mailService.sendEmailFromTemplate(variables, user, "paperCreateNotification", "Создана статья"); + return paper; + } } diff --git a/src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java b/src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java new file mode 100644 index 0000000..8449e10 --- /dev/null +++ b/src/main/java/ru/ulstu/strategy/api/EntityCreateStrategy.java @@ -0,0 +1,28 @@ +package ru.ulstu.strategy.api; + +import ru.ulstu.core.model.UserContainer; +import ru.ulstu.user.model.User; + +import java.util.List; +import java.util.stream.Collectors; + +public abstract class EntityCreateStrategy { + protected abstract List getActiveEntities(); + + protected abstract void createEntity(User user); + + protected void createDefaultEntityIfNeed(List allUsers, List entities) { + allUsers.forEach(user -> { + if (entities + .stream() + .filter(entity -> entity.getUsers().contains(user)) + .collect(Collectors.toSet()).isEmpty()) { + createEntity(user); + } + }); + } + + public void createEntityIfNeed(List allUsers) { + createDefaultEntityIfNeed(allUsers, getActiveEntities()); + } +} diff --git a/src/main/java/ru/ulstu/strategy/api/PaperCreateStrategy.java b/src/main/java/ru/ulstu/strategy/api/PaperCreateStrategy.java new file mode 100644 index 0000000..da457a1 --- /dev/null +++ b/src/main/java/ru/ulstu/strategy/api/PaperCreateStrategy.java @@ -0,0 +1,35 @@ +package ru.ulstu.strategy.api; + +import org.springframework.stereotype.Service; +import ru.ulstu.core.util.DateUtils; +import ru.ulstu.paper.model.Paper; +import ru.ulstu.paper.service.PaperService; +import ru.ulstu.user.model.User; +import ru.ulstu.user.service.UserService; + +import java.util.Date; +import java.util.List; + +@Service +public class PaperCreateStrategy extends EntityCreateStrategy { + private static final String DEFAULT_NAME = "Статья создана автоматически"; + private static final int DEFAULT_DEADLINE_DAYS = 14; + private final PaperService paperService; + private final UserService userService; + + public PaperCreateStrategy(PaperService paperService, + UserService userService) { + this.paperService = paperService; + this.userService = userService; + } + + @Override + protected List getActiveEntities() { + return paperService.findAll(); + } + + @Override + protected void createEntity(User user) { + paperService.create(DEFAULT_NAME, user, DateUtils.addDays(new Date(), DEFAULT_DEADLINE_DAYS)); + } +} diff --git a/src/main/java/ru/ulstu/strategy/api/StrategyEntityCreateExecutor.java b/src/main/java/ru/ulstu/strategy/api/StrategyEntityCreateExecutor.java new file mode 100644 index 0000000..97cd5ef --- /dev/null +++ b/src/main/java/ru/ulstu/strategy/api/StrategyEntityCreateExecutor.java @@ -0,0 +1,26 @@ +package ru.ulstu.strategy.api; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import ru.ulstu.user.service.UserService; + +import java.util.List; + +@Service +public class StrategyEntityCreateExecutor { + private final List entityCreateStrategies; + private final UserService userService; + + public StrategyEntityCreateExecutor(List entityCreateStrategies, + UserService userService) { + this.entityCreateStrategies = entityCreateStrategies; + this.userService = userService; + } + + + //@Scheduled(cron = "0 0 8 * * *") + @Scheduled(cron = "0 */5 * * * *") + public void scheduleExecuteStrategies() { + entityCreateStrategies.forEach(strategy -> strategy.createEntityIfNeed(userService.findAll())); + } +} diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index 0603620..61fe72b 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -77,6 +77,11 @@ public class UserService implements UserDetailsService { return new PageableItems<>(page.getTotalElements(), userMapper.userEntitiesToUserListDtos(page.getContent())); } + // TODO: read only active users + public List findAll() { + return userRepository.findAll(); + } + @Transactional(readOnly = true) public PageableItems getUserRoles() { final List roles = userRoleRepository.findAll().stream() diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ea20ef6..9544a7e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,8 +16,8 @@ logging.level.ru.ulstu=DEBUG # Mail Settings spring.mail.host=smtp.yandex.ru spring.mail.port=465 -spring.mail.username=balance@soft.kitchen -spring.mail.password=fkvfpbalance +spring.mail.username=nio17.ulstu@yandex.ru +spring.mail.password=nio17.ulstu.ru spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory diff --git a/src/main/resources/mail_templates/paperCreateNotification.html b/src/main/resources/mail_templates/paperCreateNotification.html new file mode 100644 index 0000000..3e71ce9 --- /dev/null +++ b/src/main/resources/mail_templates/paperCreateNotification.html @@ -0,0 +1,24 @@ + + + + Уведомление о создании статьи + + + + +

+ Уважаемый(ая) Ivan Ivanov +

+

+ Вам нужно поработать над статьей "Title". +

+

+ Срок исполнения: . +

+

+ Regards, +
+ NG-tracker. +

+ +