#120 move to services

pull/221/head
Anton Romanov 5 years ago
parent 457f1da985
commit c303d65c8d

@ -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'
}

@ -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<WebElement> getGrants() {
List<WebElement> grants = new ArrayList<>();
do {
grants.addAll(getPageOfGrants());
}
while (checkPagination());
return grants;
}
public List<WebElement> getPageOfGrants() {
WebElement listContest = driver.findElement(By.tagName("tBody"));
List<WebElement> 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();
}
}

@ -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<GrantDto> 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");
}
}

@ -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");
}
}
}
}

@ -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<GrantDto> getNewGrantsDto() throws ParseException {
webDriver.get(String.format(BASE_URL, CONTEST_STATUS_ID, CONTEST_TYPE, CONTEST_YEAR));
KiasPage kiasPage = new KiasPage(webDriver);
List<WebElement> kiasGrants = new ArrayList<>();
List<GrantDto> 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");
}
}

@ -18,6 +18,7 @@ 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;
@ -341,4 +342,8 @@ public class UserService implements UserDetailsService {
throw new UserSendingMailException(email);
}
}
public User findOneByLoginIgnoreCase(String login) {
return userRepository.findOneByLoginIgnoreCase(login);
}
}

Loading…
Cancel
Save