package ru.ulstu.conference.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.conference.model.ConferenceDto; import ru.ulstu.conference.model.ConferenceFilterDto; import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.service.ConferenceService; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.user.model.User; import springfox.documentation.annotations.ApiIgnore; 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; import static ru.ulstu.core.controller.Navigation.CONFERENCES_PAGE; import static ru.ulstu.core.controller.Navigation.CONFERENCE_PAGE; import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; @Controller() @RequestMapping(value = "/conferences") @ApiIgnore public class ConferenceController { private final ConferenceService conferenceService; public ConferenceController(ConferenceService conferenceService) { this.conferenceService = conferenceService; } @GetMapping("/conferences") public void getConferences(ModelMap modelMap) { modelMap.put("filteredConferences", new ConferenceFilterDto(conferenceService.findAllDto())); } @PostMapping("/conferences") public void filterConferences(@Valid ConferenceFilterDto conferenceFilterDto, ModelMap modelMap) { modelMap.put("filteredConferences", new ConferenceFilterDto(conferenceService.filter(conferenceFilterDto), conferenceFilterDto.getFilterUserId(), conferenceFilterDto.getYear())); } @GetMapping("/dashboard") public void getDashboard(ModelMap modelMap) { modelMap.put("conferences", conferenceService.findAllActiveDto()); conferenceService.setChartData(modelMap); // example } @GetMapping("/conference") public void getConference(ModelMap modelMap, @RequestParam(value = "id") Integer id) { if (id != null && id > 0) { modelMap.put("conferenceDto", conferenceService.getExistConferenceById(id)); } else { modelMap.put("conferenceDto", conferenceService.getNewConference()); } } @PostMapping(value = "/conference", params = "save") public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { filterEmptyDeadlines(conferenceDto); if (errors.hasErrors()) { return CONFERENCE_PAGE; } conferenceService.save(conferenceDto); return String.format(REDIRECT_TO, CONFERENCES_PAGE); } @GetMapping("/delete/{conference-id}") public String delete(@PathVariable("conference-id") Integer conferenceId) throws IOException { conferenceService.delete(conferenceId); return String.format(REDIRECT_TO, CONFERENCES_PAGE); } @PostMapping(value = "/conference", params = "addDeadline") public String addDeadline(@Valid ConferenceDto conferenceDto, Errors errors) { filterEmptyDeadlines(conferenceDto); if (errors.hasErrors()) { return CONFERENCE_PAGE; } conferenceDto.getDeadlines().add(new Deadline()); return CONFERENCE_PAGE; } @PostMapping(value = "/conference", params = "removeDeadline") public String removeDeadline(@Valid ConferenceDto conferenceDto, Errors errors, @RequestParam(value = "removeDeadline") Integer deadlineIndex) throws IOException { if (errors.hasErrors()) { return CONFERENCE_PAGE; } conferenceService.removeDeadline(conferenceDto, deadlineIndex); return CONFERENCE_PAGE; } @PostMapping(value = "/conference", params = "removePaper") public String removePaper(@Valid ConferenceDto conferenceDto, Errors errors, @RequestParam(value = "removePaper") Integer paperIndex) throws IOException { if (errors.hasErrors()) { return CONFERENCE_PAGE; } conferenceService.removePaper(conferenceDto, paperIndex); return CONFERENCE_PAGE; } @PostMapping(value = "/conference", params = "takePart") public String takePart(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { if (errors.hasErrors()) { return CONFERENCE_PAGE; } conferenceService.takePart(conferenceDto); return CONFERENCE_PAGE; } @PostMapping(value = "/conference", params = "pingConference") public String ping(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { if (errors.hasErrors()) { return CONFERENCE_PAGE; } conferenceService.ping(conferenceDto); return CONFERENCE_PAGE; } @ModelAttribute("allParticipation") public List getAllParticipation() { return conferenceService.getAllParticipations(); } @ModelAttribute("allDeposit") public List getAllDeposit() { return conferenceService.getAllDeposit(); } @ModelAttribute("allUsers") public List getAllUsers() { return conferenceService.getAllUsers(); } @ModelAttribute("allYears") public List getAllYears() { List years = new ArrayList<>(); for (int i = Calendar.getInstance().get(Calendar.YEAR); i > 2010; i--) { years.add(i); } return years; } private void filterEmptyDeadlines(ConferenceDto conferenceDto) { conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) .collect(Collectors.toList())); } }