|
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.IssueStandardModel; |
|
7
|
|
|
import easytests.core.models.IssueStandardModelInterface; |
|
8
|
|
|
import easytests.core.models.SubjectModel; |
|
9
|
|
|
import easytests.core.models.SubjectModelInterface; |
|
10
|
|
|
import easytests.core.models.empty.SubjectModelEmpty; |
|
11
|
|
|
import easytests.core.options.IssueStandardsOptions; |
|
12
|
|
|
import easytests.core.options.SubjectsOptions; |
|
13
|
|
|
import easytests.core.options.SubjectsOptionsInterface; |
|
14
|
|
|
import easytests.core.options.builder.SubjectsOptionsBuilder; |
|
15
|
|
|
import easytests.core.services.IssueStandardsService; |
|
16
|
|
|
import easytests.core.services.SubjectsService; |
|
17
|
|
|
import easytests.personal.dto.SubjectDto; |
|
18
|
|
|
import java.util.List; |
|
19
|
|
|
import javax.validation.Valid; |
|
20
|
|
|
import javax.validation.constraints.NotNull; |
|
21
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
22
|
|
|
import org.springframework.stereotype.Controller; |
|
23
|
|
|
import org.springframework.ui.Model; |
|
24
|
|
|
import org.springframework.validation.BindingResult; |
|
25
|
|
|
import org.springframework.web.bind.annotation.*; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author vkpankov |
|
30
|
|
|
*/ |
|
31
|
|
|
@SuppressWarnings({"checkstyle:MultipleStringLiterals", "checkstyle:ClassDataAbstractionCoupling"}) |
|
32
|
|
|
@Controller |
|
33
|
|
|
@RequestMapping("/personal/subjects/") |
|
34
|
|
|
public class SubjectsController extends AbstractPersonalController { |
|
35
|
|
|
|
|
36
|
|
|
@Autowired |
|
37
|
|
|
private SubjectsService subjectsService; |
|
38
|
|
|
|
|
39
|
|
|
@Autowired |
|
40
|
|
|
private IssueStandardsService issueStandardsService; |
|
41
|
|
|
|
|
42
|
|
|
@Autowired |
|
43
|
|
|
private SubjectsOptionsBuilder subjectsOptionsBuilder; |
|
44
|
|
|
|
|
45
|
|
|
private void checkModel(SubjectModelInterface subjectModel) { |
|
46
|
|
|
if (subjectModel == null) { |
|
47
|
|
|
throw new NotFoundException(); |
|
48
|
|
|
} |
|
49
|
|
|
if (!subjectModel.getUser().getId().equals(this.getCurrentUserModel().getId())) { |
|
50
|
|
|
throw new ForbiddenException(); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private SubjectModelInterface getSubjectModel(Integer id) { |
|
55
|
|
|
final SubjectModelInterface subjectModel = this.subjectsService.find(id); |
|
56
|
|
|
checkModel(subjectModel); |
|
57
|
|
|
return subjectModel; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private SubjectModelInterface getSubjectModel(Integer id, SubjectsOptionsInterface subjectsOptions) { |
|
61
|
|
|
final SubjectModelInterface subjectModel = this.subjectsService.find(id, subjectsOptions); |
|
62
|
|
|
checkModel(subjectModel); |
|
63
|
|
|
return subjectModel; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
@RequestMapping("list") |
|
67
|
|
|
public String list(Model model) { |
|
68
|
|
|
final List<SubjectModelInterface> subjects = this.subjectsService.findByUser(this.getCurrentUserModel()); |
|
69
|
|
|
model.addAttribute("subjects", subjects); |
|
70
|
|
|
return "subjects/list"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
@GetMapping("create") |
|
74
|
|
|
public String create(Model model) { |
|
75
|
|
|
final SubjectDto subject = new SubjectDto(); |
|
76
|
|
|
subject.setName(""); |
|
77
|
|
|
subject.setDescription(""); |
|
78
|
|
|
model.addAttribute("methodType", "create"); |
|
79
|
|
|
model.addAttribute("subject", subject); |
|
80
|
|
|
return "subjects/form"; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
@PostMapping("create") |
|
84
|
|
|
public String create(@Valid @NotNull SubjectDto subject, |
|
85
|
|
|
BindingResult bindingResult, |
|
86
|
|
|
Model model) { |
|
87
|
|
|
if (bindingResult.hasErrors()) { |
|
88
|
|
|
model.addAttribute("methodType", "create"); |
|
89
|
|
|
model.addAttribute("subject", subject); |
|
90
|
|
|
model.addAttribute("errors", bindingResult); |
|
91
|
|
|
return "subjects/form"; |
|
92
|
|
|
} |
|
93
|
|
|
final SubjectModelInterface subjectModel = new SubjectModel(); |
|
94
|
|
|
subject.mapInto(subjectModel); |
|
95
|
|
|
subjectModel.setUser(this.getCurrentUserModel()); |
|
96
|
|
|
subjectsService.save(subjectModel); |
|
97
|
|
|
final IssueStandardModelInterface issueStandardModel = new IssueStandardModel(); |
|
98
|
|
|
issueStandardModel.setSubject(new SubjectModelEmpty(subjectModel.getId())); |
|
99
|
|
|
issueStandardsService.save(issueStandardModel); |
|
100
|
|
|
return "redirect:/personal/subjects/list"; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
@GetMapping("{id}") |
|
104
|
|
|
public String read(@PathVariable("id") Integer id, |
|
105
|
|
|
Model model) { |
|
106
|
|
|
|
|
107
|
|
|
final SubjectsOptionsInterface subjectsOptions = |
|
108
|
|
|
new SubjectsOptions().withIssueStandard(new IssueStandardsOptions()); |
|
109
|
|
|
final SubjectModelInterface subjectModel = getSubjectModel(id, subjectsOptions); |
|
110
|
|
|
|
|
111
|
|
|
final SubjectDto subject = new SubjectDto(); |
|
112
|
|
|
subject.map(subjectModel); |
|
113
|
|
|
model.addAttribute("subject", subject); |
|
114
|
|
|
model.addAttribute("issueStandardId", subjectModel.getIssueStandard().getId()); |
|
115
|
|
|
return "subjects/view"; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
@GetMapping("update/{id}") |
|
119
|
|
|
public String update(@PathVariable("id") Integer id, |
|
120
|
|
|
Model model) { |
|
121
|
|
|
model.addAttribute("methodType", "update"); |
|
122
|
|
|
final SubjectModelInterface subjectModel = getSubjectModel(id); |
|
123
|
|
|
final SubjectDto subject = new SubjectDto(); |
|
124
|
|
|
subject.map(subjectModel); |
|
125
|
|
|
model.addAttribute("subject", subject); |
|
126
|
|
|
return "subjects/form"; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
@PostMapping("update/{id}") |
|
130
|
|
|
public String update(@PathVariable("id") Integer subjectId, |
|
131
|
|
|
@Valid @NotNull SubjectDto subject, |
|
132
|
|
|
BindingResult bindingResult, |
|
133
|
|
|
Model model) { |
|
134
|
|
|
if (bindingResult.hasErrors()) { |
|
135
|
|
|
model.addAttribute("methodType", "update"); |
|
136
|
|
|
model.addAttribute("subject", subject); |
|
137
|
|
|
model.addAttribute("errors", bindingResult); |
|
138
|
|
|
return "subjects/form"; |
|
139
|
|
|
} |
|
140
|
|
|
final SubjectModelInterface subjectModel = getSubjectModel(subjectId); |
|
141
|
|
|
subject.mapInto(subjectModel); |
|
142
|
|
|
subjectsService.save(subjectModel); |
|
143
|
|
|
return "redirect:/personal/subjects/list"; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
@GetMapping("delete/{id}") |
|
147
|
|
|
public String deleteConfirmation(@PathVariable("id") Integer id, |
|
148
|
|
|
Model model) { |
|
149
|
|
|
final SubjectDto subjectDto = new SubjectDto(); |
|
150
|
|
|
final SubjectModelInterface subjectModel = getSubjectModel(id); |
|
151
|
|
|
subjectDto.map(subjectModel); |
|
152
|
|
|
model.addAttribute("subject", subjectDto); |
|
153
|
|
|
return "subjects/delete"; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
@PostMapping("delete/{id}") |
|
157
|
|
|
public String delete(@PathVariable("id") Integer subjectId, |
|
158
|
|
|
SubjectDto subjectDto, |
|
159
|
|
|
BindingResult bindingResult, |
|
160
|
|
|
Model model) { |
|
161
|
|
|
final SubjectModelInterface subjectModel = getSubjectModel(subjectId, subjectsOptionsBuilder.forDelete()); |
|
162
|
|
|
subjectsService.delete(subjectModel, subjectsOptionsBuilder.forDelete()); |
|
163
|
|
|
return "redirect:/personal/subjects/list"; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|