checkModel(TopicModelInterface,Integer)   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
package easytests.personal.controllers;
2
3
import easytests.common.controllers.AbstractCrudController;
4
import easytests.common.exceptions.ForbiddenException;
5
import easytests.common.exceptions.NotFoundException;
6
import easytests.core.models.SubjectModelInterface;
7
import easytests.core.models.TopicModel;
8
import easytests.core.models.TopicModelInterface;
9
import easytests.core.options.SubjectsOptionsInterface;
10
import easytests.core.options.TopicsOptionsInterface;
11
import easytests.core.options.builder.SubjectsOptionsBuilder;
12
import easytests.core.options.builder.TopicsOptionsBuilder;
13
import easytests.core.services.QuestionsService;
14
import easytests.core.services.SubjectsService;
15
import easytests.core.services.TopicsService;
16
import easytests.personal.dto.TopicDto;
17
import java.util.List;
18
import javax.validation.Valid;
19
import javax.validation.constraints.NotNull;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Controller;
22
import org.springframework.ui.Model;
23
import org.springframework.validation.BindingResult;
24
import org.springframework.web.bind.annotation.GetMapping;
25
import org.springframework.web.bind.annotation.PathVariable;
26
import org.springframework.web.bind.annotation.PostMapping;
27
import org.springframework.web.bind.annotation.RequestMapping;
28
29
/**
30
 * @author DoZor-80
31
 */
32
@SuppressWarnings({"checkstyle:MultipleStringLiterals", "checkstyle:ClassDataAbstractionCoupling"})
33
@Controller
34
@RequestMapping("/personal/subjects/{subjectId}/topics")
35
public class TopicsController extends AbstractCrudController {
36
37
    @Autowired
38
    private TopicsService topicsService;
39
40
    @Autowired
41
    private SubjectsService subjectsService;
42
43
    @Autowired
44
    private QuestionsService questionsService;
45
46
    @Autowired
47
    private TopicsOptionsBuilder topicsOptionsBuilder;
48
49
    @Autowired
50
    private SubjectsOptionsBuilder subjectsOptionsBuilder;
51
52
    private void checkModel(TopicModelInterface topicModel, Integer subjectId) {
53
        if (topicModel == null) {
54
            throw new NotFoundException();
55
        }
56
        if (!topicModel.getSubject().getId().equals(this.getCurrentSubjectModel(subjectId, false).getId())) {
57
            throw new ForbiddenException();
58
        }
59
    }
60
61
    private void checkModel(SubjectModelInterface subjectModel) {
62
        if (subjectModel == null) {
63
            throw new NotFoundException();
64
        }
65
        if (!subjectModel.getUser().getId().equals(this.getCurrentUserModel().getId())) {
66
            throw new ForbiddenException();
67
        }
68
    }
69
70
    private TopicModelInterface getTopicModel(Integer id, Integer subjectId) {
71
        final TopicsOptionsInterface topicsOptionsBuilder = this.topicsOptionsBuilder.forAuth();
0 ignored issues
show
Comprehensibility introduced by
The variable topicsOptionsBuildershadows a field with the same name declared in line 46. Consider renaming it.
Loading history...
72
        final TopicModelInterface topicModel = this.topicsService.find(id, topicsOptionsBuilder);
73
        checkModel(topicModel, subjectId);
74
        return topicModel;
75
    }
76
77
    private TopicModelInterface getTopicModel(Integer id, Integer subjectId, TopicsOptionsInterface topicsOptions) {
78
        final TopicModelInterface topicModel = this.topicsService.find(id, topicsOptions);
79
        checkModel(topicModel, subjectId);
80
        return topicModel;
81
    }
82
83
    private SubjectModelInterface getCurrentSubjectModel(Integer subjectId, boolean withTopics) {
84
        final SubjectsOptionsInterface subjectsOptions = this.subjectsOptionsBuilder.forAuth();
85
        final SubjectModelInterface subjectModel = subjectsService.find(subjectId, subjectsOptions);
86
        if (withTopics) {
87
            final List<TopicModelInterface> topics = this.topicsService.findBySubject(subjectModel);
88
            subjectModel.setTopics(topics);
89
        }
90
        checkModel(subjectModel);
91
        return subjectModel;
92
    }
93
94
    @GetMapping("")
95
    public String list(Model model, @PathVariable("subjectId") Integer subjectId) {
96
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, true);
97
        final List<TopicModelInterface> topics = subjectModel.getTopics();
98
        model.addAttribute("topics", topics);
99
        model.addAttribute("subjectId", subjectId);
100
        return "topics/list";
101
    }
102
103
    @GetMapping("{topicId}")
