|
|
@ -1,8 +1,11 @@
|
|
|
|
package ru.ulstu.grant.service;
|
|
|
|
package ru.ulstu.grant.service;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import org.springframework.validation.Errors;
|
|
|
|
import ru.ulstu.deadline.model.Deadline;
|
|
|
|
import ru.ulstu.deadline.model.Deadline;
|
|
|
|
import ru.ulstu.deadline.service.DeadlineService;
|
|
|
|
import ru.ulstu.deadline.service.DeadlineService;
|
|
|
|
import ru.ulstu.file.model.FileDataDto;
|
|
|
|
import ru.ulstu.file.model.FileDataDto;
|
|
|
@ -10,6 +13,7 @@ import ru.ulstu.file.service.FileService;
|
|
|
|
import ru.ulstu.grant.model.Grant;
|
|
|
|
import ru.ulstu.grant.model.Grant;
|
|
|
|
import ru.ulstu.grant.model.GrantDto;
|
|
|
|
import ru.ulstu.grant.model.GrantDto;
|
|
|
|
import ru.ulstu.grant.repository.GrantRepository;
|
|
|
|
import ru.ulstu.grant.repository.GrantRepository;
|
|
|
|
|
|
|
|
import ru.ulstu.name.BaseService;
|
|
|
|
import ru.ulstu.paper.model.Paper;
|
|
|
|
import ru.ulstu.paper.model.Paper;
|
|
|
|
import ru.ulstu.paper.model.PaperDto;
|
|
|
|
import ru.ulstu.paper.model.PaperDto;
|
|
|
|
import ru.ulstu.paper.service.PaperService;
|
|
|
|
import ru.ulstu.paper.service.PaperService;
|
|
|
@ -22,12 +26,14 @@ import ru.ulstu.user.model.User;
|
|
|
|
import ru.ulstu.user.service.UserService;
|
|
|
|
import ru.ulstu.user.service.UserService;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
import static java.util.stream.Collectors.toList;
|
|
|
|
import static java.util.stream.Collectors.toList;
|
|
|
|
import static org.springframework.util.ObjectUtils.isEmpty;
|
|
|
|
import static org.springframework.util.ObjectUtils.isEmpty;
|
|
|
@ -35,8 +41,8 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
|
|
|
|
import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION;
|
|
|
|
import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION;
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
public class GrantService {
|
|
|
|
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 GrantRepository grantRepository;
|
|
|
|
private final ProjectService projectService;
|
|
|
|
private final ProjectService projectService;
|
|
|
@ -46,6 +52,7 @@ public class GrantService {
|
|
|
|
private final PaperService paperService;
|
|
|
|
private final PaperService paperService;
|
|
|
|
private final EventService eventService;
|
|
|
|
private final EventService eventService;
|
|
|
|
private final GrantNotificationService grantNotificationService;
|
|
|
|
private final GrantNotificationService grantNotificationService;
|
|
|
|
|
|
|
|
private final KiasService kiasService;
|
|
|
|
private final PingService pingService;
|
|
|
|
private final PingService pingService;
|
|
|
|
|
|
|
|
|
|
|
|
public GrantService(GrantRepository grantRepository,
|
|
|
|
public GrantService(GrantRepository grantRepository,
|
|
|
@ -56,8 +63,11 @@ public class GrantService {
|
|
|
|
PaperService paperService,
|
|
|
|
PaperService paperService,
|
|
|
|
EventService eventService,
|
|
|
|
EventService eventService,
|
|
|
|
GrantNotificationService grantNotificationService,
|
|
|
|
GrantNotificationService grantNotificationService,
|
|
|
|
|
|
|
|
KiasService kiasService,
|
|
|
|
PingService pingService) {
|
|
|
|
PingService pingService) {
|
|
|
|
this.grantRepository = grantRepository;
|
|
|
|
this.grantRepository = grantRepository;
|
|
|
|
|
|
|
|
this.kiasService = kiasService;
|
|
|
|
|
|
|
|
this.baseRepository = grantRepository;
|
|
|
|
this.fileService = fileService;
|
|
|
|
this.fileService = fileService;
|
|
|
|
this.deadlineService = deadlineService;
|
|
|
|
this.deadlineService = deadlineService;
|
|
|
|
this.projectService = projectService;
|
|
|
|
this.projectService = projectService;
|
|
|
@ -73,9 +83,7 @@ public class GrantService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<GrantDto> findAllDto() {
|
|
|
|
public List<GrantDto> findAllDto() {
|
|
|
|
List<GrantDto> grants = convert(findAll(), GrantDto::new);
|
|
|
|
return convert(findAll(), GrantDto::new);
|
|
|
|
grants.forEach(grantDto -> grantDto.setTitle(StringUtils.abbreviate(grantDto.getTitle(), MAX_DISPLAY_SIZE)));
|
|
|
|
|
|
|
|
return grants;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public GrantDto findOneDto(Integer id) {
|
|
|
|
public GrantDto findOneDto(Integer id) {
|
|
|
@ -180,12 +188,58 @@ public class GrantService {
|
|
|
|
return grant;
|
|
|
|
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())) {
|
|
|
|
if (isEmpty(grantDto.getId())) {
|
|
|
|
create(grantDto);
|
|
|
|
create(grantDto);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
update(grantDto);
|
|
|
|
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", "Укажите руководителя");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void checkEmptyDeadlines(GrantDto grantDto, Errors errors) {
|
|
|
|
|
|
|
|
if (grantDto.getDeadlines().isEmpty()) {
|
|
|
|
|
|
|
|
errors.rejectValue("deadlines", "errorCode", "Не может быть пусто");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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<User> getGrantAuthors(GrantDto grantDto) {
|
|
|
|
public List<User> getGrantAuthors(GrantDto grantDto) {
|
|
|
@ -220,11 +274,7 @@ public class GrantService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<PaperDto> getAllUncompletedPapers() {
|
|
|
|
public List<PaperDto> getAllUncompletedPapers() {
|
|
|
|
List<PaperDto> papers = paperService.findAllNotCompleted();
|
|
|
|
return paperService.findAllNotCompleted();
|
|
|
|
papers.stream()
|
|
|
|
|
|
|
|
.forEach(paper ->
|
|
|
|
|
|
|
|
paper.setTitle(StringUtils.abbreviate(paper.getTitle(), MAX_DISPLAY_SIZE)));
|
|
|
|
|
|
|
|
return papers;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void attachPaper(GrantDto grantDto) {
|
|
|
|
public void attachPaper(GrantDto grantDto) {
|
|
|
@ -266,6 +316,31 @@ public class GrantService {
|
|
|
|
.collect(toList());
|
|
|
|
.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()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<GrantDto> findAllActiveDto() {
|
|
|
|
|
|
|
|
return convert(findAllActive(), GrantDto::new);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<Grant> findAllActive() {
|
|
|
|
|
|
|
|
return grantRepository.findAllActive();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Grant findById(Integer id) {
|
|
|
|
public Grant findById(Integer id) {
|
|
|
|
return grantRepository.findOne(id);
|
|
|
|
return grantRepository.findOne(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|