malinink /
easy-tests
| 1 | package easytests.personal.controllers; |
||
| 2 | |||
| 3 | import easytests.common.controllers.AbstractPersonalController; |
||
| 4 | import easytests.common.exceptions.ForbiddenException; |
||
| 5 | import easytests.common.exceptions.NotFoundException; |
||
| 6 | import easytests.core.models.*; |
||
| 7 | import easytests.core.options.*; |
||
| 8 | import easytests.core.services.*; |
||
| 9 | import easytests.personal.dto.IssueStandardDto; |
||
| 10 | import easytests.personal.validators.IssueStandardDtoValidator; |
||
| 11 | import java.util.ArrayList; |
||
| 12 | import java.util.List; |
||
| 13 | import java.util.stream.Collectors; |
||
| 14 | import javax.validation.Valid; |
||
| 15 | import org.springframework.beans.factory.annotation.Autowired; |
||
| 16 | import org.springframework.stereotype.Controller; |
||
| 17 | import org.springframework.transaction.annotation.Propagation; |
||
| 18 | import org.springframework.transaction.annotation.Transactional; |
||
| 19 | import org.springframework.ui.Model; |
||
| 20 | import org.springframework.validation.BindingResult; |
||
| 21 | import org.springframework.web.bind.annotation.*; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @author SingularityA |
||
| 25 | */ |
||
| 26 | @Controller |
||
| 27 | @RequestMapping("personal/issue_standard/") |
||
| 28 | @SuppressWarnings({ |
||
| 29 | "checkstyle:MultipleStringLiterals", |
||
| 30 | "checkstyle:ClassDataAbstractionCoupling", |
||
| 31 | "checkstyle:ClassFanOutComplexity"}) |
||
| 32 | public class IssueStandardsController extends AbstractPersonalController { |
||
| 33 | |||
| 34 | @Autowired |
||
| 35 | private IssueStandardsService issueStandardsService; |
||
| 36 | |||
| 37 | @Autowired |
||
| 38 | private IssueStandardTopicPrioritiesService topicPrioritiesService; |
||
| 39 | |||
| 40 | @Autowired |
||
| 41 | private IssueStandardQuestionTypeOptionsService questionTypeOptionsService; |
||
| 42 | |||
| 43 | @Autowired |
||
| 44 | private SubjectsService subjectsService; |
||
| 45 | |||
| 46 | @Autowired |
||
| 47 | private QuestionTypesService questionTypesService; |
||
| 48 | |||
| 49 | @Autowired |
||
| 50 | private IssueStandardDtoValidator issueStandardValidator; |
||
| 51 | |||
| 52 | @ModelAttribute("questionTypes") |
||
| 53 | private List<QuestionTypeModelInterface> getQuestionTypes() { |
||
| 54 | return this.questionTypesService.findAll(); |
||
| 55 | } |
||
| 56 | |||
| 57 | @ModelAttribute("viewUrl") |
||
| 58 | private String viewUrl(@PathVariable Integer issueStandardId) { |
||
| 59 | return "/personal/issue_standard/" + Integer.toString(issueStandardId) + "/"; |
||
| 60 | } |
||
| 61 | |||
| 62 | @ModelAttribute("editUrl") |
||
| 63 | private String editUrl(@PathVariable Integer issueStandardId) { |
||
| 64 | return "/personal/issue_standard/update/" + Integer.toString(issueStandardId) + "/"; |
||
| 65 | } |
||
| 66 | |||
| 67 | @GetMapping("{issueStandardId}/") |
||
| 68 | public String view(Model model, |
||
| 69 | @PathVariable Integer issueStandardId) { |
||
| 70 | |||
| 71 | final IssueStandardModelInterface issueStandard = this.getIssueStandardModel( |
||
| 72 | issueStandardId, |
||
| 73 | getIssueStandardsOptions()); |
||
| 74 | model.addAttribute("issueStandard", issueStandard); |
||
| 75 | return "issue_standards/view"; |
||
| 76 | } |
||
| 77 | |||
| 78 | @GetMapping("update/{issueStandardId}/") |
||
| 79 | public String update(Model model, |
||
| 80 | @PathVariable Integer issueStandardId) { |
||
| 81 | |||
| 82 | final IssueStandardModelInterface issueStandard = this.getIssueStandardModel( |
||
| 83 | issueStandardId, |
||
| 84 | getIssueStandardsOptions()); |
||
| 85 | final IssueStandardDto issueStandardDto = new IssueStandardDto(); |
||
| 86 | issueStandardDto.map(issueStandard); |
||
| 87 | |||
| 88 | model.addAttribute("issueStandard", issueStandardDto); |
||
| 89 | model.addAttribute("subject", issueStandard.getSubject()); |
||
| 90 | return "issue_standards/edit"; |
||
| 91 | } |
||
| 92 | |||
| 93 | @PostMapping("update/{issueStandardId}/") |
||
| 94 | public String save(Model model, |
||
| 95 | @PathVariable Integer issueStandardId, |
||
| 96 | @Valid IssueStandardDto issueStandardDto, |
||
| 97 | BindingResult bindingResult) { |
||
| 98 | |||
| 99 | final IssueStandardModelInterface issueStandardModel = getIssueStandardModel( |
||
| 100 | issueStandardId, |
||
| 101 | new IssueStandardsOptions()); |
||
| 102 | issueStandardDto.setId(issueStandardId); |
||
| 103 | issueStandardValidator.validate(issueStandardDto, bindingResult); |
||
| 104 | |||
| 105 | // TODO: display errors on view |
||
| 106 | System.out.println(issueStandardDto); |
||
| 107 | System.out.println(bindingResult); |
||
| 108 | |||
| 109 | if (bindingResult.hasErrors()) { |
||
| 110 | final SubjectModelInterface subjectModel = this.getSubjectModel(issueStandardDto.getSubjectId()); |
||
| 111 | model.addAttribute("issueStandard", issueStandardDto); |
||
| 112 | model.addAttribute("subject", subjectModel); |
||
| 113 | model.addAttribute("errors", bindingResult); |
||
| 114 | return "issue_standards/edit"; |
||
| 115 | } |
||
| 116 | |||
| 117 | issueStandardDto.mapInto(issueStandardModel); |
||
| 118 | issueStandardModel.setId(issueStandardId); |
||
| 119 | saveIssueStandardModel(issueStandardModel); |
||
| 120 | |||
| 121 | return "redirect:/personal/issue_standard/{issueStandardId}/"; |
||
| 122 | } |
||
| 123 | |||
| 124 | private IssueStandardModelInterface getIssueStandardModel( |
||
| 125 | Integer issueStandardId, |
||
| 126 | IssueStandardsOptionsInterface issueStandardsOptions) { |
||
| 127 | |||
| 128 | final IssueStandardModelInterface issueStandardModel = this.issueStandardsService.find( |
||
| 129 | issueStandardId, |
||
| 130 | issueStandardsOptions); |
||
| 131 | if (issueStandardModel == null) { |
||
| 132 | throw new NotFoundException(); |
||
| 133 | } |
||
| 134 | final SubjectModelInterface subjectModel = this.getSubjectModel(issueStandardModel.getSubject().getId()); |
||
| 135 | issueStandardModel.setSubject(subjectModel); |
||
| 136 | return issueStandardModel; |
||
| 137 | } |
||
| 138 | |||
| 139 | private IssueStandardsOptionsInterface getIssueStandardsOptions() { |
||
| 140 | return new IssueStandardsOptions() |
||
| 141 | .withTopicPriorities(new IssueStandardTopicPrioritiesOptions()) |
||
| 142 | .withQuestionTypeOptions(new IssueStandardQuestionTypeOptionsOptions()); |
||
| 143 | } |
||
| 144 | |||
| 145 | private SubjectModelInterface getSubjectModel(Integer subjectId) { |
||
| 146 | final SubjectModelInterface subjectModel = this.subjectsService.find( |
||
| 147 | subjectId, |
||
| 148 | new SubjectsOptions() |
||
| 149 | .withTopics(new TopicsOptions())); |
||
| 150 | if (subjectModel == null) { |
||
| 151 | throw new NotFoundException(); |
||
| 152 | } |
||
| 153 | if (!subjectModel.getUser().getId().equals(this.getCurrentUserModel().getId())) { |
||
| 154 | throw new ForbiddenException(); |
||
| 155 | } |
||
| 156 | return subjectModel; |
||
| 157 | } |
||
| 158 | |||
| 159 | @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) |
||
| 160 | private void saveIssueStandardModel(IssueStandardModelInterface newIssueStandardModel) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 161 | final IssueStandardModelInterface currentIssueStandardModel |
||
| 162 | = this.getIssueStandardModel(newIssueStandardModel.getId(), getIssueStandardsOptions()); |
||
| 163 | System.out.println(currentIssueStandardModel); |
||
| 164 | |||
| 165 | final List<Integer> newTopicPriorityIds |
||
| 166 | = new ArrayList<>(newIssueStandardModel.getTopicPriorities().size()); |
||
| 167 | |||
| 168 | newTopicPriorityIds.addAll(newIssueStandardModel.getTopicPriorities().stream() |
||
| 169 | .map(IdentityInterface::getId).collect(Collectors.toList())); |
||
| 170 | |||
| 171 | final List<Integer> newQuestionTypeOptionIds |
||
| 172 | = new ArrayList<>(newIssueStandardModel.getQuestionTypeOptions().size()); |
||
| 173 | |||
| 174 | newQuestionTypeOptionIds.addAll(newIssueStandardModel.getQuestionTypeOptions().stream() |
||
| 175 | .map(IdentityInterface::getId).collect(Collectors.toList())); |
||
| 176 | |||
| 177 | for (IssueStandardTopicPriorityModelInterface currentTopicPriority |
||
| 178 | : currentIssueStandardModel.getTopicPriorities()) { |
||
| 179 | if (!newTopicPriorityIds.contains(currentTopicPriority.getId())) { |
||
| 180 | this.topicPrioritiesService.delete(currentTopicPriority); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | for (IssueStandardQuestionTypeOptionModelInterface currentQuestionTypeOption |
||
| 185 | : currentIssueStandardModel.getQuestionTypeOptions()) { |
||
| 186 | if (!newQuestionTypeOptionIds.contains(currentQuestionTypeOption.getId())) { |
||
| 187 | this.questionTypeOptionsService.delete(currentQuestionTypeOption); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | this.issueStandardsService.save(newIssueStandardModel, getIssueStandardsOptions()); |
||
| 192 | } |
||
| 193 | } |
||
| 194 |