104
    public String read(Model model,
105
                       @PathVariable("subjectId") Integer subjectId,
106
                       @PathVariable("topicId") Integer topicId
107
    ) {
108
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, false);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable subjectModel.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "subjectModel".
Loading history...
109
        final TopicModelInterface topicModel = getTopicModel(topicId, subjectId);
110
        model.addAttribute("topic", topicModel);
111
        model.addAttribute("subjectId", subjectId);
112
        return "topics/view";
113
    }
114
115
    @GetMapping("create/")
116
    public String create(Model model, @PathVariable("subjectId") Integer subjectId) {
117
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, false);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable subjectModel.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "subjectModel".
Loading history...
118
        final TopicDto topic = new TopicDto();
119
        setCreateBehaviour(model);
120
        model.addAttribute("topic", topic);
121
        model.addAttribute("subjectId", subjectId);
122
        return "topics/form";
123
    }
124
125
    @PostMapping("create/")
126
    public String create(Model model,
127
                         @Valid @NotNull TopicDto topic,
128
                         BindingResult bindingResult,
129
                         @PathVariable("subjectId") Integer subjectId) {
130
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, false);
131
        if (bindingResult.hasErrors()) {
132
            setCreateBehaviour(model);
133
            model.addAttribute("topic", topic);
134
            model.addAttribute("subjectId", subjectId);
135
            model.addAttribute("errors", bindingResult);
136
            return "topics/form";
137
        }
138
        final TopicModelInterface topicModel = new TopicModel();
139
        topic.mapInto(topicModel);
140
        topicModel.setSubject(subjectModel);
141
        this.topicsService.save(topicModel);
142
        return "redirect:/personal/subjects/" + subjectId + "/topics/";
143
    }
144
145
    @GetMapping("update/{topicId}/")
146
    public String update(Model model,
147
                         @PathVariable Integer topicId,
148
                         @PathVariable("subjectId") Integer subjectId) {
149
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, false);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable subjectModel.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "subjectModel".
Loading history...
150
        final TopicModelInterface topicModel = this.getTopicModel(topicId, subjectId);
151
        final TopicDto topic = new TopicDto();
152
        topic.map(topicModel);
153
        setUpdateBehaviour(model);
154
        model.addAttribute("topic", topic);
155
        model.addAttribute("subjectId", subjectId);
156
        return "topics/form";
157
    }
158
159
    @PostMapping("update/{topicId}/")
160
    public String update(Model model,
161
                         @PathVariable Integer topicId,
162
                         @Valid @NotNull TopicDto topic,
163
                         BindingResult bindingResult,
164
                         @PathVariable("subjectId") Integer subjectId) {
165
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, false);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable subjectModel.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "subjectModel".
Loading history...
166
        final TopicModelInterface topicModel = this.getTopicModel(topicId, subjectId);
167
        if (bindingResult.hasErrors()) {
168
            setUpdateBehaviour(model);
169
            model.addAttribute("topic", topic);
170
            model.addAttribute("subjectId", subjectId);
171
            model.addAttribute("errors", bindingResult);
172
            return "topics/form";
173
        }
174
        topic.mapInto(topicModel);
175
        this.topicsService.save(topicModel);
176
        return "redirect:/personal/subjects/" + subjectId + "/topics/";
177
    }
178
179
    @GetMapping("delete/{topicId}")
180
    public String deleteConfirmation(Model model,
181
                                     @PathVariable("topicId") Integer topicId,
182
                                     @PathVariable("subjectId") Integer subjectId) {
183
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, false);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable subjectModel.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "subjectModel".
Loading history...
184
        final TopicModelInterface topicModel = this.getTopicModel(topicId, subjectId);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable topicModel.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "topicModel".
Loading history...
185
        model.addAttribute("subjectId", subjectId);
186
        return "topics/delete";
187
    }
188
189
    @PostMapping("delete/{topicId}")
190
    public String delete(Model model,
191
                         @PathVariable("topicId") Integer topicId,
192
                         @PathVariable("subjectId") Integer subjectId) {
193
        final SubjectModelInterface subjectModel = getCurrentSubjectModel(subjectId, false);
0 ignored issues
show
Unused Code introduced by
Consider removing the unused local variable subjectModel.
Loading history...
Unused Code Code Smell introduced by
Remove this useless assignment to local variable "subjectModel".
Loading history...
194
        final TopicModelInterface topicModel = getTopicModel(topicId, subjectId, topicsOptionsBuilder.forDelete());
195
        if (questionsService.findByTopic(topicModel).isEmpty()) {
196
            topicsService.delete(topicModel);
197
        } else {
198
            topicsService.delete(topicModel, this.topicsOptionsBuilder.forDelete());
199
        }
200
        return "redirect:/personal/subjects/" + subjectId + "/topics/";
201
    }
202
}
203