From 1823c1ba50c959dde3197fbfa64a516e9ffa1b3c Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 20 May 2019 18:37:24 +0400 Subject: [PATCH 01/16] #120 create kias page --- src/test/java/grant/KiasPage.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/java/grant/KiasPage.java diff --git a/src/test/java/grant/KiasPage.java b/src/test/java/grant/KiasPage.java new file mode 100644 index 0000000..49b40d9 --- /dev/null +++ b/src/test/java/grant/KiasPage.java @@ -0,0 +1,28 @@ +package grant; + +import core.PageObject; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import java.util.List; + +public class KiasPage extends PageObject { + @Override + public String getSubTitle() { + return driver.findElement(By.tagName("h1")).getText(); + } + + public List getGrants() { + WebElement listContest = driver.findElement(By.tagName("tBody")); + return listContest.findElements(By.cssSelector("tr.tr")); + + } + + public String getGrantTitle(WebElement grant) { + return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText(); + } + + public String getFirstDeadline(WebElement grant) { + return grant.findElement(By.xpath("./td[5]")).getText(); + } +} From c0e66e81a2e50d85a0859d1f779572a7e1065f1a Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 20 May 2019 20:47:10 +0400 Subject: [PATCH 02/16] #120 drop unnecessary table column --- .../resources/db/changelog-20190520_000000-schema.xml | 8 ++++++++ src/main/resources/db/changelog-master.xml | 1 + 2 files changed, 9 insertions(+) create mode 100644 src/main/resources/db/changelog-20190520_000000-schema.xml diff --git a/src/main/resources/db/changelog-20190520_000000-schema.xml b/src/main/resources/db/changelog-20190520_000000-schema.xml new file mode 100644 index 0000000..69cdad0 --- /dev/null +++ b/src/main/resources/db/changelog-20190520_000000-schema.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index 18bd645..d2ccbca 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -42,4 +42,5 @@ + \ No newline at end of file From bb9f412480fc9948e41680a5a4c009a0c26f60b6 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Mon, 20 May 2019 20:49:03 +0400 Subject: [PATCH 03/16] #120 check unique grant name --- .../grant/controller/GrantController.java | 20 +--------- .../java/ru/ulstu/grant/model/GrantDto.java | 3 +- .../grant/repository/GrantRepository.java | 9 ++++- .../ru/ulstu/grant/service/GrantService.java | 37 ++++++++++++++++++- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index ad5e992..91b0f91 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -20,9 +20,7 @@ import springfox.documentation.annotations.ApiIgnore; import javax.validation.Valid; import java.io.IOException; import java.util.List; -import java.util.stream.Collectors; -import static org.springframework.util.StringUtils.isEmpty; import static ru.ulstu.core.controller.Navigation.GRANTS_PAGE; import static ru.ulstu.core.controller.Navigation.GRANT_PAGE; import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; @@ -62,17 +60,9 @@ public class GrantController { @PostMapping(value = "/grant", params = "save") public String save(@Valid GrantDto grantDto, Errors errors) throws IOException { - filterEmptyDeadlines(grantDto); - if (grantDto.getDeadlines().isEmpty()) { - errors.rejectValue("deadlines", "errorCode", "Не может быть пусто"); - } - if (grantDto.getLeaderId().equals(-1)) { - errors.rejectValue("leaderId", "errorCode", "Укажите руководителя"); - } - if (errors.hasErrors()) { + if (!grantService.save(grantDto, errors)) { return GRANT_PAGE; } - grantService.save(grantDto); return String.format(REDIRECT_TO, GRANTS_PAGE); } @@ -89,7 +79,7 @@ public class GrantController { @PostMapping(value = "/grant", params = "addDeadline") public String addDeadline(@Valid GrantDto grantDto, Errors errors) { - filterEmptyDeadlines(grantDto); + grantService.filterEmptyDeadlines(grantDto); if (errors.hasErrors()) { return GRANT_PAGE; } @@ -133,10 +123,4 @@ public class GrantController { public List getAllPapers() { return grantService.getAllUncompletedPapers(); } - - private void filterEmptyDeadlines(GrantDto grantDto) { - grantDto.setDeadlines(grantDto.getDeadlines().stream() - .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) - .collect(Collectors.toList())); - } } diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index 203d306..5a561b1 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -6,6 +6,7 @@ import org.apache.commons.lang3.StringUtils; import org.hibernate.validator.constraints.NotEmpty; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.file.model.FileDataDto; +import ru.ulstu.name.NameContainer; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.project.model.ProjectDto; import ru.ulstu.user.model.UserDto; @@ -17,7 +18,7 @@ import java.util.stream.Collectors; import static ru.ulstu.core.util.StreamApiUtils.convert; -public class GrantDto { +public class GrantDto extends NameContainer { private final static int MAX_AUTHORS_LENGTH = 60; private Integer id; diff --git a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java index 44c2cc0..26face4 100644 --- a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java +++ b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java @@ -1,11 +1,18 @@ package ru.ulstu.grant.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import ru.ulstu.grant.model.Grant; +import ru.ulstu.name.BaseRepository; import java.util.List; -public interface GrantRepository extends JpaRepository { +public interface GrantRepository extends JpaRepository, BaseRepository { List findByStatus(Grant.GrantStatus status); + + @Override + @Query("SELECT title FROM Grant g WHERE (g.title = :name) AND (:id IS NULL OR g.id != :id) ") + String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id); } diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index 45f9b2f..4972b6c 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -3,6 +3,7 @@ package ru.ulstu.grant.service; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.Errors; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.file.model.FileDataDto; @@ -10,6 +11,7 @@ import ru.ulstu.file.service.FileService; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.repository.GrantRepository; +import ru.ulstu.name.BaseService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.service.PaperService; @@ -27,6 +29,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,7 +37,7 @@ import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION; @Service -public class GrantService { +public class GrantService extends BaseService { private final static int MAX_DISPLAY_SIZE = 50; private final GrantRepository grantRepository; @@ -55,6 +58,7 @@ public class GrantService { EventService eventService, GrantNotificationService grantNotificationService) { this.grantRepository = grantRepository; + this.baseRepository = grantRepository; this.fileService = fileService; this.deadlineService = deadlineService; this.projectService = projectService; @@ -176,12 +180,35 @@ public class GrantService { return grant; } - public void save(GrantDto grantDto) throws IOException { + public boolean save(GrantDto grantDto, Errors errors) throws IOException { + grantDto.setName(grantDto.getTitle()); + filterEmptyDeadlines(grantDto); + checkEmptyDeadlines(grantDto, errors); + checkEmptyLeader(grantDto, errors); + checkUniqueName(grantDto, errors, grantDto.getId(), "title", "Грант с таким именем уже существует"); + if (errors.hasErrors()) { + return false; + } + if (isEmpty(grantDto.getId())) { create(grantDto); } else { update(grantDto); } + + return true; + } + + private void checkEmptyLeader(GrantDto grantDto, Errors errors) { + if (grantDto.getLeaderId().equals(-1)) { + errors.rejectValue("leaderId", "errorCode", "Укажите руководителя"); + } + } + + private void checkEmptyDeadlines(GrantDto grantDto, Errors errors) { + if (grantDto.getDeadlines().isEmpty()) { + errors.rejectValue("deadlines", "errorCode", "Не может быть пусто"); + } } public List getGrantAuthors(GrantDto grantDto) { @@ -261,4 +288,10 @@ public class GrantService { .filter(author -> Collections.frequency(authors, author) > 3) .collect(toList()); } + + public void filterEmptyDeadlines(GrantDto grantDto) { + grantDto.setDeadlines(grantDto.getDeadlines().stream() + .filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription())) + .collect(Collectors.toList())); + } } From bab621677eb65e10da2a75ba66e8c016da6389c9 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Wed, 22 May 2019 15:10:51 +0400 Subject: [PATCH 04/16] #120 add methods for checking unique title and date of grant --- .../ru/ulstu/deadline/repository/DeadlineRepository.java | 7 +++++++ .../java/ru/ulstu/deadline/service/DeadlineService.java | 5 +++++ .../java/ru/ulstu/grant/repository/GrantRepository.java | 2 ++ src/main/java/ru/ulstu/name/BaseService.java | 7 +++++++ 4 files changed, 21 insertions(+) diff --git a/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java b/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java index f26c572..5b434b9 100644 --- a/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java +++ b/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java @@ -2,8 +2,15 @@ package ru.ulstu.deadline.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import ru.ulstu.deadline.model.Deadline; +import java.util.Date; + public interface DeadlineRepository extends JpaRepository { + @Query( + value = "SELECT date FROM Deadline d WHERE (d.grant_id = ?1) AND (d.date = ?2)", + nativeQuery = true) + Date findByGrantIdAndDate(Integer grant_id, 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 0ef8a4f..e82211d 100644 --- a/src/main/java/ru/ulstu/deadline/service/DeadlineService.java +++ b/src/main/java/ru/ulstu/deadline/service/DeadlineService.java @@ -5,6 +5,7 @@ import org.springframework.transaction.annotation.Transactional; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.repository.DeadlineRepository; +import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -46,4 +47,8 @@ public class DeadlineService { public void remove(Integer deadlineId) { deadlineRepository.delete(deadlineId); } + + public Date findByGrantIdAndDate(Integer id, Date date) { + return deadlineRepository.findByGrantIdAndDate(id, date); + } } diff --git a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java index 26face4..ae9fcc8 100644 --- a/src/main/java/ru/ulstu/grant/repository/GrantRepository.java +++ b/src/main/java/ru/ulstu/grant/repository/GrantRepository.java @@ -12,6 +12,8 @@ public interface GrantRepository extends JpaRepository, BaseRepo List findByStatus(Grant.GrantStatus status); + Grant findByTitle(String title); + @Override @Query("SELECT title FROM Grant g WHERE (g.title = :name) AND (:id IS NULL OR g.id != :id) ") String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id); diff --git a/src/main/java/ru/ulstu/name/BaseService.java b/src/main/java/ru/ulstu/name/BaseService.java index 6619385..fd63cbf 100644 --- a/src/main/java/ru/ulstu/name/BaseService.java +++ b/src/main/java/ru/ulstu/name/BaseService.java @@ -13,4 +13,11 @@ public abstract class BaseService { errors.rejectValue(checkField, "errorCode", errorMessage); } } + + public String checkUniqueName(NameContainer nameContainer, Integer id) { + if (nameContainer.getName().equals(baseRepository.findByNameAndNotId(nameContainer.getName(), id))) { + return baseRepository.findByNameAndNotId(nameContainer.getName(), id); + } + return null; + } } From c21739bfc627b1aad3de8edbec7e4ad7d5d8a0dc Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Wed, 22 May 2019 15:12:45 +0400 Subject: [PATCH 05/16] #120 parse kias page --- src/test/java/IndexKiasTest.java | 67 +++++++++++++++++++++++++++++++ src/test/java/grant/KiasPage.java | 28 ++++++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/test/java/IndexKiasTest.java diff --git a/src/test/java/IndexKiasTest.java b/src/test/java/IndexKiasTest.java new file mode 100644 index 0000000..73dc646 --- /dev/null +++ b/src/test/java/IndexKiasTest.java @@ -0,0 +1,67 @@ +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import core.PageObject; +import core.TestTemplate; +import grant.KiasPage; +import org.junit.runner.RunWith; +import org.openqa.selenium.WebElement; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import ru.ulstu.NgTrackerApplication; +import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.grant.model.Grant; +import ru.ulstu.grant.model.GrantDto; +import ru.ulstu.user.repository.UserRepository; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Map; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +public class IndexKiasTest extends TestTemplate { + private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s"; + private final static String CONTEST_STATUS_ID = "1"; + private final static String CONTEST_TYPE = "-1"; + private final static String CONTEST_YEAR = "2019"; + + private final Map> navigationHolder = ImmutableMap.of( + new KiasPage(), Arrays.asList("Поиск по конкурсам", + String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)) + ); + + @Autowired + UserRepository userRepository; + + public List getNewGrantsDto() throws ParseException { + Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); + getContext().goTo(page.getValue().get(1)); + KiasPage kiasPage = (KiasPage) getContext().initPage(page.getKey()); + List kiasGrants = new ArrayList<>(); + List newGrants = new ArrayList<>(); + do { + kiasGrants.addAll(kiasPage.getPageOfGrants()); + for (WebElement grant : kiasGrants) { + GrantDto grantDto = new GrantDto(); + grantDto.setTitle(kiasPage.getGrantTitle(grant)); + String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59 + SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + Date date = formatter.parse(deadlineDate); + Deadline deadline = new Deadline(date, "Окончание приёма заявок"); + grantDto.setDeadlines(Arrays.asList(deadline)); + grantDto.setLeaderId(userRepository.findOneByLoginIgnoreCase("admin").getId()); + grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS); + newGrants.add(grantDto); + } + kiasGrants.clear(); + } + while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами + + return newGrants; + } +} diff --git a/src/test/java/grant/KiasPage.java b/src/test/java/grant/KiasPage.java index 49b40d9..7723b01 100644 --- a/src/test/java/grant/KiasPage.java +++ b/src/test/java/grant/KiasPage.java @@ -4,7 +4,9 @@ import core.PageObject; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; +import java.util.ArrayList; import java.util.List; +import java.util.NoSuchElementException; public class KiasPage extends PageObject { @Override @@ -13,9 +15,31 @@ public class KiasPage extends PageObject { } public List getGrants() { - WebElement listContest = driver.findElement(By.tagName("tBody")); - return listContest.findElements(By.cssSelector("tr.tr")); + List grants = new ArrayList<>(); + do { + grants.addAll(getPageOfGrants()); + } + while (checkPagination()); + return grants; + } + + public List getPageOfGrants() { + WebElement listContest = driver.findElement(By.tagName("tBody")); + List grants = listContest.findElements(By.cssSelector("tr.tr")); + return grants; + } + + public boolean checkPagination() { + try { + if (driver.findElements(By.id("js-ctrlNext")).size() > 0) { + driver.findElement(By.id("js-ctrlNext")).click(); + return true; + } + } catch (NoSuchElementException e) { + return false; + } + return false; } public String getGrantTitle(WebElement grant) { From 5f214620bff80e208fc4b382be55d5070057cdd6 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Wed, 22 May 2019 15:16:20 +0400 Subject: [PATCH 06/16] #120 use loading from kias --- .../ulstu/grant/service/GrantScheduler.java | 22 +++++++++++++++ .../ru/ulstu/grant/service/GrantService.java | 27 +++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/service/GrantScheduler.java b/src/main/java/ru/ulstu/grant/service/GrantScheduler.java index 1c38c4a..efe1e41 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantScheduler.java +++ b/src/main/java/ru/ulstu/grant/service/GrantScheduler.java @@ -27,4 +27,26 @@ public class GrantScheduler { grantNotificationService.sendDeadlineNotifications(grantService.findAll(), IS_DEADLINE_NOTIFICATION_BEFORE_WEEK); log.debug("GrantScheduler.checkDeadlineBeforeWeek finished"); } + + @Scheduled(cron = "0 0 8 1 * ?", zone = "Europe/Samara") + public void loadGrantsFromKias() { + log.debug("GrantScheduler.loadGrantsFromKias started"); + // + // Метод для сохранения загруженных грантов + // + +// List grants = IndexKiasTest.getNewGrantsDto(); +// grants.forEach(grantDto -> { +// try { +// if (grantService.saveFromKias(grantDto)) { +// log.debug("GrantScheduler.loadGrantsFromKias new grant was loaded"); +// } else { +// log.debug("GrantScheduler.loadGrantsFromKias grant wasn't loaded, cause it's already exists"); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } +// }); + log.debug("GrantScheduler.loadGrantsFromKias finished"); + } } diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index 4972b6c..38c16c3 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -189,16 +189,31 @@ public class GrantService extends BaseService { if (errors.hasErrors()) { return false; } - if (isEmpty(grantDto.getId())) { create(grantDto); } else { update(grantDto); } - return true; } + public boolean saveFromKias(GrantDto grantDto) throws IOException { + grantDto.setName(grantDto.getTitle()); + String title = checkUniqueName(grantDto, grantDto.getId()); //проверка уникальности имени + if (title != null) { + Grant grantFromDB = grantRepository.findByTitle(title); //грант с таким же названием из бд + if (checkSameDeadline(grantDto, grantFromDB.getId())) { //если дедайны тоже совпадают + return false; + } else { //иначе грант уже был в системе, но в другом году, поэтому надо создать + create(grantDto); + return true; + } + } else { //иначе такого гранта ещё нет, поэтому надо создать + create(grantDto); + return true; + } + } + private void checkEmptyLeader(GrantDto grantDto, Errors errors) { if (grantDto.getLeaderId().equals(-1)) { errors.rejectValue("leaderId", "errorCode", "Укажите руководителя"); @@ -211,6 +226,14 @@ public class GrantService extends BaseService { } } + private boolean checkSameDeadline(GrantDto grantDto, Integer id) { + Date date = grantDto.getDeadlines().get(0).getDate(); //дата с сайта киас + if (deadlineService.findByGrantIdAndDate(id, date).compareTo(date) == 0) { //если есть такая строка с датой + return true; + } + return false; + } + public List getGrantAuthors(GrantDto grantDto) { List filteredUsers = userService.filterByAgeAndDegree(grantDto.isHasAge(), grantDto.isHasDegree()); if (grantDto.isWasLeader()) { From 457f1da98525439184bddad051aafbce2a710325 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Wed, 22 May 2019 15:45:23 +0400 Subject: [PATCH 07/16] #120 fix checkRun and checkStyle --- .../java/ru/ulstu/deadline/repository/DeadlineRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java b/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java index 5b434b9..3f237e4 100644 --- a/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java +++ b/src/main/java/ru/ulstu/deadline/repository/DeadlineRepository.java @@ -12,5 +12,5 @@ public interface DeadlineRepository extends JpaRepository { @Query( value = "SELECT date FROM Deadline d WHERE (d.grant_id = ?1) AND (d.date = ?2)", nativeQuery = true) - Date findByGrantIdAndDate(Integer grant_id, Date date); + Date findByGrantIdAndDate(Integer grantId, Date date); } From c303d65c8d8c02fc0d86c4ec332067e334cfe7ec Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 23 May 2019 16:02:44 +0400 Subject: [PATCH 08/16] #120 move to services --- build.gradle | 2 +- .../java/ru/ulstu/grant/page/KiasPage.java | 52 ++ .../ulstu/grant/service/GrantScheduler.java | 24 +- .../ru/ulstu/grant/service/GrantService.java | 20 +- .../ru/ulstu/grant/service/KiasService.java | 83 +++ .../ru/ulstu/user/service/UserService.java | 693 +++++++++--------- src/main/resources/drivers/chromedriver | Bin src/main/resources/drivers/geckodriver | Bin 8 files changed, 512 insertions(+), 362 deletions(-) create mode 100644 src/main/java/ru/ulstu/grant/page/KiasPage.java create mode 100644 src/main/java/ru/ulstu/grant/service/KiasService.java mode change 100644 => 100755 src/main/resources/drivers/chromedriver mode change 100644 => 100755 src/main/resources/drivers/geckodriver diff --git a/build.gradle b/build.gradle index 38de2c7..71c9977 100644 --- a/build.gradle +++ b/build.gradle @@ -126,6 +126,6 @@ dependencies { compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.6.0' testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' - testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1' + compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.3.1' } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/grant/page/KiasPage.java b/src/main/java/ru/ulstu/grant/page/KiasPage.java new file mode 100644 index 0000000..4353684 --- /dev/null +++ b/src/main/java/ru/ulstu/grant/page/KiasPage.java @@ -0,0 +1,52 @@ +package ru.ulstu.grant.page; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; + +public class KiasPage { + private WebDriver driver; + public KiasPage(WebDriver webDriver) { + this.driver = webDriver; + } + + public List getGrants() { + List grants = new ArrayList<>(); + do { + grants.addAll(getPageOfGrants()); + } + while (checkPagination()); + + return grants; + } + + public List getPageOfGrants() { + WebElement listContest = driver.findElement(By.tagName("tBody")); + List grants = listContest.findElements(By.cssSelector("tr.tr")); + return grants; + } + + public boolean checkPagination() { + try { + if (driver.findElements(By.id("js-ctrlNext")).size() > 0) { + driver.findElement(By.id("js-ctrlNext")).click(); + return true; + } + } catch (NoSuchElementException e) { + return false; + } + return false; + } + + public String getGrantTitle(WebElement grant) { + return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText(); + } + + public String getFirstDeadline(WebElement grant) { + return grant.findElement(By.xpath("./td[5]")).getText(); + } +} diff --git a/src/main/java/ru/ulstu/grant/service/GrantScheduler.java b/src/main/java/ru/ulstu/grant/service/GrantScheduler.java index efe1e41..fb236af 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantScheduler.java +++ b/src/main/java/ru/ulstu/grant/service/GrantScheduler.java @@ -5,6 +5,9 @@ import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import java.io.IOException; +import java.text.ParseException; + @Service public class GrantScheduler { private final static boolean IS_DEADLINE_NOTIFICATION_BEFORE_WEEK = true; @@ -31,22 +34,11 @@ public class GrantScheduler { @Scheduled(cron = "0 0 8 1 * ?", zone = "Europe/Samara") public void loadGrantsFromKias() { log.debug("GrantScheduler.loadGrantsFromKias started"); - // - // Метод для сохранения загруженных грантов - // - -// List grants = IndexKiasTest.getNewGrantsDto(); -// grants.forEach(grantDto -> { -// try { -// if (grantService.saveFromKias(grantDto)) { -// log.debug("GrantScheduler.loadGrantsFromKias new grant was loaded"); -// } else { -// log.debug("GrantScheduler.loadGrantsFromKias grant wasn't loaded, cause it's already exists"); -// } -// } catch (IOException e) { -// e.printStackTrace(); -// } -// }); + try { + grantService.createFromKias(); + } catch (ParseException | IOException e) { + e.printStackTrace(); + } log.debug("GrantScheduler.loadGrantsFromKias finished"); } } diff --git a/src/main/java/ru/ulstu/grant/service/GrantService.java b/src/main/java/ru/ulstu/grant/service/GrantService.java index 38c16c3..8d65da7 100644 --- a/src/main/java/ru/ulstu/grant/service/GrantService.java +++ b/src/main/java/ru/ulstu/grant/service/GrantService.java @@ -1,6 +1,8 @@ package ru.ulstu.grant.service; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.Errors; @@ -23,6 +25,7 @@ import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; import java.io.IOException; +import java.text.ParseException; import java.util.Arrays; import java.util.Collections; import java.util.Date; @@ -39,6 +42,7 @@ import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION; @Service public class GrantService extends BaseService { private final static int MAX_DISPLAY_SIZE = 50; + private final Logger log = LoggerFactory.getLogger(GrantService.class); private final GrantRepository grantRepository; private final ProjectService projectService; @@ -48,6 +52,7 @@ public class GrantService extends BaseService { private final PaperService paperService; private final EventService eventService; private final GrantNotificationService grantNotificationService; + private final KiasService kiasService; public GrantService(GrantRepository grantRepository, FileService fileService, @@ -56,8 +61,10 @@ public class GrantService extends BaseService { UserService userService, PaperService paperService, EventService eventService, - GrantNotificationService grantNotificationService) { + GrantNotificationService grantNotificationService, + KiasService kiasService) { this.grantRepository = grantRepository; + this.kiasService = kiasService; this.baseRepository = grantRepository; this.fileService = fileService; this.deadlineService = deadlineService; @@ -317,4 +324,15 @@ public class GrantService extends BaseService { .filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription())) .collect(Collectors.toList())); } + + @Transactional + public void createFromKias() throws IOException, ParseException { + for (GrantDto grantDto : kiasService.getNewGrantsDto()) { + if (saveFromKias(grantDto)) { + log.debug("GrantScheduler.loadGrantsFromKias new grant was loaded"); + } else { + log.debug("GrantScheduler.loadGrantsFromKias grant wasn't loaded, cause it's already exists"); + } + } + } } diff --git a/src/main/java/ru/ulstu/grant/service/KiasService.java b/src/main/java/ru/ulstu/grant/service/KiasService.java new file mode 100644 index 0000000..6ff0bd9 --- /dev/null +++ b/src/main/java/ru/ulstu/grant/service/KiasService.java @@ -0,0 +1,83 @@ +package ru.ulstu.grant.service; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import org.springframework.stereotype.Service; +import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.grant.model.Grant; +import ru.ulstu.grant.model.GrantDto; +import ru.ulstu.grant.page.KiasPage; +import ru.ulstu.user.service.UserService; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +@Service +public class KiasService { + private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s"; + private final static String CONTEST_STATUS_ID = "1"; + private final static String CONTEST_TYPE = "-1"; + private final static int CONTEST_YEAR = Calendar.getInstance().get(Calendar.YEAR); + + private final static String DRIVER_LOCATION = "drivers/%s"; + private final static String WINDOWS_DRIVER = "chromedriver.exe"; + private final static String LINUX_DRIVER = "chromedriver"; + private final static String DRIVER_TYPE = "webdriver.chrome.driver"; + + private final UserService userService; + private WebDriver webDriver; + + public KiasService(UserService userService) { + System.setProperty(DRIVER_TYPE, getDriverExecutablePath()); + this.userService = userService; + final ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments("--headless"); + webDriver = new ChromeDriver(chromeOptions); + } + + public List getNewGrantsDto() throws ParseException { + webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)); + KiasPage kiasPage = new KiasPage(webDriver); + List kiasGrants = new ArrayList<>(); + List newGrants = new ArrayList<>(); + do { + kiasGrants.addAll(kiasPage.getPageOfGrants()); + for (WebElement grant : kiasGrants) { + GrantDto grantDto = new GrantDto(); + grantDto.setTitle(kiasPage.getGrantTitle(grant)); + String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59 + SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + Date date = formatter.parse(deadlineDate); + Deadline deadline = new Deadline(date, "Окончание приёма заявок"); + grantDto.setDeadlines(Arrays.asList(deadline)); + grantDto.setLeaderId(userService.findOneByLoginIgnoreCase("admin").getId()); + grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS); + newGrants.add(grantDto); + } + kiasGrants.clear(); + } + while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами + + return newGrants; + } + + private String getDriverExecutablePath() { + return KiasService.class.getClassLoader().getResource( + String.format(DRIVER_LOCATION, getDriverExecutable(isWindows()))).getFile(); + } + + private String getDriverExecutable(boolean isWindows) { + return isWindows ? WINDOWS_DRIVER : LINUX_DRIVER; + } + + private boolean isWindows() { + return System.getProperty("os.name").toLowerCase().contains("windows"); + } +} diff --git a/src/main/java/ru/ulstu/user/service/UserService.java b/src/main/java/ru/ulstu/user/service/UserService.java index c889caa..30eeb19 100644 --- a/src/main/java/ru/ulstu/user/service/UserService.java +++ b/src/main/java/ru/ulstu/user/service/UserService.java @@ -1,344 +1,349 @@ -package ru.ulstu.user.service; - -import com.google.common.collect.ImmutableMap; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Sort; -import org.springframework.mail.MailException; -import org.springframework.security.core.authority.SimpleGrantedAuthority; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.StringUtils; -import ru.ulstu.configuration.ApplicationProperties; -import ru.ulstu.core.error.EntityIdIsNullException; -import ru.ulstu.core.jpa.OffsetablePageRequest; -import ru.ulstu.core.model.BaseEntity; -import ru.ulstu.core.model.response.PageableItems; -import ru.ulstu.user.error.UserActivationError; -import ru.ulstu.user.error.UserEmailExistsException; -import ru.ulstu.user.error.UserIdExistsException; -import ru.ulstu.user.error.UserIsUndeadException; -import ru.ulstu.user.error.UserLoginExistsException; -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.error.UserSendingMailException; -import ru.ulstu.user.model.User; -import ru.ulstu.user.model.UserDto; -import ru.ulstu.user.model.UserListDto; -import ru.ulstu.user.model.UserResetPasswordDto; -import ru.ulstu.user.model.UserRole; -import ru.ulstu.user.model.UserRoleConstants; -import ru.ulstu.user.model.UserRoleDto; -import ru.ulstu.user.repository.UserRepository; -import ru.ulstu.user.repository.UserRoleRepository; -import ru.ulstu.user.util.UserUtils; - -import javax.mail.MessagingException; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -@Service -@Transactional -public class UserService implements UserDetailsService { - private static final String INVITE_USER_EXCEPTION = "Во время отправки приглашения произошла ошибка"; - - private final Logger log = LoggerFactory.getLogger(UserService.class); - private final UserRepository userRepository; - private final PasswordEncoder passwordEncoder; - private final UserRoleRepository userRoleRepository; - private final UserMapper userMapper; - private final MailService mailService; - private final ApplicationProperties applicationProperties; - - public UserService(UserRepository userRepository, - PasswordEncoder passwordEncoder, - UserRoleRepository userRoleRepository, - UserMapper userMapper, - MailService mailService, - ApplicationProperties applicationProperties) { - this.userRepository = userRepository; - this.passwordEncoder = passwordEncoder; - this.userRoleRepository = userRoleRepository; - this.userMapper = userMapper; - this.mailService = mailService; - this.applicationProperties = applicationProperties; - } - - private User getUserByEmail(String email) { - return userRepository.findOneByEmailIgnoreCase(email); - } - - private User getUserByActivationKey(String activationKey) { - return userRepository.findOneByActivationKey(activationKey); - } - - public User getUserByLogin(String login) { - return userRepository.findOneByLoginIgnoreCase(login); - } - - @Transactional(readOnly = true) - public UserDto getUserWithRolesById(Integer userId) { - final User userEntity = userRepository.findOneWithRolesById(userId); - if (userEntity == null) { - throw new UserNotFoundException(userId.toString()); - } - return userMapper.userEntityToUserDto(userEntity); - } - - @Transactional(readOnly = true) - public PageableItems getAllUsers(int offset, int count) { - final Page page = userRepository.findAll(new OffsetablePageRequest(offset, count, new Sort("id"))); - 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() - .map(UserRoleDto::new) - .sorted(Comparator.comparing(UserRoleDto::getViewValue)) - .collect(Collectors.toList()); - return new PageableItems<>(roles.size(), roles); - } - - public UserDto createUser(UserDto userDto) { - if (userDto.getId() != null) { - throw new UserIdExistsException(); - } - if (getUserByLogin(userDto.getLogin()) != null) { - throw new UserLoginExistsException(userDto.getLogin()); - } - if (getUserByEmail(userDto.getEmail()) != null) { - throw new UserEmailExistsException(userDto.getEmail()); - } - if (!userDto.isPasswordsValid()) { - throw new UserPasswordsNotValidOrNotMatchException(""); - } - User user = userMapper.userDtoToUserEntity(userDto); - user.setActivated(false); - user.setActivationKey(UserUtils.generateActivationKey()); - user.setRoles(Collections.singleton(new UserRole(UserRoleConstants.USER))); - user.setPassword(passwordEncoder.encode(userDto.getPassword())); - user = userRepository.save(user); - mailService.sendActivationEmail(user); - log.debug("Created Information for User: {}", user.getLogin()); - return userMapper.userEntityToUserDto(user); - } - - public UserDto activateUser(String activationKey) { - final User user = getUserByActivationKey(activationKey); - if (user == null) { - throw new UserActivationError(activationKey); - } - user.setActivated(true); - user.setActivationKey(null); - user.setActivationDate(null); - log.debug("Activated user: {}", user.getLogin()); - return userMapper.userEntityToUserDto(userRepository.save(user)); - } - - public UserDto updateUser(UserDto userDto) { - if (userDto.getId() == null) { - throw new EntityIdIsNullException(); - } - if (!Objects.equals( - Optional.ofNullable(getUserByEmail(userDto.getEmail())) - .map(BaseEntity::getId).orElse(userDto.getId()), - userDto.getId())) { - throw new UserEmailExistsException(userDto.getEmail()); - } - if (!Objects.equals( - Optional.ofNullable(getUserByLogin(userDto.getLogin())) - .map(BaseEntity::getId).orElse(userDto.getId()), - userDto.getId())) { - throw new UserLoginExistsException(userDto.getLogin()); - } - User user = userRepository.findOne(userDto.getId()); - if (user == null) { - throw new UserNotFoundException(userDto.getId().toString()); - } - if (applicationProperties.getUndeadUserLogin().equalsIgnoreCase(user.getLogin())) { - userDto.setLogin(applicationProperties.getUndeadUserLogin()); - userDto.setActivated(true); - userDto.setRoles(Collections.singletonList(new UserRoleDto(UserRoleConstants.ADMIN))); - } - user.setLogin(userDto.getLogin()); - user.setFirstName(userDto.getFirstName()); - user.setLastName(userDto.getLastName()); - user.setEmail(userDto.getEmail()); - if (userDto.isActivated() != user.getActivated()) { - if (userDto.isActivated()) { - user.setActivationKey(null); - user.setActivationDate(null); - } else { - user.setActivationKey(UserUtils.generateActivationKey()); - user.setActivationDate(new Date()); - } - } - user.setActivated(userDto.isActivated()); - final Set roles = userMapper.rolesFromDto(userDto.getRoles()); - user.setRoles(roles.isEmpty() - ? Collections.singleton(new UserRole(UserRoleConstants.USER)) - : roles); - if (!StringUtils.isEmpty(userDto.getOldPassword())) { - if (!userDto.isPasswordsValid() || !userDto.isOldPasswordValid()) { - throw new UserPasswordsNotValidOrNotMatchException(""); - } - if (!passwordEncoder.matches(userDto.getOldPassword(), user.getPassword())) { - throw new UserPasswordsNotValidOrNotMatchException(""); - } - user.setPassword(passwordEncoder.encode(userDto.getPassword())); - log.debug("Changed password for User: {}", user.getLogin()); - } - user = userRepository.save(user); - log.debug("Changed Information for User: {}", user.getLogin()); - return userMapper.userEntityToUserDto(user); - } - - public UserDto updateUserInformation(User user, UserDto updateUser) { - user.setFirstName(updateUser.getFirstName()); - user.setLastName(updateUser.getLastName()); - user.setEmail(updateUser.getEmail()); - user.setLogin(updateUser.getLogin()); - user = userRepository.save(user); - log.debug("Updated Information for User: {}", user.getLogin()); - return userMapper.userEntityToUserDto(user); - } - - public void changeUserPassword(User user, Map payload) { - if (!payload.get("password").equals(payload.get("confirmPassword"))) { - throw new UserPasswordsNotValidOrNotMatchException(""); - } - if (!passwordEncoder.matches(payload.get("oldPassword"), user.getPassword())) { - throw new UserPasswordsNotValidOrNotMatchException("Старый пароль введен неправильно"); - } - user.setPassword(passwordEncoder.encode(payload.get("password"))); - log.debug("Changed password for User: {}", user.getLogin()); - userRepository.save(user); - - mailService.sendChangePasswordMail(user); - } - - public boolean requestUserPasswordReset(String email) { - User user = userRepository.findOneByEmailIgnoreCase(email); - if (user == null) { - throw new UserNotFoundException(email); - } - if (!user.getActivated()) { - throw new UserNotActivatedException(); - } - user.setResetKey(UserUtils.generateResetKey()); - user.setResetDate(new Date()); - user = userRepository.save(user); - mailService.sendPasswordResetMail(user); - log.debug("Created Reset Password Request for User: {}", user.getLogin()); - return true; - } - - public boolean completeUserPasswordReset(String key, UserResetPasswordDto userResetPasswordDto) { - if (!userResetPasswordDto.isPasswordsValid()) { - throw new UserPasswordsNotValidOrNotMatchException(""); - } - User user = userRepository.findOneByResetKey(key); - if (user == null) { - throw new UserResetKeyError(key); - } - user.setPassword(passwordEncoder.encode(userResetPasswordDto.getPassword())); - user.setResetKey(null); - user.setResetDate(null); - user = userRepository.save(user); - log.debug("Reset Password for User: {}", user.getLogin()); - return true; - } - - public UserDto deleteUser(Integer userId) { - final User user = userRepository.findOne(userId); - if (user == null) { - throw new UserNotFoundException(userId.toString()); - } - if (applicationProperties.getUndeadUserLogin().equalsIgnoreCase(user.getLogin())) { - throw new UserIsUndeadException(user.getLogin()); - } - userRepository.delete(user); - log.debug("Deleted User: {}", user.getLogin()); - return userMapper.userEntityToUserDto(user); - } - - @Override - public UserDetails loadUserByUsername(String username) { - final User user = userRepository.findOneByLoginIgnoreCase(username); - if (user == null) { - throw new UserNotFoundException(username); - } - if (!user.getActivated()) { - throw new UserNotActivatedException(); - } - return new org.springframework.security.core.userdetails.User(user.getLogin(), - user.getPassword(), - Optional.ofNullable(user.getRoles()).orElse(Collections.emptySet()).stream() - .map(role -> new SimpleGrantedAuthority(role.getName())) - .collect(Collectors.toList())); - } - - public List findByIds(List ids) { - return userRepository.findAll(ids); - } - - public User findById(Integer id) { - return userRepository.findOne(id); - } - - public User getCurrentUser() { - String login = UserUtils.getCurrentUserLogin(); - User user = userRepository.findOneByLoginIgnoreCase(login); - if (user == null) { - throw new UserNotFoundException(login); - } - return user; - } - - public List filterByAgeAndDegree(boolean hasDegree, boolean hasAge) { - return userRepository.filterByAgeAndDegree(hasDegree, hasAge); - } - - public void inviteUser(String email) throws UserSendingMailException { - if (userRepository.findOneByEmailIgnoreCase(email) != null) { - throw new UserEmailExistsException(email); - } - - String password = UserUtils.generatePassword(); - - User user = new User(); - user.setPassword(passwordEncoder.encode(password)); - user.setLogin(email); - user.setEmail(email); - user.setFirstName("user"); - user.setLastName("user"); - user.setActivated(true); - userRepository.save(user); - - Map variables = ImmutableMap.of("password", password, "email", email); - try { - mailService.sendInviteMail(variables, email); - } catch (MessagingException | MailException e) { - throw new UserSendingMailException(email); - } - } -} +package ru.ulstu.user.service; + +import com.google.common.collect.ImmutableMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Sort; +import org.springframework.mail.MailException; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; +import ru.ulstu.configuration.ApplicationProperties; +import ru.ulstu.core.error.EntityIdIsNullException; +import ru.ulstu.core.jpa.OffsetablePageRequest; +import ru.ulstu.core.model.BaseEntity; +import ru.ulstu.core.model.response.PageableItems; +import ru.ulstu.grant.model.GrantDto; +import ru.ulstu.user.error.UserActivationError; +import ru.ulstu.user.error.UserEmailExistsException; +import ru.ulstu.user.error.UserIdExistsException; +import ru.ulstu.user.error.UserIsUndeadException; +import ru.ulstu.user.error.UserLoginExistsException; +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.error.UserSendingMailException; +import ru.ulstu.user.model.User; +import ru.ulstu.user.model.UserDto; +import ru.ulstu.user.model.UserListDto; +import ru.ulstu.user.model.UserResetPasswordDto; +import ru.ulstu.user.model.UserRole; +import ru.ulstu.user.model.UserRoleConstants; +import ru.ulstu.user.model.UserRoleDto; +import ru.ulstu.user.repository.UserRepository; +import ru.ulstu.user.repository.UserRoleRepository; +import ru.ulstu.user.util.UserUtils; + +import javax.mail.MessagingException; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +@Service +@Transactional +public class UserService implements UserDetailsService { + private static final String INVITE_USER_EXCEPTION = "Во время отправки приглашения произошла ошибка"; + + private final Logger log = LoggerFactory.getLogger(UserService.class); + private final UserRepository userRepository; + private final PasswordEncoder passwordEncoder; + private final UserRoleRepository userRoleRepository; + private final UserMapper userMapper; + private final MailService mailService; + private final ApplicationProperties applicationProperties; + + public UserService(UserRepository userRepository, + PasswordEncoder passwordEncoder, + UserRoleRepository userRoleRepository, + UserMapper userMapper, + MailService mailService, + ApplicationProperties applicationProperties) { + this.userRepository = userRepository; + this.passwordEncoder = passwordEncoder; + this.userRoleRepository = userRoleRepository; + this.userMapper = userMapper; + this.mailService = mailService; + this.applicationProperties = applicationProperties; + } + + private User getUserByEmail(String email) { + return userRepository.findOneByEmailIgnoreCase(email); + } + + private User getUserByActivationKey(String activationKey) { + return userRepository.findOneByActivationKey(activationKey); + } + + public User getUserByLogin(String login) { + return userRepository.findOneByLoginIgnoreCase(login); + } + + @Transactional(readOnly = true) + public UserDto getUserWithRolesById(Integer userId) { + final User userEntity = userRepository.findOneWithRolesById(userId); + if (userEntity == null) { + throw new UserNotFoundException(userId.toString()); + } + return userMapper.userEntityToUserDto(userEntity); + } + + @Transactional(readOnly = true) + public PageableItems getAllUsers(int offset, int count) { + final Page page = userRepository.findAll(new OffsetablePageRequest(offset, count, new Sort("id"))); + 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() + .map(UserRoleDto::new) + .sorted(Comparator.comparing(UserRoleDto::getViewValue)) + .collect(Collectors.toList()); + return new PageableItems<>(roles.size(), roles); + } + + public UserDto createUser(UserDto userDto) { + if (userDto.getId() != null) { + throw new UserIdExistsException(); + } + if (getUserByLogin(userDto.getLogin()) != null) { + throw new UserLoginExistsException(userDto.getLogin()); + } + if (getUserByEmail(userDto.getEmail()) != null) { + throw new UserEmailExistsException(userDto.getEmail()); + } + if (!userDto.isPasswordsValid()) { + throw new UserPasswordsNotValidOrNotMatchException(""); + } + User user = userMapper.userDtoToUserEntity(userDto); + user.setActivated(false); + user.setActivationKey(UserUtils.generateActivationKey()); + user.setRoles(Collections.singleton(new UserRole(UserRoleConstants.USER))); + user.setPassword(passwordEncoder.encode(userDto.getPassword())); + user = userRepository.save(user); + mailService.sendActivationEmail(user); + log.debug("Created Information for User: {}", user.getLogin()); + return userMapper.userEntityToUserDto(user); + } + + public UserDto activateUser(String activationKey) { + final User user = getUserByActivationKey(activationKey); + if (user == null) { + throw new UserActivationError(activationKey); + } + user.setActivated(true); + user.setActivationKey(null); + user.setActivationDate(null); + log.debug("Activated user: {}", user.getLogin()); + return userMapper.userEntityToUserDto(userRepository.save(user)); + } + + public UserDto updateUser(UserDto userDto) { + if (userDto.getId() == null) { + throw new EntityIdIsNullException(); + } + if (!Objects.equals( + Optional.ofNullable(getUserByEmail(userDto.getEmail())) + .map(BaseEntity::getId).orElse(userDto.getId()), + userDto.getId())) { + throw new UserEmailExistsException(userDto.getEmail()); + } + if (!Objects.equals( + Optional.ofNullable(getUserByLogin(userDto.getLogin())) + .map(BaseEntity::getId).orElse(userDto.getId()), + userDto.getId())) { + throw new UserLoginExistsException(userDto.getLogin()); + } + User user = userRepository.findOne(userDto.getId()); + if (user == null) { + throw new UserNotFoundException(userDto.getId().toString()); + } + if (applicationProperties.getUndeadUserLogin().equalsIgnoreCase(user.getLogin())) { + userDto.setLogin(applicationProperties.getUndeadUserLogin()); + userDto.setActivated(true); + userDto.setRoles(Collections.singletonList(new UserRoleDto(UserRoleConstants.ADMIN))); + } + user.setLogin(userDto.getLogin()); + user.setFirstName(userDto.getFirstName()); + user.setLastName(userDto.getLastName()); + user.setEmail(userDto.getEmail()); + if (userDto.isActivated() != user.getActivated()) { + if (userDto.isActivated()) { + user.setActivationKey(null); + user.setActivationDate(null); + } else { + user.setActivationKey(UserUtils.generateActivationKey()); + user.setActivationDate(new Date()); + } + } + user.setActivated(userDto.isActivated()); + final Set roles = userMapper.rolesFromDto(userDto.getRoles()); + user.setRoles(roles.isEmpty() + ? Collections.singleton(new UserRole(UserRoleConstants.USER)) + : roles); + if (!StringUtils.isEmpty(userDto.getOldPassword())) { + if (!userDto.isPasswordsValid() || !userDto.isOldPasswordValid()) { + throw new UserPasswordsNotValidOrNotMatchException(""); + } + if (!passwordEncoder.matches(userDto.getOldPassword(), user.getPassword())) { + throw new UserPasswordsNotValidOrNotMatchException(""); + } + user.setPassword(passwordEncoder.encode(userDto.getPassword())); + log.debug("Changed password for User: {}", user.getLogin()); + } + user = userRepository.save(user); + log.debug("Changed Information for User: {}", user.getLogin()); + return userMapper.userEntityToUserDto(user); + } + + public UserDto updateUserInformation(User user, UserDto updateUser) { + user.setFirstName(updateUser.getFirstName()); + user.setLastName(updateUser.getLastName()); + user.setEmail(updateUser.getEmail()); + user.setLogin(updateUser.getLogin()); + user = userRepository.save(user); + log.debug("Updated Information for User: {}", user.getLogin()); + return userMapper.userEntityToUserDto(user); + } + + public void changeUserPassword(User user, Map payload) { + if (!payload.get("password").equals(payload.get("confirmPassword"))) { + throw new UserPasswordsNotValidOrNotMatchException(""); + } + if (!passwordEncoder.matches(payload.get("oldPassword"), user.getPassword())) { + throw new UserPasswordsNotValidOrNotMatchException("Старый пароль введен неправильно"); + } + user.setPassword(passwordEncoder.encode(payload.get("password"))); + log.debug("Changed password for User: {}", user.getLogin()); + userRepository.save(user); + + mailService.sendChangePasswordMail(user); + } + + public boolean requestUserPasswordReset(String email) { + User user = userRepository.findOneByEmailIgnoreCase(email); + if (user == null) { + throw new UserNotFoundException(email); + } + if (!user.getActivated()) { + throw new UserNotActivatedException(); + } + user.setResetKey(UserUtils.generateResetKey()); + user.setResetDate(new Date()); + user = userRepository.save(user); + mailService.sendPasswordResetMail(user); + log.debug("Created Reset Password Request for User: {}", user.getLogin()); + return true; + } + + public boolean completeUserPasswordReset(String key, UserResetPasswordDto userResetPasswordDto) { + if (!userResetPasswordDto.isPasswordsValid()) { + throw new UserPasswordsNotValidOrNotMatchException(""); + } + User user = userRepository.findOneByResetKey(key); + if (user == null) { + throw new UserResetKeyError(key); + } + user.setPassword(passwordEncoder.encode(userResetPasswordDto.getPassword())); + user.setResetKey(null); + user.setResetDate(null); + user = userRepository.save(user); + log.debug("Reset Password for User: {}", user.getLogin()); + return true; + } + + public UserDto deleteUser(Integer userId) { + final User user = userRepository.findOne(userId); + if (user == null) { + throw new UserNotFoundException(userId.toString()); + } + if (applicationProperties.getUndeadUserLogin().equalsIgnoreCase(user.getLogin())) { + throw new UserIsUndeadException(user.getLogin()); + } + userRepository.delete(user); + log.debug("Deleted User: {}", user.getLogin()); + return userMapper.userEntityToUserDto(user); + } + + @Override + public UserDetails loadUserByUsername(String username) { + final User user = userRepository.findOneByLoginIgnoreCase(username); + if (user == null) { + throw new UserNotFoundException(username); + } + if (!user.getActivated()) { + throw new UserNotActivatedException(); + } + return new org.springframework.security.core.userdetails.User(user.getLogin(), + user.getPassword(), + Optional.ofNullable(user.getRoles()).orElse(Collections.emptySet()).stream() + .map(role -> new SimpleGrantedAuthority(role.getName())) + .collect(Collectors.toList())); + } + + public List findByIds(List ids) { + return userRepository.findAll(ids); + } + + public User findById(Integer id) { + return userRepository.findOne(id); + } + + public User getCurrentUser() { + String login = UserUtils.getCurrentUserLogin(); + User user = userRepository.findOneByLoginIgnoreCase(login); + if (user == null) { + throw new UserNotFoundException(login); + } + return user; + } + + public List filterByAgeAndDegree(boolean hasDegree, boolean hasAge) { + return userRepository.filterByAgeAndDegree(hasDegree, hasAge); + } + + public void inviteUser(String email) throws UserSendingMailException { + if (userRepository.findOneByEmailIgnoreCase(email) != null) { + throw new UserEmailExistsException(email); + } + + String password = UserUtils.generatePassword(); + + User user = new User(); + user.setPassword(passwordEncoder.encode(password)); + user.setLogin(email); + user.setEmail(email); + user.setFirstName("user"); + user.setLastName("user"); + user.setActivated(true); + userRepository.save(user); + + Map variables = ImmutableMap.of("password", password, "email", email); + try { + mailService.sendInviteMail(variables, email); + } catch (MessagingException | MailException e) { + throw new UserSendingMailException(email); + } + } + + public User findOneByLoginIgnoreCase(String login) { + return userRepository.findOneByLoginIgnoreCase(login); + } +} diff --git a/src/main/resources/drivers/chromedriver b/src/main/resources/drivers/chromedriver old mode 100644 new mode 100755 diff --git a/src/main/resources/drivers/geckodriver b/src/main/resources/drivers/geckodriver old mode 100644 new mode 100755 From 181086c9db9e7eb5683c520d4a89110d6561b38f Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 23 May 2019 16:36:48 +0400 Subject: [PATCH 09/16] #120 disable start browser on run application --- src/main/java/ru/ulstu/grant/service/KiasService.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/service/KiasService.java b/src/main/java/ru/ulstu/grant/service/KiasService.java index 6ff0bd9..449b948 100644 --- a/src/main/java/ru/ulstu/grant/service/KiasService.java +++ b/src/main/java/ru/ulstu/grant/service/KiasService.java @@ -32,17 +32,16 @@ public class KiasService { private final static String DRIVER_TYPE = "webdriver.chrome.driver"; private final UserService userService; - private WebDriver webDriver; public KiasService(UserService userService) { - System.setProperty(DRIVER_TYPE, getDriverExecutablePath()); this.userService = userService; - final ChromeOptions chromeOptions = new ChromeOptions(); - chromeOptions.addArguments("--headless"); - webDriver = new ChromeDriver(chromeOptions); } public List getNewGrantsDto() throws ParseException { + System.setProperty(DRIVER_TYPE, getDriverExecutablePath()); + final ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments("--headless"); + WebDriver webDriver = new ChromeDriver(chromeOptions); webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)); KiasPage kiasPage = new KiasPage(webDriver); List kiasGrants = new ArrayList<>(); @@ -64,7 +63,7 @@ public class KiasService { kiasGrants.clear(); } while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами - + webDriver.quit(); return newGrants; } From 59d6edb818c766e4f70776a0cf19de60119b84f2 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 15:42:08 +0400 Subject: [PATCH 10/16] #120 refactor --- .../java/ru/ulstu/grant/model/GrantDto.java | 7 +++ .../java/ru/ulstu/grant/page/KiasPage.java | 54 ++++++++++++------ .../ru/ulstu/grant/service/KiasService.java | 49 +++++++--------- src/test/java/IndexKiasTest.java | 56 ------------------- src/test/java/grant/KiasPage.java | 41 -------------- 5 files changed, 63 insertions(+), 144 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/model/GrantDto.java b/src/main/java/ru/ulstu/grant/model/GrantDto.java index 5a561b1..9e59d86 100644 --- a/src/main/java/ru/ulstu/grant/model/GrantDto.java +++ b/src/main/java/ru/ulstu/grant/model/GrantDto.java @@ -12,6 +12,7 @@ import ru.ulstu.project.model.ProjectDto; import ru.ulstu.user.model.UserDto; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -96,6 +97,12 @@ public class GrantDto extends NameContainer { this.papers = convert(grant.getPapers(), PaperDto::new); } + public GrantDto(String grantTitle, Date deadLineDate) { + this.title = grantTitle; + deadlines.add(new Deadline(deadLineDate, "Окончание приёма заявок")); + status = Grant.GrantStatus.LOADED_FROM_KIAS; + } + public Integer getId() { return id; } diff --git a/src/main/java/ru/ulstu/grant/page/KiasPage.java b/src/main/java/ru/ulstu/grant/page/KiasPage.java index 4353684..8a6e444 100644 --- a/src/main/java/ru/ulstu/grant/page/KiasPage.java +++ b/src/main/java/ru/ulstu/grant/page/KiasPage.java @@ -3,34 +3,31 @@ package ru.ulstu.grant.page; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import ru.ulstu.grant.model.GrantDto; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.NoSuchElementException; public class KiasPage { private WebDriver driver; + public KiasPage(WebDriver webDriver) { this.driver = webDriver; } - public List getGrants() { - List grants = new ArrayList<>(); + public List getKiasGrants() throws ParseException { + List newGrants = new ArrayList<>(); do { - grants.addAll(getPageOfGrants()); - } - while (checkPagination()); - - return grants; + newGrants.addAll(getGrantsFromPage()); + } while (goToNextPage()); //проверка существования следующей страницы с грантами + return newGrants; } - public List getPageOfGrants() { - WebElement listContest = driver.findElement(By.tagName("tBody")); - List grants = listContest.findElements(By.cssSelector("tr.tr")); - return grants; - } - - public boolean checkPagination() { + private boolean goToNextPage() { try { if (driver.findElements(By.id("js-ctrlNext")).size() > 0) { driver.findElement(By.id("js-ctrlNext")).click(); @@ -42,11 +39,34 @@ public class KiasPage { return false; } - public String getGrantTitle(WebElement grant) { + private List getPageOfGrants() { + WebElement listContest = driver.findElement(By.tagName("tBody")); + List grants = listContest.findElements(By.cssSelector("tr.tr")); + return grants; + } + + private String getGrantTitle(WebElement grant) { return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText(); } - public String getFirstDeadline(WebElement grant) { - return grant.findElement(By.xpath("./td[5]")).getText(); + private List getGrantsFromPage() throws ParseException { + List grants = new ArrayList<>(); + for (WebElement grantElement : getPageOfGrants()) { + GrantDto grantDto = new GrantDto( + getGrantTitle(grantElement), + parseDeadLineDate(grantElement)); + grants.add(grantDto); + } + return grants; + } + + private Date parseDeadLineDate(WebElement grantElement) throws ParseException { + String deadlineDate = getFirstDeadline(grantElement); //10.06.2019 23:59 + SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + return formatter.parse(deadlineDate); + } + + private String getFirstDeadline(WebElement grantElement) { + return grantElement.findElement(By.xpath("./td[5]")).getText(); } } diff --git a/src/main/java/ru/ulstu/grant/service/KiasService.java b/src/main/java/ru/ulstu/grant/service/KiasService.java index 449b948..6881292 100644 --- a/src/main/java/ru/ulstu/grant/service/KiasService.java +++ b/src/main/java/ru/ulstu/grant/service/KiasService.java @@ -1,22 +1,17 @@ package ru.ulstu.grant.service; import org.openqa.selenium.WebDriver; -import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.springframework.stereotype.Service; -import ru.ulstu.deadline.model.Deadline; -import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.page.KiasPage; import ru.ulstu.user.service.UserService; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; -import java.util.Date; import java.util.List; @Service @@ -24,7 +19,6 @@ public class KiasService { private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s"; private final static String CONTEST_STATUS_ID = "1"; private final static String CONTEST_TYPE = "-1"; - private final static int CONTEST_YEAR = Calendar.getInstance().get(Calendar.YEAR); private final static String DRIVER_LOCATION = "drivers/%s"; private final static String WINDOWS_DRIVER = "chromedriver.exe"; @@ -38,33 +32,28 @@ public class KiasService { } public List getNewGrantsDto() throws ParseException { + WebDriver webDriver = getDriver(); + Integer leaderId = userService.findOneByLoginIgnoreCase("admin").getId(); + List grants = new ArrayList<>(); + for (Integer year : generateGrantYears()) { + webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year)); + grants.addAll(new KiasPage(webDriver).getKiasGrants()); + } + grants.forEach(grantDto -> grantDto.setLeaderId(leaderId)); + webDriver.quit(); + return grants; + } + + private List generateGrantYears() { + return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR), + Calendar.getInstance().get(Calendar.YEAR) + 1); + } + + private WebDriver getDriver() { System.setProperty(DRIVER_TYPE, getDriverExecutablePath()); final ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("--headless"); - WebDriver webDriver = new ChromeDriver(chromeOptions); - webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)); - KiasPage kiasPage = new KiasPage(webDriver); - List kiasGrants = new ArrayList<>(); - List newGrants = new ArrayList<>(); - do { - kiasGrants.addAll(kiasPage.getPageOfGrants()); - for (WebElement grant : kiasGrants) { - GrantDto grantDto = new GrantDto(); - grantDto.setTitle(kiasPage.getGrantTitle(grant)); - String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59 - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); - Date date = formatter.parse(deadlineDate); - Deadline deadline = new Deadline(date, "Окончание приёма заявок"); - grantDto.setDeadlines(Arrays.asList(deadline)); - grantDto.setLeaderId(userService.findOneByLoginIgnoreCase("admin").getId()); - grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS); - newGrants.add(grantDto); - } - kiasGrants.clear(); - } - while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами - webDriver.quit(); - return newGrants; + return new ChromeDriver(chromeOptions); } private String getDriverExecutablePath() { diff --git a/src/test/java/IndexKiasTest.java b/src/test/java/IndexKiasTest.java index 73dc646..8228e76 100644 --- a/src/test/java/IndexKiasTest.java +++ b/src/test/java/IndexKiasTest.java @@ -1,67 +1,11 @@ -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import core.PageObject; import core.TestTemplate; -import grant.KiasPage; import org.junit.runner.RunWith; -import org.openqa.selenium.WebElement; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import ru.ulstu.NgTrackerApplication; -import ru.ulstu.deadline.model.Deadline; -import ru.ulstu.grant.model.Grant; -import ru.ulstu.grant.model.GrantDto; -import ru.ulstu.user.repository.UserRepository; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Map; @RunWith(SpringRunner.class) @SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class IndexKiasTest extends TestTemplate { - private final static String BASE_URL = "https://www.rfbr.ru/rffi/ru/contest_search?CONTEST_STATUS_ID=%s&CONTEST_TYPE=%s&CONTEST_YEAR=%s"; - private final static String CONTEST_STATUS_ID = "1"; - private final static String CONTEST_TYPE = "-1"; - private final static String CONTEST_YEAR = "2019"; - private final Map> navigationHolder = ImmutableMap.of( - new KiasPage(), Arrays.asList("Поиск по конкурсам", - String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR)) - ); - - @Autowired - UserRepository userRepository; - - public List getNewGrantsDto() throws ParseException { - Map.Entry> page = Iterables.get(navigationHolder.entrySet(), 0); - getContext().goTo(page.getValue().get(1)); - KiasPage kiasPage = (KiasPage) getContext().initPage(page.getKey()); - List kiasGrants = new ArrayList<>(); - List newGrants = new ArrayList<>(); - do { - kiasGrants.addAll(kiasPage.getPageOfGrants()); - for (WebElement grant : kiasGrants) { - GrantDto grantDto = new GrantDto(); - grantDto.setTitle(kiasPage.getGrantTitle(grant)); - String deadlineDate = kiasPage.getFirstDeadline(grant); //10.06.2019 23:59 - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); - Date date = formatter.parse(deadlineDate); - Deadline deadline = new Deadline(date, "Окончание приёма заявок"); - grantDto.setDeadlines(Arrays.asList(deadline)); - grantDto.setLeaderId(userRepository.findOneByLoginIgnoreCase("admin").getId()); - grantDto.setStatus(Grant.GrantStatus.LOADED_FROM_KIAS); - newGrants.add(grantDto); - } - kiasGrants.clear(); - } - while (kiasPage.checkPagination()); //проверка существования следующей страницы с грантами - - return newGrants; - } } diff --git a/src/test/java/grant/KiasPage.java b/src/test/java/grant/KiasPage.java index 7723b01..5864511 100644 --- a/src/test/java/grant/KiasPage.java +++ b/src/test/java/grant/KiasPage.java @@ -2,51 +2,10 @@ package grant; import core.PageObject; import org.openqa.selenium.By; -import org.openqa.selenium.WebElement; - -import java.util.ArrayList; -import java.util.List; -import java.util.NoSuchElementException; public class KiasPage extends PageObject { @Override public String getSubTitle() { return driver.findElement(By.tagName("h1")).getText(); } - - public List getGrants() { - List grants = new ArrayList<>(); - do { - grants.addAll(getPageOfGrants()); - } - while (checkPagination()); - - return grants; - } - - public List getPageOfGrants() { - WebElement listContest = driver.findElement(By.tagName("tBody")); - List grants = listContest.findElements(By.cssSelector("tr.tr")); - return grants; - } - - public boolean checkPagination() { - try { - if (driver.findElements(By.id("js-ctrlNext")).size() > 0) { - driver.findElement(By.id("js-ctrlNext")).click(); - return true; - } - } catch (NoSuchElementException e) { - return false; - } - return false; - } - - public String getGrantTitle(WebElement grant) { - return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText(); - } - - public String getFirstDeadline(WebElement grant) { - return grant.findElement(By.xpath("./td[5]")).getText(); - } } From 477a9fd6ffbff5d4128d31f298cc1212a4f0cc0b Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 15:48:32 +0400 Subject: [PATCH 11/16] #120 move to service --- .../java/ru/ulstu/grant/page/KiasPage.java | 29 +++---------------- .../ru/ulstu/grant/service/KiasService.java | 23 ++++++++++++++- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/main/java/ru/ulstu/grant/page/KiasPage.java b/src/main/java/ru/ulstu/grant/page/KiasPage.java index 8a6e444..ea9bdb4 100644 --- a/src/main/java/ru/ulstu/grant/page/KiasPage.java +++ b/src/main/java/ru/ulstu/grant/page/KiasPage.java @@ -3,11 +3,9 @@ package ru.ulstu.grant.page; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; -import ru.ulstu.grant.model.GrantDto; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.NoSuchElementException; @@ -19,15 +17,7 @@ public class KiasPage { this.driver = webDriver; } - public List getKiasGrants() throws ParseException { - List newGrants = new ArrayList<>(); - do { - newGrants.addAll(getGrantsFromPage()); - } while (goToNextPage()); //проверка существования следующей страницы с грантами - return newGrants; - } - - private boolean goToNextPage() { + public boolean goToNextPage() { try { if (driver.findElements(By.id("js-ctrlNext")).size() > 0) { driver.findElement(By.id("js-ctrlNext")).click(); @@ -39,28 +29,17 @@ public class KiasPage { return false; } - private List getPageOfGrants() { + public List getPageOfGrants() { WebElement listContest = driver.findElement(By.tagName("tBody")); List grants = listContest.findElements(By.cssSelector("tr.tr")); return grants; } - private String getGrantTitle(WebElement grant) { + public String getGrantTitle(WebElement grant) { return grant.findElement(By.cssSelector("td.tertiary")).findElement(By.tagName("a")).getText(); } - private List getGrantsFromPage() throws ParseException { - List grants = new ArrayList<>(); - for (WebElement grantElement : getPageOfGrants()) { - GrantDto grantDto = new GrantDto( - getGrantTitle(grantElement), - parseDeadLineDate(grantElement)); - grants.add(grantDto); - } - return grants; - } - - private Date parseDeadLineDate(WebElement grantElement) throws ParseException { + public Date parseDeadLineDate(WebElement grantElement) throws ParseException { String deadlineDate = getFirstDeadline(grantElement); //10.06.2019 23:59 SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); return formatter.parse(deadlineDate); diff --git a/src/main/java/ru/ulstu/grant/service/KiasService.java b/src/main/java/ru/ulstu/grant/service/KiasService.java index 6881292..618e83c 100644 --- a/src/main/java/ru/ulstu/grant/service/KiasService.java +++ b/src/main/java/ru/ulstu/grant/service/KiasService.java @@ -1,6 +1,7 @@ package ru.ulstu.grant.service; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.springframework.stereotype.Service; @@ -37,13 +38,33 @@ public class KiasService { List grants = new ArrayList<>(); for (Integer year : generateGrantYears()) { webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, year)); - grants.addAll(new KiasPage(webDriver).getKiasGrants()); + grants.addAll(getKiasGrants(webDriver)); } grants.forEach(grantDto -> grantDto.setLeaderId(leaderId)); webDriver.quit(); return grants; } + public List getKiasGrants(WebDriver webDriver) throws ParseException { + List newGrants = new ArrayList<>(); + KiasPage kiasPage = new KiasPage(webDriver); + do { + newGrants.addAll(getGrantsFromPage(kiasPage)); + } while (kiasPage.goToNextPage()); //проверка существования следующей страницы с грантами + return newGrants; + } + + private List getGrantsFromPage(KiasPage kiasPage) throws ParseException { + List grants = new ArrayList<>(); + for (WebElement grantElement : kiasPage.getPageOfGrants()) { + GrantDto grantDto = new GrantDto( + kiasPage.getGrantTitle(grantElement), + kiasPage.parseDeadLineDate(grantElement)); + grants.add(grantDto); + } + return grants; + } + private List generateGrantYears() { return Arrays.asList(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.YEAR) + 1); From 916bbae41494d0420a4aaede0c633d3730ca4806 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 21:15:21 +0400 Subject: [PATCH 12/16] #120 add debug email --- .../ulstu/configuration/ApplicationProperties.java | 10 ++++++++++ .../java/ru/ulstu/user/service/MailService.java | 13 ++++++++++--- src/main/resources/application.properties | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/ulstu/configuration/ApplicationProperties.java b/src/main/java/ru/ulstu/configuration/ApplicationProperties.java index f75f53f..49efac4 100644 --- a/src/main/java/ru/ulstu/configuration/ApplicationProperties.java +++ b/src/main/java/ru/ulstu/configuration/ApplicationProperties.java @@ -21,6 +21,8 @@ public class ApplicationProperties { private boolean checkRun; + private String debugEmail; + public boolean isUseHttps() { return useHttps; } @@ -60,4 +62,12 @@ public class ApplicationProperties { public void setCheckRun(boolean checkRun) { this.checkRun = checkRun; } + + public String getDebugEmail() { + return debugEmail; + } + + public void setDebugEmail(String debugEmail) { + this.debugEmail = debugEmail; + } } diff --git a/src/main/java/ru/ulstu/user/service/MailService.java b/src/main/java/ru/ulstu/user/service/MailService.java index 864b749..3724792 100644 --- a/src/main/java/ru/ulstu/user/service/MailService.java +++ b/src/main/java/ru/ulstu/user/service/MailService.java @@ -1,5 +1,6 @@ package ru.ulstu.user.service; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.mail.MailProperties; @@ -21,11 +22,9 @@ import java.util.Map; @Service public class MailService { - private final Logger log = LoggerFactory.getLogger(MailService.class); - private static final String USER = "user"; private static final String BASE_URL = "baseUrl"; - + private final Logger log = LoggerFactory.getLogger(MailService.class); private final JavaMailSender javaMailSender; private final SpringTemplateEngine templateEngine; private final MailProperties mailProperties; @@ -48,10 +47,18 @@ public class MailService { message.setFrom(mailProperties.getUsername()); message.setSubject(subject); message.setText(content, true); + modifyForDebug(message, subject); javaMailSender.send(mimeMessage); log.debug("Sent email to User '{}'", to); } + private void modifyForDebug(MimeMessageHelper message, String originalSubject) throws MessagingException { + if (!StringUtils.isEmpty(applicationProperties.getDebugEmail())) { + message.setTo(applicationProperties.getDebugEmail()); + message.setSubject("To " + applicationProperties.getDebugEmail() + "; " + originalSubject); + } + } + @Async public void sendEmailFromTemplate(User user, String templateName, String subject) { Context context = new Context(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 038ddcf..cdd7569 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -35,5 +35,6 @@ liquibase.change-log=classpath:db/changelog-master.xml ng-tracker.base-url=http://127.0.0.1:8080 ng-tracker.undead-user-login=admin ng-tracker.dev-mode=true +ng-tracker.debug_email=romanov73@gmail.com ng-tracker.use-https=false ng-tracker.check-run=false \ No newline at end of file From 678397e970099cb6c655cc3ece6f43c3a47abf30 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 21:15:54 +0400 Subject: [PATCH 13/16] #120 test classes refactor --- .../{IndexConferenceTest.java => ConferenceTest.java} | 2 +- src/test/java/IndexKiasTest.java | 11 ----------- .../java/{IndexTaskTest.java => StudentTaskTest.java} | 2 +- 3 files changed, 2 insertions(+), 13 deletions(-) rename src/test/java/{IndexConferenceTest.java => ConferenceTest.java} (99%) delete mode 100644 src/test/java/IndexKiasTest.java rename src/test/java/{IndexTaskTest.java => StudentTaskTest.java} (99%) diff --git a/src/test/java/IndexConferenceTest.java b/src/test/java/ConferenceTest.java similarity index 99% rename from src/test/java/IndexConferenceTest.java rename to src/test/java/ConferenceTest.java index 269561f..3fae34b 100644 --- a/src/test/java/IndexConferenceTest.java +++ b/src/test/java/ConferenceTest.java @@ -26,7 +26,7 @@ import java.util.Map; @RunWith(SpringRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class IndexConferenceTest extends TestTemplate { +public class ConferenceTest extends TestTemplate { private final Map> navigationHolder = ImmutableMap.of( new ConferencesPage(), Arrays.asList("КОНФЕРЕНЦИИ", "/conferences/conferences"), new ConferencePage(), Arrays.asList("РЕДАКТИРОВАНИЕ КОНФЕРЕНЦИИ", "/conferences/conference?id=0"), diff --git a/src/test/java/IndexKiasTest.java b/src/test/java/IndexKiasTest.java deleted file mode 100644 index 8228e76..0000000 --- a/src/test/java/IndexKiasTest.java +++ /dev/null @@ -1,11 +0,0 @@ -import core.TestTemplate; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; -import ru.ulstu.NgTrackerApplication; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class IndexKiasTest extends TestTemplate { - -} diff --git a/src/test/java/IndexTaskTest.java b/src/test/java/StudentTaskTest.java similarity index 99% rename from src/test/java/IndexTaskTest.java rename to src/test/java/StudentTaskTest.java index 51b5e07..f15b55c 100644 --- a/src/test/java/IndexTaskTest.java +++ b/src/test/java/StudentTaskTest.java @@ -24,7 +24,7 @@ import java.util.Map; @RunWith(SpringRunner.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) @SpringBootTest(classes = NgTrackerApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) -public class IndexTaskTest extends TestTemplate { +public class StudentTaskTest extends TestTemplate { private final Map> navigationHolder = ImmutableMap.of( new TasksPage(), Arrays.asList("Список задач", "/students/tasks"), new TasksDashboardPage(), Arrays.asList("Панель управления", "/students/dashboard"), From 828d320e0f9f9d38b6beca019c9cdf3b97245a49 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 21:16:26 +0400 Subject: [PATCH 14/16] #120 try to run test in docker --- .gitlab-ci.yml | 2 +- src/main/java/ru/ulstu/grant/page/KiasPage.java | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fb4c0ed..6709891 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -22,7 +22,7 @@ checkRun: checkStyle: stage: test - script: ./gradlew check -x test + script: ./gradlew check deploy: stage: deploy diff --git a/src/main/java/ru/ulstu/grant/page/KiasPage.java b/src/main/java/ru/ulstu/grant/page/KiasPage.java index ea9bdb4..631f5db 100644 --- a/src/main/java/ru/ulstu/grant/page/KiasPage.java +++ b/src/main/java/ru/ulstu/grant/page/KiasPage.java @@ -8,9 +8,9 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; -import java.util.NoSuchElementException; public class KiasPage { + private final static String KIAS_GRANT_DATE_FORMAT = "dd.MM.yyyy HH:mm"; private WebDriver driver; public KiasPage(WebDriver webDriver) { @@ -23,10 +23,9 @@ public class KiasPage { driver.findElement(By.id("js-ctrlNext")).click(); return true; } - } catch (NoSuchElementException e) { + } finally { return false; } - return false; } public List getPageOfGrants() { @@ -41,7 +40,7 @@ public class KiasPage { public Date parseDeadLineDate(WebElement grantElement) throws ParseException { String deadlineDate = getFirstDeadline(grantElement); //10.06.2019 23:59 - SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm"); + SimpleDateFormat formatter = new SimpleDateFormat(KIAS_GRANT_DATE_FORMAT); return formatter.parse(deadlineDate); } From 280c39dae4ddc76db07b21e5d12dbc2d47c59ca9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 21:32:20 +0400 Subject: [PATCH 15/16] #120 fix variable --- src/test/java/StudentTaskTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/StudentTaskTest.java b/src/test/java/StudentTaskTest.java index f15b55c..f55a679 100644 --- a/src/test/java/StudentTaskTest.java +++ b/src/test/java/StudentTaskTest.java @@ -31,7 +31,7 @@ public class StudentTaskTest extends TestTemplate { new TaskPage(), Arrays.asList("Создать задачу", "/students/task?id=0") ); - private final String TAG = "ATag"; + private final String tag = "ATag"; @Autowired private ApplicationProperties applicationProperties; @@ -168,13 +168,13 @@ public class StudentTaskTest extends TestTemplate { String taskName = "Task " + (new Date()).getTime(); taskPage.setName(taskName); - taskPage.setTag(TAG); + taskPage.setTag(tag); Thread.sleep(1000); taskPage.addDeadlineDate("01.01.2020", 0); taskPage.addDeadlineDescription("Description", 0); taskPage.save(); - Assert.assertTrue(tasksPage.findTaskByTag(taskName, TAG)); + Assert.assertTrue(tasksPage.findTaskByTag(taskName, tag)); } @Test @@ -185,7 +185,7 @@ public class StudentTaskTest extends TestTemplate { TasksPage tasksPage = (TasksPage) getContext().initPage(page.getKey()); - Assert.assertTrue(tasksPage.findTag(TAG)); + Assert.assertTrue(tasksPage.findTag(tag)); } @Test @@ -194,9 +194,9 @@ public class StudentTaskTest extends TestTemplate { getContext().goTo(applicationProperties.getBaseUrl() + page.getValue().get(1)); TasksPage tasksPage = (TasksPage) getContext().initPage(page.getKey()); - tasksPage.selectTag(TAG); + tasksPage.selectTag(tag); - Assert.assertTrue(tasksPage.findTasksByTag(TAG)); + Assert.assertTrue(tasksPage.findTasksByTag(tag)); } @Test From 0cd3055176e76f2214478755da97830d44ee6bc9 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Sat, 25 May 2019 21:33:06 +0400 Subject: [PATCH 16/16] #120 disable tests --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6709891..fb4c0ed 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -22,7 +22,7 @@ checkRun: checkStyle: stage: test - script: ./gradlew check + script: ./gradlew check -x test deploy: stage: deploy