Create Controller&Service

This commit is contained in:
T-Midnight 2018-12-23 02:22:41 +04:00
parent c2a32d6c27
commit a2fa574888
2 changed files with 262 additions and 0 deletions

View File

@ -0,0 +1,98 @@
package ru.ulstu.grant.model.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.model.service.GrantService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.model.PaperFilterDto;
import ru.ulstu.paper.service.PaperService;
import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.user.model.User;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;
import static org.springframework.util.StringUtils.isEmpty;
@Controller()
@RequestMapping(value = "/grants")
public class GrantController {
private final GrantService grantService;
public GrantController(GrantService grantService) {
this.grantService = grantService;
}
@GetMapping("/grants")
public void getGrants(ModelMap modelMap) {
modelMap.put("grants", grantService.findAllDto());
}
@GetMapping("/dashboard")
public void getDashboard(ModelMap modelMap) {
modelMap.put("dashboard", grantService.findAllDto());
}
@GetMapping("/grant")
public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) {
if (id != null && id > 0) {
modelMap.put("grantDto", grantService.findOneDto(id));
} else {
modelMap.put("grantDto", new GrantDto());
}
}
@PostMapping(value = "/grant", params = "save")
public String save(@Valid GrantDto grantDto, Errors errors) throws IOException {
filterEmptyDeadlines(grantDto);
if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode","Не может быть пустым");
}
if (errors.hasErrors()) {
return "/grants/grant";
}
grantService.save(grantDto);
return "redirect:/grants/grants";
}
@PostMapping(value = "/grant", params = "addDeadline")
public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
filterEmptyDeadlines(grantDto);
if (errors.hasErrors()) {
return "/grants/grant";
}
grantDto.getDeadlines().add(new DeadlineDto());
return "/grants/grant";
}
@PostMapping(value = "/grant", params = "addProject")
public String addProject(@Valid GrantDto grantDto, Errors errors) {
if (errors.hasErrors()) {
return "/grants/grant";
}
grantDto.setProject(new ProjectDto());
return "/grants/grant";
}
@GetMapping("/delete/{grant-id}")
public String delete(@PathVariable("grant-id") Integer grantId) throws IOException {
grantService.delete(grantId);
return "redirect:/grants/grants";
}
private void filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
.collect(Collectors.toList()));
}
}

View File

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