Create Controller&Service
parent
c2a32d6c27
commit
a2fa574888
@ -0,0 +1,164 @@
|
|||||||
|
package ru.ulstu.grant.model.service;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import ru.ulstu.deadline.model.Deadline;
|
||||||
|
import ru.ulstu.deadline.service.DeadlineService;
|
||||||
|
import ru.ulstu.file.service.FileService;
|
||||||
|
import ru.ulstu.grant.model.Grant;
|
||||||
|
import ru.ulstu.grant.model.GrantDto;
|
||||||
|
import ru.ulstu.grant.model.repository.GrantRepository;
|
||||||
|
import ru.ulstu.paper.model.PaperDto;
|
||||||
|
import ru.ulstu.project.model.Project;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static org.springframework.util.ObjectUtils.isEmpty;
|
||||||
|
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.IN_WORK;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GrantService {
|
||||||
|
private final static int MAX_DISPLAY_SIZE = 40;
|
||||||
|
|
||||||
|
private final GrantRepository grantRepository;
|
||||||
|
private final DeadlineService deadlineService;
|
||||||
|
private final FileService fileService;
|
||||||
|
|
||||||
|
public GrantService(GrantRepository grantRepository,
|
||||||
|
FileService fileService,
|
||||||
|
DeadlineService deadlineService) {
|
||||||
|
this.grantRepository = grantRepository;
|
||||||
|
this.fileService = fileService;
|
||||||
|
this.deadlineService = deadlineService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Grant> findAll() {
|
||||||
|
return sortGrants(grantRepository.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<GrantDto> findAllDto() {
|
||||||
|
List<GrantDto> grants = convert(findAll(), ru.ulstu.grant.model.GrantDto::new);
|
||||||
|
grants.forEach(grantDto -> grantDto.setTitle(StringUtils.abbreviate(grantDto.getTitle(), MAX_DISPLAY_SIZE)));
|
||||||
|
return grants;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GrantDto findOneDto(Integer id) {
|
||||||
|
return new ru.ulstu.grant.model.GrantDto(grantRepository.findOne(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Integer create(GrantDto grantDto) throws IOException {
|
||||||
|
Grant newGrant = copyFromDto(new Grant(), grantDto);
|
||||||
|
newGrant = grantRepository.save(newGrant);
|
||||||
|
//paperNotificationService.sendCreateNotification(newGrant);
|
||||||
|
return newGrant.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
private 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.setProject(grantDto.getProject()); //TODO: Исправить!
|
||||||
|
grant.setDeadlines(deadlineService.saveOrCreate(grantDto.getDeadlines()));
|
||||||
|
if (grantDto.getApplicationFileName() != null) {
|
||||||
|
grant.setApplication(fileService.createFileFromTmp(grantDto.getApplicationFileName()));
|
||||||
|
}
|
||||||
|
return grant;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Integer update(GrantDto grantDto) throws IOException {
|
||||||
|
Grant grant = grantRepository.findOne(grantDto.getId());
|
||||||
|
Grant.GrantStatus oldStatus = grant.getStatus();
|
||||||
|
if (grantDto.getApplicationFileName() != null && grant.getApplication() != null) {
|
||||||
|
fileService.deleteFile(grant.getApplication());
|
||||||
|
}
|
||||||
|
// if (paper.getStatus() != oldStatus) { //TODO: доделать потом все уведомления
|
||||||
|
// paperNotificationService.statusChangeNotification(paper, oldStatus);
|
||||||
|
// }
|
||||||
|
grantRepository.save(copyFromDto(grant, grantDto));
|
||||||
|
return grant.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void delete(Integer grantId) throws IOException {
|
||||||
|
Grant grant = grantRepository.findOne(grantId);
|
||||||
|
if (grant.getApplication() != null) {
|
||||||
|
fileService.deleteFile(grant.getApplication());
|
||||||
|
}
|
||||||
|
//возможно при удалении гранта будет удаляться и проект, к нему привязанный
|
||||||
|
grantRepository.delete(grant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Grant.GrantStatus> getGrantStatuses() {
|
||||||
|
return Arrays.asList(Grant.GrantStatus.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Grant create(String title, Project project_id, Date deadlineDate) {
|
||||||
|
Grant grant = new Grant();
|
||||||
|
grant.setTitle(title);
|
||||||
|
grant.setComment("Комментарий к гранту 1");
|
||||||
|
grant.setProject(project_id);
|
||||||
|
grant.setStatus(APPLICATION);
|
||||||
|
grant.getDeadlines().add(new Deadline(deadlineDate, "первый дедлайн"));
|
||||||
|
grant = grantRepository.save(grant);
|
||||||
|
// paperNotificationService.sendCreateNotification(paper);
|
||||||
|
return grant;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public List<PaperDto> filter(PaperFilterDto filterDto) {
|
||||||
|
// return convert(sortPapers(paperRepository.filter(
|
||||||
|
// filterDto.getFilterAuthorId() == null ? null : userService.findById(filterDto.getFilterAuthorId()),
|
||||||
|
// filterDto.getYear())), PaperDto::new);
|
||||||
|
// }
|
||||||
|
|
||||||
|
private List<Grant> sortGrants(List<Grant> grants) {
|
||||||
|
return grants.stream().sorted((grant1, grant2) -> {
|
||||||
|
int statusCompareResult =
|
||||||
|
Integer.valueOf(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant1.getStatus()))
|
||||||
|
.compareTo(Arrays.asList(Grant.GrantStatus.values()).indexOf(grant2.getStatus()));
|
||||||
|
if (statusCompareResult != 0) {
|
||||||
|
return statusCompareResult;
|
||||||
|
}
|
||||||
|
return grant1.getTitle().compareTo(grant2.getTitle());
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ru.ulstu.grant.model.GrantDto findGrant(int id) {
|
||||||
|
return new ru.ulstu.grant.model.GrantDto(grantRepository.getOne(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void closeFailedGrants() {
|
||||||
|
List<Grant> grants = grantRepository.findAll()
|
||||||
|
.stream()
|
||||||
|
.filter(grant -> grant.getNextDeadline().isPresent()
|
||||||
|
&& (grant.getStatus() == APPLICATION
|
||||||
|
|| grant.getStatus() == IN_WORK)
|
||||||
|
&& grant.getNextDeadline().get().getDate().before(new Date()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
grants.forEach(grant -> {
|
||||||
|
//Grant.GrantStatus oldStatus = grant.getStatus();
|
||||||
|
grant.setStatus(Grant.GrantStatus.FAILED);
|
||||||
|
grantRepository.save(grant);
|
||||||
|
//paperNotificationService.sendFailedNotification(grant, oldStatus);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save(GrantDto grantDto) throws IOException {
|
||||||
|
if (isEmpty(grantDto.getId())) {
|
||||||
|
create(grantDto);
|
||||||
|
} else {
|
||||||
|
update(grantDto);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public GrantDto findById(Integer grantId) {
|
||||||
|
return new GrantDto(grantRepository.findOne(grantId));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue