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 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.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.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 { 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; } @Override public PageableItems findAll(int offset, int count) { final Page 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.setId(grantDto.getId()); 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 void doActionsOnDiffObjects(Grant oldEntity, Grant entity) { entity.getAuthors().forEach(author -> { if (!oldEntity.getAuthors().contains(author)) { grantNotificationService.sendAuthorsChangeNotification(entity, oldEntity.getAuthors()); } }); if (!entity.getLeader().equals(oldEntity.getLeader())) { grantNotificationService.sendLeaderChangeNotification(entity, oldEntity.getLeader()); } } @Override protected List getEvents(Grant entity) { return eventService.findByGrant(entity); } public List 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 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; } @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"); } } } @Override public PageableItems findAllActiveDto(int offset, int count) { return convertPageable(findAllActive(offset, count), GrantDashboardDto::new); } @Override public PageableItems findAllActive(int offset, int count) { Page activeGrantsPage = grantRepository.findAllActive(new OffsetablePageRequest(offset, count)); return new PageableItems<>(activeGrantsPage.getTotalElements(), activeGrantsPage.getContent()); } @Transactional public void ping(int grantId) { pingService.addPing(findById(grantId)); } }