229 lines
9.0 KiB
Java
229 lines
9.0 KiB
Java
package ru.ulstu.activity.grant.service;
|
||
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.data.domain.Page;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.transaction.annotation.Transactional;
|
||
import org.springframework.validation.Errors;
|
||
import ru.ulstu.activity.common.service.ActivityService;
|
||
import ru.ulstu.activity.deadline.service.DeadlineService;
|
||
import ru.ulstu.activity.file.service.FileService;
|
||
import ru.ulstu.activity.grant.model.Grant;
|
||
import ru.ulstu.activity.grant.model.GrantDashboardDto;
|
||
import ru.ulstu.activity.grant.model.GrantDto;
|
||
import ru.ulstu.activity.grant.model.GrantListDto;
|
||
import ru.ulstu.activity.paper.model.Paper;
|
||
import ru.ulstu.activity.paper.model.PaperDto;
|
||
import ru.ulstu.activity.paper.service.PaperService;
|
||
import ru.ulstu.activity.ping.service.PingService;
|
||
import ru.ulstu.activity.project.service.ProjectService;
|
||
import ru.ulstu.activity.timeline.model.Event;
|
||
import ru.ulstu.activity.timeline.service.EventService;
|
||
import ru.ulstu.core.jpa.OffsetablePageRequest;
|
||
import ru.ulstu.core.model.response.PageableItems;
|
||
import ru.ulstu.core.util.DateUtils;
|
||
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.Date;
|
||
import java.util.List;
|
||
|
||
import static java.util.stream.Collectors.toList;
|
||
import static ru.ulstu.activity.grant.model.Grant.GrantStatus.APPLICATION;
|
||
import static ru.ulstu.core.util.StreamApiUtils.convertPageable;
|
||
|
||
@Service
|
||
public class GrantService extends ActivityService<GrantListDto, Grant, GrantDto> {
|
||
private final Logger log = LoggerFactory.getLogger(GrantService.class);
|
||
|
||
private final GrantRepository grantRepository;
|
||
private final ProjectService projectService;
|
||
private final DeadlineService deadlineService;
|
||
private final FileService fileService;
|
||
private final UserService userService;
|
||
private final PaperService paperService;
|
||
private final EventService eventService;
|
||
private final GrantNotificationService grantNotificationService;
|
||
private final KiasService kiasService;
|
||
private final PingService pingService;
|
||
|
||
public GrantService(GrantRepository grantRepository,
|
||
FileService fileService,
|
||
DeadlineService deadlineService,
|
||
ProjectService projectService,
|
||
UserService userService,
|
||
PaperService paperService,
|
||
EventService eventService,
|
||
GrantNotificationService grantNotificationService,
|
||
KiasService kiasService,
|
||
PingService pingService) {
|
||
super(grantRepository, grantNotificationService, pingService, eventService);
|
||
this.grantRepository = grantRepository;
|
||
this.kiasService = kiasService;
|
||
this.fileService = fileService;
|
||
this.deadlineService = deadlineService;
|
||
this.projectService = projectService;
|
||
this.userService = userService;
|
||
this.paperService = paperService;
|
||
this.eventService = eventService;
|
||
this.grantNotificationService = grantNotificationService;
|
||
this.pingService = pingService;
|
||
}
|
||
|
||
public GrantDto getExistGrantById(Integer id) {
|
||
return new GrantDto(findById(id));
|
||
}
|
||
|
||
@Override
|
||
public PageableItems<Grant> findAll(int offset, int count) {
|
||
final Page<Grant> page = grantRepository.findAll(new OffsetablePageRequest(offset, count));
|
||
return new PageableItems<>(page.getTotalElements(), page.getContent());
|
||
}
|
||
|
||
@Override
|
||
protected GrantListDto getActivityListDto(Grant entity) {
|
||
return new GrantListDto(entity);
|
||
}
|
||
|
||
@Override
|
||
protected Grant getNewActivity() {
|
||
return new Grant();
|
||
}
|
||
|
||
@Override
|
||
protected GrantDto getNewActivityDto(Grant entity) {
|
||
return new GrantDto(entity);
|
||
}
|
||
|
||
protected Grant copyFromDto(Grant grant, GrantDto grantDto) throws IOException {
|
||
grant.setComment(grantDto.getComment());
|
||
grant.setStatus(grantDto.getStatus() == null ? APPLICATION : grantDto.getStatus());
|
||
grant.setTitle(grantDto.getTitle());
|
||
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
|
||
if (!grant.getFiles().isEmpty()) {
|
||
grant.setFiles(fileService.saveOrCreate(grantDto.getFiles().stream()
|
||
.filter(f -> !f.isDeleted())
|
||
.collect(toList())));
|
||
}
|
||
grant.getAuthors().clear();
|
||
if (grantDto.getMembers() != null && !grantDto.getMembers().isEmpty()) {
|
||
grantDto.getMembers().forEach(memberDto -> grant.getAuthors().add(userService.findById(memberDto.getId())));
|
||
}
|
||
if (grantDto.getLeaderId() != null) {
|
||
grant.setLeader(userService.findById(grantDto.getLeaderId()));
|
||
}
|
||
grant.getPapers().clear();
|
||
if (grantDto.getPaperIds() != null && !grantDto.getPaperIds().isEmpty()) {
|
||
grantDto.getPaperIds().forEach(paperIds -> grant.getPapers().add(paperService.findById(paperIds)));
|
||
}
|
||
return grant;
|
||
}
|
||
|
||
@Override
|
||
protected List<Event> getEvents(Grant entity) {
|
||
return eventService.findByGrant(entity);
|
||
}
|
||
|
||
@Transactional
|
||
public boolean delete(Integer grantId) {
|
||
Grant grant = findById(grantId);
|
||
if (grant != null) {
|
||
grantRepository.delete(grant);
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public List<Grant.GrantStatus> getGrantStatuses() {
|
||
return Arrays.asList(Grant.GrantStatus.values());
|
||
}
|
||
|
||
private boolean saveFromKias(GrantDto grantDto) throws IOException {
|
||
grantDto.setTitle(grantDto.getTitle());
|
||
if (checkUniqueName(grantDto.getTitle(), grantDto.getId())) {
|
||
Grant grantFromDB = grantRepository.findByTitle(grantDto.getTitle()); //грант с таким же названием из бд
|
||
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 = DateUtils.clearTime(grantDto.getDeadlines().get(0).getDate()); //дата с сайта киас
|
||
Date foundGrantDate = DateUtils.clearTime(deadlineService.findByGrantIdAndDate(id, date));
|
||
return foundGrantDate != null && foundGrantDate.compareTo(date) == 0;
|
||
}
|
||
|
||
public List<PaperDto> getGrantPapers(List<Integer> paperIds) {
|
||
return paperService.findAllSelect(paperIds);
|
||
}
|
||
|
||
public List<PaperDto> getAllUncompletedPapers() {
|
||
return paperService.findAllNotCompleted();
|
||
}
|
||
|
||
private List<User> getCompletedPapersAuthors(Paper.PaperType type) {
|
||
List<Paper> papers = paperService.findAllCompletedByType(type);
|
||
return papers.stream()
|
||
.filter(paper -> paper.getAuthors() != null)
|
||
.flatMap(paper -> paper.getAuthors().stream())
|
||
.collect(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 PageableItems<GrantDashboardDto> findAllActiveDto(int offset, int count) {
|
||
return convertPageable(findAllActive(offset, count), GrantDashboardDto::new);
|
||
}
|
||
|
||
public PageableItems<Grant> findAllActive(int offset, int count) {
|
||
Page<Grant> activeGrantsPage = grantRepository.findAllActive(new OffsetablePageRequest(offset, count));
|
||
return new PageableItems<>(activeGrantsPage.getTotalElements(), activeGrantsPage.getContent());
|
||
|
||
}
|
||
|
||
public GrantDto findGrantById(Integer id) {
|
||
return new GrantDto(findById(id));
|
||
}
|
||
|
||
public Grant findById(Integer id) {
|
||
return grantRepository.getOne(id);
|
||
}
|
||
|
||
@Transactional
|
||
public void ping(int grantId) {
|
||
pingService.addPing(findById(grantId));
|
||
}
|
||
}
|